141 lines
3.4 KiB
Go
141 lines
3.4 KiB
Go
package parser
|
|
|
|
import "git.sharkk.net/Sharkk/Mako/lexer"
|
|
|
|
type Node interface {
|
|
TokenLiteral() string
|
|
}
|
|
|
|
type Statement interface {
|
|
Node
|
|
statementNode()
|
|
}
|
|
|
|
type Expression interface {
|
|
Node
|
|
expressionNode()
|
|
}
|
|
|
|
type Program struct {
|
|
Statements []Statement
|
|
}
|
|
|
|
func (p *Program) TokenLiteral() string {
|
|
if len(p.Statements) > 0 {
|
|
return p.Statements[0].TokenLiteral()
|
|
}
|
|
return ""
|
|
}
|
|
|
|
type VariableStatement struct {
|
|
Token lexer.Token
|
|
Name *Identifier
|
|
Value Expression
|
|
}
|
|
|
|
func (vs *VariableStatement) statementNode() {}
|
|
func (vs *VariableStatement) TokenLiteral() string { return vs.Token.Value }
|
|
|
|
type EchoStatement struct {
|
|
Token lexer.Token
|
|
Value Expression
|
|
}
|
|
|
|
func (es *EchoStatement) statementNode() {}
|
|
func (es *EchoStatement) TokenLiteral() string { return es.Token.Value }
|
|
|
|
type Identifier struct {
|
|
Token lexer.Token
|
|
Value string
|
|
}
|
|
|
|
func (i *Identifier) expressionNode() {}
|
|
func (i *Identifier) TokenLiteral() string { return i.Token.Value }
|
|
|
|
type StringLiteral struct {
|
|
Token lexer.Token
|
|
Value string
|
|
}
|
|
|
|
func (sl *StringLiteral) expressionNode() {}
|
|
func (sl *StringLiteral) TokenLiteral() string { return sl.Token.Value }
|
|
|
|
type NumberLiteral struct {
|
|
Token lexer.Token
|
|
Value float64
|
|
}
|
|
|
|
func (nl *NumberLiteral) expressionNode() {}
|
|
func (nl *NumberLiteral) TokenLiteral() string { return nl.Token.Value }
|
|
|
|
// TableLiteral represents a table: {key1 = val1, key2 = val2}
|
|
type TableLiteral struct {
|
|
Token lexer.Token
|
|
Pairs map[Expression]Expression
|
|
}
|
|
|
|
func (tl *TableLiteral) expressionNode() {}
|
|
func (tl *TableLiteral) TokenLiteral() string { return tl.Token.Value }
|
|
|
|
// IndexExpression represents table access: table[key]
|
|
type IndexExpression struct {
|
|
Token lexer.Token
|
|
Left Expression
|
|
Index Expression
|
|
}
|
|
|
|
func (ie *IndexExpression) expressionNode() {}
|
|
func (ie *IndexExpression) TokenLiteral() string { return ie.Token.Value }
|
|
|
|
// IndexAssignmentStatement represents: table[key] = value
|
|
type IndexAssignmentStatement struct {
|
|
Token lexer.Token
|
|
Left Expression
|
|
Index Expression
|
|
Value Expression
|
|
}
|
|
|
|
func (ias *IndexAssignmentStatement) statementNode() {}
|
|
func (ias *IndexAssignmentStatement) TokenLiteral() string { return ias.Token.Value }
|
|
|
|
// BlockStatement represents a block of code enclosed in braces
|
|
type BlockStatement struct {
|
|
Token lexer.Token
|
|
Statements []Statement
|
|
}
|
|
|
|
func (bs *BlockStatement) statementNode() {}
|
|
func (bs *BlockStatement) TokenLiteral() string { return bs.Token.Value }
|
|
|
|
// New AST nodes for arithmetic expressions
|
|
|
|
// InfixExpression represents binary operations like: a + b
|
|
type InfixExpression struct {
|
|
Token lexer.Token // The operator token, e.g. +
|
|
Left Expression
|
|
Operator string
|
|
Right Expression
|
|
}
|
|
|
|
func (ie *InfixExpression) expressionNode() {}
|
|
func (ie *InfixExpression) TokenLiteral() string { return ie.Token.Value }
|
|
|
|
// PrefixExpression represents unary operations like: -a
|
|
type PrefixExpression struct {
|
|
Token lexer.Token // The prefix token, e.g. -
|
|
Operator string
|
|
Right Expression
|
|
}
|
|
|
|
func (pe *PrefixExpression) expressionNode() {}
|
|
func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Value }
|
|
|
|
// GroupedExpression represents an expression in parentheses: (a + b)
|
|
type GroupedExpression struct {
|
|
Token lexer.Token // The '(' token
|
|
Expr Expression
|
|
}
|
|
|
|
func (ge *GroupedExpression) expressionNode() {}
|
|
func (ge *GroupedExpression) TokenLiteral() string { return ge.Token.Value }
|