248 lines
5.2 KiB
Go
248 lines
5.2 KiB
Go
package parser
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"git.sharkk.net/Sharkk/Mako/lexer"
|
|
)
|
|
|
|
type Parser struct {
|
|
l *lexer.Lexer
|
|
curToken lexer.Token
|
|
peekToken lexer.Token
|
|
errors []string
|
|
}
|
|
|
|
func New(l *lexer.Lexer) *Parser {
|
|
p := &Parser{l: l, errors: []string{}}
|
|
p.nextToken()
|
|
p.nextToken()
|
|
return p
|
|
}
|
|
|
|
func (p *Parser) nextToken() {
|
|
p.curToken = p.peekToken
|
|
p.peekToken = p.l.NextToken()
|
|
}
|
|
|
|
func (p *Parser) Errors() []string {
|
|
return p.errors
|
|
}
|
|
|
|
func (p *Parser) ParseProgram() *Program {
|
|
program := &Program{Statements: []Statement{}}
|
|
|
|
for p.curToken.Type != lexer.TokenEOF {
|
|
stmt := p.parseStatement()
|
|
if stmt != nil {
|
|
program.Statements = append(program.Statements, stmt)
|
|
}
|
|
p.nextToken()
|
|
}
|
|
|
|
return program
|
|
}
|
|
|
|
func (p *Parser) parseStatement() Statement {
|
|
switch p.curToken.Type {
|
|
case lexer.TokenIdentifier:
|
|
if p.peekToken.Type == lexer.TokenEqual {
|
|
return p.parseVariableStatement()
|
|
} else if p.peekToken.Type == lexer.TokenLeftBracket {
|
|
return p.parseIndexAssignmentStatement()
|
|
}
|
|
case lexer.TokenEcho:
|
|
return p.parseEchoStatement()
|
|
case lexer.TokenLeftBrace:
|
|
return p.parseBlockStatement()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Parser) parseBlockStatement() *BlockStatement {
|
|
block := &BlockStatement{Token: p.curToken}
|
|
block.Statements = []Statement{}
|
|
|
|
p.nextToken() // Skip '{'
|
|
|
|
for p.curToken.Type != lexer.TokenRightBrace && p.curToken.Type != lexer.TokenEOF {
|
|
stmt := p.parseStatement()
|
|
if stmt != nil {
|
|
block.Statements = append(block.Statements, stmt)
|
|
}
|
|
p.nextToken()
|
|
}
|
|
|
|
return block
|
|
}
|
|
|
|
func (p *Parser) parseVariableStatement() *VariableStatement {
|
|
stmt := &VariableStatement{Token: p.curToken}
|
|
|
|
stmt.Name = &Identifier{Token: p.curToken, Value: p.curToken.Value}
|
|
|
|
p.nextToken() // Skip identifier
|
|
p.nextToken() // Skip =
|
|
|
|
stmt.Value = p.parseExpression()
|
|
|
|
if p.peekToken.Type == lexer.TokenSemicolon {
|
|
p.nextToken()
|
|
}
|
|
|
|
return stmt
|
|
}
|
|
|
|
func (p *Parser) parseEchoStatement() *EchoStatement {
|
|
stmt := &EchoStatement{Token: p.curToken}
|
|
|
|
p.nextToken()
|
|
|
|
stmt.Value = p.parseExpression()
|
|
|
|
if p.peekToken.Type == lexer.TokenSemicolon {
|
|
p.nextToken()
|
|
}
|
|
|
|
return stmt
|
|
}
|
|
|
|
func (p *Parser) parseIndexAssignmentStatement() *IndexAssignmentStatement {
|
|
stmt := &IndexAssignmentStatement{
|
|
Token: p.curToken,
|
|
Left: &Identifier{Token: p.curToken, Value: p.curToken.Value},
|
|
}
|
|
|
|
p.nextToken() // Skip identifier
|
|
p.nextToken() // Skip '['
|
|
|
|
stmt.Index = p.parseExpression()
|
|
|
|
if p.peekToken.Type != lexer.TokenRightBracket {
|
|
p.errors = append(p.errors, "expected ] after index expression")
|
|
return stmt
|
|
}
|
|
|
|
p.nextToken() // Skip index
|
|
p.nextToken() // Skip ']'
|
|
|
|
// Fix: Check current token, not peek token
|
|
if p.curToken.Type != lexer.TokenEqual {
|
|
p.errors = append(p.errors, "expected = after index expression")
|
|
return stmt
|
|
}
|
|
|
|
p.nextToken() // Skip =
|
|
|
|
stmt.Value = p.parseExpression()
|
|
|
|
if p.peekToken.Type == lexer.TokenSemicolon {
|
|
p.nextToken()
|
|
}
|
|
|
|
return stmt
|
|
}
|
|
|
|
func (p *Parser) parseExpression() Expression {
|
|
switch p.curToken.Type {
|
|
case lexer.TokenString:
|
|
return &StringLiteral{Token: p.curToken, Value: p.curToken.Value}
|
|
case lexer.TokenNumber:
|
|
num, err := strconv.ParseFloat(p.curToken.Value, 64)
|
|
if err != nil {
|
|
p.errors = append(p.errors, fmt.Sprintf("could not parse %q as float", p.curToken.Value))
|
|
}
|
|
return &NumberLiteral{Token: p.curToken, Value: num}
|
|
case lexer.TokenIdentifier:
|
|
if p.peekToken.Type == lexer.TokenLeftBracket {
|
|
return p.parseIndexExpression()
|
|
}
|
|
return &Identifier{Token: p.curToken, Value: p.curToken.Value}
|
|
case lexer.TokenLeftBrace:
|
|
return p.parseTableLiteral()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Parser) parseTableLiteral() *TableLiteral {
|
|
table := &TableLiteral{
|
|
Token: p.curToken,
|
|
Pairs: make(map[Expression]Expression),
|
|
}
|
|
|
|
p.nextToken() // Skip '{'
|
|
|
|
if p.curToken.Type == lexer.TokenRightBrace {
|
|
return table // Empty table
|
|
}
|
|
|
|
// Parse the first key-value pair
|
|
key := p.parseExpression()
|
|
|
|
if p.peekToken.Type != lexer.TokenEqual {
|
|
p.errors = append(p.errors, "expected = after table key")
|
|
return table
|
|
}
|
|
|
|
p.nextToken() // Skip key
|
|
p.nextToken() // Skip =
|
|
|
|
value := p.parseExpression()
|
|
table.Pairs[key] = value
|
|
|
|
p.nextToken() // Skip value
|
|
|
|
// Parse remaining key-value pairs
|
|
for p.curToken.Type == lexer.TokenComma {
|
|
p.nextToken() // Skip comma
|
|
|
|
if p.curToken.Type == lexer.TokenRightBrace {
|
|
break // Allow trailing comma
|
|
}
|
|
|
|
key = p.parseExpression()
|
|
|
|
if p.peekToken.Type != lexer.TokenEqual {
|
|
p.errors = append(p.errors, "expected = after table key")
|
|
return table
|
|
}
|
|
|
|
p.nextToken() // Skip key
|
|
p.nextToken() // Skip =
|
|
|
|
value = p.parseExpression()
|
|
table.Pairs[key] = value
|
|
|
|
p.nextToken() // Skip value
|
|
}
|
|
|
|
if p.curToken.Type != lexer.TokenRightBrace {
|
|
p.errors = append(p.errors, "expected } or , after table entry")
|
|
}
|
|
|
|
return table
|
|
}
|
|
|
|
func (p *Parser) parseIndexExpression() *IndexExpression {
|
|
exp := &IndexExpression{
|
|
Token: p.curToken,
|
|
Left: &Identifier{Token: p.curToken, Value: p.curToken.Value},
|
|
}
|
|
|
|
p.nextToken() // Skip identifier
|
|
p.nextToken() // Skip '['
|
|
|
|
exp.Index = p.parseExpression()
|
|
|
|
if p.peekToken.Type != lexer.TokenRightBracket {
|
|
p.errors = append(p.errors, "expected ] after index expression")
|
|
return exp
|
|
}
|
|
|
|
p.nextToken() // Skip index
|
|
p.nextToken() // Skip ']'
|
|
|
|
return exp
|
|
}
|