From 3ebba83bceb73d53fca9b955cabc795f287b9b57 Mon Sep 17 00:00:00 2001 From: Celestino Amoroso <celestino.amoroso@gmail.com> Date: Tue, 9 Jul 2024 07:50:50 +0200 Subject: [PATCH] term.go: replaced self receiver --- term.go | 150 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 75 insertions(+), 75 deletions(-) diff --git a/term.go b/term.go index 73df20f..62af546 100644 --- a/term.go +++ b/term.go @@ -53,21 +53,21 @@ type term struct { evalFunc evalFuncType } -func (self *term) String() string { +func (term *term) String() string { var sb strings.Builder - self.toString(&sb) + term.toString(&sb) return sb.String() } -func (self *term) toString(sb *strings.Builder) { - if self.position == posLeaf { - sb.WriteString(self.tk.String()) +func (term *term) toString(sb *strings.Builder) { + if term.position == posLeaf { + sb.WriteString(term.tk.String()) } else { sb.WriteByte('[') - sb.WriteString(self.tk.String()) - if self.children != nil { + sb.WriteString(term.tk.String()) + if term.children != nil { sb.WriteByte('(') - for i, c := range self.children { + for i, c := range term.children { if i > 0 { sb.WriteByte(' ') } @@ -79,17 +79,17 @@ func (self *term) toString(sb *strings.Builder) { } } -func (self *term) getChildrenCount() (count int) { - if self.position == posLeaf || self.children == nil { +func (term *term) getChildrenCount() (count int) { + if term.position == posLeaf || term.children == nil { count = 0 } else { - count = len(self.children) + count = len(term.children) } return } -func (self *term) getRoom() (room int) { - switch self.position { +func (term *term) getRoom() (room int) { + switch term.position { case posLeaf: room = 0 case posInfix: @@ -102,139 +102,139 @@ func (self *term) getRoom() (room int) { return } -func (self *term) isComplete() bool { - return self.getChildrenCount() == self.getRoom() +func (term *term) isComplete() bool { + return term.getChildrenCount() == term.getRoom() } -func (self *term) removeLastChild() (child *term) { - if self.children != nil { - child = self.children[len(self.children)-1] - self.children = self.children[0 : len(self.children)-1] +func (term *term) removeLastChild() (child *term) { + if term.children != nil { + child = term.children[len(term.children)-1] + term.children = term.children[0 : len(term.children)-1] } else { panic("Can't get last child") } return } -func (self *term) isLeaf() bool { - return self.position == posLeaf +func (term *term) isLeaf() bool { + return term.position == posLeaf } -func (self *term) getPriority() termPriority { - return self.priority +func (term *term) getPriority() termPriority { + return term.priority } -func (self *term) setParent(parent *term) { - self.parent = parent +func (term *term) setParent(parent *term) { + term.parent = parent 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 { - return self.tk.Sym +func (term *term) symbol() Symbol { + return term.tk.Sym } -func (self *term) source() string { - return self.tk.source +func (term *term) source() string { + return term.tk.source } -func (self *term) value() any { - return self.tk.Value +func (term *term) value() any { + return term.tk.Value } -func (self *term) compute(ctx ExprContext) (v any, err error) { - if self.evalFunc == nil { - err = self.tk.Errorf("undefined eval-func for %q term", self.source()) +func (term *term) compute(ctx ExprContext) (v any, err error) { + if term.evalFunc == nil { + err = term.tk.Errorf("undefined eval-func for %q term", term.source()) } else { - v, err = self.evalFunc(ctx, self) + v, err = term.evalFunc(ctx, term) } return } -func (self *term) toInt(computedValue any, valueDescription string) (i int, err error) { - if index64, ok := computedValue.(int64); ok { - i = int(index64) - } else { - err = self.Errorf("%s, got %s (%v)", valueDescription, TypeName(computedValue), computedValue) - } - return -} +// func (term *term) toInt(computedValue any, valueDescription string) (i int, err error) { +// if index64, ok := computedValue.(int64); ok { +// i = int(index64) +// } else { +// err = term.Errorf("%s, got %s (%v)", valueDescription, TypeName(computedValue), computedValue) +// } +// return +// } -func (self *term) errIncompatibleTypes(leftValue, rightValue any) error { +func (term *term) errIncompatibleTypes(leftValue, rightValue any) error { leftType := TypeName(leftValue) leftText := getFormatted(leftValue, Truncate) rightType := TypeName(rightValue) 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", leftText, leftType, rightText, rightType, - self.source()) + term.source()) } -func (self *term) errIncompatibleType(value any) error { - return self.tk.Errorf( +func (term *term) errIncompatibleType(value any) error { + return term.tk.Errorf( "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) { - err = self.tk.Errorf(template, args...) +func (term *term) Errorf(template string, args ...any) (err error) { + err = term.tk.Errorf(template, args...) return } -func (self *term) checkOperands() (err error) { - switch self.position { +func (term *term) checkOperands() (err error) { + switch term.position { case posInfix: - if self.children == nil || len(self.children) != 2 || self.anyChildrenNil() { - err = self.tk.Errorf("infix operator %q requires two non-nil operands, got %d", self.source(), self.getChildrenCount()) + if term.children == nil || len(term.children) != 2 || term.anyChildrenNil() { + err = term.tk.Errorf("infix operator %q requires two non-nil operands, got %d", term.source(), term.getChildrenCount()) } case posPrefix: - if self.children == nil || len(self.children) != 1 || self.children[0] == nil { - err = self.tk.Errorf("prefix operator %q requires one not nil operand", self.tk.String()) + if term.children == nil || len(term.children) != 1 || term.children[0] == nil { + err = term.tk.Errorf("prefix operator %q requires one not nil operand", term.tk.String()) } case posPostfix: - 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()) + if term.children == nil || len(term.children) != 1 || term.anyChildrenNil() { + err = term.tk.Errorf("postfix operator %q requires one not nil operand", term.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()) + if term.children == nil || len(term.children) < 3 || term.anyChildrenNil() { + err = term.tk.Errorf("infix operator %q requires at least three not operands, got %d", term.source(), term.getChildrenCount()) } } return } -func (self *term) anyChildrenNil() bool { - for _, child := range self.children { +func (term *term) anyChildrenNil() bool { + for _, child := range term.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 { - rightValue, err = self.children[1].compute(ctx) +func (term *term) evalInfix(ctx ExprContext) (leftValue, rightValue any, err error) { + if err = term.checkOperands(); err == nil { + if leftValue, err = term.children[0].compute(ctx); err == nil { + rightValue, err = term.children[1].compute(ctx) } } return } -func (self *term) evalPrefix(ctx ExprContext) (childValue any, err error) { - if err = self.checkOperands(); err == nil { - childValue, err = self.children[0].compute(ctx) +func (term *term) evalPrefix(ctx ExprContext) (childValue any, err error) { + if err = term.checkOperands(); err == nil { + childValue, err = term.children[0].compute(ctx) } return } // NOTE Temporary solution to support function parameters with default value -func (self *term) forceChild(c *term) { - if self.children == nil { - self.children = make([]*term, 0, 1) +func (t *term) forceChild(c *term) { + if t.children == nil { + t.children = make([]*term, 0, 1) } - self.children = append(self.children, c) + t.children = append(t.children, c) }