Compare commits

..

9 Commits

24 changed files with 488 additions and 104 deletions
+15 -15
View File
@@ -254,18 +254,18 @@ func setFunc(ctx kern.ExprContext, name string, args map[string]any) (result any
return
}
func unsetFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
var varName string
var ok bool
// func unsetFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
// var varName string
// var ok bool
if varName, ok = args[kern.ParamName].(string); !ok {
return nil, kern.ErrWrongParamType(name, kern.ParamName, kern.TypeString, args[kern.ParamName])
} else {
ctx.GetParent().DeleteVar(varName)
result = nil
}
return
}
// if varName, ok = args[kern.ParamName].(string); !ok {
// return nil, kern.ErrWrongParamType(name, kern.ParamName, kern.TypeString, args[kern.ParamName])
// } else {
// ctx.GetParent().DeleteVar(varName)
// result = nil
// }
// return
// }
//// import
@@ -307,10 +307,10 @@ func ImportBuiltinsFuncs(ctx kern.ExprContext) {
NewFuncParam(kern.ParamValue),
})
ctx.RegisterFunc("unset", kern.NewGolangFunctor(unsetFunc), kern.TypeAny, []kern.ExprFuncParam{
NewFuncParam(kern.ParamName),
NewFuncParam(kern.ParamValue),
})
// ctx.RegisterFunc("unset", kern.NewGolangFunctor(unsetFunc), kern.TypeAny, []kern.ExprFuncParam{
// NewFuncParam(kern.ParamName),
// NewFuncParam(kern.ParamValue),
// })
}
func init() {
+2
View File
@@ -1,6 +1,8 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
//go:build graph
// graph.go
package expr
+2 -2
View File
@@ -34,7 +34,7 @@ func ErrCantConvert(funcName string, value any, kind string) error {
}
func ErrExpectedGot(funcName string, kind string, value any) error {
return fmt.Errorf("%s(): expected %s, got %s (%v)", funcName, kind, TypeName(value), value)
return fmt.Errorf("%s(): expected %s, got %s (%#v)", funcName, kind, TypeName(value), value)
}
func ErrFuncDivisionByZero(funcName string) error {
@@ -52,7 +52,7 @@ func ErrFuncDivisionByZero(funcName string) error {
// }
func ErrInvalidParameterValue(funcName, paramName string, paramValue any) error {
return fmt.Errorf("%s(): invalid value %s (%v) for parameter %q", funcName, TypeName(paramValue), paramValue, paramName)
return fmt.Errorf("%s(): invalid value %s (%#v) for parameter %q", funcName, TypeName(paramValue), paramValue, paramName)
}
func undefArticle(s string) (article string) {
+37
View File
@@ -0,0 +1,37 @@
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
package kern
import "reflect"
func Equal(value1, value2 any) (equal bool) {
if value1 == nil && value2 == nil {
equal = true
} else if value1 == nil || value2 == nil {
equal = false
} else if IsBool(value1) && IsBool(value2) {
equal = value1.(bool) == value2.(bool)
} else if IsList(value1) && IsList(value2) {
ls1 := value1.(*ListType)
ls2 := value2.(*ListType)
equal = ls1.Equals(*ls2)
} else if IsDict(value1) && IsDict(value2) {
d1 := value1.(*DictType)
d2 := value2.(*DictType)
equal = d1.Equals(*d2)
} else if IsInteger(value1) && IsInteger(value2) {
equal = value1.(int64) == value2.(int64)
} else if IsString(value1) && IsString(value2) {
equal = value1.(string) == value2.(string)
} else if IsFloat(value1) && IsFloat(value2) {
equal = value1.(float64) == value2.(float64)
} else if IsNumOrFract(value1) && IsNumOrFract(value2) {
if eq, err := CmpAnyFract(value1, value2); err == nil {
equal = eq == 0
}
} else if !reflect.DeepEqual(value1, value2) {
equal = false
}
return
}
+21 -4
View File
@@ -143,13 +143,12 @@ func (dict *DictType) HasKey(target any) (ok bool) {
return
}
func (dict *DictType) SetItem(key any, value any) (err error) {
func (dict *DictType) SetItem(key any, value any) {
(*dict)[key] = value
return
}
func (dict *DictType) GetItem(key any) (value any, err error) {
value = (*dict)[key]
func (dict *DictType) GetItem(key any) (value any, exists bool) {
value, exists = (*dict)[key]
return
}
@@ -169,6 +168,24 @@ func (dict *DictType) Merge(second *DictType) {
}
}
func (dict *DictType) Equals(dict2 DictType) (answer bool) {
if dict2 != nil && len(*dict) == len(dict2) {
answer = true
for key, value1 := range *dict {
if value2, exists := dict2.GetItem(key); exists {
if !Equal(value1, value2) {
answer = false
break
}
} else {
answer = false
break
}
}
}
return
}
////////////////
type DictFormat interface {
+14 -11
View File
@@ -220,11 +220,12 @@ func anyToFract(v any) (f *FractionType, err error) {
if f, ok = v.(*FractionType); !ok {
if n, ok := v.(int64); ok {
f = intToFraction(n)
} else if dec, ok := v.(float64); ok {
f, err = Float64ToFraction(dec)
} else {
err = ErrExpectedGot("fract", TypeFraction, v)
}
}
if f == nil {
err = ErrExpectedGot("fract", TypeFraction, v)
}
return
}
@@ -273,14 +274,16 @@ func CmpAnyFract(af1, af2 any) (result int, err error) {
// =0 if af1 == af2
// >0 if af1 > af2
func cmpFract(f1, f2 *FractionType) (result int) {
f2.num = -f2.num
f := SumFract(f1, f2)
if f.num < 0 {
result = -1
} else if f.num > 0 {
result = 1
} else {
result = 0
if f1 != nil && f2 != nil {
f2.num = -f2.num
f := SumFract(f1, f2)
if f.num < 0 {
result = -1
} else if f.num > 0 {
result = 1
} else {
result = 0
}
}
return
}
+22 -19
View File
@@ -50,13 +50,13 @@ func ListFromStrings(stringList []string) (list *ListType) {
return
}
func (ls *ListType) ToString(opt FmtOpt) (s string) {
func (dict *ListType) ToString(opt FmtOpt) (s string) {
indent := GetFormatIndent(opt)
flags := GetFormatFlags(opt)
var sb strings.Builder
sb.WriteByte('[')
if len(*ls) > 0 {
if len(*dict) > 0 {
innerOpt := MakeFormatOptions(flags, indent+1)
nest := strings.Repeat(" ", indent+1)
@@ -64,7 +64,7 @@ func (ls *ListType) ToString(opt FmtOpt) (s string) {
sb.WriteByte('\n')
sb.WriteString(nest)
}
for i, item := range []any(*ls) {
for i, item := range []any(*dict) {
if i > 0 {
if flags&MultiLine != 0 {
sb.WriteString(",\n")
@@ -96,19 +96,19 @@ func (ls *ListType) ToString(opt FmtOpt) (s string) {
return
}
func (ls *ListType) String() string {
return ls.ToString(0)
func (dict *ListType) String() string {
return dict.ToString(0)
}
func (ls *ListType) TypeName() string {
func (dict *ListType) TypeName() string {
return "list"
}
func (ls *ListType) Contains(t *ListType) (answer bool) {
if len(*ls) >= len(*t) {
func (dict *ListType) Contains(t *ListType) (answer bool) {
if len(*dict) >= len(*t) {
answer = true
for _, item := range *t {
if answer = ls.IndexDeepSameCmp(item) >= 0; !answer {
if answer = dict.IndexDeepSameCmp(item) >= 0; !answer {
break
}
}
@@ -120,8 +120,11 @@ func (ls1 *ListType) Equals(ls2 ListType) (answer bool) {
if ls2 != nil && len(*ls1) == len(ls2) {
answer = true
for index, i1 := range *ls1 {
// if i1 != (ls2)[index] {
if !reflect.DeepEqual(i1, ls2[index]) {
// if !reflect.DeepEqual(i1, ls2[index]) {
// answer = false
// break
// }
if !Equal(i1, ls2[index]) {
answer = false
break
}
@@ -130,11 +133,11 @@ func (ls1 *ListType) Equals(ls2 ListType) (answer bool) {
return
}
func (list *ListType) IndexDeepSameCmp(target any) (index int) {
func (dict *ListType) IndexDeepSameCmp(target any) (index int) {
var eq bool
var err error
index = -1
for i, item := range *list {
for i, item := range *dict {
if eq, err = deepSame(item, target, SameContent); err != nil {
break
} else if eq {
@@ -185,15 +188,15 @@ func deepSame(a, b any, deepCmp DeepFuncTemplate) (eq bool, err error) {
return
}
func (list *ListType) SetItem(index int64, value any) (err error) {
if index >= 0 && index < int64(len(*list)) {
(*list)[index] = value
func (dict *ListType) SetItem(index int64, value any) (err error) {
if index >= 0 && index < int64(len(*dict)) {
(*dict)[index] = value
} else {
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*list)-1)
err = fmt.Errorf("index %d out of bounds (0, %d)", index, len(*dict)-1)
}
return
}
func (list *ListType) AppendItem(value any) {
*list = append(*list, value)
func (dict *ListType) AppendItem(value any) {
*dict = append(*dict, value)
}
+1 -1
View File
@@ -49,7 +49,7 @@ func assignCollectionItem(ctx kern.ExprContext, collectionTerm, keyListTerm *ter
err = keyListTerm.Errorf("integer expected, got %v [%s]", keyValue, kern.TypeName(keyValue))
}
case *kern.DictType:
err = collection.SetItem(keyValue, value)
collection.SetItem(keyValue, value)
default:
err = collectionTerm.Errorf("collection expected")
}
+4 -4
View File
@@ -43,8 +43,8 @@ func evalDigest(ctx kern.ExprContext, opTerm *term) (v any, err error) {
lastValue = nil
for item, err = it.Next(); err == nil; item, err = it.Next() {
ctx.SetVar("_", item)
ctx.SetVar("_index", it.Index())
ctx.SetVar("_count", it.Count())
ctx.SetVar("__", it.Index())
ctx.SetVar("_#", it.Count())
if rightValue, err = opTerm.children[1].Compute(ctx); err == nil {
if rightValue == nil {
break
@@ -52,8 +52,8 @@ func evalDigest(ctx kern.ExprContext, opTerm *term) (v any, err error) {
lastValue = rightValue
}
}
ctx.DeleteVar("_count")
ctx.DeleteVar("_index")
ctx.DeleteVar("_#")
ctx.DeleteVar("__")
ctx.DeleteVar("_")
if err != nil {
break
+7 -2
View File
@@ -45,15 +45,20 @@ func evalDot(ctx kern.ExprContext, opTerm *term) (v any, err error) {
err = indexTerm.tk.ErrorExpectedGot("identifier")
}
case *kern.DictType:
var ok bool
s := opTerm.children[1].symbol()
if s == SymVariable || s == SymString {
src := opTerm.children[1].Source()
if len(src) > 1 && src[0] == '"' && src[len(src)-1] == '"' {
src = src[1 : len(src)-1]
}
v, err = unboxedValue.GetItem(src)
if v, ok = unboxedValue.GetItem(src); !ok {
err = opTerm.errKeyNotFound(src)
}
} else if rightValue, err = opTerm.children[1].Compute(ctx); err == nil {
v, err = unboxedValue.GetItem(rightValue)
if v, ok = unboxedValue.GetItem(rightValue); !ok {
err = opTerm.errKeyNotFound(rightValue)
}
}
default:
if rightValue, err = opTerm.children[1].Compute(ctx); err == nil {
+4 -4
View File
@@ -43,8 +43,8 @@ func evalFilter(ctx kern.ExprContext, opTerm *term) (v any, err error) {
values := kern.NewListA()
for item, err = it.Next(); err == nil; item, err = it.Next() {
ctx.SetVar("_", item)
ctx.SetVar("_index", it.Index())
ctx.SetVar("_count", it.Count())
ctx.SetVar("__", it.Index())
ctx.SetVar("_#", it.Count())
if rightValue, err = opTerm.children[1].Compute(ctx); err == nil {
if success, valid := kern.ToBool(rightValue); valid {
if success {
@@ -54,8 +54,8 @@ func evalFilter(ctx kern.ExprContext, opTerm *term) (v any, err error) {
err = fmt.Errorf("filter expression must return a boolean or a castable to boolean, got %v [%T]", rightValue, rightValue)
}
}
ctx.DeleteVar("_count")
ctx.DeleteVar("_index")
ctx.DeleteVar("_#")
ctx.DeleteVar("__")
ctx.DeleteVar("_")
if err != nil {
break
+105
View File
@@ -0,0 +1,105 @@
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// operator-groupby.go
package expr
import (
"fmt"
"io"
"strconv"
"git.portale-stac.it/go-pkg/expr/kern"
)
//-------- group by term
func newGroupByTerm(tk *Token) (inst *term) {
return &term{
tk: *tk,
children: make([]*term, 0, 2),
position: posInfix,
priority: priIterOp,
evalFunc: evalGroupBy,
}
}
func evalGroupBy(ctx kern.ExprContext, opTerm *term) (v any, err error) {
var leftValue, rightValue any
var it kern.Iterator
var item any
var sKey string
var keyByIndex bool
if err = opTerm.checkOperands(); err != nil {
return
}
if leftValue, err = opTerm.children[0].Compute(ctx); err != nil {
return
}
if it, err = NewIterator(leftValue); err != nil {
return nil, fmt.Errorf("left operand of MAP must be an iterable data-source; got %s", kern.TypeName(leftValue))
}
rightTk := opTerm.children[1].tk
if rightTk.IsSymbol(SymVariable) && rightTk.source == "__" {
keyByIndex = true
} else if rightValue, err = opTerm.children[1].Compute(ctx); err != nil {
return
} else if kern.IsString(rightValue) {
sKey = rightValue.(string)
} else {
return nil, fmt.Errorf("right operand of GROUPBY must be a string or identifier '__'; got %s", kern.TypeName(rightValue))
}
values := kern.MakeDict()
for item, err = it.Next(); err == nil; item, err = it.Next() {
ctx.SetVar("_", item)
ctx.SetVar("__", it.Index())
ctx.SetVar("_#", it.Count())
var sItemKey string
if d, ok := item.(*kern.DictType); ok {
if keyByIndex || len(sKey) == 0 {
sItemKey = strconv.Itoa(int(it.Index()))
} else if d.HasKey(sKey) {
if keyValue, exists := d.GetItem(sKey); exists {
sItemKey = fmt.Sprintf("%v", keyValue)
} else {
sItemKey = "_"
}
} else {
sItemKey = "_"
}
} else {
sItemKey = strconv.Itoa(int(it.Index()))
}
var ls *kern.ListType
if lsAny, exists := values.GetItem(sItemKey); exists && lsAny != nil {
ls = lsAny.(*kern.ListType)
}
if ls == nil {
ls = kern.NewListA()
}
ls.AppendItem(item)
values.SetItem(sItemKey, ls)
ctx.DeleteVar("_#")
ctx.DeleteVar("__")
ctx.DeleteVar("_")
}
if err == io.EOF {
err = nil
}
v = values
return
}
// init
func init() {
registerTermConstructor(SymKwGroupBy, newGroupByTerm)
}
+7 -5
View File
@@ -43,13 +43,13 @@ func evalMap(ctx kern.ExprContext, opTerm *term) (v any, err error) {
values := kern.NewListA()
for item, err = it.Next(); err == nil; item, err = it.Next() {
ctx.SetVar("_", item)
ctx.SetVar("_index", it.Index())
ctx.SetVar("_count", it.Count())
ctx.SetVar("__", it.Index())
ctx.SetVar("_#", it.Count())
if rightValue, err = opTerm.children[1].Compute(ctx); err == nil {
values.AppendItem(rightValue)
}
ctx.DeleteVar("_count")
ctx.DeleteVar("_index")
ctx.DeleteVar("_#")
ctx.DeleteVar("__")
ctx.DeleteVar("_")
if err != nil {
break
@@ -58,7 +58,9 @@ func evalMap(ctx kern.ExprContext, opTerm *term) (v any, err error) {
if err == io.EOF {
err = nil
}
v = values
if err == nil {
v = values
}
return
}
+33 -4
View File
@@ -139,6 +139,7 @@ func init() {
SymKwFilter: {"filter", symClassOperator, posInfix},
SymKwDigest: {"digest", symClassOperator, posInfix},
SymKwJoin: {"join", symClassOperator, posInfix},
SymKwGroupBy: {"groupby", symClassOperator, posInfix},
SymKwFunc: {"func(", symClassDeclaration, posPrefix},
SymKwBuiltin: {"builtin", symClassOperator, posPrefix},
SymKwPlugin: {"plugin", symClassOperator, posPrefix},
@@ -182,21 +183,49 @@ func StringEndsWithOperator(s string) bool {
return endingOperator(s) != SymNone
}
// func endingOperator(s string) (sym Symbol) {
// var matchLength = 0
// sym = SymNone
// lower := strings.TrimRight(strings.ToLower(s), " \t")
// for symbol, spec := range symbolMap {
// if strings.HasSuffix(lower, spec.repr) {
// if len(spec.repr) > matchLength {
// matchLength = len(spec.repr)
// if spec.kind == symClassOperator && (spec.opType == posInfix || spec.opType == posPrefix) {
// sym = symbol
// } else {
// sym = SymNone
// }
// }
// }
// }
// return
// }
func endingOperator(s string) (sym Symbol) {
var matchLength = 0
var repr string
sym = SymNone
lower := strings.TrimRight(strings.ToLower(s), " \t")
for symbol, spec := range symbolMap {
if strings.HasSuffix(lower, spec.repr) {
if len(spec.repr) > matchLength {
matchLength = len(spec.repr)
if spec.kind == symClassOperator && (spec.opType == posInfix || spec.opType == posPrefix) {
if len(spec.repr) > matchLength || repr == spec.repr {
if strings.HasSuffix(lower, spec.repr) {
if isNotEndingSymbol(spec) && repr != spec.repr {
repr = spec.repr
matchLength = len(spec.repr)
sym = symbol
} else {
sym = SymNone
break
// matchLength = 0
}
}
}
}
return
}
func isNotEndingSymbol(spec symbolSpec) bool {
return (spec.kind == symClassOperator && (spec.opType == posInfix || spec.opType == posPrefix)) ||
(spec.kind == symClassParenthesis && spec.opType == posPrefix)
}
+2
View File
@@ -122,6 +122,7 @@ const (
SymKwMap
SymKwFilter
SymKwDigest
SymKwGroupBy
SymKwJoin
SymKwNil
SymKwUnset
@@ -147,5 +148,6 @@ func init() {
"UNSET": SymKwUnset,
"DIGEST": SymKwDigest,
"JOIN": SymKwJoin,
"GROUPBY": SymKwGroupBy,
}
}
+53
View File
@@ -72,3 +72,56 @@ func TestFuncBase(t *testing.T) {
// runTestSuiteSpec(t, section, inputs, 49)
runTestSuite(t, section, inputs)
}
func TestFuncBaseString(t *testing.T) {
section := "Builtin-Base-String"
inputs := []inputType{
/* 1 */ {`string(3)`, "3", nil},
/* 2 */ {`string(3.5)`, "3.5", nil},
/* 3 */ {`string(true)`, "true", nil},
/* 4 */ {`string(false)`, "false", nil},
/* 5 */ {`string("123")`, "123", nil},
/* 6 */ {`string(1:2)`, "1:2", nil},
}
// t.Setenv("EXPR_PATH", ".")
// runTestSuiteSpec(t, section, inputs, 49)
runTestSuite(t, section, inputs)
}
func TestFuncBaseFraction(t *testing.T) {
section := "Builtin-Base-Fraction"
inputs := []inputType{
/* 1 */ {`fract(-0.5)`, kern.NewFraction(-1, 2), nil},
/* 2 */ {`fract("")`, (*kern.FractionType)(nil), `bad syntax`},
/* 3 */ {`fract("-1")`, kern.NewFraction(-1, 1), nil},
/* 4 */ {`fract("+1")`, kern.NewFraction(1, 1), nil},
/* 5 */ {`fract("1a")`, (*kern.FractionType)(nil), `strconv.ParseInt: parsing "1a": invalid syntax`},
/* 6 */ {`fract(1,0)`, nil, `fract(): division by zero`},
/* 7 */ {`fract(5, "1")`, nil, `fract(): expected integer, got string ("1")`},
/* 8 */ {`fract(true)`, kern.NewFraction(1, 1), nil},
/* 9 */ {`fract(false)`, kern.NewFraction(0, 1), nil},
}
// t.Setenv("EXPR_PATH", ".")
// runTestSuiteSpec(t, section, inputs, 8)
runTestSuite(t, section, inputs)
}
func TestFuncBaseOthers(t *testing.T) {
section := "Builtin-Base-Others"
inputs := []inputType{
/* 1 */ {`set("a", 3); a`, int64(3), nil},
/* 2 */ {`set(true, 3)`, nil, `set(): the "name" parameter must be a string, got a bool (true)`},
// /* 3 */ {`a=3; unset("a"); a`, nil, `undefined variable or function "a"`},
// /* 4 */ {`unset("a")`, nil, `undefined variable or function "a"`},
}
// runTestSuiteSpec(t, section, inputs, 4)
runTestSuite(t, section, inputs)
}
+1 -14
View File
@@ -6,7 +6,6 @@ package expr
import (
"errors"
"reflect"
"strings"
"testing"
@@ -74,7 +73,6 @@ func doTest(t *testing.T, ctx kern.ExprContext, section string, input *inputType
var ast Expr
var gotResult any
var gotErr error
var eq, eqDone bool
wantErr := getWantedError(input)
@@ -93,18 +91,7 @@ func doTest(t *testing.T, ctx kern.ExprContext, section string, input *inputType
gotResult, gotErr = ast.Eval(ctx)
}
if input.wantResult != nil && gotResult != nil {
if ls1, ok := input.wantResult.(*kern.ListType); ok {
if ls2, ok := gotResult.(*kern.ListType); ok {
eq = ls1.Equals(*ls2)
eqDone = true
}
}
}
if !eqDone {
eq = reflect.DeepEqual(gotResult, input.wantResult)
}
eq := kern.Equal(gotResult, input.wantResult)
if !eq /*gotResult != input.wantResult*/ {
t.Errorf(">>>%s/%d: `%s` -> result = %v [%s], want = %v [%s]", section, count, input.source, gotResult, kern.TypeName(gotResult), input.wantResult, kern.TypeName(input.wantResult))
+5 -11
View File
@@ -28,18 +28,12 @@ func TestFractionsParser(t *testing.T) {
/* 13 */ {`builtin "math.arith"; mul(1:2, 2:3)`, kern.NewFraction(2, 6), nil},
/* 14 */ {`builtin "math.arith"; mul(1:2, 1.0, 2)`, float64(1.0), nil},
/* 15 */ {`1:0`, nil, `[1:3] division by zero`},
/* 16 */ {`fract(-0.5)`, kern.NewFraction(-1, 2), nil},
/* 17 */ {`fract("")`, (*kern.FractionType)(nil), `bad syntax`},
/* 18 */ {`fract("-1")`, kern.NewFraction(-1, 1), nil},
/* 19 */ {`fract("+1")`, kern.NewFraction(1, 1), nil},
/* 20 */ {`fract("1a")`, (*kern.FractionType)(nil), `strconv.ParseInt: parsing "1a": invalid syntax`},
/* 21 */ {`fract(1,0)`, nil, `fract(): division by zero`},
/* 22 */ {`string(1:2)`, "1:2", nil},
/* 23 */ {`1+1:2+0.5`, float64(2), nil},
/* 24 */ {`1:(2-2)`, nil, `[1:3] division by zero`},
/* 25 */ {`[0,1][1-1]:1`, kern.NewFraction(0, 1), nil},
/* 16 */ {`1+1:2+0.5`, float64(2), nil},
/* 17 */ {`1:(2-2)`, nil, `[1:3] division by zero`},
/* 18 */ {`[0,1][1-1]:1`, kern.NewFraction(0, 1), nil},
/* 19 */ {`1:2 == 0.5`, true, nil},
}
// runTestSuiteSpec(t, section, inputs, 25)
// runTestSuiteSpec(t, section, inputs, 26)
runTestSuite(t, section, inputs)
}
+12
View File
@@ -49,6 +49,18 @@ func TestFuncs(t *testing.T) {
runTestSuite(t, section, inputs)
}
func TestFuncs2(t *testing.T) {
section := "Funcs2"
inputs := []inputType{
/* 1 */ {`sum=func(a,b){a+b}; sum(1,2)`, int64(3), nil},
/* 2 */ {`sum=func(a,b){a+b}; sum(1)`, nil, `sum(): too few params -- expected 2, got 1`},
/* 3 */ {`["1", "2", "3"] map int()`, nil, `int(): too few params -- expected 1, got 0`},
/* 4 */ {`builtin "iterator"; times=func(a,b){a*b}; run($(["1", "2", "3"]), times)`, nil, `operator(): missing params -- a, b`},
}
// runTestSuiteSpec(t, section, inputs, 4)
runTestSuite(t, section, inputs)
}
func dummy(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
return
}
+2
View File
@@ -1,6 +1,8 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
//go:build graph
// t_graph_test.go
package expr
+2 -1
View File
@@ -35,7 +35,7 @@ func TestIteratorParser(t *testing.T) {
/* 20 */ {`it=$({1:"one",2:"two",3:"three"}, "default", "value"); it++`, "one", nil},
/* 21 */ {`it=$({1:"one",2:"two",3:"three"}, "desc", "key"); it++`, int64(3), nil},
/* 22 */ {`it=$({1:"one",2:"two",3:"three"}, "asc", "item"); it++`, kern.NewList([]any{int64(1), "one"}), nil},
/* 23 */ {`builtin "os.file"; fileReadIterator("test-file.txt") map ${_index}`, kern.NewList([]any{int64(0), int64(1)}), nil},
/* 23 */ {`builtin "os.file"; fileReadIterator("test-file.txt") map ${__}`, kern.NewList([]any{int64(0), int64(1)}), nil},
/* 24 */ {`builtin "os.file"; #(fileReadIterator("test-file.txt") filter (#${_} == 2))`, int64(0), nil},
/* 25 */ {`builtin "os.file"; #(fileReadIterator("test-file.txt") filter (#${_} == 3))`, int64(2), nil},
/* 26 */ {`#($(10) map ${_})`, int64(10), nil},
@@ -43,6 +43,7 @@ func TestIteratorParser(t *testing.T) {
/* 28 */ {`$(10) digest ${_}`, int64(9), nil},
/* 29 */ {`$(10,0) digest ${_}`, int64(1), nil},
/* 30 */ {`$(10,0,-2) digest ${_}`, int64(2), nil},
/* 31 */ {`[3,4,5] map ${_#}`, kern.NewList([]any{int64(1), int64(2), int64(3)}), nil},
}
// runTestSuiteSpec(t, section, inputs, 10)
+75 -7
View File
@@ -35,16 +35,84 @@ func TestOperator(t *testing.T) {
/* 20 */ {`a=1; a^=2`, int64(3), nil},
/* 21 */ {`a=1; ++a`, int64(2), nil},
/* 22 */ {`a=1; --a`, int64(0), nil},
/* 23 */ {`[1,2,3] map var("_")`, kern.NewList([]any{int64(1), int64(2), int64(3)}), nil},
/* 24 */ {`[1,2,3] map $_`, kern.NewList([]any{int64(1), int64(2), int64(3)}), nil},
/* 25 */ {`[1,2,3,4] filter ($_ % 2 == 0)`, kern.NewList([]any{int64(2), int64(4)}), nil},
/* 26 */ {`max=0; [2,3,1] digest max=(($_ > max) ? {$_} :: {max})`, int64(3), nil},
/* 27 */ {`["a","b"] join ["x"]`, kern.NewList([]any{"a", "b", "x"}), nil},
/* 28 */ {`["a","b"] join ["x"-true]`, nil, `[1:21] left operand 'x' [string] and right operand 'true' [bool] are not compatible with operator "-"`},
}
// t.Setenv("EXPR_PATH", ".")
// runTestSuiteSpec(t, section, inputs, 28)
// runTestSuiteSpec(t, section, inputs, 22)
runTestSuite(t, section, inputs)
}
func TestOperatorMap(t *testing.T) {
section := "Operator-Map"
inputs := []inputType{
/* 1 */ {`a=1; --a`, int64(0), nil},
/* 2 */ {`[1,2,3] map var("_")`, kern.NewList([]any{int64(1), int64(2), int64(3)}), nil},
/* 3 */ {`[1,2,3] map $_`, kern.NewList([]any{int64(1), int64(2), int64(3)}), nil},
}
// runTestSuiteSpec(t, section, inputs, 3)
runTestSuite(t, section, inputs)
}
func TestOperatorFilter(t *testing.T) {
section := "Operator-Filter"
inputs := []inputType{
/* 1 */ {`[1,2,3,4] filter ($_ % 2 == 0)`, kern.NewList([]any{int64(2), int64(4)}), nil},
}
// runTestSuiteSpec(t, section, inputs, 1)
runTestSuite(t, section, inputs)
}
func TestOperatorDigest(t *testing.T) {
section := "Operator-Digest"
inputs := []inputType{
/* 1 */ {`max=0; [2,3,1] digest max=(($_ > max) ? {$_} :: {max})`, int64(3), nil},
}
// runTestSuiteSpec(t, section, inputs, 29)
runTestSuite(t, section, inputs)
}
func TestOperatorJoin(t *testing.T) {
section := "Operator-Join"
inputs := []inputType{
/* 1 */ {`["a","b"] join ["x"]`, kern.NewList([]any{"a", "b", "x"}), nil},
/* 2 */ {`["a","b"] join ["x"-true]`, nil, `[1:21] left operand 'x' [string] and right operand 'true' [bool] are not compatible with operator "-"`},
}
// runTestSuiteSpec(t, section, inputs, 2)
runTestSuite(t, section, inputs)
}
func TestOperatorGroupBy(t *testing.T) {
section := "Operator-GroupBy"
inputs := []inputType{
/* 1 */ {`L=[{"num": 1, "alpha": "one"}, {"num": 2, "alpha": "two"}, {"num": 3, "alpha": "three"}]; L groupby "num"`,
kern.NewDict(map[any]any{
"1": kern.NewListA(kern.NewDict(map[any]any{"num": int64(1), "alpha": "one"})),
"2": kern.NewListA(kern.NewDict(map[any]any{"num": int64(2), "alpha": "two"})),
"3": kern.NewListA(kern.NewDict(map[any]any{"num": int64(3), "alpha": "three"})),
}),
nil},
/* 2 */ {`cars = [{"model": "compas", "vendor": "jeep"}, {"model": "limited", "vendor": "jeep"}, {"model": "600", "vendor":"fiat"}]; cars groupby "vendor"`,
kern.NewDict(map[any]any{
"jeep": kern.NewListA(
kern.NewDict(map[any]any{"model": "compas", "vendor": "jeep"}),
kern.NewDict(map[any]any{"model": "limited", "vendor": "jeep"})),
"fiat": kern.NewListA(kern.NewDict(map[any]any{"model": "600", "vendor": "fiat"})),
}),
nil},
/* 3 */ {`[3,4,5] groupby $__`,
kern.NewDict(map[any]any{
"0": kern.NewListA(int64(3)),
"1": kern.NewListA(int64(4)),
"2": kern.NewListA(int64(5)),
}),
nil},
}
runTestSuiteSpec(t, section, inputs, 3)
// runTestSuite(t, section, inputs)
}
+58
View File
@@ -0,0 +1,58 @@
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
// All rights reserved.
// t_symbol_test.go
package expr
import (
"testing"
"git.portale-stac.it/go-pkg/expr/kern"
)
func TestOperatorEnding(t *testing.T) {
section := "Symbol"
inputs := []inputType{
/* 1 */ {`a++`, false, nil},
/* 2 */ {`a;`, true, nil},
/* 3 */ {`a +`, true, nil},
/* 4 */ {`a + 1`, false, nil},
/* 5 */ {`a - `, true, nil},
/* 6 */ {`a( `, true, nil},
/* 7 */ {`a) `, false, nil},
/* 8 */ {`++a `, false, nil},
}
// testStringArrayEndingWithOperatorSpec(t, section, inputs, 6)
testStringArrayEndingWithOperator(t, section, inputs)
}
func testStringEndingWithOperator(source string) bool {
return StringEndsWithOperator(source)
}
func testStringArrayEndingWithOperatorSpec(t *testing.T, section string, inputs []inputType, spec ...int) {
for i := range spec {
if spec[i] < 1 || spec[i] > len(inputs) {
t.Errorf("Invalid test spec index: %d (must be between 1 and %d)", spec[i], len(inputs))
continue
}
doEndingTest(t, section, inputs[spec[i]-1], spec[i]-1)
}
}
func testStringArrayEndingWithOperator(t *testing.T, section string, inputs []inputType) {
for i, input := range inputs {
doEndingTest(t, section, input, i)
}
}
func doEndingTest(t *testing.T, section string, input inputType, i int) {
wantErr := getWantedError(&input)
logTest(t, i+1, section, input.source, input.wantResult, wantErr)
gotResult := testStringEndingWithOperator(input.source)
t.Logf("Is %s op ending? %v", input.source, gotResult)
if gotResult != input.wantResult.(bool) {
t.Errorf("%d: `%s` -> result = %v [%s], want = %v [%s]", i+1, input.source, gotResult, kern.TypeName(gotResult), input.wantResult, kern.TypeName(input.wantResult))
}
}
+4
View File
@@ -235,6 +235,10 @@ func (t *term) errDivisionByZero() error {
return t.tk.Errorf("division by zero")
}
func (t *term) errKeyNotFound(key any) error {
return t.tk.Errorf("key '%v' not found", key)
}
func (t *term) Errorf(template string, args ...any) (err error) {
err = t.tk.Errorf(template, args...)
return