aboutsummaryrefslogtreecommitdiff
path: root/syntax/scan.go
diff options
context:
space:
mode:
authorHittorp <38788498+Hittorp@users.noreply.github.com>2018-08-09 15:02:30 +0300
committeralandonovan <adonovan@google.com>2018-08-09 08:02:30 -0400
commit0a5e39a3470a7db0846fb24f517d5a41cd344f64 (patch)
treedd79a3fe16c8914f92bbd9f67863c85b4983844a /syntax/scan.go
parent878b17a75d1975b64e9242cce2178ea1f599b871 (diff)
downloadstarlark-go-0a5e39a3470a7db0846fb24f517d5a41cd344f64.tar.gz
Add missing bitwise ops: xor, unary not (~), and shifts (#117)
* implement bitwise xor for integers and symmetric difference for sets (^ and ^= operators) * implement unary ~ operator for integers * implement left and right bitwise shifts for integers * enable xor, unary not, and shifts bitwise ops only when -bitwise flag is set * enable bitwise & and | only when -bitwise flag is set * add &= and |= operators
Diffstat (limited to 'syntax/scan.go')
-rw-r--r--syntax/scan.go60
1 files changed, 53 insertions, 7 deletions
diff --git a/syntax/scan.go b/syntax/scan.go
index 3a0239a..d38db8d 100644
--- a/syntax/scan.go
+++ b/syntax/scan.go
@@ -44,6 +44,10 @@ const (
PERCENT // %
AMP // &
PIPE // |
+ CIRCUMFLEX // ^
+ LTLT // <<
+ GTGT // >>
+ TILDE // ~
DOT // .
COMMA // ,
EQ // =
@@ -61,12 +65,17 @@ const (
LE // <=
EQL // ==
NEQ // !=
- PLUS_EQ // += (keep order consistent with PLUS..PERCENT)
+ PLUS_EQ // += (keep order consistent with PLUS..GTGT)
MINUS_EQ // -=
STAR_EQ // *=
SLASH_EQ // /=
SLASHSLASH_EQ // //=
PERCENT_EQ // %=
+ AMP_EQ // &=
+ PIPE_EQ // |=
+ CIRCUMFLEX_EQ // ^=
+ LTLT_EQ // <<=
+ GTGT_EQ // >>=
STARSTAR // **
// Keywords
@@ -119,6 +128,10 @@ var tokenNames = [...]string{
PERCENT: "%",
AMP: "&",
PIPE: "|",
+ CIRCUMFLEX: "^",
+ LTLT: "<<",
+ GTGT: ">>",
+ TILDE: "~",
DOT: ".",
COMMA: ",",
EQ: "=",
@@ -142,6 +155,11 @@ var tokenNames = [...]string{
SLASH_EQ: "/=",
SLASHSLASH_EQ: "//=",
PERCENT_EQ: "%=",
+ AMP_EQ: "&=",
+ PIPE_EQ: "|=",
+ CIRCUMFLEX_EQ: "^=",
+ LTLT_EQ: "<<=",
+ GTGT_EQ: ">>=",
STARSTAR: "**",
AND: "and",
BREAK: "break",
@@ -624,7 +642,7 @@ start:
// other punctuation
defer sc.endToken(val)
switch c {
- case '=', '<', '>', '!', '+', '-', '%', '/': // possibly followed by '='
+ case '=', '<', '>', '!', '+', '-', '%', '/', '&', '|', '^', '~': // possibly followed by '='
start := sc.pos
sc.readRune()
if sc.peekRune() == '=' {
@@ -646,14 +664,38 @@ start:
return SLASH_EQ
case '%':
return PERCENT_EQ
+ case '&':
+ return AMP_EQ
+ case '|':
+ return PIPE_EQ
+ case '^':
+ return CIRCUMFLEX_EQ
}
}
switch c {
case '=':
return EQ
case '<':
+ if sc.peekRune() == '<' {
+ sc.readRune()
+ if sc.peekRune() == '=' {
+ sc.readRune()
+ return LTLT_EQ
+ } else {
+ return LTLT
+ }
+ }
return LT
case '>':
+ if sc.peekRune() == '>' {
+ sc.readRune()
+ if sc.peekRune() == '=' {
+ sc.readRune()
+ return GTGT_EQ
+ } else {
+ return GTGT
+ }
+ }
return GT
case '!':
sc.error(start, "unexpected input character '!'")
@@ -674,20 +716,24 @@ start:
return SLASH
case '%':
return PERCENT
+ case '&':
+ return AMP
+ case '|':
+ return PIPE
+ case '^':
+ return CIRCUMFLEX
+ case '~':
+ return TILDE
}
panic("unreachable")
- case ':', ';', '|', '&': // single-char tokens (except comma)
+ case ':', ';': // single-char tokens (except comma)
sc.readRune()
switch c {
case ':':
return COLON
case ';':
return SEMI
- case '|':
- return PIPE
- case '&':
- return AMP
}
panic("unreachable")