Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f63ff5953e | |||
| b9d37a5b4c | |||
| 23b8eec74a | |||
| bb6b6d17ec | |||
| 53acacbadf |
+15
-15
@@ -254,18 +254,18 @@ func setFunc(ctx kern.ExprContext, name string, args map[string]any) (result any
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func unsetFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
// func unsetFunc(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||||
var varName string
|
// var varName string
|
||||||
var ok bool
|
// var ok bool
|
||||||
|
|
||||||
if varName, ok = args[kern.ParamName].(string); !ok {
|
// if varName, ok = args[kern.ParamName].(string); !ok {
|
||||||
return nil, kern.ErrWrongParamType(name, kern.ParamName, kern.TypeString, args[kern.ParamName])
|
// return nil, kern.ErrWrongParamType(name, kern.ParamName, kern.TypeString, args[kern.ParamName])
|
||||||
} else {
|
// } else {
|
||||||
ctx.GetParent().DeleteVar(varName)
|
// ctx.GetParent().DeleteVar(varName)
|
||||||
result = nil
|
// result = nil
|
||||||
}
|
// }
|
||||||
return
|
// return
|
||||||
}
|
// }
|
||||||
|
|
||||||
//// import
|
//// import
|
||||||
|
|
||||||
@@ -307,10 +307,10 @@ func ImportBuiltinsFuncs(ctx kern.ExprContext) {
|
|||||||
NewFuncParam(kern.ParamValue),
|
NewFuncParam(kern.ParamValue),
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx.RegisterFunc("unset", kern.NewGolangFunctor(unsetFunc), kern.TypeAny, []kern.ExprFuncParam{
|
// ctx.RegisterFunc("unset", kern.NewGolangFunctor(unsetFunc), kern.TypeAny, []kern.ExprFuncParam{
|
||||||
NewFuncParam(kern.ParamName),
|
// NewFuncParam(kern.ParamName),
|
||||||
NewFuncParam(kern.ParamValue),
|
// NewFuncParam(kern.ParamValue),
|
||||||
})
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
|
|
||||||
|
//go:build graph
|
||||||
|
|
||||||
// graph.go
|
// graph.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ func ErrCantConvert(funcName string, value any, kind string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ErrExpectedGot(funcName string, kind string, value any) 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 {
|
func ErrFuncDivisionByZero(funcName string) error {
|
||||||
@@ -52,7 +52,7 @@ func ErrFuncDivisionByZero(funcName string) error {
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
func ErrInvalidParameterValue(funcName, paramName string, paramValue any) 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) {
|
func undefArticle(s string) (article string) {
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
|
// All rights reserved.
|
||||||
|
|
||||||
package kern
|
package kern
|
||||||
|
|
||||||
import "reflect"
|
import "reflect"
|
||||||
|
|||||||
+3
-1
@@ -58,7 +58,9 @@ func evalMap(ctx kern.ExprContext, opTerm *term) (v any, err error) {
|
|||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
v = values
|
if err == nil {
|
||||||
|
v = values
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,3 +72,56 @@ func TestFuncBase(t *testing.T) {
|
|||||||
// runTestSuiteSpec(t, section, inputs, 49)
|
// runTestSuiteSpec(t, section, inputs, 49)
|
||||||
runTestSuite(t, section, inputs)
|
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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -92,23 +92,6 @@ func doTest(t *testing.T, ctx kern.ExprContext, section string, input *inputType
|
|||||||
}
|
}
|
||||||
|
|
||||||
eq := kern.Equal(gotResult, input.wantResult)
|
eq := kern.Equal(gotResult, input.wantResult)
|
||||||
// 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
|
|
||||||
// }
|
|
||||||
// } else if dict1, ok := input.wantResult.(*kern.DictType); ok {
|
|
||||||
// if dict2, ok := gotResult.(*kern.DictType); ok {
|
|
||||||
// eq = dict1.Equals(*dict2)
|
|
||||||
// eqDone = true
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if !eqDone {
|
|
||||||
// eq = reflect.DeepEqual(gotResult, input.wantResult)
|
|
||||||
// }
|
|
||||||
|
|
||||||
if !eq /*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))
|
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))
|
||||||
|
|||||||
+4
-11
@@ -28,17 +28,10 @@ func TestFractionsParser(t *testing.T) {
|
|||||||
/* 13 */ {`builtin "math.arith"; mul(1:2, 2:3)`, kern.NewFraction(2, 6), nil},
|
/* 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},
|
/* 14 */ {`builtin "math.arith"; mul(1:2, 1.0, 2)`, float64(1.0), nil},
|
||||||
/* 15 */ {`1:0`, nil, `[1:3] division by zero`},
|
/* 15 */ {`1:0`, nil, `[1:3] division by zero`},
|
||||||
/* 16 */ {`fract(-0.5)`, kern.NewFraction(-1, 2), nil},
|
/* 16 */ {`1+1:2+0.5`, float64(2), nil},
|
||||||
/* 17 */ {`fract("")`, (*kern.FractionType)(nil), `bad syntax`},
|
/* 17 */ {`1:(2-2)`, nil, `[1:3] division by zero`},
|
||||||
/* 18 */ {`fract("-1")`, kern.NewFraction(-1, 1), nil},
|
/* 18 */ {`[0,1][1-1]:1`, kern.NewFraction(0, 1), nil},
|
||||||
/* 19 */ {`fract("+1")`, kern.NewFraction(1, 1), nil},
|
/* 19 */ {`1:2 == 0.5`, true, 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},
|
|
||||||
/* 26 */ {`1:2 == 0.5`, true, nil},
|
|
||||||
}
|
}
|
||||||
// runTestSuiteSpec(t, section, inputs, 26)
|
// runTestSuiteSpec(t, section, inputs, 26)
|
||||||
runTestSuite(t, section, inputs)
|
runTestSuite(t, section, inputs)
|
||||||
|
|||||||
@@ -49,6 +49,18 @@ func TestFuncs(t *testing.T) {
|
|||||||
runTestSuite(t, section, inputs)
|
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) {
|
func dummy(ctx kern.ExprContext, name string, args map[string]any) (result any, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
// All rights reserved.
|
// All rights reserved.
|
||||||
|
|
||||||
|
//go:build graph
|
||||||
|
|
||||||
// t_graph_test.go
|
// t_graph_test.go
|
||||||
package expr
|
package expr
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user