Add null literal
Some checks failed
Build / build (push) Successful in 1m30s
Test / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-19 16:33:13 -04:00
parent 60d27f09d7
commit 97eeae0c1a
9 changed files with 59 additions and 0 deletions

View File

@@ -64,6 +64,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerPrefix(token.MINUS, p.parsePrefixExpression)
p.registerPrefix(token.TRUE, p.parseBoolean)
p.registerPrefix(token.FALSE, p.parseBoolean)
p.registerPrefix(token.NULL, p.parseNull)
p.registerPrefix(token.LPAREN, p.parseGroupedExpression)
p.registerPrefix(token.IF, p.parseIfExpression)
p.registerPrefix(token.FUNCTION, p.parseFunctionLiteral)
@@ -552,3 +553,7 @@ func (p *Parser) parseAssignmentStatement() ast.Statement {
func (p *Parser) parseComment() ast.Statement {
return &ast.Comment{Token: p.curToken, Value: p.curToken.Literal}
}
func (p *Parser) parseNull() ast.Expression {
return &ast.Null{Token: p.curToken}
}

View File

@@ -458,6 +458,29 @@ func TestIfExpression(t *testing.T) {
}
}
func TestNullExpression(t *testing.T) {
l := lexer.New("null")
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)
if len(program.Statements) != 1 {
t.Fatalf("program has not enough statements. got=%d",
len(program.Statements))
}
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T",
program.Statements[0])
}
_, ok = stmt.Expression.(*ast.Null)
if !ok {
t.Fatalf("exp not *ast.Null. got=%T", stmt.Expression)
}
}
func TestIfElseExpression(t *testing.T) {
input := `if (x < y) { x } else { y }`