diff options
author | Kyle Gunger <kgunger12@gmail.com> | 2021-12-10 00:53:58 -0500 |
---|---|---|
committer | Kyle Gunger <kgunger12@gmail.com> | 2021-12-10 00:53:58 -0500 |
commit | c3b9e4a8be18338ed5da223037d6259eaef54e8f (patch) | |
tree | 9f159c104fb183e16433e3dd64888347cf8a9899 | |
parent | 9fc8b79c79975e99ea314b5e767b6c204a91f0ff (diff) |
[EXEC] Some literal handlers. Does not compile ATM.
-rw-r--r-- | src/texec/eval.go | 79 | ||||
-rw-r--r-- | src/texec/worldbuilder.go | 6 | ||||
-rwxr-xr-x | tests/run-tests.sh | 2 |
3 files changed, 74 insertions, 13 deletions
diff --git a/src/texec/eval.go b/src/texec/eval.go index 0542a97..d1bf366 100644 --- a/src/texec/eval.go +++ b/src/texec/eval.go @@ -19,6 +19,8 @@ package texec import "tparse" import "fmt" +import "strconv" + /* So here's what I care to support at present: Type checking, basic types, writing to stdout or a file @@ -38,9 +40,9 @@ import "fmt" // Error helper -func errOut(path TArtifact, place tparse.Token) { +func errOut(msg string, place tparse.Token) { fmt.Println("Error in eval:") - fmt.Println(path) + fmt.Println(msg) fmt.Println(place) panic("EVAL ERROR") } @@ -125,15 +127,15 @@ func getArtifact(a TArtifact, root *TModule) *tparse.Node { // Type related stuff // Checking type equality -func equateType(a, b TType) bool { +func equateTypePS(a, b TType, preskip int) bool { if len(a.Pre) != len(b.Pre) || len(a.Post) != len(b.Post) { return false } else if len(a.T.Path) != len(b.T.Path) { return false } - for i := 0; i < len(a.Pre); i++ { - if a.Pre[i] != b.Pre[i] { + for i := preskip; i < len(a.Pre); i++ { + if a.Pre[i] != b.Pre[i - preskip] { return false } } @@ -157,6 +159,10 @@ func equateType(a, b TType) bool { return true; } +func equateType(a, b TType) bool { + return equateTypePS(a, b, 0) +} + // Generate a TType from a 'type' node func getType(t tparse.Node) TType { out := TType{} @@ -204,23 +210,72 @@ func getType(t tparse.Node) TType { // Value generation func getStringLiteral(v tparse.Node) []byte { - return []byte{} + str, err := strconv.Unquote(v.Data.Data) + + if err != nil { + errOut("Failed to parse string literal.", v.Data) + } + + return []byte(str) } func getCharLiteral(v tparse.Node) byte { - return 0 + val, mb, _, err := strconv.UnquoteChar(v.Data.Data, byte('\'')) + + if err != nil || mb == true{ + errOut("Failed to parse character as single byte.", v.Data) + } + + return byte(val) } -func getIntLiteral(v tparse.Node) int { - return 0 +func getIntLiteral(v tparse.Node) int64 { + i, err := strconv.ParseInt(v.Data.Data, 0, 64) + + if err != nil { + errOut("Failed to parse integer literal.", v.Data) + } + + return i } -// Get a literal value from nodes. Must specify type of literal to generate. -func getLiteral(v tparse.Node, t TType) interface{} { - return 0 +func getLiteral(v tparse.Node) (interface{}, TType) { + str, err := strconv.Unquote(v.Data.Data) + + if err == nil { + return []byte(str), tString + } else { + val, mb, _, err := strconv.UnquoteChar(v.Data.Data, byte('\'')) + + if err == nil { + return byte(val), tCharp + } else { + i, err := strconv.ParseInt(v.Data.Data, 0, 64) + + if err == nil { + return i, tInt + } else { + errOut("Failed to parse literal in any way.", v.Data) + } + } + } + + return nil, tNull } +//##################### +//# Finding Artifacts # +//##################### + + + //################# //# Runtime funcs # //################# +// Value statement parsing + +// Get a value from nodes. Must specify type of value to generate. +func evalValue(v tparse.Node, t TType) TVariable { + return TVariable{} +}
\ No newline at end of file diff --git a/src/texec/worldbuilder.go b/src/texec/worldbuilder.go index 023c730..9fd29b7 100644 --- a/src/texec/worldbuilder.go +++ b/src/texec/worldbuilder.go @@ -45,6 +45,12 @@ func evalPreLiteral(n tparse.Node) string { return "" } +//Generate a variable for a module +// For sub = 0, give the vlist +func modDef(n tparse.Node, t TType, sub int) ([]string, []TVariable) { + +} + // Parse a file and make an AST from it. func parseFile(p string) tparse.Node { tokens := tparse.TokenizeFile(p) diff --git a/tests/run-tests.sh b/tests/run-tests.sh index d910e93..2b8f460 100755 --- a/tests/run-tests.sh +++ b/tests/run-tests.sh @@ -5,7 +5,7 @@ PARSEFILE=" " parse () { echo "ATTEMPTING TO PARSE $1-test.tnsl" - $PARSECMD $2 -in $1-test.tnsl -out $1-test.tnt + $PARSECMD $2 -in $1-test.tnsl -out $1-test.tnt if [ $? -eq 0 ]; then echo "SUCCESS!" fi |