From 1100ac865074effb3a4735c7449779f7193b7d0c Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Fri, 3 Dec 2021 18:25:03 -0500 Subject: General updates + Fill out eval a little and make sure that this builds. ~ CF kinda broken in AST. Gonna have to fix that. Upcoming Parser update. --- README.md | 2 +- small-tests/examp.tnsl | 57 ++++++++++++++++++++++------------------------- src/texec/eval.go | 45 +++++++++++++++++++++++++++++++++---- src/texec/libtnsl.go | 18 +++++++-------- src/texec/world.go | 2 +- src/tparse/tree-value.go | 7 ++++++ tests/block-test.tnsl | 18 +++++++-------- tests/comment-test.tnsl | 14 +++++++----- tests/literal-test.tnsl | 10 +++++---- tests/parameter-test.tnsl | 2 +- tests/statement-test.tnsl | 2 +- 11 files changed, 110 insertions(+), 67 deletions(-) diff --git a/README.md b/README.md index efb196e..4897dad 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Binaries will be dumped in the "build" folder. ## Status: - Parser: sorta works (sometimes) + Parser: sorta works sometimes (subtext: AST generater is at least a little broken, but works for at least some use cases) Interpreter: broken With some of the code I've written, I'm kinda supprised that this even compiles. \ No newline at end of file diff --git a/small-tests/examp.tnsl b/small-tests/examp.tnsl index f8579a0..c8fa2fb 100644 --- a/small-tests/examp.tnsl +++ b/small-tests/examp.tnsl @@ -19,7 +19,7 @@ # pass a variable ;int s = 3 -;int8 bitbyte = .2 +;int8 bitbyte = 2 # generic scope block @@ -42,7 +42,7 @@ # Sig: [output1, output2] (input1, input2) # Main may have /;main ({}string str) [int] # Doesn't matter what order the sig is in - # Main may also omit either for void sig + # Main may also omit either/both for void sig # {} represents a tnsl style array @@ -66,31 +66,32 @@ # The struct keyword is followed by {values} -;struct s1 {string Name, string Message = "Default message (c-style strings)"} +;struct S1 {string name, string message} # Most people should declare as such: -;struct s1 { - string Name, - string Message = "Default message (c-style strings)" +;struct S1 { + string + name, + message } # When defining a new struct, use {} -;s1 a = {} +;S1 a = {} # Same as -;s1 a{} -;a.Name = "Kyle Gunger" +;S1 a = {}[S1] +;a.name = "Kyle Gunger" ;~s1 b = ~a -;b~.Name # "Kyle Gunger" +;b`.name # "Kyle Gunger" # Quick initialization -;s1 c = {"", ""} -# These come in the same order that they do in the struct, so {Name, Message} in this case. +;S1 c = {"", ""} +# These come in the same order that they do in the struct, so {name, message} in this case. -# You can also specify -;s1 d{ - Message = "Message", - Name = "Name" +# You can also specify the order +;s1 d = { + message = "Message", + name = "Name" } @@ -101,16 +102,12 @@ 1, 2, 3, 4 } -# Same as -;{}int a{ - 1, 2, 3, 4 -} - # You may also define an initializer like such: -/;s1 [s1] +/;S1 [S1] # Initializer must be named same as struct, and must return one of the structs as its only output - return new s1{"Kyle", "TNSL Creator"} + # Called when keyword "new" is used. + return {"Kyle", "TNSL Creator"} ;/ /; if (i == 3) @@ -125,12 +122,12 @@ ;int t = 0 # Case block - /;case 1 + /;case (1) ;i = 0 ;t = 2 ;break - ;;case 2 + ;;case (2) ;i = 1 ;t = 2 ;break @@ -141,15 +138,15 @@ ;/ # You can do stuff here too - /; if [t == 2] + /; if (t == 2) ;i = t - i - ;;else if [t==3] + ;;else if (t == 3) ;i = t+i ;/ # Second case block - /;case 1 + /;case (1) ;i = 4 ;/ ;/ @@ -161,10 +158,10 @@ } # This seems dumb -;gen(int) j{2} +;gen(int) j = {2} # But this seems dumber -;{}gen(gen(int)) j{ +;{}gen(gen(int)) j = { {{1}}, {{2}}, {{3}} diff --git a/src/texec/eval.go b/src/texec/eval.go index 93cbfdb..0542a97 100644 --- a/src/texec/eval.go +++ b/src/texec/eval.go @@ -160,6 +160,43 @@ func equateType(a, b TType) bool { // Generate a TType from a 'type' node func getType(t tparse.Node) TType { out := TType{} + i := 0 + + // Pre + for ; i < len(t.Sub); i++ { + if t.Sub[i].Data.Type == tparse.DEFWORD || t.Sub[i].Data.Type == tparse.KEYTYPE { + break + } else { + out.Pre = append(out.Pre, t.Sub[i].Data.Data) + } + } + + // T + for ; i < len(t.Sub); i++ { + if t.Sub[i].Data.Type == tparse.KEYTYPE { + out.T.Name = t.Sub[i].Data.Data + i++ + break + } else if t.Sub[i].Data.Type == tparse.DEFWORD { + if i < len(t.Sub) - 1 { + if t.Sub[i + 1].Data.Type == tparse.DEFWORD { + out.T.Path = append(out.T.Path, t.Sub[i].Data.Data) + } else { + out.T.Name = t.Sub[i].Data.Data + break + } + } else { + out.T.Name = t.Sub[i].Data.Data + } + } + } + + // Post + if i < len(t.Sub) { + if t.Sub[i].Data.Data == "`" { + out.Post = "`" + } + } return out } @@ -167,20 +204,20 @@ func getType(t tparse.Node) TType { // Value generation func getStringLiteral(v tparse.Node) []byte { - + return []byte{} } func getCharLiteral(v tparse.Node) byte { - + return 0 } func getIntLiteral(v tparse.Node) int { - + return 0 } // Get a literal value from nodes. Must specify type of literal to generate. func getLiteral(v tparse.Node, t TType) interface{} { - + return 0 } //################# diff --git a/src/texec/libtnsl.go b/src/texec/libtnsl.go index 000af7d..b1c1907 100644 --- a/src/texec/libtnsl.go +++ b/src/texec/libtnsl.go @@ -43,14 +43,14 @@ import ( // Generic in-built types var ( - tFile = TType{Pre: []string{}, T: TArtifact{Path: []string{"tnsl", "io"}, Name: "File"}, Post: []string{}} - tString = TType{Pre: []string{"{}"}, T: TArtifact{Path: []string{}, Name:"charp"}, Post: []string{}} - tInt = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"int"}, Post: []string{}} - tByte = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"uint8"}, Post: []string{}} - tByteArray = TType{Pre: []string{"{}"}, T: TArtifact{Path: []string{}, Name:"uint8"}, Post: []string{}} - tFloat = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"float"}, Post: []string{}} - tCharp = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"charp"}, Post: []string{}} - tNull = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name: "null"}, Post: []string{}} + tFile = TType{Pre: []string{}, T: TArtifact{Path: []string{"tnsl", "io"}, Name: "File"}, Post: ""} + tString = TType{Pre: []string{"{}"}, T: TArtifact{Path: []string{}, Name:"charp"}, Post: ""} + tInt = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"int"}, Post: ""} + tByte = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"uint8"}, Post: ""} + tByteArray = TType{Pre: []string{"{}"}, T: TArtifact{Path: []string{}, Name:"uint8"}, Post: ""} + tFloat = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"float"}, Post: ""} + tCharp = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name:"charp"}, Post: ""} + tNull = TType{Pre: []string{}, T: TArtifact{Path: []string{}, Name: "null"}, Post: ""} ) // tells if the stub supports a function @@ -150,7 +150,7 @@ func tfile_write(file, in TVariable) { b := []byte{0} b[0] = (in.Data).(byte) (file.Data).(*os.File).Write(b) - } else if equateType(in.Type, tByteArray) { + } else if equateType(in.Type, tByteArray) || equateType(in.Type, tString) { (file.Data).(*os.File).Write((in.Data).([]byte)) } } else { diff --git a/src/texec/world.go b/src/texec/world.go index b1c6def..d0435a0 100644 --- a/src/texec/world.go +++ b/src/texec/world.go @@ -28,7 +28,7 @@ type TArtifact struct { type TType struct { Pre []string T TArtifact - Post []string + Post string } // TVariable represents a single variable in the program diff --git a/src/tparse/tree-value.go b/src/tparse/tree-value.go index 609e9d7..c44cdb6 100644 --- a/src/tparse/tree-value.go +++ b/src/tparse/tree-value.go @@ -410,8 +410,15 @@ func parseType(tokens *[]Token, tok, max int, param bool) (Node, int) { tmp.Data = t tok++ } + out.Sub = append(out.Sub, tmp) + if param && (*tokens)[tok].Data == "`" { + tmp = Node{(*tokens)[tok], []Node{}} + out.Sub = append(out.Sub, tmp) + tok++ + } + return out, tok case KEYWORD: diff --git a/tests/block-test.tnsl b/tests/block-test.tnsl index c48d7de..2bd4053 100644 --- a/tests/block-test.tnsl +++ b/tests/block-test.tnsl @@ -22,10 +22,8 @@ ;i = 2 ;/ - /: include - "this" - "that" - :/ + :include "this" + :include "that" # ;; can be used as a quick block re-definition @@ -53,17 +51,17 @@ /;method Vector2 /; operator + (~Vector2 v) - ;self.x += v`.x - ;self.y += v`.y + ;self.x = self.x + v`.x + ;self.y = self.y + v`.y ;/ /; operator + (int32 a) - ;self.x += a - ;self.y += a + ;self.x = self.x + a + ;self.y = self.y + a ;/ ;/ - ;struct FVector2 () {} + ;struct FVector2 () {float x, y} -;/ \ No newline at end of file +;/ diff --git a/tests/comment-test.tnsl b/tests/comment-test.tnsl index 6643695..ffc6e3a 100644 --- a/tests/comment-test.tnsl +++ b/tests/comment-test.tnsl @@ -14,14 +14,16 @@ limitations under the License. #/ -;/# -#/; +/;/# +#/;/ + +;a a -a/# +;a/# #/a -/;/# -#/;/ +; /# +#/; /# Ok, so this should give no output either @@ -43,4 +45,4 @@ a# Comment end #; if (thing) ;; else if (other_thing) ;# Comment start -End #/ \ No newline at end of file +End #/ diff --git a/tests/literal-test.tnsl b/tests/literal-test.tnsl index 73f5f77..6e55323 100644 --- a/tests/literal-test.tnsl +++ b/tests/literal-test.tnsl @@ -28,17 +28,19 @@ # Invalid (some may be weeded out through the verify phase): -;string s "" +#;string s "" -;int 0 i +#;int 0 i # Invalid ops should also be detected if dealing with literals -;char c ~= 's' +#;char c ~= 's' # Debate over weather these are legal -;int k = .1 +#;int k = .1 ;int l = 0x01 +;int i +;a a; diff --git a/tests/parameter-test.tnsl b/tests/parameter-test.tnsl index 96f6d41..cae89ab 100644 --- a/tests/parameter-test.tnsl +++ b/tests/parameter-test.tnsl @@ -14,5 +14,5 @@ limitations under the License. #/ ;int another = 0 -/; loop (int initial = 0, complex = 2) [initial < max || complex < 40, initial++, complex += 7, another += 2] +/; loop (int initial = 0, complex = 2) [initial < max || complex < 40; initial++; complex += 7; another += 2] ;/ diff --git a/tests/statement-test.tnsl b/tests/statement-test.tnsl index 71bd64b..b771e06 100644 --- a/tests/statement-test.tnsl +++ b/tests/statement-test.tnsl @@ -3,4 +3,4 @@ /; if_block ;; else_block -;/ \ No newline at end of file +;/ -- cgit v1.2.3