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/eval.go | 236 ++++++++++++++++++++++++++++++++------------------- src/texec/libtnsl.go | 29 +++++-- 2 files changed, 167 insertions(+), 98 deletions(-) (limited to 'src') diff --git a/src/texec/eval.go b/src/texec/eval.go index 29b49a0..f2b420f 100644 --- a/src/texec/eval.go +++ b/src/texec/eval.go @@ -369,6 +369,16 @@ func getIntLiteral(v tparse.Node) int { return int(i) } +func getFloatLiteral(v tparse.Node) float64 { + i, err := strconv.ParseFloat(v.Data.Data, 64) + + if err != nil { + errOut(fmt.Sprintf("Failed to parse float literal. %v", v.Data)) + } + + return float64(i) +} + func getLiteralComposite(v tparse.Node) []interface{} { out := []interface{}{} @@ -379,8 +389,10 @@ func getLiteralComposite(v tparse.Node) []interface{} { out = append(out, getCharLiteral(v.Sub[i])) } else if v.Sub[i].Data.Data == "comp" { out = append(out, getLiteralComposite(v.Sub[i])) - } else { + } else if v.Sub[i].Data.Data[0] == '0' { out = append(out, getIntLiteral(v.Sub[i])) + } else { + out = append(out, getFloatLiteral(v.Sub[i])) } } @@ -392,15 +404,16 @@ func getBoolLiteral(v tparse.Node) bool { } func getLiteral(v tparse.Node, t TType) interface{} { - - if equateType(t, tInt) { - return getIntLiteral(v) + if equateType(t, tFloat) { + return getFloatLiteral(v) } else if equateType(t, tCharp) { return getCharLiteral(v) } else if equateType(t, tString) { return getStringLiteral(v) } else if equateType(t, tBool) { - getBoolLiteral(v) + return getBoolLiteral(v) + } else if equateType(t, tInt) { + return getIntLiteral(v) } return getLiteralComposite(v) @@ -415,20 +428,20 @@ func getLiteralType(v tparse.Node) TType { return tStruct } else if v.Data.Data == "true" || v.Data.Data == "false" { return tBool - } else { + } else if v.Data.Data[0] == '0' { return tInt + } else { + return tFloat } - - return tNull } // Convert Value to Struct from Array (cvsa) // USE ONLY IN THE CASE OF tStruct! -func cvsa(sct TType, dat []interface{}) VarMap { - sv := searchDef(sct.T) +func cvsa(sct TArtifact, dat []interface{}) VarMap { + sv := searchDef(sct) old_c := cart - cart = sct.T + cart = sct vars := sv.Data.([]TVariable) if len(vars) != len(dat) { @@ -440,9 +453,7 @@ func cvsa(sct TType, dat []interface{}) VarMap { for i:=0;i 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