From 4adc90d9a8e48a5c1841874cae0cb30c3deee6e1 Mon Sep 17 00:00:00 2001 From: Kyle Gunger Date: Sun, 20 Sep 2020 18:18:26 -0400 Subject: Basic updates - Move preprocessor to it's own file - Begin working on new delimiters - Begin working on blocks in the tree --- src/tparse/preproc.go | 1 + src/tparse/tokenize.go | 34 +++++++++++++++++++++++++++------- src/tparse/tree.go | 32 ++++++++++++++++++++++++++++++++ src/tparse/type.go | 16 +++++++++++----- 4 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 src/tparse/preproc.go (limited to 'src') diff --git a/src/tparse/preproc.go b/src/tparse/preproc.go new file mode 100644 index 0000000..f591b44 --- /dev/null +++ b/src/tparse/preproc.go @@ -0,0 +1 @@ +package tparse diff --git a/src/tparse/tokenize.go b/src/tparse/tokenize.go index 79a0605..f5e3f54 100644 --- a/src/tparse/tokenize.go +++ b/src/tparse/tokenize.go @@ -126,12 +126,32 @@ func stripBlockComments(t []Token) []Token { out := []Token{} bc := false for _, tok := range t { - if tok.Type == DELIMIT && tok.Data == "/#" { - bc = true - continue - } else if tok.Type == DELIMIT && tok.Data == "#/" { - bc = false - continue + + if tok.Type == DELIMIT { + switch tok.Data { + case ";#": + out = append(out, Token{DELIMIT, ";/", tok.Line, tok.Char}) + bc = true + continue + case ":#": + out = append(out, Token{DELIMIT, ":/", tok.Line, tok.Char}) + bc = true + continue + case "/#": + bc = true + continue + case "#;": + out = append(out, Token{DELIMIT, "/;", tok.Line, tok.Char}) + bc = false + continue + case "#:": + out = append(out, Token{DELIMIT, "/:", tok.Line, tok.Char}) + bc = false + continue + case "#/": + bc = false + continue + } } else if bc { continue } @@ -158,7 +178,7 @@ func TokenizeFile(path string) []Token { max := maxResRunes() - ln, cn, last := int(0), int(-1), int(0) + ln, cn, last := int(1), int(-1), int(0) sp := false for r := rune(' '); ; r, _, err = read.ReadRune() { diff --git a/src/tparse/tree.go b/src/tparse/tree.go index 48c198c..417580a 100644 --- a/src/tparse/tree.go +++ b/src/tparse/tree.go @@ -27,6 +27,38 @@ func handleCode(tokens *[]Token, start int) (Node, int) { return out, start } +func handleBlock(tokens *[]Token, start int) (Node, int) { + var out Node + var tmp Node + + l := len(*tokens) + + if start >= l { + panic((*tokens)[l-1]) + } + + for ; start < l; start++ { + t := (*tokens)[start] + switch t.Type { + case LINESEP: + if t.Data == ";" { + tmp, start = handleCode(tokens, start+1) + } + break + case DELIMIT: + if t.Data == "/;" { + tmp, start = handleCode(tokens, start+1) + } + break + default: + panic(t) + } + out.SubNodes = append(out.SubNodes, tmp) + } + + return out, start +} + func handlePre(tokens *[]Token, start int) (Node, int) { out := Node{} diff --git a/src/tparse/type.go b/src/tparse/type.go index 735f681..3e6ef50 100644 --- a/src/tparse/type.go +++ b/src/tparse/type.go @@ -34,14 +34,11 @@ var RESWORD = map[string]int{ "import": PREWORD, "bool": KEYTYPE, - "byte": KEYTYPE, "char": KEYTYPE, "int": KEYTYPE, "float": KEYTYPE, - "string": KEYTYPE, - "struct": KEYWORD, "type": KEYWORD, @@ -94,12 +91,12 @@ var RESRUNE = map[rune]int{ // Start of pre-proc directive ':': LINESEP, - // Start of line + // Statement seperator ';': LINESEP, // Comment line '#': LINESEP, - // Seperate arguments + // Seperate arguments or enclosed statements ',': ARGNSEP, // Assignment @@ -156,8 +153,17 @@ var RESRUNES = map[string]int{ // Comment block "/#": DELIMIT, "#/": DELIMIT, + // Preproc block + "/:": DELIMIT, + ":/": DELIMIT, + // Redef blocks ";;": DELIMIT, + "::": DELIMIT, + ";#": DELIMIT, + ":#": DELIMIT, + "#;": DELIMIT, + "#:": DELIMIT, // Boolean equ "==": AUGMENT, -- cgit v1.2.3