Selector operator, multi-operand, added

This commit is contained in:
2024-04-08 22:16:07 +02:00
parent f74e523617
commit d3f388f7e1
10 changed files with 263 additions and 34 deletions
+19 -5
View File
@@ -41,6 +41,7 @@ const (
priRelational
priSum
priProduct
priSelector
priSign
priFact
priCoalesce
@@ -54,6 +55,7 @@ const (
posInfix
posPrefix
posPostfix
posMultifix
)
type evalFuncType func(ctx exprContext, self *term) (v any, err error)
@@ -196,21 +198,33 @@ func (self *term) Errorf(template string, args ...any) (err error) {
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 = self.tk.Errorf("infix operator %q requires two operands, got %d", self.source(), self.getChildrenCount())
if self.children == nil || len(self.children) != 2 || self.anyChildrenNil() {
err = self.tk.Errorf("infix operator %q requires two not nil operands, got %d", self.source(), self.getChildrenCount())
}
case posPrefix:
if self.children == nil || len(self.children) != 1 || self.children[0] == nil {
err = self.tk.Errorf("prefix operator %q requires one operand", self.tk.String())
err = self.tk.Errorf("prefix operator %q requires one not nil operand", self.tk.String())
}
case posPostfix:
if self.children == nil || len(self.children) != 1 || self.children[0] == nil {
err = self.tk.Errorf("postfix operator %q requires one operand", self.tk.String())
if self.children == nil || len(self.children) != 1 || self.anyChildrenNil() {
err = self.tk.Errorf("postfix operator %q requires one not nil operand", self.tk.String())
}
case posMultifix:
if self.children == nil || len(self.children) < 3 || self.anyChildrenNil() {
err = self.tk.Errorf("infix operator %q requires at least three not operands, got %d", self.source(), self.getChildrenCount())
}
}
return
}
func (self *term) anyChildrenNil() bool {
for _, child := range self.children {
if child == nil {
return true
}
}
return false
}
func (self *term) evalInfix(ctx exprContext) (leftValue, rightValue any, err error) {
if err = self.checkOperands(); err == nil {
if leftValue, err = self.children[0].compute(ctx); err == nil {