equals and such

This commit is contained in:
Tyrel Souza 2021-11-30 16:07:55 -05:00
parent e6ed97ba11
commit 7e9304ed11
3 changed files with 53 additions and 3 deletions

View File

@ -34,13 +34,25 @@ func (l *Lexer) NextToken() token.Token {
switch l.ch {
case '=':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
tok = token.Token{Type: token.EQ, Literal: string(ch) + string(l.ch)}
} else {
tok = newToken(token.ASSIGN, l.ch)
}
case '+':
tok = newToken(token.PLUS, l.ch)
case '-':
tok = newToken(token.MINUS, l.ch)
case '!':
if l.peekChar() == '=' {
ch := l.ch
l.readChar()
tok = token.Token{Type: token.NOT_EQ, Literal: string(ch) + string(l.ch)}
} else {
tok = newToken(token.BANG, l.ch)
}
case '/':
tok = newToken(token.SLASH, l.ch)
case '*':
@ -114,3 +126,11 @@ func (l *Lexer) readNumber() string {
}
return l.input[position:l.position]
}
func (l *Lexer) peekChar() byte {
if l.readPosition >= len(l.input) {
return 0
} else {
return l.input[l.readPosition]
}
}

View File

@ -22,7 +22,9 @@ if (5 < 10) {
} else {
return false;
}
// [...]
10 == 10;
10 != 9;
`
tests := []struct {
@ -77,6 +79,31 @@ if (5 < 10) {
{token.GT, ">"},
{token.INT, "5"},
{token.SEMICOLON, ";"},
{token.IF, "if"},
{token.LPAREN, "("},
{token.INT, "5"},
{token.LT, "<"},
{token.INT, "10"},
{token.RPAREN, ")"},
{token.LBRACE, "{"},
{token.RETURN, "return"},
{token.TRUE, "true"},
{token.SEMICOLON, ";"},
{token.RBRACE, "}"},
{token.ELSE, "else"},
{token.LBRACE, "{"},
{token.RETURN, "return"},
{token.FALSE, "false"},
{token.SEMICOLON, ";"},
{token.RBRACE, "}"},
{token.INT, "10"},
{token.EQ, "=="},
{token.INT, "10"},
{token.SEMICOLON, ";"},
{token.INT, "10"},
{token.NOT_EQ, "!="},
{token.INT, "9"},
{token.SEMICOLON, ";"},
{token.EOF, ""},
}

View File

@ -26,6 +26,9 @@ const (
LT = "<"
GT = ">"
EQ = "=="
NOT_EQ = "!="
//DELIM
COMMA = ""
SEMICOLON = ";"