From 628dd83397c47ff484f7c81b06dcd6d1e4af628b Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Sun, 1 Aug 2021 16:37:02 -0400 Subject: Split main file --- LICENSE | 2 +- README.md | 21 ++++++++++++++++++- gobuild.sh | 6 +++--- small-tests/out.tnt | 2 +- src/exec.go | 46 +++++++++++++++++++++++++++++++++++++++++ src/main.go | 43 -------------------------------------- src/parse.go | 49 ++++++++++++++++++++++++++++++++++++++++++++ src/tparse/token.go | 6 ++++++ src/tparse/tree-statement.go | 10 +++++++-- src/tparse/tree.go | 2 +- src/tparse/type.go | 4 +--- 11 files changed, 136 insertions(+), 55 deletions(-) mode change 100755 => 100644 gobuild.sh create mode 100644 src/exec.go delete mode 100644 src/main.go create mode 100644 src/parse.go diff --git a/LICENSE b/LICENSE index f83ea9a..0f88824 100644 --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright 2020 Kyle Gunger + Copyright 2020-2021 Kyle Gunger Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/README.md b/README.md index 46bffc9..a88faf5 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,23 @@ The tokenizer for the [TNSL Language](https://github.com/CoreChg/tnsl-lang). Written in Go for the moment. The goal is to get this part written, and then write a backend for any arch (x86, arm, risc-v, etc.). -After that, work can begin on the real parser which will be written in native TNSL \ No newline at end of file +After that, work can begin on the real parser/compiler which will be written in native TNSL + +## interpreter + +This project was originally supposed to form a go based compiler for the language, but in the interest of time, it seems more efficient to build an interpreter instead so we can work on the TNSL based compiler sooner. + +To build the parser: + + ./gobuild.sh parse + +To build the interpreter: + + ./gobuild.sh exec + +Binaries will be dumped in the "build" folder. + +## Status: + + Parser: broke + Interpreter: not started \ No newline at end of file diff --git a/gobuild.sh b/gobuild.sh old mode 100755 new mode 100644 index 67b7acf..daa0a55 --- a/gobuild.sh +++ b/gobuild.sh @@ -2,7 +2,7 @@ SRCDIR=$(pwd) -export GOPATH="$GOPATH:$SRCDIR" +GOPATH="$GOPATH:$SRCDIR" +GO111MODULE=off -go env -w GO111MODULE=off -go build -o build/parse src/main.go +go build -o build/${1} src/${1}.go diff --git a/small-tests/out.tnt b/small-tests/out.tnt index 9f77b4b..385db24 100644 --- a/small-tests/out.tnt +++ b/small-tests/out.tnt @@ -1 +1 @@ -{{9 test.tnsl 0 0} [{{10 block 0 0} [{{10 block 0 0} [{{11 ; 0 0} []} {{11 ; 0 0} []}]} {{10 block 0 0} [{{11 ; 0 0} []} {{11 ; 0 0} []}]} {{11 ; 0 0} []} {{11 ; 0 0} []}]} {{11 ; 0 0} []}]} +[{2 ( 4 15} {2 ) 4 66} {0 : 13 0} {8 import 13 1} {4 'what/what.tnsl' 13 8} {8 a 13 25} {0 : 15 0} {8 import 15 1} {4 "what/what.tnsl" 15 8} {0 ; 20 0} {8 int 20 1} {8 s 20 5} {3 = 20 7} {4 3 20 9} {0 ; 22 0} {8 byte 22 1} {8 bitbyte 22 6} {3 = 22 14} {3 . 22 16} {4 2 22 17} {2 /; 29 0} {0 ; 31 2} {8 int 31 3} {8 d 31 7} {3 = 31 9} {4 1 31 11} {0 ; 33 2} {8 s 33 3} {3 = 33 5} {8 d 33 7} {2 ;/ 35 0} {2 /; 44 0} {8 main 44 2} {2 ( 44 7} {2 { 44 8} {2 } 44 9} {8 string 44 10} {8 str 44 17} {2 ) 44 20} {2 [ 44 22} {8 int 44 23} {2 ] 44 26} {0 ; 52 2} {8 int 52 3} {8 i 52 7} {3 = 52 9} {4 1 52 11} {0 ; 53 2} {3 ~ 53 3} {8 int 53 4} {8 j 53 8} {3 ~= 53 10} {8 i 53 13} {8 j 54 2} {3 ~ 54 3} {3 = 54 5} {4 2 54 7} {2 /; 59 2} {7 loop 59 4} {2 [ 59 9} {8 i 59 10} {3 !== 59 11} {4 1 59 14} {2 ] 59 16} {0 ; 61 4} {8 i 61 6} {3 = 61 8} {4 1 61 10} {2 ;/ 62 2} {2 ;/ 64 0} {0 ; 69 0} {7 struct 69 1} {2 [ 69 8} {8 s1 69 9} {2 ] 69 11} {2 { 69 13} {8 string 69 14} {8 Name 69 21} {1 , 69 25} {8 string 69 27} {8 Message 69 34} {3 = 69 42} {4 "Default message (c-style strings)" 69 44} {2 } 69 79} {0 ; 72 0} {7 struct 72 1} {2 [ 72 8} {8 s1 72 9} {2 ] 72 11} {2 { 72 13} {8 string 73 4} {8 Name 73 11} {1 , 73 15} {8 string 74 4} {8 Message 74 11} {3 = 74 19} {4 "Default message (c-style strings)" 74 21} {2 } 75 0} {0 ; 78 0} {8 s1 78 1} {8 a 78 4} {3 = 78 6} {2 { 78 8} {2 } 78 9} {0 ; 80 0} {8 s1 80 1} {8 a 80 4} {2 { 80 5} {2 } 80 6} {0 ; 81 0} {8 a 81 1} {3 . 81 2} {8 Name 81 3} {3 = 81 8} {4 "Kyle Gunger" 81 10} {0 ; 83 0} {3 ~ 83 1} {8 s1 83 2} {8 b 83 5} {3 = 83 7} {3 ~ 83 9} {8 a 83 10} {0 ; 84 0} {8 b 84 1} {3 ~ 84 2} {3 . 84 3} {8 Name 84 4} {0 ; 87 0} {8 s1 87 1} {8 c 87 4} {3 = 87 6} {2 { 87 8} {4 "" 87 9} {1 , 87 11} {4 "" 87 13} {2 } 87 15} {0 ; 91 0} {8 s1 91 1} {8 d 91 4} {2 { 91 5} {8 Message 92 4} {3 = 92 12} {4 "Message" 92 14} {1 , 92 23} {8 Name 93 4} {3 = 93 9} {4 "Name" 93 11} {2 } 94 0} {0 ; 100 0} {2 { 100 1} {2 } 100 2} {8 int 100 3} {8 a 100 7} {3 = 100 9} {2 { 100 11} {4 1, 101 2} {4 2, 101 6} {4 3, 101 10} {4 4 101 14} {2 } 102 0} {0 ; 105 0} {2 { 105 1} {2 } 105 2} {8 int 105 3} {8 a 105 7} {2 { 105 8} {4 1, 106 2} {4 2, 106 6} {4 3, 106 10} {4 4 106 14} {2 } 107 0} {2 /; 111 0} {8 s1 111 2} {2 [ 111 5} {8 s1 111 6} {2 ] 111 8} {8 return 113 2} {8 new 113 9} {8 s1 113 13} {2 { 113 15} {4 "Kyle" 113 16} {1 , 113 22} {4 "TNSL Creator" 113 24} {2 } 113 38} {2 ;/ 114 0} {2 /; 116 0} {7 if 116 3} {2 ( 116 6} {8 i 116 7} {3 == 116 9} {4 3 116 12} {2 ) 116 14} {2 ;; 119 0} {7 else 119 2} {2 ;/ 121 0} {2 /; 123 0} {8 switch 123 3} {2 ( 123 10} {8 i 123 11} {2 ) 123 12} {0 ; 125 2} {8 int 125 3} {8 t 125 7} {3 = 125 9} {4 0 125 11} {2 /; 128 2} {7 case 128 4} {4 1 128 9} {0 ; 129 4} {8 i 129 5} {3 = 129 7} {4 0 129 9} {0 ; 130 4} {8 t 130 5} {3 = 130 7} {4 2 130 9} {0 ; 131 4} {7 break 131 5} {2 ;; 133 2} {7 case 133 4} {4 2 133 9} {0 ; 134 4} {8 i 134 5} {3 = 134 7} {4 1 134 9} {0 ; 135 4} {8 t 135 5} {3 = 135 7} {4 2 135 9} {0 ; 136 4} {7 break 136 5} {2 ;; 138 2} {7 default 138 4} {0 ; 139 4} {8 i 139 5} {3 = 139 7} {4 3 139 9} {0 ; 140 4} {7 break 140 5} {2 ;/ 141 2} {2 /; 144 2} {7 if 144 5} {2 [ 144 8} {8 t 144 9} {3 == 144 11} {4 2 144 14} {2 ] 144 16} {0 ; 145 4} {8 i 145 5} {3 = 145 7} {8 t 145 9} {3 - 145 11} {8 i 145 13} {2 ;; 147 2} {7 else 147 4} {7 if 147 9} {2 [ 147 12} {8 t 147 13} {3 == 147 14} {4 3 147 16} {2 ] 147 18} {0 ; 148 4} {8 i 148 5} {3 = 148 7} {8 t 148 9} {3 + 148 10} {8 i 148 11} {2 ;/ 149 2} {2 /; 152 2} {7 case 152 4} {4 1 152 9} {0 ; 153 4} {8 i 153 5} {3 = 153 7} {4 4 153 9} {2 ;/ 154 2} {2 ;/ 155 0} {0 ; 159 0} {7 struct 159 2} {2 [ 159 9} {8 gen 159 10} {2 ] 159 13} {2 ( 159 15} {5 type 159 16} {8 T 159 21} {2 ) 159 22} {2 { 159 24} {8 T 160 2} {8 i 160 4} {2 } 161 0} {0 ; 164 0} {8 gen 164 1} {2 ( 164 4} {8 int 164 5} {2 ) 164 8} {8 j 164 10} {2 { 164 11} {4 2 164 12} {2 } 164 14} {0 ; 167 0} {2 { 167 1} {2 } 167 2} {8 gen 167 3} {2 ( 167 6} {8 gen 167 7} {2 ( 167 10} {8 int 167 11} {2 ) 167 14} {2 ) 167 15} {8 j 167 17} {2 { 167 18} {2 { 168 2} {2 { 168 3} {4 1 168 4} {2 } 168 6} {2 } 168 7} {1 , 168 8} {2 { 169 2} {2 { 169 3} {4 2 169 4} {2 } 169 6} {2 } 169 7} {1 , 169 8} {2 { 170 2} {2 { 170 3} {4 3 170 4} {2 } 170 6} {2 } 170 7} {2 } 171 0}] diff --git a/src/exec.go b/src/exec.go new file mode 100644 index 0000000..f92f435 --- /dev/null +++ b/src/exec.go @@ -0,0 +1,46 @@ +/* + Copyright 2020 Kyle Gunger + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import "fmt" +import "tparse" +import "texec" +import "flag" + +func main() { + inputFile := flag.String("in", "", "The file to execute") + progFlags := flag.String("flags", "", "Flags for the executing program") + + flag.Parse() + + if err != nil { + fmt.Println(err.Error()) + return + } + + tokens := tparse.TokenizeFile(*inputFile) + + switch *writeLevel { + case 0: + fd.WriteString(fmt.Sprint(tokens) + "\n") + case 1: + tree := tparse.MakeTree(&tokens, *inputFile) + fd.WriteString(fmt.Sprint(tree) + "\n") + } + + fd.Close() +} \ No newline at end of file diff --git a/src/main.go b/src/main.go deleted file mode 100644 index e6efcd9..0000000 --- a/src/main.go +++ /dev/null @@ -1,43 +0,0 @@ -/* - Copyright 2020 Kyle Gunger - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package main - -import "fmt" -import "tparse" -import "flag" -import "os" - -func main() { - inputFile := flag.String("in", "", "The file to parse") - outputFile := flag.String("out", "out.tnt", "The file to store the node tree") - - flag.Parse() - - fd, err := os.Create(*outputFile) - - if err != nil { - fmt.Println(err.Error()) - return - } - - tokens := tparse.TokenizeFile(*inputFile) - tree := tparse.MakeTree(&tokens, *inputFile) - - fd.WriteString(fmt.Sprint(tree) + "\n") - - fd.Close() -} diff --git a/src/parse.go b/src/parse.go new file mode 100644 index 0000000..7849857 --- /dev/null +++ b/src/parse.go @@ -0,0 +1,49 @@ +/* + Copyright 2020 Kyle Gunger + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package main + +import "fmt" +import "tparse" +import "flag" +import "os" + +func main() { + inputFile := flag.String("in", "", "The file to parse") + outputFile := flag.String("out", "out.tnt", "The file to store the node tree") + writeLevel := flag.Int("writelevel", 1, "The level of parsing to write to the file (for debugging)") + + flag.Parse() + + fd, err := os.Create(*outputFile) + + if err != nil { + fmt.Println(err.Error()) + return + } + + tokens := tparse.TokenizeFile(*inputFile) + + switch *writeLevel { + case 0: + fd.WriteString(fmt.Sprint(tokens) + "\n") + case 1: + tree := tparse.MakeTree(&tokens, *inputFile) + fd.WriteString(fmt.Sprint(tree) + "\n") + } + + fd.Close() +} diff --git a/src/tparse/token.go b/src/tparse/token.go index 182b2e0..02502eb 100644 --- a/src/tparse/token.go +++ b/src/tparse/token.go @@ -27,6 +27,12 @@ type Token struct { // Node represents a node in an AST type Node struct { Data Token + + IsBlock bool + BlockQs []Token + BlockIn []Node + BlockOut []Node + Sub []Node } diff --git a/src/tparse/tree-statement.go b/src/tparse/tree-statement.go index 874528a..167cee2 100644 --- a/src/tparse/tree-statement.go +++ b/src/tparse/tree-statement.go @@ -23,6 +23,9 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) { tok++ + def := Node{} + def.Data = Token{Type: 10, Data: "blockdef"} + for ;tok < max; tok++{ t := (*tokens)[tok] @@ -30,14 +33,15 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) { case DELIMIT: if t.Data == "(" { tmp, tok = parseParamList(tokens, tok, max) - out.Sub = append(out.Sub, tmp) + def.Sub = append(out.Sub, tmp) } else if t.Data == "[" { tmp, tok = parseTypeList(tokens, tok, max) - out.Sub = append(out.Sub, tmp) + def.Sub = append(out.Sub, tmp) } else { goto BREAK } case DEFWORD: + case KEYWORD: case LINESEP: goto BREAK @@ -46,6 +50,8 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) { BREAK: + out.Sub = append(out.Sub, def) + for ;tok < max; { t := (*tokens)[tok] diff --git a/src/tparse/tree.go b/src/tparse/tree.go index 9e9e554..08c94bf 100644 --- a/src/tparse/tree.go +++ b/src/tparse/tree.go @@ -19,7 +19,7 @@ package tparse import "fmt" // ID 9 = ast root -// ID 10 = ast list +// ID 10 = ast token func errOut(message string, token Token) { fmt.Println(message) diff --git a/src/tparse/type.go b/src/tparse/type.go index 73bb94d..b860f02 100644 --- a/src/tparse/type.go +++ b/src/tparse/type.go @@ -50,7 +50,7 @@ var PREWORDS = []string{ "extern", "size", "align", - "origin", + "address", "rootfile", "if", "else", @@ -130,8 +130,6 @@ var RESWORD = map[string]int{ "module": KEYWORD, "export": KEYWORD, - - "drop": KEYWORD, } func checkResWord(s string) int { -- cgit v1.2.3