bitwise operators and boolean operators
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-23 10:00:02 -04:00
parent cbb430b47d
commit ef8c8f8f04
13 changed files with 427 additions and 188 deletions

View File

@@ -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()

View File

@@ -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, ""},
}