Skip to content

Commit

Permalink
Adding the parser identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
IAPOLINARIO committed Oct 22, 2020
1 parent a8f6c31 commit 4d4b705
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
37 changes: 36 additions & 1 deletion parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ type Parser struct {
infixParseFns map[token.TokenType]infixParseFn
}

const (
_ int = iota
LOWEST
EQUALS // ==
LESSGREATER // > or <
SUM // +
PRODUCT // *
PREFIX // -X or !X
CALL // myFunction(X)
)

type (
prefixParseFn func() ast.Expression
infixParseFn func(ast.Expression) ast.Expression
Expand All @@ -31,9 +42,24 @@ func New(l *lexer.Lexer) *Parser {
// Read two tokens, so curToken and peekToken are both set
p.nextToken()
p.nextToken()
p.prefixParseFns = make(map[token.TokenType]prefixParseFn)
p.registerPrefix(token.IDENTIFICADOR, p.parseIdentifier)
return p
}

func (p *Parser) parseIdentifier() ast.Expression {
return &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal}
}

func (p *Parser) parseExpression(precedence int) ast.Expression {
prefix := p.prefixParseFns[p.curToken.Type]
if prefix == nil {
return nil
}
leftExp := prefix()
return leftExp
}

func (p *Parser) Errors() []string {
return p.errors
}
Expand All @@ -56,7 +82,7 @@ func (p *Parser) parseStatement() ast.Statement {
case token.DEVORVE:
return p.parseDevorveStatement()
default:
return nil
return p.parseExpressionStatement()
}
}

Expand Down Expand Up @@ -128,3 +154,12 @@ func (p *Parser) registerPrefix(tokenType token.TokenType, fn prefixParseFn) {
func (p *Parser) registerInfix(tokenType token.TokenType, fn infixParseFn) {
p.infixParseFns[tokenType] = fn
}

func (p *Parser) parseExpressionStatement() *ast.ExpressionStatement {
stmt := &ast.ExpressionStatement{Token: p.curToken}
stmt.Expression = p.parseExpression(LOWEST)
if p.peekTokenIs(token.SEMICOLON) {
p.nextToken()
}
return stmt
}
29 changes: 29 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,32 @@ func TestReturnStatements(t *testing.T) {
}
}
}

func TestIdentifierExpression(t *testing.T) {
input := "foobar;"
l := lexer.New(input)
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])
}
ident, ok := stmt.Expression.(*ast.Identifier)
if !ok {
t.Fatalf("exp not *ast.Identifier. got=%T", stmt.Expression)
}
if ident.Value != "foobar" {
t.Errorf("ident.Value not %s. got=%s", "foobar", ident.Value)
}
if ident.TokenLiteral() != "foobar" {
t.Errorf("ident.TokenLiteral not %s. got=%s", "foobar",
ident.TokenLiteral())
}
}

0 comments on commit 4d4b705

Please sign in to comment.