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 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 147 insertions(+), 89 deletions(-) (limited to 'src/texec/eval.go') 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