true/false/nil
This commit is contained in:
parent
80b121a9ab
commit
e22a844dd4
@ -67,6 +67,25 @@ type StringLiteral struct {
|
|||||||
func (sl *StringLiteral) expressionNode() {}
|
func (sl *StringLiteral) expressionNode() {}
|
||||||
func (sl *StringLiteral) String() string { return fmt.Sprintf(`"%s"`, sl.Value) }
|
func (sl *StringLiteral) String() string { return fmt.Sprintf(`"%s"`, sl.Value) }
|
||||||
|
|
||||||
|
// BooleanLiteral represents boolean literals
|
||||||
|
type BooleanLiteral struct {
|
||||||
|
Value bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bl *BooleanLiteral) expressionNode() {}
|
||||||
|
func (bl *BooleanLiteral) String() string {
|
||||||
|
if bl.Value {
|
||||||
|
return "true"
|
||||||
|
}
|
||||||
|
return "false"
|
||||||
|
}
|
||||||
|
|
||||||
|
// NilLiteral represents nil literal
|
||||||
|
type NilLiteral struct{}
|
||||||
|
|
||||||
|
func (nl *NilLiteral) expressionNode() {}
|
||||||
|
func (nl *NilLiteral) String() string { return "nil" }
|
||||||
|
|
||||||
// InfixExpression represents binary operations
|
// InfixExpression represents binary operations
|
||||||
type InfixExpression struct {
|
type InfixExpression struct {
|
||||||
Left Expression
|
Left Expression
|
||||||
|
@ -29,6 +29,9 @@ func NewParser(lexer *Lexer) *Parser {
|
|||||||
p.registerPrefix(IDENT, p.parseIdentifier)
|
p.registerPrefix(IDENT, p.parseIdentifier)
|
||||||
p.registerPrefix(NUMBER, p.parseNumberLiteral)
|
p.registerPrefix(NUMBER, p.parseNumberLiteral)
|
||||||
p.registerPrefix(STRING, p.parseStringLiteral)
|
p.registerPrefix(STRING, p.parseStringLiteral)
|
||||||
|
p.registerPrefix(TRUE, p.parseBooleanLiteral)
|
||||||
|
p.registerPrefix(FALSE, p.parseBooleanLiteral)
|
||||||
|
p.registerPrefix(NIL, p.parseNilLiteral)
|
||||||
p.registerPrefix(LPAREN, p.parseGroupedExpression)
|
p.registerPrefix(LPAREN, p.parseGroupedExpression)
|
||||||
|
|
||||||
p.infixParseFns = make(map[TokenType]func(Expression) Expression)
|
p.infixParseFns = make(map[TokenType]func(Expression) Expression)
|
||||||
@ -153,6 +156,14 @@ func (p *Parser) parseStringLiteral() Expression {
|
|||||||
return &StringLiteral{Value: p.curToken.Literal}
|
return &StringLiteral{Value: p.curToken.Literal}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseBooleanLiteral() Expression {
|
||||||
|
return &BooleanLiteral{Value: p.curTokenIs(TRUE)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseNilLiteral() Expression {
|
||||||
|
return &NilLiteral{}
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Parser) parseGroupedExpression() Expression {
|
func (p *Parser) parseGroupedExpression() Expression {
|
||||||
p.nextToken()
|
p.nextToken()
|
||||||
|
|
||||||
|
@ -8,6 +8,9 @@ const (
|
|||||||
IDENT TokenType = iota
|
IDENT TokenType = iota
|
||||||
NUMBER
|
NUMBER
|
||||||
STRING
|
STRING
|
||||||
|
TRUE
|
||||||
|
FALSE
|
||||||
|
NIL
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
ASSIGN // =
|
ASSIGN // =
|
||||||
@ -59,7 +62,10 @@ var precedences = map[TokenType]Precedence{
|
|||||||
// lookupIdent checks if an identifier is a keyword
|
// lookupIdent checks if an identifier is a keyword
|
||||||
func lookupIdent(ident string) TokenType {
|
func lookupIdent(ident string) TokenType {
|
||||||
keywords := map[string]TokenType{
|
keywords := map[string]TokenType{
|
||||||
"var": VAR,
|
"var": VAR,
|
||||||
|
"true": TRUE,
|
||||||
|
"false": FALSE,
|
||||||
|
"nil": NIL,
|
||||||
}
|
}
|
||||||
|
|
||||||
if tok, ok := keywords[ident]; ok {
|
if tok, ok := keywords[ident]; ok {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user