Expressions now support function definition
This commit is contained in:
+31
-25
@@ -18,12 +18,6 @@ func TestParser(t *testing.T) {
|
||||
wantErr error
|
||||
}
|
||||
|
||||
// inputs1 := []inputType{
|
||||
// {`a=5; a`, int64(5), nil},
|
||||
// {`a=5; b=2; add(a, b*3)`, int64(11), nil},
|
||||
// {`"a" < "b" AND ~ 2 == 1`, true, nil},
|
||||
// }
|
||||
|
||||
inputs := []inputType{
|
||||
/* 1 */ {`1+/*5*/2`, int64(3), nil},
|
||||
/* 2 */ {`3 == 4`, false, nil},
|
||||
@@ -66,10 +60,10 @@ func TestParser(t *testing.T) {
|
||||
/* 39 */ {`(((1)))`, int64(1), nil},
|
||||
/* 40 */ {`"uno_" + var2`, `uno_abc`, nil},
|
||||
/* 41 */ {`0 || 0.0 && "hello"`, false, nil},
|
||||
/* 42 */ {`"s" + true`, nil, errors.New(`left operand 's' [string] and right operand 'true' [bool] are not compatible with operator "+"`)},
|
||||
/* 43 */ {`+false`, nil, errors.New(`prefix/postfix operator "+" do not support operand 'false' [bool]`)},
|
||||
/* 42 */ {`"s" + true`, nil, errors.New(`[1:6] left operand 's' [string] and right operand 'true' [bool] are not compatible with operator "+"`)},
|
||||
/* 43 */ {`+false`, nil, errors.New(`[1:2] prefix/postfix operator "+" do not support operand 'false' [bool]`)},
|
||||
/* 44 */ {`false // very simple expression`, false, nil},
|
||||
/* 45 */ {`1 + // Missing right operator`, nil, errors.New(`infix operator "+" requires two operands, got 1`)},
|
||||
/* 45 */ {`1 + // Missing right operator`, nil, errors.New(`[1:4] infix operator "+" requires two operands, got 1`)},
|
||||
/* 46 */ {"", nil, errors.New(`empty expression`)},
|
||||
/* 47 */ {"4!", int64(24), nil},
|
||||
/* 48 */ {"(-4)!", nil, errors.New(`factorial of a negative integer (-4) is not allowed`)},
|
||||
@@ -84,18 +78,18 @@ func TestParser(t *testing.T) {
|
||||
/* 57 */ {`"1.5" > "7"`, false, nil},
|
||||
/* 58 */ {`"1.5" == "7"`, false, nil},
|
||||
/* 59 */ {`"1.5" != "7"`, true, nil},
|
||||
/* 60 */ {"1.5 < ", nil, errors.New(`infix operator "<" requires two operands, got 1`)},
|
||||
/* 61 */ {"1.5 > ", nil, errors.New(`infix operator ">" requires two operands, got 1`)},
|
||||
/* 62 */ {"1.5 <= ", nil, errors.New(`infix operator "<=" requires two operands, got 1`)},
|
||||
/* 63 */ {"1.5 >= ", nil, errors.New(`infix operator ">=" requires two operands, got 1`)},
|
||||
/* 64 */ {"1.5 != ", nil, errors.New(`infix operator "!=" requires two operands, got 1`)},
|
||||
/* 65 */ {"1.5 == ", nil, errors.New(`infix operator "==" requires two operands, got 1`)},
|
||||
/* 66 */ {`"1.5" < `, nil, errors.New(`infix operator "<" requires two operands, got 1`)},
|
||||
/* 67 */ {`"1.5" > `, nil, errors.New(`infix operator ">" requires two operands, got 1`)},
|
||||
/* 68 */ {`"1.5" == `, nil, errors.New(`infix operator "==" requires two operands, got 1`)},
|
||||
/* 69 */ {`"1.5" != `, nil, errors.New(`infix operator "!=" requires two operands, got 1`)},
|
||||
/* 60 */ {"1.5 < ", nil, errors.New(`[1:6] infix operator "<" requires two operands, got 1`)},
|
||||
/* 61 */ {"1.5 > ", nil, errors.New(`[1:6] infix operator ">" requires two operands, got 1`)},
|
||||
/* 62 */ {"1.5 <= ", nil, errors.New(`[1:6] infix operator "<=" requires two operands, got 1`)},
|
||||
/* 63 */ {"1.5 >= ", nil, errors.New(`[1:6] infix operator ">=" requires two operands, got 1`)},
|
||||
/* 64 */ {"1.5 != ", nil, errors.New(`[1:6] infix operator "!=" requires two operands, got 1`)},
|
||||
/* 65 */ {"1.5 == ", nil, errors.New(`[1:6] infix operator "==" requires two operands, got 1`)},
|
||||
/* 66 */ {`"1.5" < `, nil, errors.New(`[1:8] infix operator "<" requires two operands, got 1`)},
|
||||
/* 67 */ {`"1.5" > `, nil, errors.New(`[1:8] infix operator ">" requires two operands, got 1`)},
|
||||
/* 68 */ {`"1.5" == `, nil, errors.New(`[1:8] infix operator "==" requires two operands, got 1`)},
|
||||
/* 69 */ {`"1.5" != `, nil, errors.New(`[1:8] infix operator "!=" requires two operands, got 1`)},
|
||||
/* 70 */ {"+1.5", float64(1.5), nil},
|
||||
/* 71 */ {"+", nil, errors.New(`prefix operator "+" requires one operand`)},
|
||||
/* 71 */ {"+", nil, errors.New(`[1:2] prefix operator "+" requires one operand`)},
|
||||
/* 72 */ {"4 / 0", nil, errors.New(`division by zero`)},
|
||||
/* 73 */ {"4.0 / 0", nil, errors.New(`division by zero`)},
|
||||
/* 74 */ {"4.0 / \n2", float64(2.0), nil},
|
||||
@@ -109,7 +103,7 @@ func TestParser(t *testing.T) {
|
||||
/* 82 */ {`5 % 2`, int64(1), nil},
|
||||
/* 83 */ {`5 % (-2)`, int64(1), nil},
|
||||
/* 84 */ {`-5 % 2`, int64(-1), nil},
|
||||
/* 85 */ {`5 % 2.0`, nil, errors.New(`left operand '5' [int64] and right operand '2' [float64] are not compatible with operator "%"`)},
|
||||
/* 85 */ {`5 % 2.0`, nil, errors.New(`[1:4] left operand '5' [int64] and right operand '2' [float64] are not compatible with operator "%"`)},
|
||||
/* 86 */ {`"a" < "b" AND NOT (2 < 1)`, true, nil},
|
||||
/* 87 */ {`"a" < "b" AND NOT (2 == 1)`, true, nil},
|
||||
/* 88 */ {`"a" < "b" AND ~ 2 == 1`, true, nil},
|
||||
@@ -129,12 +123,24 @@ func TestParser(t *testing.T) {
|
||||
/* 102 */ {`a=5; a`, int64(5), nil},
|
||||
/* 103 */ {`a=5; b=2; add(a, b*3)`, int64(11), nil},
|
||||
/* 104 */ {`2=5`, nil, errors.New(`assign operator ("=") must be preceded by a variable`)},
|
||||
/* 105 */ {`2+a=5`, nil, errors.New(`left operand of "=" must be a variable`)},
|
||||
/* 105 */ {`2+a=5`, nil, errors.New(`[1:3] left operand of "=" must be a variable`)},
|
||||
/* 106 */ {`2+(a=5)`, int64(7), nil},
|
||||
/* 107 */ {`two=func(){2}; two()`, int64(2), nil},
|
||||
/* 108 */ {`double=func(x) {2*x}; (double(3))`, int64(6), nil},
|
||||
/* 109 */ {`double=func(x){2*x}; double(3)`, int64(6), nil},
|
||||
/* 110 */ {`double=func(x){2*x}; a=5; double(3+a) + 1`, int64(17), nil},
|
||||
/* 111 */ {`double=func(x){2*x}; a=5; two=func() {2}; (double(3+a) + 1) * two()`, int64(34), nil},
|
||||
}
|
||||
succeeded := 0
|
||||
failed := 0
|
||||
|
||||
// inputs1 := []inputType{
|
||||
// {`add(1,2,3)`, int64(6), nil},
|
||||
// {`a=5; a`, int64(5), nil},
|
||||
// {`a=5; b=2; add(a, b*3)`, int64(11), nil},
|
||||
// {`"a" < "b" AND ~ 2 == 1`, true, nil},
|
||||
// }
|
||||
|
||||
for i, input := range inputs {
|
||||
var expr *ast
|
||||
var gotResult any
|
||||
@@ -153,7 +159,7 @@ func TestParser(t *testing.T) {
|
||||
|
||||
good := true
|
||||
if expr, gotErr = parser.parse(scanner); gotErr == nil {
|
||||
gotResult, gotErr = expr.Eval(ctx)
|
||||
gotResult, gotErr = expr.Eval(ctx, true)
|
||||
}
|
||||
|
||||
if gotResult != input.wantResult {
|
||||
@@ -177,7 +183,7 @@ func TestParser(t *testing.T) {
|
||||
t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed))
|
||||
}
|
||||
|
||||
func NoTestListParser(t *testing.T) {
|
||||
func TestListParser(t *testing.T) {
|
||||
type inputType struct {
|
||||
source string
|
||||
wantResult any
|
||||
@@ -227,7 +233,7 @@ func NoTestListParser(t *testing.T) {
|
||||
|
||||
good := true
|
||||
if expr, gotErr = parser.parse(scanner); gotErr == nil {
|
||||
gotResult, gotErr = expr.Eval(ctx)
|
||||
gotResult, gotErr = expr.Eval(ctx, true)
|
||||
}
|
||||
|
||||
if (gotResult == nil && input.wantResult != nil) || (gotResult != nil && input.wantResult == nil) {
|
||||
|
||||
Reference in New Issue
Block a user