diff options
Diffstat (limited to 'src/tparse/tree-statement.go')
-rw-r--r-- | src/tparse/tree-statement.go | 146 |
1 files changed, 108 insertions, 38 deletions
diff --git a/src/tparse/tree-statement.go b/src/tparse/tree-statement.go index 167cee2..bb8bde2 100644 --- a/src/tparse/tree-statement.go +++ b/src/tparse/tree-statement.go @@ -17,32 +17,48 @@ package tparse func parseBlock(tokens *[]Token, tok, max int) (Node, int) { - out := Node{} + out, tmp, def, name := Node{}, Node{}, Node{}, false out.Data = Token{Type: 10, Data: "block"} - var tmp Node + def.Data = Token{Type: 10, Data: "blockdef"} tok++ - def := Node{} - def.Data = Token{Type: 10, Data: "blockdef"} - for ;tok < max; tok++{ t := (*tokens)[tok] - + tmp = Node{} switch t.Type { case DELIMIT: if t.Data == "(" { - tmp, tok = parseParamList(tokens, tok, max) - def.Sub = append(out.Sub, tmp) + tmp, tok = parseValueList(tokens, tok, max) + def.Sub = append(def.Sub, tmp) } else if t.Data == "[" { tmp, tok = parseTypeList(tokens, tok, max) - def.Sub = append(out.Sub, tmp) + def.Sub = append(def.Sub, tmp) } else { goto BREAK } case DEFWORD: - + if name { + errOut("Unexpected defword, the block has already been given a name or keyword.", t) + } + tmp.Data = t + def.Sub = append(def.Sub, tmp) + name = true case KEYWORD: + if name { + errOut("Unexpected keyword, the block has already been given a name or keyword.", t) + } + switch t.Data { + case "else": + if (*tokens)[tok+1].Data == "if" { + tmp.Data = Token{KEYWORD, "elif", t.Line, t.Char} + tok++ + name = true + continue + } + case "if", "match", "case", "loop": + case "export", "inline", "raw", "override": + } case LINESEP: goto BREAK } @@ -86,49 +102,103 @@ func parseStatement(tokens *[]Token, tok, max int) (Node, int) { out.Data = Token{Type: 11, Data: ";"} var tmp Node - tok++ - - for ; tok < max; tok++ { - t := (*tokens)[tok] - - switch t.Type { - case LINESEP, DELIMIT: - return out, tok - case INLNSEP: - tok++ - default: - errOut("Error: unexpected token when parsing a list of types", t) + // Check for keyword, definition, then if none of those apply, assume it's a value. + if (*tokens)[tok].Type == KEYWORD { + tmp, tok = keywordStatement(tokens, tok, max) + out.Sub = append(out.Sub, tmp) + } else { + // do check for definition + if false { + // if not, parse a value + tmp, tok = parseDefinition(tokens, tok, max) + } else { + // if not, parse a value + tmp, tok = parseValue(tokens, tok, max) } - - 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) { +// Works? Please test. +func keywordStatement(tokens *[]Token, tok, max int) (Node, int) { out := Node{} - out.Data = Token{Type: 10, Data: "list"} + out.Data = (*tokens)[tok] + out.IsBlock = false var tmp Node + tmp.IsBlock = false - tok++ + if tok + 1 < max { + tok++ + } else { + return out, max + } - for ; tok < max; tok++ { - t := (*tokens)[tok] + switch out.Data.Data { + case "raw": + // Something, something... code. + if (*tokens)[tok].Data != "struct" { + errOut("Unexpected use of raw operator in a statement", out.Data) + } + tok++ + fallthrough + case "struct": + // Check for defword, (), and {} then dip + if (*tokens)[tok].Type != DEFWORD { + errOut("Expected defword after struct keyword.", (*tokens)[tok]) + } + tmp.Data = (*tokens)[tok] + out.Sub = append(out.Sub, tmp) + tok++ + if (*tokens)[tok].Data == "(" { + tmp, tok = parseValueList(tokens, tok, max) + out.Sub = append(out.Sub, tmp) + } - switch t.Data { - case ")", "]", "}": - return out, tok - case ",": - tok++ - default: - errOut("Error: unexpected token when parsing a list of types", t) + if (*tokens)[tok].Data != "{" { + errOut("Could not find struct member list", (*tokens)[tok]) } - tmp, tok = parseType(tokens, tok, max, true) - out.Sub = append(out.Sub, tmp) + tmp, tok = parseValueList(tokens, tok, max) + + case "goto", "label": + if (*tokens)[tok].Type != DEFWORD { + errOut("Expected defword after goto or label keyword.", out.Data) + } + tmp.Data = (*tokens)[tok] + tok++ + // Check for a defword and dip + case "continue", "break": + if (*tokens)[tok].Type != LITERAL { + return out, tok + } + tmp.Data = (*tokens)[tok] + tok++ + // Check for a numerical value and dip + case "alloc", "salloc": + // Parse value list + tmp, tok = parseValueList(tokens, tok, max) + case "delete": + // Parse value list + tmp, tok = parseValueList(tokens, tok, max) } + out.Sub = append(out.Sub, tmp) + + return out, tok +} + +func parseDef(tokens *[]Token, tok, max int) (Node, int) { + out := Node{} + out.Data = Token{} + var tmp Node + + + tmp, tok = parseType(tokens, tok, max, false) + out.Sub = append(out.Sub, tmp) + tmp, tok = parseValueList(tokens, tok, max) + out.Sub = append(out.Sub, tmp) + return out, tok } |