aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo/const.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2023-01-11 23:34:42 +0100
committerRon Evans <[email protected]>2023-01-12 08:51:03 +0100
commite11df5c2123fccadd490f91b24d0342b4d9b2790 (patch)
tree0af50d6b8492b066021f8facc36d2467521e956b /cgo/const.go
parent656805d91f15725eda4aab62aaa9f8bfc58ca294 (diff)
downloadtinygo-e11df5c2123fccadd490f91b24d0342b4d9b2790.tar.gz
tinygo-e11df5c2123fccadd490f91b24d0342b4d9b2790.zip
cgo: add support for bitwise operators
Diffstat (limited to 'cgo/const.go')
-rw-r--r--cgo/const.go27
1 files changed, 25 insertions, 2 deletions
diff --git a/cgo/const.go b/cgo/const.go
index 7501a62c8..6420af4b9 100644
--- a/cgo/const.go
+++ b/cgo/const.go
@@ -14,6 +14,9 @@ import (
var (
prefixParseFns map[token.Token]func(*tokenizer) (ast.Expr, *scanner.Error)
precedences = map[token.Token]int{
+ token.OR: precedenceOr,
+ token.XOR: precedenceXor,
+ token.AND: precedenceAnd,
token.ADD: precedenceAdd,
token.SUB: precedenceAdd,
token.MUL: precedenceMul,
@@ -24,6 +27,9 @@ var (
const (
precedenceLowest = iota + 1
+ precedenceOr
+ precedenceXor
+ precedenceAnd
precedenceAdd
precedenceMul
precedencePrefix
@@ -76,7 +82,7 @@ func parseConstExpr(t *tokenizer, precedence int) (ast.Expr, *scanner.Error) {
for t.peekToken != token.EOF && precedence < precedences[t.peekToken] {
switch t.peekToken {
- case token.ADD, token.SUB, token.MUL, token.QUO, token.REM:
+ case token.OR, token.XOR, token.AND, token.ADD, token.SUB, token.MUL, token.QUO, token.REM:
t.Next()
leftExpr, err = parseBinaryExpr(t, leftExpr)
}
@@ -199,7 +205,18 @@ func (t *tokenizer) Next() {
// https://en.cppreference.com/w/cpp/string/byte/isspace
t.peekPos++
t.buf = t.buf[1:]
- case c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/' || c == '%':
+ case len(t.buf) >= 2 && (string(t.buf[:2]) == "||" || string(t.buf[:2]) == "&&"):
+ // Two-character tokens.
+ switch c {
+ case '&':
+ t.peekToken = token.LAND
+ case '|':
+ t.peekToken = token.LOR
+ }
+ t.peekValue = t.buf[:2]
+ t.buf = t.buf[2:]
+ return
+ case c == '(' || c == ')' || c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '&' || c == '|' || c == '^':
// Single-character tokens.
// TODO: ++ (increment) and -- (decrement) operators.
switch c {
@@ -217,6 +234,12 @@ func (t *tokenizer) Next() {
t.peekToken = token.QUO
case '%':
t.peekToken = token.REM
+ case '&':
+ t.peekToken = token.AND
+ case '|':
+ t.peekToken = token.OR
+ case '^':
+ t.peekToken = token.XOR
}
t.peekValue = t.buf[:1]
t.buf = t.buf[1:]