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 --- 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 +--- 7 files changed, 111 insertions(+), 49 deletions(-) create mode 100644 src/exec.go delete mode 100644 src/main.go create mode 100644 src/parse.go (limited to 'src') 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