New operators 'but' and assignment ('=')
This commit is contained in:
parent
43fd457383
commit
d0572260c7
43
operator-assign.go
Normal file
43
operator-assign.go
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
|
// All rights reserved.
|
||||||
|
|
||||||
|
// operator-assign.go
|
||||||
|
package expr
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
//-------- assign term
|
||||||
|
|
||||||
|
func newAssignTerm(tk *Token) (inst *term) {
|
||||||
|
return &term{
|
||||||
|
tk: *tk,
|
||||||
|
class: classOperator,
|
||||||
|
kind: kindUnknown,
|
||||||
|
children: make([]*term, 0, 2),
|
||||||
|
position: posInfix,
|
||||||
|
priority: priAssign,
|
||||||
|
evalFunc: evalAssign,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func evalAssign(ctx exprContext, self *term) (v any, err error) {
|
||||||
|
if err = self.checkOperands(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
leftTerm := self.children[0]
|
||||||
|
if leftTerm.tk.Sym != SymIdentifier {
|
||||||
|
err = fmt.Errorf("left operand of %q must be a variable", self.tk.source)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if v, err = self.children[1].compute(ctx); err == nil {
|
||||||
|
ctx.SetValue(leftTerm.tk.source, v)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// init
|
||||||
|
func init() {
|
||||||
|
registerTermConstructor(SymEqual, newAssignTerm)
|
||||||
|
}
|
29
operator-but.go
Normal file
29
operator-but.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
// Copyright (c) 2024 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||||
|
// All rights reserved.
|
||||||
|
|
||||||
|
// operator-but.go
|
||||||
|
package expr
|
||||||
|
|
||||||
|
//-------- but term
|
||||||
|
|
||||||
|
func newButTerm(tk *Token) (inst *term) {
|
||||||
|
return &term{
|
||||||
|
tk: *tk,
|
||||||
|
class: classOperator,
|
||||||
|
kind: kindUnknown,
|
||||||
|
children: make([]*term, 0, 2),
|
||||||
|
position: posInfix,
|
||||||
|
priority: priBut,
|
||||||
|
evalFunc: evalBut,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func evalBut(ctx exprContext, self *term) (v any, err error) {
|
||||||
|
_, v, err = self.evalInfix(ctx)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// init
|
||||||
|
func init() {
|
||||||
|
registerTermConstructor(SymKwBut, newButTerm)
|
||||||
|
}
|
@ -26,7 +26,7 @@ func TestParser(t *testing.T) {
|
|||||||
importMathFuncs(ctx)
|
importMathFuncs(ctx)
|
||||||
|
|
||||||
// inputs1 := []inputType{
|
// inputs1 := []inputType{
|
||||||
// {`add(1,2,3)`, int64(6), nil},
|
// {`x=2 but x*10`, int64(20), nil},
|
||||||
// }
|
// }
|
||||||
|
|
||||||
inputs := []inputType{
|
inputs := []inputType{
|
||||||
@ -121,7 +121,11 @@ func TestParser(t *testing.T) {
|
|||||||
/* 89 */ {`~ 2 > 1`, false, nil},
|
/* 89 */ {`~ 2 > 1`, false, nil},
|
||||||
/* 90 */ {`~ true && true`, false, nil},
|
/* 90 */ {`~ true && true`, false, nil},
|
||||||
/* 91 */ {`~ false || true`, true, nil},
|
/* 91 */ {`~ false || true`, true, nil},
|
||||||
/* 92 */ {`~ (false || true)`, false, nil},
|
/* 92 */ {`false but true`, true, nil},
|
||||||
|
/* 93 */ {`2+3 but 5*2`, int64(10), nil},
|
||||||
|
/* 94 */ {`add(1,2) but var2`, "abc", nil},
|
||||||
|
/* 95 */ {`x=2`, int64(2), nil},
|
||||||
|
/* 95 */ {`x=2 but x*10`, int64(20), nil},
|
||||||
}
|
}
|
||||||
succeeded := 0
|
succeeded := 0
|
||||||
failed := 0
|
failed := 0
|
||||||
@ -132,11 +136,7 @@ func TestParser(t *testing.T) {
|
|||||||
var gotResult any
|
var gotResult any
|
||||||
var gotErr error
|
var gotErr error
|
||||||
|
|
||||||
if input.wantErr == nil {
|
logTest(t, i+1, input.source, input.wantResult, input.wantErr)
|
||||||
t.Log(fmt.Sprintf("[+]Test nr %2d -- %q", i+1, input.source))
|
|
||||||
} else {
|
|
||||||
t.Log(fmt.Sprintf("[-]Test nr %2d -- %q", i+1, input.source))
|
|
||||||
}
|
|
||||||
|
|
||||||
r := strings.NewReader(input.source)
|
r := strings.NewReader(input.source)
|
||||||
scanner := NewScanner(r, DefaultTranslations())
|
scanner := NewScanner(r, DefaultTranslations())
|
||||||
@ -196,6 +196,7 @@ func TestListParser(t *testing.T) {
|
|||||||
/* 8 */ {`add([1,[2,2],3,2])`, int64(10), nil},
|
/* 8 */ {`add([1,[2,2],3,2])`, int64(10), nil},
|
||||||
/* 9 */ {`mul([1,4,3.0,2])`, float64(24.0), nil},
|
/* 9 */ {`mul([1,4,3.0,2])`, float64(24.0), nil},
|
||||||
/* 10 */ {`add([1,"hello"])`, nil, errors.New(`add(): param nr 2 has wrong type string, number expected`)},
|
/* 10 */ {`add([1,"hello"])`, nil, errors.New(`add(): param nr 2 has wrong type string, number expected`)},
|
||||||
|
/* 11 */ {`[a=1,b=2,c=3] but a+b+c`, int64(6), nil},
|
||||||
// /* 8 */ {`[int(x)|x=csv("test.csv",1,all(),1)]`, []any{int64(10), int64(40), int64(20)}, nil},
|
// /* 8 */ {`[int(x)|x=csv("test.csv",1,all(),1)]`, []any{int64(10), int64(40), int64(20)}, nil},
|
||||||
// /* 9 */ {`sum(@[int(x)|x=csv("test.csv",1,all(),1)])`, []any{int64(10), int64(40), int64(20)}, nil},
|
// /* 9 */ {`sum(@[int(x)|x=csv("test.csv",1,all(),1)])`, []any{int64(10), int64(40), int64(20)}, nil},
|
||||||
}
|
}
|
||||||
@ -209,11 +210,7 @@ func TestListParser(t *testing.T) {
|
|||||||
var gotResult any
|
var gotResult any
|
||||||
var gotErr error
|
var gotErr error
|
||||||
|
|
||||||
if input.wantErr == nil {
|
logTest(t, i+1, input.source, input.wantResult, input.wantErr)
|
||||||
t.Log(fmt.Sprintf("[+]Test nr %2d -- %q --> %v", i+1, input.source, input.wantResult))
|
|
||||||
} else {
|
|
||||||
t.Log(fmt.Sprintf("[-]Test nr %2d -- %q --> %v", i+1, input.source, input.wantResult))
|
|
||||||
}
|
|
||||||
|
|
||||||
r := strings.NewReader(input.source)
|
r := strings.NewReader(input.source)
|
||||||
scanner := NewScanner(r, DefaultTranslations())
|
scanner := NewScanner(r, DefaultTranslations())
|
||||||
@ -269,3 +266,11 @@ func TestListParser(t *testing.T) {
|
|||||||
t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed))
|
t.Log(fmt.Sprintf("test count: %d, succeeded count: %d, failed count: %d", len(inputs), succeeded, failed))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func logTest(t *testing.T, n int, source string, wantResult any, wantErr error) {
|
||||||
|
if wantErr == nil {
|
||||||
|
t.Log(fmt.Sprintf("[+]Test nr %2d -- %q --> %v", n, source, wantResult))
|
||||||
|
} else {
|
||||||
|
t.Log(fmt.Sprintf("[-]Test nr %2d -- %q --> %v", n, source, wantErr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user