add additional keywords and evaluators to lexer
This commit is contained in:
15
.idea/git_toolbox_prj.xml
generated
Normal file
15
.idea/git_toolbox_prj.xml
generated
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="GitToolBoxProjectSettings">
|
||||||
|
<option name="commitMessageIssueKeyValidationOverride">
|
||||||
|
<BoolValueOverride>
|
||||||
|
<option name="enabled" value="true" />
|
||||||
|
</BoolValueOverride>
|
||||||
|
</option>
|
||||||
|
<option name="commitMessageValidationEnabledOverride">
|
||||||
|
<BoolValueOverride>
|
||||||
|
<option name="enabled" value="true" />
|
||||||
|
</BoolValueOverride>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -25,6 +25,14 @@ func (l *Lexer) readChar() {
|
|||||||
l.readPosition += 1
|
l.readPosition += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Lexer) peekChar() byte {
|
||||||
|
if l.readPosition >= len(l.input) {
|
||||||
|
return 0
|
||||||
|
} else {
|
||||||
|
return l.input[l.readPosition]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (l *Lexer) NextToken() token.Token {
|
func (l *Lexer) NextToken() token.Token {
|
||||||
var tok token.Token
|
var tok token.Token
|
||||||
|
|
||||||
@@ -32,17 +40,50 @@ func (l *Lexer) NextToken() token.Token {
|
|||||||
|
|
||||||
switch l.ch {
|
switch l.ch {
|
||||||
case '=':
|
case '=':
|
||||||
|
if l.peekChar() == '=' {
|
||||||
|
ch := l.ch
|
||||||
|
l.readChar()
|
||||||
|
literal := string(ch) + string(l.ch)
|
||||||
|
tok = token.Token{
|
||||||
|
Type: token.EQ,
|
||||||
|
Literal: literal,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
tok = newToken(token.ASSIGN, l.ch)
|
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()
|
||||||
|
literal := string(ch) + string(l.ch)
|
||||||
|
tok = token.Token{
|
||||||
|
Type: token.NOT_EQ,
|
||||||
|
Literal: literal,
|
||||||
|
}
|
||||||
|
} 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 ';':
|
case ';':
|
||||||
tok = newToken(token.SEMICOLON, l.ch)
|
tok = newToken(token.SEMICOLON, l.ch)
|
||||||
|
case ',':
|
||||||
|
tok = newToken(token.COMMA, l.ch)
|
||||||
case '(':
|
case '(':
|
||||||
tok = newToken(token.LPAREN, l.ch)
|
tok = newToken(token.LPAREN, l.ch)
|
||||||
case ')':
|
case ')':
|
||||||
tok = newToken(token.RPAREN, l.ch)
|
tok = newToken(token.RPAREN, l.ch)
|
||||||
case ',':
|
|
||||||
tok = newToken(token.COMMA, l.ch)
|
|
||||||
case '+':
|
|
||||||
tok = newToken(token.PLUS, l.ch)
|
|
||||||
case '{':
|
case '{':
|
||||||
tok = newToken(token.LBRACE, l.ch)
|
tok = newToken(token.LBRACE, l.ch)
|
||||||
case '}':
|
case '}':
|
||||||
|
|||||||
@@ -14,6 +14,17 @@ let add = fn(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 {
|
tests := []struct {
|
||||||
@@ -56,6 +67,52 @@ let result = add(five, ten);
|
|||||||
{token.IDENT, "ten"},
|
{token.IDENT, "ten"},
|
||||||
{token.RPAREN, ")"},
|
{token.RPAREN, ")"},
|
||||||
{token.SEMICOLON, ";"},
|
{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, ""},
|
{token.EOF, ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,16 @@ const (
|
|||||||
// Operators
|
// Operators
|
||||||
ASSIGN = "="
|
ASSIGN = "="
|
||||||
PLUS = "+"
|
PLUS = "+"
|
||||||
|
MINUS = "-"
|
||||||
|
BANG = "!"
|
||||||
|
ASTERISK = "*"
|
||||||
|
SLASH = "/"
|
||||||
|
|
||||||
|
LT = "<"
|
||||||
|
GT = ">"
|
||||||
|
|
||||||
|
EQ = "=="
|
||||||
|
NOT_EQ = "!="
|
||||||
|
|
||||||
// Delimiters
|
// Delimiters
|
||||||
COMMA = ","
|
COMMA = ","
|
||||||
@@ -31,11 +41,21 @@ const (
|
|||||||
// Keywords
|
// Keywords
|
||||||
FUNCTION = "FUNCTION"
|
FUNCTION = "FUNCTION"
|
||||||
LET = "LET"
|
LET = "LET"
|
||||||
|
TRUE = "TRUE"
|
||||||
|
FALSE = "FALSE"
|
||||||
|
IF = "IF"
|
||||||
|
ELSE = "ELSE"
|
||||||
|
RETURN = "RETURN"
|
||||||
)
|
)
|
||||||
|
|
||||||
var keywords = map[string]TokenType{
|
var keywords = map[string]TokenType{
|
||||||
"fn": FUNCTION,
|
"fn": FUNCTION,
|
||||||
"let": LET,
|
"let": LET,
|
||||||
|
"true": TRUE,
|
||||||
|
"false": FALSE,
|
||||||
|
"if": IF,
|
||||||
|
"else": ELSE,
|
||||||
|
"return": RETURN,
|
||||||
}
|
}
|
||||||
|
|
||||||
func LookupIdent(ident string) TokenType {
|
func LookupIdent(ident string) TokenType {
|
||||||
|
|||||||
Reference in New Issue
Block a user