Skip to content

Commit

Permalink
changed return
Browse files Browse the repository at this point in the history
  • Loading branch information
ax4w committed Nov 19, 2023
1 parent 767685c commit 7758f14
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 67 deletions.
115 changes: 54 additions & 61 deletions eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var (
strongSplit = []string{"&&", "!&", "||", "!|"}
)

/*
* HELPER FUNCTIONS
*/

func validateDate(d string) bool {
_, err := time.Parse("2006-01-02", d)
if err != nil {
Expand All @@ -35,6 +39,33 @@ func validateDate(d string) bool {
return true
}

func splitForStrings(f string) (r []string) {
var tmp string
inString := false
for _, v := range f {
if string(v) != "'" {
tmp += string(v)
} else {
if inString {
tmp += string(v)
inString = false
r = append(r, tmp)
tmp = ""
continue
}
inString = true
r = append(r, tmp)
tmp = ""
tmp += string(v)

}
}
if len(tmp) > 0 {
r = append(r, tmp)
}
return r
}

func compareByteArray(b1, b2 []byte) bool {
if len(b1) != len(b2) {
return false
Expand Down Expand Up @@ -91,6 +122,20 @@ func compareDates(d1, d2 string) int {
return 0
}

func compareCheck(r, l *token) {
if !(r.tokenType == tokenTypeInt && l.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeInt && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate) {
panic("< is only supported for int, float and date")
}
}

/*£
* -----------------------------------------
*/

func evaluate(f *token) *token {
if f.left == nil && f.right == nil {
return f
Expand All @@ -100,13 +145,7 @@ func evaluate(f *token) *token {
r := evaluate(f.right)
switch string(m.value) {
case "<":
if !(r.tokenType == tokenTypeInt && l.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeInt && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate) {
panic("< is only supported for int, float and date")
}
compareCheck(r, l)
if l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate {
i := compareDates(string(l.value), string(r.value))
if i == -2 {
Expand All @@ -121,15 +160,8 @@ func evaluate(f *token) *token {
}
return &token{value: []byte("f"), tokenType: tokenTypeBoolean}
}

case ">":
if !(r.tokenType == tokenTypeInt && l.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeInt && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate) {
panic("<= is only supported for int, float and date")
}
compareCheck(r, l)
if l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate {
i := compareDates(string(l.value), string(r.value))
if i == 2 {
Expand All @@ -144,15 +176,8 @@ func evaluate(f *token) *token {
}
return &token{value: []byte("f"), tokenType: tokenTypeBoolean}
}

case ">=":
if !(r.tokenType == tokenTypeInt && l.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeInt && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate) {
panic("<= is only supported for int, float and date")
}
compareCheck(r, l)
if l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate {
i := compareDates(string(l.value), string(r.value))
if i == 1 {
Expand All @@ -167,15 +192,8 @@ func evaluate(f *token) *token {
}
return &token{value: []byte("f"), tokenType: tokenTypeBoolean}
}

case "<=":
if !(r.tokenType == tokenTypeInt && l.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeInt && r.tokenType == tokenTypeFloat ||
l.tokenType == tokenTypeFloat && r.tokenType == tokenTypeInt ||
l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate) {
panic("<= is only supported for int, float and date")
}
compareCheck(r, l)
if l.tokenType == tokenTypeDate && r.tokenType == tokenTypeDate {
i := compareDates(string(l.value), string(r.value))
if i == -1 {
Expand Down Expand Up @@ -208,7 +226,6 @@ func evaluate(f *token) *token {
}
return &token{value: []byte("f"), tokenType: tokenTypeBoolean}
}

case "!=":
if !(l.tokenType == r.tokenType ||
l.tokenType == tokenTypeChar && r.tokenType == tokenTypeInt ||
Expand Down Expand Up @@ -343,33 +360,6 @@ func traverseTree(t *token) {
traverseTree(t.right)
}

func splitForStrings(f string) (r []string) {
var tmp string
inString := false
for _, v := range f {
if string(v) != "'" {
tmp += string(v)
} else {
if inString {
tmp += string(v)
inString = false
r = append(r, tmp)
tmp = ""
continue
}
inString = true
r = append(r, tmp)
tmp = ""
tmp += string(v)

}
}
if len(tmp) > 0 {
r = append(r, tmp)
}
return r
}

func parse(f string) []*token {
var nodes []*token
split := splitForStrings(f)
Expand Down Expand Up @@ -429,7 +419,7 @@ func parse(f string) []*token {
return nodes
}

func eval(f string) string {
func eval(f string) bool {
p := parse(f)
t := toTree(p)
if len(t) == 0 {
Expand All @@ -439,5 +429,8 @@ func eval(f string) string {
if e == nil {
panic("Eval returned nil")
}
return string(e.value)
if string(e.value) == "t" {
return true
}
return false
}
10 changes: 5 additions & 5 deletions eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ import (
//

func TestEval(t *testing.T) {
if eval("( 'William' == 'William' && 2 == 2 ) || 85.5 >= 90.0") != "t" {
if !eval("( 'William' == 'William' && 2 == 2 ) || 85.5 >= 90.0") {
t.Fatalf("Should return true")
}
if eval("1 != 1") != "f" {
if eval("1 != 1") {
t.Fatalf("Should be false")
}
if eval("( 'Hi' == 'hi' ) || ( 1 == 1 && ( 5 != 5 !& ( t == f ) ) ) && 1.0 < 1.1") != "t" {
if !eval("( 'Hi' == 'hi' ) || ( 1 == 1 && ( 5 != 5 !& ( t == f ) ) ) && 1.0 < 1.1") {
t.Fatalf("Should be true")
}
if eval("2023-11-19 == 2023-11-19") != "t" {
if !eval("2023-11-19 == 2023-11-19") {
t.Fatalf("Should be true")
}
if eval("2023-11-19 == 2023-11-19") != "t" {
if !eval("2023-11-19 == 2023-11-19") {
t.Fatalf("Should be true")
}
}
2 changes: 1 addition & 1 deletion gorage_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (g *Table) Where(f string) *Table {
tmp = append(tmp, k)
}
q := strings.Join(tmp, " ")
if e := eval(q); e == "t" {
if eval(q) {
res.Rows = append(res.Rows, v)
}
}
Expand Down

0 comments on commit 7758f14

Please sign in to comment.