Skip to content

Commit

Permalink
completed lexer and tokens for the language spec
Browse files Browse the repository at this point in the history
  • Loading branch information
mcapell committed Aug 22, 2024
1 parent 9ca8248 commit 9ed1b70
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
38 changes: 35 additions & 3 deletions lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,33 @@ func (l *Lexer) NextToken() token.Token {

switch l.ch {
case '=':
tok = newToken(token.ASSIGN, l.ch)
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 '*':
tok = newToken(token.ASTERISK, l.ch)
case '<':
tok = newToken(token.LT, l.ch)
case '>':
tok = newToken(token.GT, l.ch)
case ';':
tok = newToken(token.SEMICOLON, l.ch)
case '(':
Expand All @@ -44,8 +70,6 @@ func (l *Lexer) NextToken() token.Token {
tok = newToken(token.RPAREN, l.ch)
case ',':
tok = newToken(token.COMMA, l.ch)
case '+':
tok = newToken(token.PLUS, l.ch)
case '{':
tok = newToken(token.LBRACE, l.ch)
case '}':
Expand Down Expand Up @@ -90,6 +114,14 @@ func (l *Lexer) readNumber() string {
return l.input[position:l.position]
}

func (l *Lexer) peekChar() byte {
if l.readPosition >= len(l.input) {
return 0
}

return l.input[l.readPosition]
}

func (l *Lexer) skipWhitespace() {
for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' {
l.readChar()
Expand Down
51 changes: 50 additions & 1 deletion lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,19 @@ let add = fn(x, y) {
x + y;
};
let result = add(five, ten);`
let result = add(five, ten);
!-/*5;
5 < 10 > 5;
if (5 < 10) {
return true;
} else {
return false;
}
10 == 10;
10 != 9;
`

tests := []struct {
expectedType token.TokenType
Expand Down Expand Up @@ -56,6 +68,43 @@ let result = add(five, ten);`
{token.IDENT, "ten"},
{token.RPAREN, ")"},
{token.SEMICOLON, ";"},
{token.BANG, "!"},
{token.MINUS, "-"},
{token.SLASH, "/"},
{token.ASTERISK, "*"},
{token.INT, "5"},
{token.SEMICOLON, ";"},
{token.INT, "5"},
{token.LT, "<"},
{token.INT, "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, ""},
}

Expand Down
27 changes: 23 additions & 4 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ const (
INT = "INT" // Integer

// Operators
ASSIGN = "="
PLUS = "+"
ASSIGN = "="
PLUS = "+"
MINUS = "-"
BANG = "!"
ASTERISK = "*"
SLASH = "/"

LT = "<"
GT = ">"
EQ = "=="
NOT_EQ = "!="

// Delimiters
COMMA = ","
Expand All @@ -31,11 +40,21 @@ const (
// Keywords
FUNCTION = "FUNCTION"
LET = "LET"
TRUE = "TRUE"
FALSE = "FALSE"
IF = "IF"
ELSE = "ELSE"
RETURN = "RETURN"
)

var keywords = map[string]TokenType{
"fn": FUNCTION,
"let": LET,
"fn": FUNCTION,
"let": LET,
"true": TRUE,
"false": FALSE,
"if": IF,
"else": ELSE,
"return": RETURN,
}

func LookupIdent(ident string) TokenType {
Expand Down

0 comments on commit 9ed1b70

Please sign in to comment.