diff options
Diffstat (limited to 'src/tparse')
-rw-r--r-- | src/tparse/tree-list.go | 96 | ||||
-rw-r--r-- | src/tparse/tree-statement.go | 78 | ||||
-rw-r--r-- | src/tparse/type.go | 2 |
3 files changed, 143 insertions, 33 deletions
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 { |