summaryrefslogtreecommitdiff
path: root/src/texec/libtnsl.go
diff options
context:
space:
mode:
authorKyle Gunger <kgunger12@gmail.com>2022-04-07 00:47:20 -0400
committerKyle Gunger <kgunger12@gmail.com>2022-04-07 00:47:20 -0400
commitda6f4e15ad2fa3edd59108913065c883aee897ed (patch)
tree4f37118329733febbfb19e3d6528d44331312429 /src/texec/libtnsl.go
parent9d2b6e985b008492fc99a79f1f674cc2ea83be2f (diff)
[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
Diffstat (limited to 'src/texec/libtnsl.go')
-rw-r--r--src/texec/libtnsl.go29
1 files changed, 20 insertions, 9 deletions
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