bitwise operators and boolean operators
This commit is contained in:
@@ -74,7 +74,7 @@ func (l *Lexer) NextToken() token.Token {
|
||||
Literal: literal,
|
||||
}
|
||||
} else {
|
||||
tok = newToken(token.BANG, l.ch)
|
||||
tok = newToken(token.NOT, l.ch)
|
||||
}
|
||||
|
||||
case '/':
|
||||
@@ -83,12 +83,34 @@ func (l *Lexer) NextToken() token.Token {
|
||||
tok.Type = token.COMMENT
|
||||
tok.Literal = l.readLine()
|
||||
} else {
|
||||
tok = newToken(token.SLASH, l.ch)
|
||||
tok = newToken(token.DIVIDE, l.ch)
|
||||
}
|
||||
case '*':
|
||||
tok = newToken(token.ASTERISK, l.ch)
|
||||
tok = newToken(token.MULTIPLY, l.ch)
|
||||
case '%':
|
||||
tok = newToken(token.PERCENT, l.ch)
|
||||
tok = newToken(token.MODULO, l.ch)
|
||||
case '&':
|
||||
if l.peekChar() == '&' {
|
||||
ch := l.ch
|
||||
l.readChar()
|
||||
literal := string(ch) + string(l.ch)
|
||||
tok = token.Token{Type: token.AND, Literal: literal}
|
||||
} else {
|
||||
tok = newToken(token.BITWISE_AND, l.ch)
|
||||
}
|
||||
case '|':
|
||||
if l.peekChar() == '|' {
|
||||
ch := l.ch
|
||||
l.readChar()
|
||||
literal := string(ch) + string(l.ch)
|
||||
tok = token.Token{Type: token.OR, Literal: literal}
|
||||
} else {
|
||||
tok = newToken(token.BITWISE_OR, l.ch)
|
||||
}
|
||||
case '^':
|
||||
tok = newToken(token.BITWISE_XOR, l.ch)
|
||||
case '~':
|
||||
tok = newToken(token.BITWISE_NOT, l.ch)
|
||||
case '<':
|
||||
if l.peekChar() == '=' {
|
||||
l.readChar()
|
||||
|
||||
@@ -33,6 +33,8 @@ func TestNextToken(t *testing.T) {
|
||||
[1, 2];
|
||||
{"foo": "bar"}
|
||||
d.foo
|
||||
&|^~
|
||||
!&&||
|
||||
`
|
||||
|
||||
tests := []struct {
|
||||
@@ -74,10 +76,10 @@ func TestNextToken(t *testing.T) {
|
||||
{token.RPAREN, ")"},
|
||||
{token.SEMICOLON, ";"},
|
||||
|
||||
{token.BANG, "!"},
|
||||
{token.NOT, "!"},
|
||||
{token.MINUS, "-"},
|
||||
{token.SLASH, "/"},
|
||||
{token.ASTERISK, "*"},
|
||||
{token.DIVIDE, "/"},
|
||||
{token.MULTIPLY, "*"},
|
||||
{token.INT, "5"},
|
||||
{token.SEMICOLON, ";"},
|
||||
{token.INT, "5"},
|
||||
@@ -137,6 +139,13 @@ func TestNextToken(t *testing.T) {
|
||||
{token.IDENT, "d"},
|
||||
{token.DOT, "."},
|
||||
{token.IDENT, "foo"},
|
||||
{token.BitwiseAND, "&"},
|
||||
{token.BitwiseOR, "|"},
|
||||
{token.BitwiseXOR, "^"},
|
||||
{token.BitwiseNOT, "~"},
|
||||
{token.NOT, "!"},
|
||||
{token.AND, "&&"},
|
||||
{token.OR, "||"},
|
||||
{token.EOF, ""},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user