diff --git a/lexer/lexer.go b/lexer/lexer.go index 03565b6..b723f75 100644 --- a/lexer/lexer.go +++ b/lexer/lexer.go @@ -29,6 +29,8 @@ func (l *Lexer) readChar() { func (l *Lexer) NextToken() token.Token { var tok token.Token + l.skipWhitespace() + switch l.ch { case '=': tok = newToken(token.ASSIGN, l.ch) @@ -49,12 +51,56 @@ func (l *Lexer) NextToken() token.Token { case 0: tok.Literal = "" tok.Type = token.EOF + default: + if isLetter(l.ch) { + tok.Literal = l.readIdentifier() + tok.Type = token.LookupIdent(tok.Literal) + return tok + } else if isDigit(l.ch) { + tok.Type = token.INT + tok.Literal = l.readNumber() + return tok + } else { + tok = newToken(token.ILLEGAL, l.ch) + } } l.readChar() return tok } +func (l *Lexer) readNumber() string { + position := l.position + for isDigit(l.ch) { + l.readChar() + } + + return l.input[position:l.position] +} + +func (l *Lexer) readIdentifier() string { + position := l.position + for isLetter(l.ch) { + l.readChar() + } + + return l.input[position:l.position] +} + +func (l *Lexer) skipWhitespace() { + for l.ch == ' ' || l.ch == '\t' || l.ch == '\n' || l.ch == '\r' { + l.readChar() + } +} + +func isLetter(ch byte) bool { + return 'a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_' +} + +func isDigit(ch byte) bool { + return '0' <= ch && ch <= '9' +} + func newToken(tokenType token.TokenType, ch byte) token.Token { return token.Token{Type: tokenType, Literal: string(ch)} } diff --git a/lexer/lexer_test.go b/lexer/lexer_test.go index 5df7544..b01fba2 100644 --- a/lexer/lexer_test.go +++ b/lexer/lexer_test.go @@ -6,21 +6,56 @@ import ( "git.cybercyst.me/monkeylang/token" ) -func TextNextToken(t *testing.T) { - input := `=+(){},;` +func TestNextToken(t *testing.T) { + input := `let five = 5; +let ten = 10; +let add = fn(x, y) { + x + y; +}; +let result = add(five, ten); +` tests := []struct { expectedType token.TokenType expectedLiteral string }{ + {token.LET, "let"}, + {token.IDENT, "five"}, {token.ASSIGN, "="}, - {token.PLUS, "+"}, + {token.INT, "5"}, + {token.SEMICOLON, ";"}, + {token.LET, "let"}, + {token.IDENT, "ten"}, + {token.ASSIGN, "="}, + {token.INT, "10"}, + {token.SEMICOLON, ";"}, + {token.LET, "let"}, + {token.IDENT, "add"}, + {token.ASSIGN, "="}, + {token.FUNCTION, "fn"}, {token.LPAREN, "("}, + {token.IDENT, "x"}, + {token.COMMA, ","}, + {token.IDENT, "y"}, {token.RPAREN, ")"}, {token.LBRACE, "{"}, - {token.RBRACE, "}"}, - {token.COMMA, ","}, + {token.IDENT, "x"}, + {token.PLUS, "+"}, + {token.IDENT, "y"}, {token.SEMICOLON, ";"}, + {token.RBRACE, "}"}, + {token.SEMICOLON, ";"}, + {token.LET, "let"}, + {token.IDENT, "result"}, + {token.ASSIGN, "="}, + {token.IDENT, "add"}, + {token.LPAREN, "("}, + {token.IDENT, "five"}, + {token.COMMA, ","}, + {token.IDENT, "ten"}, + {token.RPAREN, ")"}, + {token.SEMICOLON, ";"}, + {token.EOF, ""}, } l := New(input) diff --git a/token/token.go b/token/token.go index 23c9a9a..d050230 100644 --- a/token/token.go +++ b/token/token.go @@ -29,3 +29,15 @@ const ( FUNCTION = "FUNCTION" LET = "LET" ) + +var keywords = map[string]TokenType{ + "fn": FUNCTION, + "let": LET, +} + +func LookupIdent(ident string) TokenType { + if tokType, ok := keywords[ident]; ok { + return tokType + } + return IDENT +}