This repository has been archived on 2026-05-13. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
sfdl/parser/ast.go
2025-12-04 18:20:07 +08:00

53 lines
884 B
Go

package parser
import "sfdl/scanner"
type ExprNode struct {
Op scanner.TokenType
Left *ExprNode
Right *ExprNode
Value float64
Func func(float64) float64
}
func NewConstNode(v float64) *ExprNode {
return &ExprNode{
Op: scanner.CONST_ID,
Value: v,
}
}
func NewTNode() *ExprNode {
return &ExprNode{
Op: scanner.T,
}
}
func NewFuncNode(fn func(float64) float64, child *ExprNode) *ExprNode {
return &ExprNode{
Op: scanner.FUNC,
Left: child,
Func: fn,
}
}
func NewBinaryNode(op scanner.TokenType, left, right *ExprNode) *ExprNode {
return &ExprNode{
Op: op,
Left: left,
Right: right,
}
}
func NewUnaryNode(op scanner.TokenType, child *ExprNode) *ExprNode {
if op == scanner.PLUS {
return child
}
// -x -> 0 - x
if op == scanner.MINUS {
zero := NewConstNode(0)
return NewBinaryNode(scanner.MINUS, zero, child)
}
return child
}