moved scanner sources to package 'scan'
This commit is contained in:
+53
-52
@@ -8,21 +8,22 @@ import (
|
||||
"strings"
|
||||
|
||||
"git.portale-stac.it/go-pkg/expr/kern"
|
||||
"git.portale-stac.it/go-pkg/expr/scan"
|
||||
)
|
||||
|
||||
//-------- multiply term
|
||||
|
||||
func newMultiplyTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priProduct,
|
||||
evalFunc: evalMultiply,
|
||||
func newMultiplyTerm(tk *scan.Token) (inst *scan.Term) {
|
||||
return &scan.Term{
|
||||
Tk: *tk,
|
||||
Children: make([]*scan.Term, 0, 2),
|
||||
Position: scan.PosInfix,
|
||||
Priority: scan.PriProduct,
|
||||
EvalFunc: evalMultiply,
|
||||
}
|
||||
}
|
||||
|
||||
func mulValues(opTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
func mulValues(opTerm *scan.Term, leftValue, rightValue any) (v any, err error) {
|
||||
if kern.IsString(leftValue) && kern.IsInteger(rightValue) {
|
||||
s, _ := leftValue.(string)
|
||||
n, _ := rightValue.(int64)
|
||||
@@ -38,15 +39,15 @@ func mulValues(opTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
v = leftInt * rightInt
|
||||
}
|
||||
} else {
|
||||
err = opTerm.errIncompatibleTypes(leftValue, rightValue)
|
||||
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func evalMultiply(ctx kern.ExprContext, prodTerm *term) (v any, err error) {
|
||||
func evalMultiply(ctx kern.ExprContext, prodTerm *scan.Term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = prodTerm.evalInfix(ctx); err != nil {
|
||||
if leftValue, rightValue, err = prodTerm.EvalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -55,22 +56,22 @@ func evalMultiply(ctx kern.ExprContext, prodTerm *term) (v any, err error) {
|
||||
|
||||
//-------- divide term
|
||||
|
||||
func newDivideTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priProduct,
|
||||
evalFunc: evalDivide,
|
||||
func newDivideTerm(tk *scan.Token) (inst *scan.Term) {
|
||||
return &scan.Term{
|
||||
Tk: *tk,
|
||||
Children: make([]*scan.Term, 0, 2),
|
||||
Position: scan.PosInfix,
|
||||
Priority: scan.PriProduct,
|
||||
EvalFunc: evalDivide,
|
||||
}
|
||||
}
|
||||
|
||||
func divValues(opTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
func divValues(opTerm *scan.Term, leftValue, rightValue any) (v any, err error) {
|
||||
if kern.IsNumOrFract(leftValue) && kern.IsNumOrFract(rightValue) {
|
||||
if kern.IsFloat(leftValue) || kern.IsFloat(rightValue) {
|
||||
d := kern.NumAsFloat(rightValue)
|
||||
if d == 0.0 {
|
||||
err = opTerm.errDivisionByZero()
|
||||
err = opTerm.ErrDivisionByZero()
|
||||
} else {
|
||||
v = kern.NumAsFloat(leftValue) / d
|
||||
}
|
||||
@@ -79,7 +80,7 @@ func divValues(opTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
} else {
|
||||
leftInt, _ := leftValue.(int64)
|
||||
if rightInt, _ := rightValue.(int64); rightInt == 0 {
|
||||
err = opTerm.errDivisionByZero()
|
||||
err = opTerm.ErrDivisionByZero()
|
||||
} else {
|
||||
v = leftInt / rightInt
|
||||
}
|
||||
@@ -92,7 +93,7 @@ func divValues(opTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
source := leftValue.(string)
|
||||
partSize := int(rightValue.(int64))
|
||||
if partSize == 0 {
|
||||
err = opTerm.errDivisionByZero()
|
||||
err = opTerm.ErrDivisionByZero()
|
||||
} else {
|
||||
partCount := len(source) / partSize
|
||||
remainder := len(source) % partSize
|
||||
@@ -110,15 +111,15 @@ func divValues(opTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
v = kern.NewList(parts)
|
||||
}
|
||||
} else {
|
||||
err = opTerm.errIncompatibleTypes(leftValue, rightValue)
|
||||
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func evalDivide(ctx kern.ExprContext, opTerm *term) (v any, err error) {
|
||||
func evalDivide(ctx kern.ExprContext, opTerm *scan.Term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = opTerm.evalInfix(ctx); err != nil {
|
||||
if leftValue, rightValue, err = opTerm.EvalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -127,66 +128,66 @@ func evalDivide(ctx kern.ExprContext, opTerm *term) (v any, err error) {
|
||||
|
||||
//-------- divide as float term
|
||||
|
||||
func newDivideAsFloatTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priProduct,
|
||||
evalFunc: evalDivideAsFloat,
|
||||
func newDivideAsFloatTerm(tk *scan.Token) (inst *scan.Term) {
|
||||
return &scan.Term{
|
||||
Tk: *tk,
|
||||
Children: make([]*scan.Term, 0, 2),
|
||||
Position: scan.PosInfix,
|
||||
Priority: scan.PriProduct,
|
||||
EvalFunc: evalDivideAsFloat,
|
||||
}
|
||||
}
|
||||
|
||||
func evalDivideAsFloat(ctx kern.ExprContext, floatDivTerm *term) (v any, err error) {
|
||||
func evalDivideAsFloat(ctx kern.ExprContext, floatDivTerm *scan.Term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = floatDivTerm.evalInfix(ctx); err != nil {
|
||||
if leftValue, rightValue, err = floatDivTerm.EvalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if kern.IsNumOrFract(leftValue) && kern.IsNumOrFract(rightValue) {
|
||||
d := kern.NumAsFloat(rightValue)
|
||||
if d == 0.0 {
|
||||
err = floatDivTerm.errDivisionByZero()
|
||||
err = floatDivTerm.ErrDivisionByZero()
|
||||
} else {
|
||||
v = kern.NumAsFloat(leftValue) / d
|
||||
}
|
||||
} else {
|
||||
err = floatDivTerm.errIncompatibleTypes(leftValue, rightValue)
|
||||
err = floatDivTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//-------- reminder term
|
||||
|
||||
func newRemainderTerm(tk *Token) (inst *term) {
|
||||
return &term{
|
||||
tk: *tk,
|
||||
children: make([]*term, 0, 2),
|
||||
position: posInfix,
|
||||
priority: priProduct,
|
||||
evalFunc: evalRemainder,
|
||||
func newRemainderTerm(tk *scan.Token) (inst *scan.Term) {
|
||||
return &scan.Term{
|
||||
Tk: *tk,
|
||||
Children: make([]*scan.Term, 0, 2),
|
||||
Position: scan.PosInfix,
|
||||
Priority: scan.PriProduct,
|
||||
EvalFunc: evalRemainder,
|
||||
}
|
||||
}
|
||||
func remainderValues(opTerm *term, leftValue, rightValue any) (v any, err error) {
|
||||
func remainderValues(opTerm *scan.Term, leftValue, rightValue any) (v any, err error) {
|
||||
if kern.IsInteger(leftValue) && kern.IsInteger(rightValue) {
|
||||
rightInt, _ := rightValue.(int64)
|
||||
if rightInt == 0 {
|
||||
err = opTerm.errDivisionByZero()
|
||||
err = opTerm.ErrDivisionByZero()
|
||||
} else {
|
||||
leftInt, _ := leftValue.(int64)
|
||||
v = leftInt % rightInt
|
||||
}
|
||||
} else {
|
||||
err = opTerm.errIncompatibleTypes(leftValue, rightValue)
|
||||
err = opTerm.ErrIncompatibleTypes(leftValue, rightValue)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func evalRemainder(ctx kern.ExprContext, remainderTerm *term) (v any, err error) {
|
||||
func evalRemainder(ctx kern.ExprContext, remainderTerm *scan.Term) (v any, err error) {
|
||||
var leftValue, rightValue any
|
||||
|
||||
if leftValue, rightValue, err = remainderTerm.evalInfix(ctx); err != nil {
|
||||
if leftValue, rightValue, err = remainderTerm.EvalInfix(ctx); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -195,8 +196,8 @@ func evalRemainder(ctx kern.ExprContext, remainderTerm *term) (v any, err error)
|
||||
|
||||
// init
|
||||
func init() {
|
||||
registerTermConstructor(SymStar, newMultiplyTerm)
|
||||
registerTermConstructor(SymSlash, newDivideTerm)
|
||||
registerTermConstructor(SymDotSlash, newDivideAsFloatTerm)
|
||||
registerTermConstructor(SymPercent, newRemainderTerm)
|
||||
scan.RegisterTermConstructor(scan.SymStar, newMultiplyTerm)
|
||||
scan.RegisterTermConstructor(scan.SymSlash, newDivideTerm)
|
||||
scan.RegisterTermConstructor(scan.SymDotSlash, newDivideAsFloatTerm)
|
||||
scan.RegisterTermConstructor(scan.SymPercent, newRemainderTerm)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user