Skip to content

Commit

Permalink
Refactor constant definition parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
aleury committed Jan 20, 2024
1 parent 72d09cf commit 4521be5
Showing 1 changed file with 2 additions and 20 deletions.
22 changes: 2 additions & 20 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ import (
"unicode/utf8"
)

var ErrInvalidOperand error = errors.New("invalid operand")
var ErrInvalidSyntax error = errors.New("invalid syntax")
var ErrInvalidIntegerLiteral error = errors.New("invalid integer literal")
var ErrInvalidConstDefinition error = errors.New("invalid constant definition")
var ErrInvalidVariableDefinition error = errors.New("invalid variable definition")

type expressionParserFn func() ast.Expression

Expand Down Expand Up @@ -93,23 +90,8 @@ func (p *Parser) parseVariableDefinitionStatement() ast.Statement {

func (p *Parser) parseConstantDefinitionStatement() ast.Statement {
stmt := ast.ConstantDefinitionStatement{Token: p.curToken}

if p.peekToken.Type != token.IDENT {
p.errors = append(p.errors, fmt.Errorf("%w: %s at line %d", ErrInvalidConstDefinition, p.peekToken.Literal, p.peekToken.Line))
return nil
}

p.nextToken()
stmt.Name = ast.Identifier{Token: p.curToken, Value: p.curToken.Literal}

if p.peekToken.Type != token.INT {
p.errors = append(p.errors, fmt.Errorf("%w: %s at line %d", ErrInvalidConstDefinition, p.peekToken.Literal, p.peekToken.Line))
return nil
}

p.nextToken()
stmt.Value = p.parseIntegerLiteral()

stmt.Name = p.expectOneOf(token.IDENT).(ast.Identifier)
stmt.Value = p.expectOneOf(token.INT)
return stmt
}

Expand Down

0 comments on commit 4521be5

Please sign in to comment.