aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-05-20 16:43:22 +0200
committerRon Evans <[email protected]>2021-05-21 17:54:13 +0200
commit711889bc3fc8237dfb56c79befa11f984fb3e605 (patch)
tree1bacca04d0e80770fcab02c420d5a073c2618378 /cgo
parent70f8eeaca0288af63ad31229acad96933799900d (diff)
downloadtinygo-711889bc3fc8237dfb56c79befa11f984fb3e605.tar.gz
tinygo-711889bc3fc8237dfb56c79befa11f984fb3e605.zip
cgo: implement prefix parsing
This implements expressions such as "-5" and "-5 - 2", in other words, negative numbers.
Diffstat (limited to 'cgo')
-rw-r--r--cgo/const.go12
-rw-r--r--cgo/const_test.go4
2 files changed, 16 insertions, 0 deletions
diff --git a/cgo/const.go b/cgo/const.go
index 245bb899f..7501a62c8 100644
--- a/cgo/const.go
+++ b/cgo/const.go
@@ -39,6 +39,7 @@ func init() {
token.STRING: parseBasicLit,
token.CHAR: parseBasicLit,
token.LPAREN: parseParenExpr,
+ token.SUB: parseUnaryExpr,
}
}
@@ -131,6 +132,17 @@ func parseBinaryExpr(t *tokenizer, left ast.Expr) (ast.Expr, *scanner.Error) {
return expression, err
}
+func parseUnaryExpr(t *tokenizer) (ast.Expr, *scanner.Error) {
+ expression := &ast.UnaryExpr{
+ OpPos: t.curPos,
+ Op: t.curToken,
+ }
+ t.Next()
+ x, err := parseConstExpr(t, precedencePrefix)
+ expression.X = x
+ return expression, err
+}
+
// unexpectedToken returns an error of the form "unexpected token FOO, expected
// BAR".
func unexpectedToken(t *tokenizer, expected token.Token) *scanner.Error {
diff --git a/cgo/const_test.go b/cgo/const_test.go
index c984ec6f6..a8fba70df 100644
--- a/cgo/const_test.go
+++ b/cgo/const_test.go
@@ -44,6 +44,10 @@ func TestParseConst(t *testing.T) {
{`(1 - 2) * 3`, `(1 - 2) * 3`},
{`1 * 2 - 3`, `1*2 - 3`},
{`1 * (2 - 3)`, `1 * (2 - 3)`},
+ // Unary operators.
+ {`-5`, `-5`},
+ {`-5-2`, `-5 - 2`},
+ {`5 - - 2`, `5 - -2`},
} {
fset := token.NewFileSet()
startPos := fset.AddFile("", -1, 1000).Pos(0)