Added expression package
All checks were successful
/ test (push) Successful in 31s

This commit is contained in:
Eduard Urbach 2025-06-21 20:18:27 +02:00
parent 14bccadd0f
commit eb08b3bb37
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
10 changed files with 624 additions and 0 deletions

View file

@ -0,0 +1,94 @@
package expression
import (
"strings"
"git.urbach.dev/cli/q/src/token"
)
// Expression is a tree that can represent a mathematical expression with precedence levels.
type Expression struct {
Parent *Expression
Children []*Expression
Token token.Token
precedence int8
}
// AddChild adds a child to the expression.
func (expr *Expression) AddChild(child *Expression) {
if expr.Children == nil {
expr.Children = make([]*Expression, 0, 2)
}
expr.Children = append(expr.Children, child)
child.Parent = expr
}
// Reset resets all values to the default.
func (expr *Expression) Reset() {
expr.Parent = nil
if expr.Children != nil {
expr.Children = expr.Children[:0]
}
expr.Token.Reset()
expr.precedence = 0
}
// EachLeaf iterates through all leaves in the tree.
func (expr *Expression) EachLeaf(call func(*Expression) error) error {
if expr.IsLeaf() {
return call(expr)
}
for _, child := range expr.Children {
err := child.EachLeaf(call)
if err != nil {
return err
}
}
return nil
}
// RemoveChild removes a child from the expression.
func (expr *Expression) RemoveChild(child *Expression) {
for i, c := range expr.Children {
if c == child {
expr.Children = append(expr.Children[:i], expr.Children[i+1:]...)
child.Parent = nil
return
}
}
}
// InsertAbove replaces this expression in its parent's children with the given new parent,
// and attaches this expression as a child of the new parent. Effectively, it promotes the
// given tree above the current node. It assumes that the caller is the last child.
func (expr *Expression) InsertAbove(tree *Expression) {
if expr.Parent != nil {
expr.Parent.Children[len(expr.Parent.Children)-1] = tree
tree.Parent = expr.Parent
}
tree.AddChild(expr)
}
// IsLeaf returns true if the expression has no children.
func (expr *Expression) IsLeaf() bool {
return len(expr.Children) == 0
}
// LastChild returns the last child.
func (expr *Expression) LastChild() *Expression {
return expr.Children[len(expr.Children)-1]
}
// String generates a textual representation of the expression.
func (expr *Expression) String(source []byte) string {
builder := strings.Builder{}
expr.write(&builder, source)
return builder.String()
}

View file

@ -0,0 +1,182 @@
package expression_test
import (
"errors"
"testing"
"git.urbach.dev/cli/q/src/expression"
"git.urbach.dev/cli/q/src/token"
"git.urbach.dev/go/assert"
)
func TestParse(t *testing.T) {
tests := []struct {
Name string
Expression string
Result string
}{
{"Identity", "1", "1"},
{"Basic calculation", "1+2", "(+ 1 2)"},
{"Same operator", "1+2+3", "(+ (+ 1 2) 3)"},
{"Same operator 2", "1+2+3+4", "(+ (+ (+ 1 2) 3) 4)"},
{"Different operator", "1+2-3", "(- (+ 1 2) 3)"},
{"Different operator 2", "1+2-3+4", "(+ (- (+ 1 2) 3) 4)"},
{"Different operator 3", "1+2-3+4-5", "(- (+ (- (+ 1 2) 3) 4) 5)"},
{"Grouped identity", "(1)", "1"},
{"Grouped identity 2", "((1))", "1"},
{"Grouped identity 3", "(((1)))", "1"},
{"Adding identity", "(1)+(2)", "(+ 1 2)"},
{"Adding identity 2", "(1)+(2)+(3)", "(+ (+ 1 2) 3)"},
{"Adding identity 3", "(1)+(2)+(3)+(4)", "(+ (+ (+ 1 2) 3) 4)"},
{"Grouping", "(1+2)", "(+ 1 2)"},
{"Grouping 2", "(1+2+3)", "(+ (+ 1 2) 3)"},
{"Grouping 3", "((1)+(2)+(3))", "(+ (+ 1 2) 3)"},
{"Grouping left", "(1+2)*3", "(* (+ 1 2) 3)"},
{"Grouping right", "1*(2+3)", "(* 1 (+ 2 3))"},
{"Grouping same operator", "1+(2+3)", "(+ 1 (+ 2 3))"},
{"Grouping same operator 2", "1+(2+3)+(4+5)", "(+ (+ 1 (+ 2 3)) (+ 4 5))"},
{"Two groups", "(1+2)*(3+4)", "(* (+ 1 2) (+ 3 4))"},
{"Two groups 2", "(1+2-3)*(3+4-5)", "(* (- (+ 1 2) 3) (- (+ 3 4) 5))"},
{"Two groups 3", "(1+2)*(3+4-5)", "(* (+ 1 2) (- (+ 3 4) 5))"},
{"Operator priority", "1+2*3", "(+ 1 (* 2 3))"},
{"Operator priority 2", "1*2+3", "(+ (* 1 2) 3)"},
{"Operator priority 3", "1+2*3+4", "(+ (+ 1 (* 2 3)) 4)"},
{"Operator priority 4", "1+2*(3+4)+5", "(+ (+ 1 (* 2 (+ 3 4))) 5)"},
{"Operator priority 5", "1+2*3*4", "(+ 1 (* (* 2 3) 4))"},
{"Operator priority 6", "1+2*3+4*5", "(+ (+ 1 (* 2 3)) (* 4 5))"},
{"Operator priority 7", "1+2*3*4*5*6", "(+ 1 (* (* (* (* 2 3) 4) 5) 6))"},
{"Operator priority 8", "1*2*3+4*5*6", "(+ (* (* 1 2) 3) (* (* 4 5) 6))"},
{"Complex", "(1+2-3*4)*(5+6-7*8)", "(* (- (+ 1 2) (* 3 4)) (- (+ 5 6) (* 7 8)))"},
{"Complex 2", "(1+2*3-4)*(5+6*7-8)", "(* (- (+ 1 (* 2 3)) 4) (- (+ 5 (* 6 7)) 8))"},
{"Complex 3", "(1+2*3-4)*(5+6*7-8)+9-10*11", "(- (+ (* (- (+ 1 (* 2 3)) 4) (- (+ 5 (* 6 7)) 8)) 9) (* 10 11))"},
{"Unary not", "!", "!"},
{"Unary not 2", "!a", "(! a)"},
{"Unary not 3", "!(!a)", "(! (! a))"},
{"Unary not 4", "!(a||b)", "(! (|| a b))"},
{"Unary not 5", "a || !b", "(|| a (! b))"},
{"Unary minus", "-", "-"},
{"Unary minus 2", "-a", "(- a)"},
{"Unary minus 3", "-(-a)", "(- (- a))"},
{"Unary minus 4", "-a+b", "(+ (- a) b)"},
{"Unary minus 5", "-(a+b)", "(- (+ a b))"},
{"Unary minus 6", "a + -b", "(+ a (- b))"},
{"Unary minus 7", "-a + -b", "(+ (- a) (- b))"},
{"Assign bitwise operation", "a|=b", "(|= a b)"},
{"Assign bitwise operation 2", "a|=b<<c", "(|= a (<< b c))"},
{"Function calls", "a()", "(λ a)"},
{"Function calls 2", "a(1)", "(λ a 1)"},
{"Function calls 3", "a(1)+1", "(+ (λ a 1) 1)"},
{"Function calls 4", "1+a(1)", "(+ 1 (λ a 1))"},
{"Function calls 5", "a(1,2)", "(λ a 1 2)"},
{"Function calls 6", "a(1,2,3)", "(λ a 1 2 3)"},
{"Function calls 7", "a(1,2+2,3)", "(λ a 1 (+ 2 2) 3)"},
{"Function calls 8", "a(1,2+2,3+3)", "(λ a 1 (+ 2 2) (+ 3 3))"},
{"Function calls 9", "a(1+1,2,3)", "(λ a (+ 1 1) 2 3)"},
{"Function calls 10", "a(1+1,2+2,3+3)", "(λ a (+ 1 1) (+ 2 2) (+ 3 3))"},
{"Function calls 11", "a(b())", "(λ a (λ b))"},
{"Function calls 12", "a(b(),c())", "(λ a (λ b) (λ c))"},
{"Function calls 13", "a(b(),c(),d())", "(λ a (λ b) (λ c) (λ d))"},
{"Function calls 14", "a(b(1))", "(λ a (λ b 1))"},
{"Function calls 15", "a(b(1),c(2),d(3))", "(λ a (λ b 1) (λ c 2) (λ d 3))"},
{"Function calls 16", "a(b(1)+1)", "(λ a (+ (λ b 1) 1))"},
{"Function calls 17", "a(b(1)+1,c(2),d(3))", "(λ a (+ (λ b 1) 1) (λ c 2) (λ d 3))"},
{"Function calls 18", "a(b(1)*c(2))", "(λ a (* (λ b 1) (λ c 2)))"},
{"Function calls 19", "a(b(1)*c(2),d(3)+e(4),f(5)/f(6))", "(λ a (* (λ b 1) (λ c 2)) (+ (λ d 3) (λ e 4)) (/ (λ f 5) (λ f 6)))"},
{"Function calls 20", "a(b(1,2)+c(3,4)*d(5,6))", "(λ a (+ (λ b 1 2) (* (λ c 3 4) (λ d 5 6))))"},
{"Function calls 21", "a((b(1,2)+c(3,4))*d(5,6))", "(λ a (* (+ (λ b 1 2) (λ c 3 4)) (λ d 5 6)))"},
{"Function calls 22", "a((b(1,2)+c(3,4))*d(5,6),e())", "(λ a (* (+ (λ b 1 2) (λ c 3 4)) (λ d 5 6)) (λ e))"},
{"Function calls 23", "a((b(1,2)+c(3,4))*d(5,6),e(7+8,9-10*11,12))", "(λ a (* (+ (λ b 1 2) (λ c 3 4)) (λ d 5 6)) (λ e (+ 7 8) (- 9 (* 10 11)) 12))"},
{"Function calls 24", "a((b(1,2,bb())+c(3,4,cc(0)))*d(5,6,dd(0)),e(7+8,9-10*11,12,ee(0)))", "(λ a (* (+ (λ b 1 2 (λ bb)) (λ c 3 4 (λ cc 0))) (λ d 5 6 (λ dd 0))) (λ e (+ 7 8) (- 9 (* 10 11)) 12 (λ ee 0)))"},
{"Function calls 25", "a(1-2*3)", "(λ a (- 1 (* 2 3)))"},
{"Function calls 26", "1+2*a()+4", "(+ (+ 1 (* 2 (λ a))) 4)"},
{"Function calls 27", "a(b,c)*2+15*4", "(+ (* (λ a b c) 2) (* 15 4))"},
{"Package function calls", "a.b(c)", "(λ (. a b) c)"},
{"Package function calls 2", "a.b(c,d)", "(λ (. a b) c d)"},
{"Package function calls 3", "a.b.c(d,e)", "(λ (. (. a b) c) d e)"},
{"Array access", "a[0]", "(@ a 0)"},
{"Array access 2", "a[b+c]", "(@ a (+ b c))"},
{"Array access 3", "a.b[c]", "(@ (. a b) c)"},
{"Array access 4", "a.b[c+d]", "(@ (. a b) (+ c d))"},
{"Array access 5", "a()[b]", "(@ (λ a) b)"},
{"Array access 6", "a.b()[c]", "(@ (λ (. a b)) c)"},
{"Array access 7", "a.b(c)[d]", "(@ (λ (. a b) c) d)"},
{"Array access 8", "a.b(c)[d][e]", "(@ (@ (λ (. a b) c) d) e)"},
{"Array access 9", "a[0](1)[2](3)", "(λ (@ (λ (@ a 0) 1) 2) 3)"},
{"Dereferencing", "[a]", "(@ a)"},
{"Dereferencing 2", "[a+b]", "(@ (+ a b))"},
{"Dereferencing 3", "[a+b]=c", "(= (@ (+ a b)) c)"},
{"Dereferencing 4", "[a+b]=c+d", "(= (@ (+ a b)) (+ c d))"},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
src := []byte(test.Expression)
tokens := token.Tokenize(src)
expr := expression.Parse(tokens)
defer expr.Reset()
assert.NotNil(t, expr)
assert.Equal(t, expr.String(src), test.Result)
})
}
}
func TestEachLeaf(t *testing.T) {
src := []byte("(1+2-3*4)+(5*6-7+8)")
tokens := token.Tokenize(src)
expr := expression.Parse(tokens)
leaves := []string{}
err := expr.EachLeaf(func(leaf *expression.Expression) error {
leaves = append(leaves, leaf.Token.String(src))
return nil
})
assert.Nil(t, err)
assert.DeepEqual(t, leaves, []string{"1", "2", "3", "4", "5", "6", "7", "8"})
err = expr.EachLeaf(func(leaf *expression.Expression) error {
return errors.New("error")
})
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "error")
}
func TestRemoveChild(t *testing.T) {
src := []byte("(1+2-3*4)+(5*6-7+8)")
tokens := token.Tokenize(src)
expr := expression.Parse(tokens)
left := expr.Children[0]
right := expr.Children[1]
expr.RemoveChild(left)
assert.Equal(t, expr.Children[0], right)
}
func TestNilExpression(t *testing.T) {
src := []byte("")
tokens := token.Tokenize(src)
expr := expression.Parse(tokens)
assert.Nil(t, expr)
}
func TestNilGroup(t *testing.T) {
src := []byte("()")
tokens := token.Tokenize(src)
expr := expression.Parse(tokens)
assert.Nil(t, expr)
}

6
src/expression/New.go Normal file
View file

@ -0,0 +1,6 @@
package expression
// New creates a new expression.
func New() *Expression {
return &Expression{}
}

View file

@ -0,0 +1,8 @@
package expression
import "git.urbach.dev/cli/q/src/token"
// NewLeaf creates a new leaf node.
func NewLeaf(t token.Token) *Expression {
return &Expression{Token: t}
}

17
src/expression/NewList.go Normal file
View file

@ -0,0 +1,17 @@
package expression
import (
"git.urbach.dev/cli/q/src/token"
)
// NewList generates a list of expressions from comma separated parameters.
func NewList(tokens token.List) []*Expression {
var list []*Expression
for param := range tokens.Split {
expression := Parse(param)
list = append(list, expression)
}
return list
}

173
src/expression/Parse.go Normal file
View file

@ -0,0 +1,173 @@
package expression
import (
"math"
"git.urbach.dev/cli/q/src/token"
)
// Parse generates an expression tree from tokens.
func Parse(tokens []token.Token) *Expression {
var (
cursor *Expression
root *Expression
groupLevel = 0
groupPosition = 0
)
for i, t := range tokens {
if t.Kind == token.GroupStart || t.Kind == token.ArrayStart {
groupLevel++
if groupLevel == 1 {
groupPosition = i + 1
}
continue
}
if t.Kind == token.GroupEnd || t.Kind == token.ArrayEnd {
groupLevel--
if groupLevel != 0 {
continue
}
// Function call or array access
if isComplete(cursor) {
parameters := NewList(tokens[groupPosition:i])
node := New()
node.Token.Position = tokens[groupPosition].Position
switch t.Kind {
case token.GroupEnd:
node.Token.Kind = token.Call
case token.ArrayEnd:
node.Token.Kind = token.Array
}
node.precedence = precedence(node.Token.Kind)
if cursor.Token.IsOperator() && node.precedence > cursor.precedence {
cursor.LastChild().InsertAbove(node)
} else {
if cursor == root {
root = node
}
cursor.InsertAbove(node)
}
for _, param := range parameters {
node.AddChild(param)
}
cursor = node
continue
}
group := Parse(tokens[groupPosition:i])
if group == nil {
continue
}
group.precedence = math.MaxInt8
if cursor == nil {
if t.Kind == token.ArrayEnd {
cursor = New()
cursor.Token.Position = tokens[groupPosition].Position
cursor.Token.Kind = token.Array
cursor.precedence = precedence(token.Array)
cursor.AddChild(group)
root = cursor
} else {
cursor = group
root = group
}
} else {
cursor.AddChild(group)
}
continue
}
if groupLevel > 0 {
continue
}
if t.Kind == token.Identifier || t.Kind == token.Number || t.Kind == token.String || t.Kind == token.Rune {
if cursor != nil {
node := NewLeaf(t)
cursor.AddChild(node)
} else {
cursor = NewLeaf(t)
root = cursor
}
continue
}
if !t.IsOperator() {
continue
}
if cursor == nil {
cursor = NewLeaf(t)
cursor.precedence = precedence(t.Kind)
root = cursor
continue
}
node := NewLeaf(t)
node.precedence = precedence(t.Kind)
if cursor.Token.IsOperator() {
oldPrecedence := cursor.precedence
newPrecedence := node.precedence
if newPrecedence > oldPrecedence {
if len(cursor.Children) == numOperands(cursor.Token.Kind) {
cursor.LastChild().InsertAbove(node)
} else {
cursor.AddChild(node)
}
} else {
start := cursor
for start != nil {
precedence := start.precedence
if precedence < newPrecedence {
start.LastChild().InsertAbove(node)
break
}
if precedence == newPrecedence {
if start == root {
root = node
}
start.InsertAbove(node)
break
}
start = start.Parent
}
if start == nil {
root.InsertAbove(node)
root = node
}
}
} else {
node.AddChild(cursor)
root = node
}
cursor = node
}
return root
}

View file

@ -0,0 +1,17 @@
package expression_test
import (
"testing"
"git.urbach.dev/cli/q/src/expression"
"git.urbach.dev/cli/q/src/token"
)
func BenchmarkExpression(b *testing.B) {
src := []byte("(1+2-3*4)+(5*6-7+8)")
tokens := token.Tokenize(src)
for b.Loop() {
expression.Parse(tokens)
}
}

View file

@ -0,0 +1,27 @@
package expression
import "git.urbach.dev/cli/q/src/token"
// isComplete returns true if the expression is complete (a binary operation with a single operand is incomplete).
func isComplete(expr *Expression) bool {
if expr == nil {
return false
}
switch expr.Token.Kind {
case token.Identifier, token.Number, token.String:
// These aren't operators but they always count as complete expressions.
return true
case token.Call:
// Even though token.Call is an operator and could be handled by the upcoming branch,
// the number of operands is variable.
// Therefore we consider every single call expression as complete.
return true
}
if expr.Token.IsOperator() && len(expr.Children) == numOperands(expr.Token.Kind) {
return true
}
return false
}

View file

@ -0,0 +1,67 @@
package expression
import (
"math"
"git.urbach.dev/cli/q/src/token"
)
// operator represents an operator for mathematical expressions.
type operator struct {
Symbol string
Precedence int8
Operands int8
}
// operators defines the operators used in the language.
// The number corresponds to the operator priority and can not be zero.
var operators = [64]operator{
token.Dot: {".", 13, 2},
token.Call: {"λ", 12, 1},
token.Array: {"@", 12, 2},
token.Negate: {"-", 11, 1},
token.Not: {"!", 11, 1},
token.Mul: {"*", 10, 2},
token.Div: {"/", 10, 2},
token.Mod: {"%", 10, 2},
token.Add: {"+", 9, 2},
token.Sub: {"-", 9, 2},
token.Shr: {">>", 8, 2},
token.Shl: {"<<", 8, 2},
token.And: {"&", 7, 2},
token.Xor: {"^", 6, 2},
token.Or: {"|", 5, 2},
token.Greater: {">", 4, 2},
token.Less: {"<", 4, 2},
token.GreaterEqual: {">=", 4, 2},
token.LessEqual: {"<=", 4, 2},
token.Equal: {"==", 3, 2},
token.NotEqual: {"!=", 3, 2},
token.LogicalAnd: {"&&", 2, 2},
token.LogicalOr: {"||", 1, 2},
token.Range: {"..", 0, 2},
token.Separator: {",", 0, 2},
token.Assign: {"=", math.MinInt8, 2},
token.Define: {":=", math.MinInt8, 2},
token.AddAssign: {"+=", math.MinInt8, 2},
token.SubAssign: {"-=", math.MinInt8, 2},
token.MulAssign: {"*=", math.MinInt8, 2},
token.DivAssign: {"/=", math.MinInt8, 2},
token.ModAssign: {"%=", math.MinInt8, 2},
token.AndAssign: {"&=", math.MinInt8, 2},
token.OrAssign: {"|=", math.MinInt8, 2},
token.XorAssign: {"^=", math.MinInt8, 2},
token.ShrAssign: {">>=", math.MinInt8, 2},
token.ShlAssign: {"<<=", math.MinInt8, 2},
}
func numOperands(symbol token.Kind) int {
return int(operators[symbol].Operands)
}
func precedence(symbol token.Kind) int8 {
return operators[symbol].Precedence
}

33
src/expression/write.go Normal file
View file

@ -0,0 +1,33 @@
package expression
import (
"strings"
"git.urbach.dev/cli/q/src/token"
)
// write generates a textual representation of the expression.
func (expr *Expression) write(builder *strings.Builder, source []byte) {
if expr.IsLeaf() {
builder.WriteString(expr.Token.String(source))
return
}
builder.WriteByte('(')
switch expr.Token.Kind {
case token.Call:
builder.WriteString(operators[token.Call].Symbol)
case token.Array:
builder.WriteString(operators[token.Array].Symbol)
default:
builder.WriteString(expr.Token.String(source))
}
for _, child := range expr.Children {
builder.WriteByte(' ')
child.write(builder, source)
}
builder.WriteByte(')')
}