Skip to content

Commit

Permalink
more complex eval tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ax4w committed Nov 13, 2023
1 parent 725a74b commit 97879af
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
13 changes: 13 additions & 0 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,3 +329,16 @@ func parse(f string) []*token {
}
return nodes
}

func runEval(f string) string {
p := parse(f)
t := toTree(p)
if len(t) == 0 {
panic("Error while runEval")
}
e := eval(t[0])
if e == nil {
panic("Eval returned nil")
}
return string(e.value)
}
16 changes: 9 additions & 7 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ import (
func TestEval(t *testing.T) {
//println(float64(int(rune('a'))))
//f := fmt.Sprintf("'%s' = 'hallo welt' & %d = 3 & %d = 4", "hallo welt", 3, 4)

p := parse("( 'William' == 'William' && 2 == 2 ) || 85.5 >= 90.0")
tr := toTree(p)
//traverseTree(tr[0])
//println(len(tr))
//println(len(tr))
println(string(eval(tr[0]).value))
if runEval("( 'William' == 'William' && 2 == 2 ) || 85.5 >= 90.0") != "t" {
t.Fatalf("Should return true")
}
if runEval("1 != 1") != "f" {
t.Fatalf("Should be false")
}
if runEval("( 'Hi' == 'hi' ) || ( 1 == 1 && ( 5 != 5 !& ( t == f ) ) ) && 1.0 < 1.1") != "t" {
t.Fatalf("Should be true")
}
//traverseTree(tr[0])

//_printTree(&tr[0])
Expand Down
2 changes: 1 addition & 1 deletion gorageTable.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (g *GorageTable) Where(f string) *GorageTable {
tmp = append(tmp, k)
}
q := strings.Join(tmp, " ")
if e := eval(toTree(parse(q))[0]); e != nil && string(e.value) == "t" {
if e := runEval(q); e == "t" {
res.Rows = append(res.Rows, v)
}
}
Expand Down

0 comments on commit 97879af

Please sign in to comment.