Added all source files
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
// operator-sum.go
|
||||
package expr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
//-------- plus term
|
||||
|
||||
func newPlusTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
class: classOperator,
|
||||
kind: kindUnknown,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priSum,
|
||||
evalFunc: evalPlus,
|
||||
}
|
||||
}
|
||||
|
||||
func evalPlus(ctx exprContext, self *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if (isString(leftValue) && isNumberString(rightValue)) || (isString(rightValue) && isNumberString(leftValue)) {
|
||||
v = fmt.Sprintf("%v%v", leftValue, rightValue)
|
||||
} else if isNumber(leftValue) && isNumber(rightValue) {
|
||||
if isFloat(leftValue) || isFloat(rightValue) {
|
||||
v = numAsFloat(leftValue) + numAsFloat(rightValue)
|
||||
} else {
|
||||
leftInt, _ := leftValue.(int64)
|
||||
rightInt, _ := rightValue.(int64)
|
||||
v = leftInt + rightInt
|
||||
}
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//-------- minus term
|
||||
|
||||
func newMinusTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
class: classOperator,
|
||||
kind: kindUnknown,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priSum,
|
||||
evalFunc: evalMinus,
|
||||
}
|
||||
}
|
||||
|
||||
func evalMinus(ctx exprContext, self *term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = self.evalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if isNumber(leftValue) && isNumber(rightValue) {
|
||||
if isFloat(leftValue) || isFloat(rightValue) {
|
||||
v = numAsFloat(leftValue) - numAsFloat(rightValue)
|
||||
} else {
|
||||
leftInt, _ := leftValue.(int64)
|
||||
rightInt, _ := rightValue.(int64)
|
||||
v = leftInt - rightInt
|
||||
}
|
||||
} else {
|
||||
err = self.errIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymPlus, newPlusTerm)
|
||||
registerTermConstructor(SymMinus, newMinusTerm)
|
||||
}
|
||||
Reference in New Issue
Block a user