diff options
| author | Kyle Gunger <kgunger12@gmail.com> | 2021-08-31 00:07:27 -0400 | 
|---|---|---|
| committer | Kyle Gunger <kgunger12@gmail.com> | 2021-08-31 00:07:27 -0400 | 
| commit | f0a18c2e6d0f53b2955c26b0dd58a4ccd719e287 (patch) | |
| tree | ad116624362c7c2380e02f6dc75b0dddc83143ca | |
| parent | ea5ef2fe245c09b35c783977928d6e995110cfb4 (diff) | |
Spaghetti code checks for delimiters in values
| -rw-r--r-- | src/tparse/tree-value.go | 76 | 
1 files changed, 70 insertions, 6 deletions
| diff --git a/src/tparse/tree-value.go b/src/tparse/tree-value.go index 7880a8b..ca7b20b 100644 --- a/src/tparse/tree-value.go +++ b/src/tparse/tree-value.go @@ -95,6 +95,17 @@ func parseUnaryOps(tokens *[]Token, tok, max int) (Node) {  	for ; tok < max && !val; tok++ {  		t := (*tokens)[tok]  		switch t.Type { +		case DELIMIT: +			var tmp Node +			switch t.Data { +			case "(": // Parenthetical value +				return parseBinaryOp(tokens, tok, max) +			case "{": // Array or struct evaluation +				tmp, _ = parseValueList(tokens, tok, max) +				return tmp +			default: +				errOut("Unexpected delimiter when parsing value", t) +			}  		case DEFWORD:  			fallthrough  		case LITERAL: @@ -125,7 +136,8 @@ func parseUnaryOps(tokens *[]Token, tok, max int) (Node) {  			var tmp Node  			switch t.Data {  			case "(": // Function call -				//TODO: parse list of values here +				tmp, tok = parseValueList(tokens, tok, max) +				out.Sub = append(out.Sub, tmp)  			case "[": // Typecasting  				tmp, tok = parseType(tokens, tok, max, false)  				out.Sub = append(out.Sub, tmp) @@ -158,13 +170,36 @@ func parseBinaryOp(tokens *[]Token, tok, max int) (Node) {  	out := Node{IsBlock: false}  	first := tok  	var high, highOrder, bincount int = first, 8, 0 +	var curl, brak, parn int = 0, 0, 0  	// Find first high-order op  	for ; tok < max; tok++ {  		t := (*tokens)[tok] -		if t.Type == AUGMENT { +		if t.Type == DELIMIT { +			switch t.Data { +			case "{": +				curl++ +			case "[": +				brak++ +			case "(": +				parn++ +			 +			case "}": +				curl-- +			case "]": +				brak-- +			case ")": +				parn-- +			} + +			if curl < 0 || brak < 0 || parn < 0 { +				if curl > 0 || brak > 0 || parn > 0 { +					errOut("Un-matched closing delimiter when parsing a type.", t) +				} +			} +		} else if t.Type == AUGMENT {  			order, prs := ORDER[t.Data] -			if !prs { +			if !prs || curl > 0 || brak > 0 || parn > 0 {  				continue  			} else if order > highOrder {  				high, highOrder = tok, order @@ -191,19 +226,48 @@ func parseBinaryOp(tokens *[]Token, tok, max int) (Node) {  // TODO: fix this  func parseValue(tokens *[]Token, tok, max int) (Node, int) {  	first := tok -	 +	var curl, brak, parn int = 0, 0, 0  	for ; tok < max; tok++ {  		t := (*tokens)[tok]  		switch t.Type {  		case LINESEP: +			if curl > 0 || brak > 0 || parn > 0 { +				errOut("Encountered end of statement before all delimiter pairs were closed while looking for the end of a value.", t) +			} +			fallthrough  		case INLNSEP: +			if curl > 0 || brak > 0 || parn > 0 { +				continue +			} +			goto PARSEBIN  		case DELIMIT: -		case AUGMENT: -		case LITERAL: +			switch t.Data { +			case "{": +				curl++ +			case "[": +				brak++ +			case "(": +				parn++ +			 +			case "}": +				curl-- +			case "]": +				brak-- +			case ")": +				parn-- +			} + +			if curl < 0 || brak < 0 || parn < 0 { +				if curl > 0 || brak > 0 || parn > 0 { +					errOut("Un-matched closing delimiter when parsing a value.", t) +				} +			}  		}  	} +	PARSEBIN: +  	return parseBinaryOp(tokens, first, tok), tok  } |