From 6d25f97d1035a775d6eb9f135f8e8231521087af Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Thu, 7 Apr 2022 13:05:08 -0400 Subject: [EXEC] Some fixes + Fix recursion error when converting arrays + Add conversion from bool to num + Fix conversion from num to bool + Flush out evalParams ~ Change type parsing for block returns --- examples/README.md | 10 ++++++ examples/basic.tnsl | 3 ++ src/texec/eval.go | 75 ++++++++++++++++++++++++++++++++------------ src/texec/libtnsl.go | 7 ++++- src/tparse/tree-statement.go | 4 +-- 5 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 examples/README.md create mode 100644 examples/basic.tnsl diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..7607012 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,10 @@ +# Example tnsl programs + +These are working example programs that have been verified to work with the currnet version of the interpreter. The language is not garunteed to stay constant between the interpreter and the compiled version later down the line (This is all still pre-alpha software). + +Current examples: +- basic.tnsl: Simply returns zero + +Running examples: + + ./tint -in [-flags ] \ No newline at end of file diff --git a/examples/basic.tnsl b/examples/basic.tnsl new file mode 100644 index 0000000..71d784d --- /dev/null +++ b/examples/basic.tnsl @@ -0,0 +1,3 @@ +/; main [int] + ;return 0 +;/ diff --git a/src/texec/eval.go b/src/texec/eval.go index f2b420f..1ca290b 100644 --- a/src/texec/eval.go +++ b/src/texec/eval.go @@ -339,14 +339,21 @@ func stripType(t TType, s int) TType { // Value generation -func getStringLiteral(v tparse.Node) []byte { +func getStringLiteral(v tparse.Node) []interface{} { str, err := strconv.Unquote(v.Data.Data) if err != nil { errOut(fmt.Sprintf("Failed to parse string literal %v", v.Data)) } - return []byte(str) + dat := []byte(str) + out := []interface{}{} + + for i := 0; i < len(dat); i++ { + out = append(out, dat) + } + + return out } func getCharLiteral(v tparse.Node) byte { @@ -472,7 +479,7 @@ func cata(st TArtifact, dat []interface{}) []interface{} { for i := 0; i < len(dat); i++ { switch v := dat[i].(type) { case []interface{}: - out = append(out, cata(st, dat)) + out = append(out, cata(st, v)) case VarMap: out = append(out, csts(st, v)) default: @@ -517,12 +524,13 @@ func csts(st TArtifact, dat VarMap) VarMap { func convertValPS(to TType, sk int, dat interface{}) interface{} { var numcv float64 - switch v := dat.( type ) { + switch v := dat.(type) { case []interface{}: - if isStruct(to, sk) { - return cvsa(to.T, v) - } else if isArray(to, sk) { + if isArray(to, sk) { return cata(to.T, v) + } else if isStruct(to, sk) { + fmt.Println(to) + return cvsa(to.T, v) } case VarMap: return csts(to.T, v) @@ -535,7 +543,12 @@ func convertValPS(to TType, sk int, dat interface{}) interface{} { case float64: numcv = v goto NCV - + case bool: + numcv = 0 + if v { + numcv = 1 + } + goto NCV } errOut(fmt.Sprintf("Unable to convert between two types.\nTO: %v\nSK: %d\nDT: %v", to, sk, dat)) @@ -549,15 +562,15 @@ func convertValPS(to TType, sk int, dat interface{}) interface{} { } else if equateTypePSO(to, tByte, sk) { return byte(numcv) } else if equateTypePSO(to, tBool, sk) { - return numcv == 0 + return numcv != 0 } errOut(fmt.Sprintf("Unable to convert between two types.\nTO: %v\nSK: %d\nDT: %v", to, sk, dat)) return nil } -func convertVal(dat TVariable, to TType) interface{} { - return convertValPS(to, 0, dat.Data) +func convertVal(dat TVariable, to TType) *TVariable { + return &TVariable{to, convertValPS(to, 0, dat.Data)} } //##################### @@ -628,9 +641,7 @@ func isPointer(t TType, skp int) bool { } func isArray(t TType, skp int) bool { - for ;skp < len(t.Pre) && t.Pre[skp] == "const"; skp++ {} - - if len(t.Pre) >= skp { + if len(t.Pre) <= skp { return false } @@ -774,7 +785,7 @@ func evalDef(v tparse.Node, ctx *VarMap) { for i := 0; i < len(v.Sub[1].Sub); i++ { if v.Sub[1].Sub[i].Data.Data == "=" { - (*ctx)[v.Sub[1].Sub[i].Sub[0].Data.Data] = &TVariable{t, convertVal(*evalValue(v.Sub[1].Sub[i].Sub[1], ctx), t)} + (*ctx)[v.Sub[1].Sub[i].Sub[0].Data.Data] = convertVal(*evalValue(v.Sub[1].Sub[i].Sub[1], ctx), t) } else { (*ctx)[v.Sub[1].Sub[i].Data.Data] = &TVariable{t, nil} } @@ -789,7 +800,19 @@ func evalCF(v tparse.Node, ctx *VarMap) (bool, TVariable) { } func evalParams(pd tparse.Node, params *[]TVariable, ctx *VarMap) { - + if len(pd.Sub) == 0 { + return + } + cvt := getType(pd.Sub[0]) + pi := 0 + for i := 1; i < len(pd.Sub); i++ { + if pd.Sub[i].Data.Type == 10 && pd.Sub[i].Data.Data == "type" { + cvt = getType(pd.Sub[i]) + } else if pd.Sub[i].Data.Type == tparse.DEFWORD { + (*ctx)[pd.Sub[i].Data.Data] = convertVal((*params)[pi], cvt) + pi++ + } + } } func evalBlock(b tparse.Node, params []TVariable) TVariable { @@ -800,7 +823,7 @@ func evalBlock(b tparse.Node, params []TVariable) TVariable { if b.Sub[0].Data.Data == "bdef" { for i := 0; i < len(b.Sub[0].Sub); i++ { if b.Sub[0].Sub[i].Data.Data == "[]" { - rty = getType(b.Sub[0].Sub[i].Sub[0]) + rty = getType(b.Sub[0].Sub[i]) } else if b.Sub[0].Sub[i].Data.Data == "()" { evalParams(b.Sub[0].Sub[i], ¶ms, &ctx) } @@ -819,7 +842,7 @@ func evalBlock(b tparse.Node, params []TVariable) TVariable { case "block": ret, val := evalCF(b.Sub[i].Sub[0], &ctx) if ret { - return TVariable{rty, convertVal(val, rty)} + return *convertVal(val, rty) } case "return": fmt.Println("--- Block return ---") @@ -827,7 +850,7 @@ func evalBlock(b tparse.Node, params []TVariable) TVariable { ret := *evalValue(b.Sub[i].Sub[0].Sub[0], &ctx) fmt.Println(ret) fmt.Println("--- Return end ---") - return TVariable{rty, convertVal(ret, rty)} + return *convertVal(ret, rty) } } @@ -840,12 +863,24 @@ func EvalTNSL(root *TModule, args string) TVariable { sarg := strings.Split(args, " ") + saif := []interface{}{} + + for i := 0; i < len(sarg); i++ { + tmp := []interface{}{} + dat := []byte(sarg[i]) + for j := 0; j < len(dat); j++ { + tmp = append(tmp, dat[j]) + } + + saif = append(saif, tmp) + } + targ := TVariable { TType { []string{"{}", "{}"}, TArtifact { []string{}, "charp" }, "" }, - sarg } + saif } mainNode := getNode(prog, "main") diff --git a/src/texec/libtnsl.go b/src/texec/libtnsl.go index 6c34473..6fe431b 100644 --- a/src/texec/libtnsl.go +++ b/src/texec/libtnsl.go @@ -179,7 +179,12 @@ func tfile_write(file, in TVariable) { b[0] = (in.Data).(byte) (file.Data).(*os.File).Write(b) } else if equateType(in.Type, tByteArray) || equateType(in.Type, tString) { - (file.Data).(*os.File).Write((in.Data).([]byte)) + dat := (in.Data).([]interface{}) + wrt := []byte{} + for i := 0; i < len(dat); i++ { + wrt = append(wrt, dat[i].(byte)) + } + (file.Data).(*os.File).Write(wrt) } } else { (file.Data).(*os.File).Close() diff --git a/src/tparse/tree-statement.go b/src/tparse/tree-statement.go index 25ddab6..b4f891b 100644 --- a/src/tparse/tree-statement.go +++ b/src/tparse/tree-statement.go @@ -40,7 +40,7 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) { if sparse { tmp, tok = parseStatementList(tokens, tok + 1, max) } else { - tmp, tok = parseTypeList(tokens, tok + 1, max) + tmp, tok = parseType(tokens, tok + 1, max, false) } tmp.Data.Data = "[]" def.Sub = append(def.Sub, tmp) @@ -215,7 +215,7 @@ func keywordStatement(tokens *[]Token, tok, max int) (Node, int) { out.Sub = append(out.Sub, tmp) tok++ if (*tokens)[tok].Data == "[" { - tmp, tok = parseTypeList(tokens, tok + 1, max) + tmp, tok = parseType(tokens, tok + 1, max, false) out.Sub = append(out.Sub, tmp) tok++ } -- cgit v1.2.3