Expressions now support function definition

This commit is contained in:
2024-04-02 04:36:03 +02:00
parent f58ec3ac32
commit 072dab4144
20 changed files with 376 additions and 143 deletions
+17 -24
View File
@@ -5,7 +5,6 @@
package expr
import (
"fmt"
"strings"
)
@@ -146,14 +145,6 @@ func (self *term) isLeaf() bool {
return self.position == posLeaf
}
// func (self *term) getKind() termKind {
// return self.kind
// }
// func (self *term) getClass() termClass {
// return self.class
// }
func (self *term) getPriority() termPriority {
return self.priority
}
@@ -165,21 +156,17 @@ func (self *term) setParent(parent *term) {
}
}
// func (self *term) isOperand() bool {
// return self.getClass() != classOperator
// }
// func (self *term) isOperator() bool {
// return self.getClass() == classOperator
// }
func (self *term) source() string {
return self.tk.source
}
func (self *term) value() any {
return self.tk.Value
}
func (self *term) compute(ctx exprContext) (v any, err error) {
if self.evalFunc == nil {
err = fmt.Errorf("undefined eval-func for %v term type", self.kind)
err = self.tk.Errorf("undefined eval-func for %v term type", self.kind)
} else {
v, err = self.evalFunc(ctx, self)
}
@@ -187,7 +174,7 @@ func (self *term) compute(ctx exprContext) (v any, err error) {
}
func (self *term) errIncompatibleTypes(leftValue, rightValue any) error {
return fmt.Errorf(
return self.tk.Errorf(
"left operand '%v' [%T] and right operand '%v' [%T] are not compatible with operator %q",
leftValue, leftValue,
rightValue, rightValue,
@@ -195,23 +182,29 @@ func (self *term) errIncompatibleTypes(leftValue, rightValue any) error {
}
func (self *term) errIncompatibleType(value any) error {
return fmt.Errorf(
"prefix/postfix operator %q do not support operand '%v' [%T]", self.source(), value, value)
return self.tk.Errorf(
"prefix/postfix operator %q do not support operand '%v' [%T]",
self.source(), value, value)
}
func (self *term) Errorf(template string, args ...any) (err error) {
err = self.tk.Errorf(template, args...)
return
}
func (self *term) checkOperands() (err error) {
switch self.position {
case posInfix:
if self.children == nil || len(self.children) != 2 || self.children[0] == nil || self.children[1] == nil {
err = fmt.Errorf("infix operator %q requires two operands, got %d", self.source(), self.getChildrenCount())
err = self.tk.Errorf("infix operator %q requires two operands, got %d", self.source(), self.getChildrenCount())
}
case posPrefix:
if self.children == nil || len(self.children) != 1 || self.children[0] == nil {
err = fmt.Errorf("prefix operator %q requires one operand", self.tk.String())
err = self.tk.Errorf("prefix operator %q requires one operand", self.tk.String())
}
case posPostfix:
if self.children == nil || len(self.children) != 1 || self.children[0] == nil {
err = fmt.Errorf("postfix operator %q requires one operand", self.tk.String())
err = self.tk.Errorf("postfix operator %q requires one operand", self.tk.String())
}
}
return