summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Gunger <corechg@gmail.com>2020-12-06 16:02:51 -0500
committerKyle Gunger <corechg@gmail.com>2020-12-06 16:02:51 -0500
commit4bcbc40328a16aa8f53f69bb304784d9b5d5729b (patch)
treef5a4f6d648b35ba4541df04c1d48b4c48d9708d7
parent0f76282fec329ae90b254c0ad348654f6d14bae8 (diff)
Small update
-rwxr-xr-xgobuild.sh4
-rw-r--r--src/tparse/tree-list.go96
-rw-r--r--src/tparse/tree-statement.go78
-rw-r--r--src/tparse/type.go2
4 files changed, 144 insertions, 36 deletions
diff --git a/gobuild.sh b/gobuild.sh
index 604554b..684fd52 100755
--- a/gobuild.sh
+++ b/gobuild.sh
@@ -2,8 +2,6 @@
SRCDIR=$(pwd)
-GOPATH="$GOPATH:$SRCDIR"
-
-go env -w GOPATH=$GOPATH
+export GOPATH="$GOPATH:$SRCDIR"
go build -o build/parse src/main.go
diff --git a/src/tparse/tree-list.go b/src/tparse/tree-list.go
index 70f5fff..78e45a8 100644
--- a/src/tparse/tree-list.go
+++ b/src/tparse/tree-list.go
@@ -16,19 +16,34 @@
package tparse
+func getClosing(start string) string {
+ switch start {
+ case "{":
+ return "}"
+ case "[":
+ return "]"
+ case "(":
+ return ")"
+ }
+
+ return ""
+}
+
// Parse a list of values
func parseValueList(tokens *[]Token, tok, max int) (Node, int) {
out := Node{}
out.Data = Token{Type: 10, Data: "list"}
var tmp Node
+ c := getClosing((*tokens)[tok].Data)
+
tok++
for ; tok < max; tok++ {
t := (*tokens)[tok]
switch t.Data {
- case ")", "]", "}":
+ case c:
return out, tok
case ",":
tok++
@@ -43,7 +58,7 @@ func parseValueList(tokens *[]Token, tok, max int) (Node, int) {
return out, tok
}
-// Parses a list of things
+// Parses a list of definitions
func parseDefList(tokens *[]Token, tok, max int) (Node, int) {
out := Node{}
out.Data = Token{Type: 9, Data: "list"}
@@ -51,72 +66,87 @@ func parseDefList(tokens *[]Token, tok, max int) (Node, int) {
currentType := Node{}
currentType.Data = Token{Data: "undefined"}
- for ; tok < max; tok++ {
- t0 := (*tokens)[tok]
- t1 := (*tokens)[tok+1]
+ c := getClosing((*tokens)[tok].Data)
- switch t1.Data {
- case ")", "]", "}", ",":
+ tok++
+
+ for ; tok < max; tok++ {
+ switch (*tokens)[tok].Data {
+ case c:
+ return out, tok
+ case ",":
+ tok++
default:
- currentType, tok = parseType(tokens, tok, max, true)
- t0 = (*tokens)[tok]
- t1 = (*tokens)[tok+1]
+ errOut("Unexpected token when reading parameter definition", (*tokens)[tok])
}
- switch t0.Type {
+ t := (*tokens)[tok+1]
- case DEFWORD:
- var tmp Node
- if currentType.Data.Data == "undefined" {
- errOut("Error: expected type before first parameter", t0)
- } else if currentType.Data.Data == "type" {
- tmp, tok = parseType(tokens, tok, max, true)
- } else {
- tmp = Node{Data: t0}
- }
+ if t.Data != "," && t.Data != c {
+ currentType, tok = parseType(tokens, tok, max, true)
+ }
- typ := currentType
- makeParent(&typ, tmp)
- makeParent(&out, typ)
+ t = (*tokens)[tok]
- default:
- errOut("Error: unexpected token when parsing list, expected user-defined variable or ", t0)
+ if t.Type != DEFWORD {
+ errOut("Unexpected token in parameter definition. Expected variable identifier", t)
}
- switch t1.Data {
- case ")", "]", "}":
+ }
+
+ return out, tok
+}
+
+func parseTypeList(tokens *[]Token, tok, max int) (Node, int) {
+ out := Node{}
+ out.Data = Token{Type: 9, Data: "list"}
+ var tmp Node
+
+ c := getClosing((*tokens)[tok].Data)
+
+ tok++
+
+ for ; tok < max; tok++ {
+ t := (*tokens)[tok]
+
+ switch t.Data {
+ case c:
return out, tok
case ",":
+ tok++
default:
- errOut("Error: unexpected token when parsing list, expected ',' or end of list", t1)
+ errOut("Error: unexpected token when parsing a list of types", t)
}
- tok++
+ tmp, tok = parseType(tokens, tok, max, true)
+ out.Sub = append(out.Sub, tmp)
}
return out, tok
}
-func parseTypeList(tokens *[]Token, tok, max int) (Node, int) {
+func parseStatementList(tokens *[]Token, tok, max int) (Node, int) {
out := Node{}
out.Data = Token{Type: 9, Data: "list"}
var tmp Node
+ c := getClosing((*tokens)[tok].Data)
+
tok++
for ; tok < max; tok++ {
t := (*tokens)[tok]
switch t.Data {
- case ")", "]", "}":
+ case c:
return out, tok
case ",":
tok++
default:
- errOut("Error: unexpected token when parsing a list of types", t)
+ errOut("Error: unexpected token when parsing a list of statements", t)
}
- tmp, tok = parseType(tokens, tok, max, true)
+ tmp, tok = parseStatement(tokens, tok, max)
out.Sub = append(out.Sub, tmp)
}
diff --git a/src/tparse/tree-statement.go b/src/tparse/tree-statement.go
index 9d0b0d6..f6d3452 100644
--- a/src/tparse/tree-statement.go
+++ b/src/tparse/tree-statement.go
@@ -15,3 +15,81 @@
*/
package tparse
+
+func parseBlock(tokens *[]Token, tok, max int) (Node, int) {
+ out := Node{}
+ out.Data = Token{Type: 9, Data: "list"}
+ var tmp Node
+
+ tok++
+
+ for ; tok < max; tok++ {
+ t := (*tokens)[tok]
+
+ switch t.Data {
+ case ")", "]", "}":
+ return out, tok
+ case ",":
+ tok++
+ default:
+ errOut("Error: unexpected token when parsing a list of types", t)
+ }
+
+ tmp, tok = parseType(tokens, tok, max, true)
+ out.Sub = append(out.Sub, tmp)
+ }
+
+ return out, tok
+}
+
+func parseStatement(tokens *[]Token, tok, max int) (Node, int) {
+ out := Node{}
+ out.Data = Token{Type: 9, Data: "list"}
+ var tmp Node
+
+ tok++
+
+ for ; tok < max; tok++ {
+ t := (*tokens)[tok]
+
+ switch t.Data {
+ case ")", "]", "}":
+ return out, tok
+ case ",":
+ tok++
+ default:
+ errOut("Error: unexpected token when parsing a list of types", t)
+ }
+
+ tmp, tok = parseType(tokens, tok, max, true)
+ out.Sub = append(out.Sub, tmp)
+ }
+
+ return out, tok
+}
+
+func parseDef(tokens *[]Token, tok, max int) (Node, int) {
+ out := Node{}
+ out.Data = Token{Type: 9, Data: "list"}
+ var tmp Node
+
+ tok++
+
+ for ; tok < max; tok++ {
+ t := (*tokens)[tok]
+
+ switch t.Data {
+ case ")", "]", "}":
+ return out, tok
+ case ",":
+ tok++
+ default:
+ errOut("Error: unexpected token when parsing a list of types", t)
+ }
+
+ tmp, tok = parseType(tokens, tok, max, true)
+ out.Sub = append(out.Sub, tmp)
+ }
+
+ return out, tok
+}
diff --git a/src/tparse/type.go b/src/tparse/type.go
index ea8e6eb..70d8776 100644
--- a/src/tparse/type.go
+++ b/src/tparse/type.go
@@ -127,6 +127,8 @@ var RESWORD = map[string]int{
"module": KEYWORD,
"export": KEYWORD,
+
+ "_": KEYWORD,
}
func checkResWord(s string) int {