diff options
| author | Kyle Gunger <kgunger12@gmail.com> | 2021-10-31 20:42:37 -0400 | 
|---|---|---|
| committer | Kyle Gunger <kgunger12@gmail.com> | 2021-10-31 20:42:37 -0400 | 
| commit | eb05a05ac4f10672c573dd53cb3aaa27deb4f6a3 (patch) | |
| tree | 64eac6e24ee8540cb4a86cf4263b595744b6dce9 | |
| parent | c0a3940eb07abf13123c7190a5fa8a613bba9db0 (diff) | |
[AST] Handle control flow blocks
+ Initial support for control flow blocks. Things will be fixed as I find bugs.
| -rw-r--r-- | src/tparse/tree-list.go | 22 | ||||
| -rw-r--r-- | src/tparse/tree-preproc.go | 2 | ||||
| -rw-r--r-- | src/tparse/tree-statement.go | 33 | ||||
| -rw-r--r-- | src/tparse/tree.go | 2 | 
4 files changed, 39 insertions, 20 deletions
| diff --git a/src/tparse/tree-list.go b/src/tparse/tree-list.go index 740c06a..d415594 100644 --- a/src/tparse/tree-list.go +++ b/src/tparse/tree-list.go @@ -120,27 +120,29 @@ func parseTypeList(tokens *[]Token, tok, max int) (Node, int) {  func parseStatementList(tokens *[]Token, tok, max int) (Node, int) {  	out := Node{} -	out.Data = Token{Type: 10, Data: "statement"} +	out.Data = Token{Type: 10, Data: "slist"}  	var tmp Node -	c := getClosing((*tokens)[tok].Data) +	if (*tokens)[tok].Data == ")" || (*tokens)[tok].Data == "}" || (*tokens)[tok].Data == "]" { +		return out, tok +	} -	tok++ +	tmp, tok = parseStatement(tokens, tok, max) +	out.Sub = append(out.Sub, tmp) -	for ; tok < max; tok++ { +	for ; tok < max; {  		t := (*tokens)[tok] -		switch t.Data { -		case c: +		switch t.Type { +		case DELIMIT:  			return out, tok -		case ",": +		case LINESEP:  			tok++ +			tmp, tok = parseStatement(tokens, tok, max) +			out.Sub = append(out.Sub, tmp)  		default:  			errOut("Error: unexpected token when parsing a list of statements", t)  		} - -		tmp, tok = parseStatement(tokens, tok, max) -		out.Sub = append(out.Sub, tmp)  	}  	return out, tok diff --git a/src/tparse/tree-preproc.go b/src/tparse/tree-preproc.go index a3e65f5..8fec30c 100644 --- a/src/tparse/tree-preproc.go +++ b/src/tparse/tree-preproc.go @@ -25,7 +25,7 @@ func parsePreBlock (tokens *[]Token, tok, max int) (Node, int) {  	for ; tok < max; tok++ {  		t := (*tokens)[tok] -		if t.Data == ":/" { +		if t.Data == ":/" || t.Data == ":;" {  			break  		} diff --git a/src/tparse/tree-statement.go b/src/tparse/tree-statement.go index 110d859..277cd95 100644 --- a/src/tparse/tree-statement.go +++ b/src/tparse/tree-statement.go @@ -18,21 +18,32 @@ package tparse  // TODO: re-validate this code.  I forgot if it works or not.  func parseBlock(tokens *[]Token, tok, max int) (Node, int) { -	out, tmp, def, name := Node{}, Node{}, Node{}, false +	out, tmp, def, name, sparse := Node{}, Node{}, Node{}, false, false  	out.Data = Token{Type: 10, Data: "block"}  	out.IsBlock = true  	def.Data = Token{Type: 10, Data: "bdef"} +  	for ;tok < max; tok++{  		t := (*tokens)[tok]  		tmp = Node{}  		switch t.Type {  		case DELIMIT:  			if t.Data == "(" { -				tmp, tok = parseParamList(tokens, tok + 1, max) +				if sparse { +					tmp, tok = parseStatementList(tokens, tok + 1, max) +				} else { +					tmp, tok = parseParamList(tokens, tok + 1, max) +				} +				tmp.Data.Data = "()"  				def.Sub = append(def.Sub, tmp)  			} else if t.Data == "[" { -				tmp, tok = parseTypeList(tokens, tok + 1, max) +				if sparse { +					tmp, tok = parseStatementList(tokens, tok + 1, max) +				} else { +					tmp, tok = parseTypeList(tokens, tok + 1, max) +				} +				tmp.Data.Data = "[]"  				def.Sub = append(def.Sub, tmp)  			} else {  				goto BREAK @@ -50,6 +61,8 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) {  			}  			switch t.Data {  			case "else": +				name = true +				sparse = true  				if (*tokens)[tok+1].Data == "if" {  					tmp.Data = Token{KEYWORD, "elif", t.Line, t.Char}  					def.Sub = append(def.Sub, tmp) @@ -58,6 +71,7 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) {  				}  			case "if", "match", "case", "loop":  				name = true +				sparse = true  				fallthrough  			case "export", "inline", "raw", "override":  				tmp.Data = t @@ -83,7 +97,7 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) {  		case ";":  			tmp, tok = parseStatement(tokens, tok + 1, max) -		case "/;": +		case "/;", ":;":  			REBLOCK:  			tmp, tok = parseBlock(tokens, tok + 1, max) @@ -94,6 +108,10 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) {  			} else if (*tokens)[tok].Data == ";/" {  				tok++  			} +		case "/:": +			tmp, tok = parsePreBlock(tokens, tok + 1, max) +		case ":": +			tmp, tok = parsePre(tokens, tok + 1, max)  		default:  			errOut("Error: unexpected token when parsing a code block", t)  		} @@ -107,7 +125,7 @@ func parseBlock(tokens *[]Token, tok, max int) (Node, int) {  // This should work once isTypeThenValue properly functions  func parseStatement(tokens *[]Token, tok, max int) (Node, int) {  	out := Node{} -	out.Data = Token{Type: 11, Data: ";"} +	out.Data = Token{Type: 11, Data: "value"}  	var tmp Node  	// Check for keyword, definition, then if none of those apply, assume it's a value. @@ -121,7 +139,6 @@ func parseStatement(tokens *[]Token, tok, max int) (Node, int) {  		} else {  			// if not, parse a value  			tmp, tok = parseValue(tokens, tok, max) -			out.Data.Data = "value"  		}  		out.Sub = append(out.Sub, tmp)  	} @@ -160,7 +177,7 @@ func keywordStatement(tokens *[]Token, tok, max int) (Node, int) {  		out.Sub = append(out.Sub, tmp)  		tok++  		if (*tokens)[tok].Data == "(" { -			tmp, tok = parseValueList(tokens, tok, max) +			tmp, tok = parseValueList(tokens, tok + 1, max)  			out.Sub = append(out.Sub, tmp)  		} @@ -168,7 +185,7 @@ func keywordStatement(tokens *[]Token, tok, max int) (Node, int) {  			errOut("Could not find struct member list", (*tokens)[tok])  		} -		tmp, tok = parseParamList(tokens, tok, max) +		tmp, tok = parseParamList(tokens, tok + 1, max)  	case "goto", "label":  		if (*tokens)[tok].Type != DEFWORD { diff --git a/src/tparse/tree.go b/src/tparse/tree.go index 03d2a3d..30d6b0c 100644 --- a/src/tparse/tree.go +++ b/src/tparse/tree.go @@ -59,7 +59,7 @@ func MakeTree(tokens *[]Token, file string) Node {  			}  		case ";":  			tmp, tok = parseStatement(tokens, tok + 1, max) -		case "/:": +		case "/:", ";:":  			tmp, tok = parsePreBlock(tokens, tok + 1, max)  		case ":":  			tmp, tok = parsePre(tokens, tok + 1, max) |