From da6f4e15ad2fa3edd59108913065c883aee897ed Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 7 Apr 2022 00:47:20 -0400 Subject: [EXEC] A few updates + Value conversion + Value deep copy + The most basic program (returning 0) works + Opening a file to read is now differnet from opening a file to write --- src/texec/libtnsl.go | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'src/texec/libtnsl.go') diff --git a/src/texec/libtnsl.go b/src/texec/libtnsl.go index cd2077a..6c34473 100644 --- a/src/texec/libtnsl.go +++ b/src/texec/libtnsl.go @@ -26,7 +26,7 @@ import ( Parts included: - io.print - io.println - - io.open_file + - io.openFile - io.File API for file objects Types included: @@ -63,8 +63,7 @@ func tnslResolve(callPath TArtifact) int { l := len(callPath.Path) if l < 2 || l > 3 || callPath.Path[0] != "tnsl" || callPath.Path[1] != "io" { return -1 - } - if l > 2 && callPath.Path[2] != "File" { + } else if l > 2 && callPath.Path[2] != "File" { return -1 } @@ -73,7 +72,7 @@ func tnslResolve(callPath TArtifact) int { return 1; } } else { - if callPath.Name == "print" || callPath.Name == "println" || callPath.Name == "open_file" { + if callPath.Name == "print" || callPath.Name == "println" || callPath.Name == "readFile" || callPath.Name == "writeFile" { return 0; } } @@ -91,8 +90,10 @@ func tnslEval(in TVariable, function string) TVariable { tprint(in) case "println": tprintln(in) - case "open_file": - return topen_file(in) + case "readFile": + return topenReadFile(in) + case "writeFile": + return topenWriteFile(in) } return TVariable{tNull, nil} } @@ -132,17 +133,27 @@ func tprintln(in TVariable) { } } -func topen_file(in TVariable) TVariable { +func topenWriteFile(in TVariable) TVariable { if equateType(in.Type, tString) { - panic("Tried to open a file, but did not use a string type for the file name.") + panic("Tried to open a file (for writing), but did not use a string type for the file name.") } fd, err := os.Create(in.Data.(string)) if err != nil { - panic(fmt.Sprintf("Failed to open file %v as requested by the program. Aborting.\n%v", in.Data, err)) + panic(fmt.Sprintf("Failed to open file (for writing) %v as requested by the program. Aborting.\n%v", in.Data, err)) } return TVariable{tFile, fd} } +func topenReadFile(in TVariable) TVariable { + if equateType(in.Type, tString) { + panic("Tried to open a file (for reading), but did not use a string type for the file name.") + } + fd, err := os.Open(in.Data.(string)) + if err != nil { + panic(fmt.Sprintf("Failed to open file (for reading) %v as requested by the program. Aborting.\n%v", in.Data, err)) + } + return TVariable{tFile, fd} +} // File API -- cgit v1.2.3