term.go: replaced self receiver
This commit is contained in:
parent
6b3bfa2a11
commit
3ebba83bce
150
term.go
150
term.go
@ -53,21 +53,21 @@ type term struct {
|
|||||||
evalFunc evalFuncType
|
evalFunc evalFuncType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) String() string {
|
func (term *term) String() string {
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
self.toString(&sb)
|
term.toString(&sb)
|
||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) toString(sb *strings.Builder) {
|
func (term *term) toString(sb *strings.Builder) {
|
||||||
if self.position == posLeaf {
|
if term.position == posLeaf {
|
||||||
sb.WriteString(self.tk.String())
|
sb.WriteString(term.tk.String())
|
||||||
} else {
|
} else {
|
||||||
sb.WriteByte('[')
|
sb.WriteByte('[')
|
||||||
sb.WriteString(self.tk.String())
|
sb.WriteString(term.tk.String())
|
||||||
if self.children != nil {
|
if term.children != nil {
|
||||||
sb.WriteByte('(')
|
sb.WriteByte('(')
|
||||||
for i, c := range self.children {
|
for i, c := range term.children {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
sb.WriteByte(' ')
|
sb.WriteByte(' ')
|
||||||
}
|
}
|
||||||
@ -79,17 +79,17 @@ func (self *term) toString(sb *strings.Builder) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) getChildrenCount() (count int) {
|
func (term *term) getChildrenCount() (count int) {
|
||||||
if self.position == posLeaf || self.children == nil {
|
if term.position == posLeaf || term.children == nil {
|
||||||
count = 0
|
count = 0
|
||||||
} else {
|
} else {
|
||||||
count = len(self.children)
|
count = len(term.children)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) getRoom() (room int) {
|
func (term *term) getRoom() (room int) {
|
||||||
switch self.position {
|
switch term.position {
|
||||||
case posLeaf:
|
case posLeaf:
|
||||||
room = 0
|
room = 0
|
||||||
case posInfix:
|
case posInfix:
|
||||||
@ -102,139 +102,139 @@ func (self *term) getRoom() (room int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) isComplete() bool {
|
func (term *term) isComplete() bool {
|
||||||
return self.getChildrenCount() == self.getRoom()
|
return term.getChildrenCount() == term.getRoom()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) removeLastChild() (child *term) {
|
func (term *term) removeLastChild() (child *term) {
|
||||||
if self.children != nil {
|
if term.children != nil {
|
||||||
child = self.children[len(self.children)-1]
|
child = term.children[len(term.children)-1]
|
||||||
self.children = self.children[0 : len(self.children)-1]
|
term.children = term.children[0 : len(term.children)-1]
|
||||||
} else {
|
} else {
|
||||||
panic("Can't get last child")
|
panic("Can't get last child")
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) isLeaf() bool {
|
func (term *term) isLeaf() bool {
|
||||||
return self.position == posLeaf
|
return term.position == posLeaf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) getPriority() termPriority {
|
func (term *term) getPriority() termPriority {
|
||||||
return self.priority
|
return term.priority
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) setParent(parent *term) {
|
func (term *term) setParent(parent *term) {
|
||||||
self.parent = parent
|
term.parent = parent
|
||||||
if parent != nil && len(parent.children) < cap(parent.children) {
|
if parent != nil && len(parent.children) < cap(parent.children) {
|
||||||
parent.children = append(parent.children, self)
|
parent.children = append(parent.children, term)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) symbol() Symbol {
|
func (term *term) symbol() Symbol {
|
||||||
return self.tk.Sym
|
return term.tk.Sym
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) source() string {
|
func (term *term) source() string {
|
||||||
return self.tk.source
|
return term.tk.source
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) value() any {
|
func (term *term) value() any {
|
||||||
return self.tk.Value
|
return term.tk.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) compute(ctx ExprContext) (v any, err error) {
|
func (term *term) compute(ctx ExprContext) (v any, err error) {
|
||||||
if self.evalFunc == nil {
|
if term.evalFunc == nil {
|
||||||
err = self.tk.Errorf("undefined eval-func for %q term", self.source())
|
err = term.tk.Errorf("undefined eval-func for %q term", term.source())
|
||||||
} else {
|
} else {
|
||||||
v, err = self.evalFunc(ctx, self)
|
v, err = term.evalFunc(ctx, term)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) toInt(computedValue any, valueDescription string) (i int, err error) {
|
// func (term *term) toInt(computedValue any, valueDescription string) (i int, err error) {
|
||||||
if index64, ok := computedValue.(int64); ok {
|
// if index64, ok := computedValue.(int64); ok {
|
||||||
i = int(index64)
|
// i = int(index64)
|
||||||
} else {
|
// } else {
|
||||||
err = self.Errorf("%s, got %s (%v)", valueDescription, TypeName(computedValue), computedValue)
|
// err = term.Errorf("%s, got %s (%v)", valueDescription, TypeName(computedValue), computedValue)
|
||||||
}
|
// }
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (self *term) errIncompatibleTypes(leftValue, rightValue any) error {
|
func (term *term) errIncompatibleTypes(leftValue, rightValue any) error {
|
||||||
leftType := TypeName(leftValue)
|
leftType := TypeName(leftValue)
|
||||||
leftText := getFormatted(leftValue, Truncate)
|
leftText := getFormatted(leftValue, Truncate)
|
||||||
rightType := TypeName(rightValue)
|
rightType := TypeName(rightValue)
|
||||||
rightText := getFormatted(rightValue, Truncate)
|
rightText := getFormatted(rightValue, Truncate)
|
||||||
return self.tk.Errorf(
|
return term.tk.Errorf(
|
||||||
"left operand '%s' [%s] and right operand '%s' [%s] are not compatible with operator %q",
|
"left operand '%s' [%s] and right operand '%s' [%s] are not compatible with operator %q",
|
||||||
leftText, leftType,
|
leftText, leftType,
|
||||||
rightText, rightType,
|
rightText, rightType,
|
||||||
self.source())
|
term.source())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) errIncompatibleType(value any) error {
|
func (term *term) errIncompatibleType(value any) error {
|
||||||
return self.tk.Errorf(
|
return term.tk.Errorf(
|
||||||
"prefix/postfix operator %q do not support operand '%v' [%s]",
|
"prefix/postfix operator %q do not support operand '%v' [%s]",
|
||||||
self.source(), value, TypeName(value))
|
term.source(), value, TypeName(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) Errorf(template string, args ...any) (err error) {
|
func (term *term) Errorf(template string, args ...any) (err error) {
|
||||||
err = self.tk.Errorf(template, args...)
|
err = term.tk.Errorf(template, args...)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) checkOperands() (err error) {
|
func (term *term) checkOperands() (err error) {
|
||||||
switch self.position {
|
switch term.position {
|
||||||
case posInfix:
|
case posInfix:
|
||||||
if self.children == nil || len(self.children) != 2 || self.anyChildrenNil() {
|
if term.children == nil || len(term.children) != 2 || term.anyChildrenNil() {
|
||||||
err = self.tk.Errorf("infix operator %q requires two non-nil operands, got %d", self.source(), self.getChildrenCount())
|
err = term.tk.Errorf("infix operator %q requires two non-nil operands, got %d", term.source(), term.getChildrenCount())
|
||||||
}
|
}
|
||||||
case posPrefix:
|
case posPrefix:
|
||||||
if self.children == nil || len(self.children) != 1 || self.children[0] == nil {
|
if term.children == nil || len(term.children) != 1 || term.children[0] == nil {
|
||||||
err = self.tk.Errorf("prefix operator %q requires one not nil operand", self.tk.String())
|
err = term.tk.Errorf("prefix operator %q requires one not nil operand", term.tk.String())
|
||||||
}
|
}
|
||||||
case posPostfix:
|
case posPostfix:
|
||||||
if self.children == nil || len(self.children) != 1 || self.anyChildrenNil() {
|
if term.children == nil || len(term.children) != 1 || term.anyChildrenNil() {
|
||||||
err = self.tk.Errorf("postfix operator %q requires one not nil operand", self.tk.String())
|
err = term.tk.Errorf("postfix operator %q requires one not nil operand", term.tk.String())
|
||||||
}
|
}
|
||||||
case posMultifix:
|
case posMultifix:
|
||||||
if self.children == nil || len(self.children) < 3 || self.anyChildrenNil() {
|
if term.children == nil || len(term.children) < 3 || term.anyChildrenNil() {
|
||||||
err = self.tk.Errorf("infix operator %q requires at least three not operands, got %d", self.source(), self.getChildrenCount())
|
err = term.tk.Errorf("infix operator %q requires at least three not operands, got %d", term.source(), term.getChildrenCount())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) anyChildrenNil() bool {
|
func (term *term) anyChildrenNil() bool {
|
||||||
for _, child := range self.children {
|
for _, child := range term.children {
|
||||||
if child == nil {
|
if child == nil {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
func (self *term) evalInfix(ctx ExprContext) (leftValue, rightValue any, err error) {
|
func (term *term) evalInfix(ctx ExprContext) (leftValue, rightValue any, err error) {
|
||||||
if err = self.checkOperands(); err == nil {
|
if err = term.checkOperands(); err == nil {
|
||||||
if leftValue, err = self.children[0].compute(ctx); err == nil {
|
if leftValue, err = term.children[0].compute(ctx); err == nil {
|
||||||
rightValue, err = self.children[1].compute(ctx)
|
rightValue, err = term.children[1].compute(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *term) evalPrefix(ctx ExprContext) (childValue any, err error) {
|
func (term *term) evalPrefix(ctx ExprContext) (childValue any, err error) {
|
||||||
if err = self.checkOperands(); err == nil {
|
if err = term.checkOperands(); err == nil {
|
||||||
childValue, err = self.children[0].compute(ctx)
|
childValue, err = term.children[0].compute(ctx)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE Temporary solution to support function parameters with default value
|
// NOTE Temporary solution to support function parameters with default value
|
||||||
|
|
||||||
func (self *term) forceChild(c *term) {
|
func (t *term) forceChild(c *term) {
|
||||||
if self.children == nil {
|
if t.children == nil {
|
||||||
self.children = make([]*term, 0, 1)
|
t.children = make([]*term, 0, 1)
|
||||||
}
|
}
|
||||||
self.children = append(self.children, c)
|
t.children = append(t.children, c)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user