Skip to content

Commit

Permalink
orbis: impl '+' and '-' op
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed May 26, 2020
1 parent f57bd67 commit 90eb2dc
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions orbis/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ type node struct {
}

func TestConstraint(t *testing.T) {
q := `!"test"`
val := "test"
q := `123 | "hello " + "world"`
val := "hello world"

bc := 0 // byte count
cc := 0 // char count
Expand Down Expand Up @@ -529,6 +529,65 @@ func TestConstraint(t *testing.T) {
default:
panic(`'<=' not paired with int or float`)
}
case tokPlus:
l := len(vals) - 2
r := len(vals) - 1
switch vals[l].typ {
case nodeInt:
switch vals[r].typ {
case nodeInt:
vals[l] = node{typ: nodeInt, int: vals[l].int + vals[r].int}
case nodeFloat:
vals[l] = node{typ: nodeFloat, float: float64(vals[l].int) + vals[r].float}
default:
panic(`lhs is int, rhs for '+' must be an int or float`)
}
case nodeFloat:
switch vals[r].typ {
case nodeInt:
vals[l] = node{typ: nodeFloat, float: vals[l].float + float64(vals[r].int)}
case nodeFloat:
vals[l] = node{typ: nodeFloat, float: vals[l].float + vals[r].float}
default:
panic(`lhs is float, rhs for '+' must be an int or float`)
}
case nodeText:
switch vals[r].typ {
case nodeText:
vals[l] = node{typ: nodeText, text: vals[l].text + vals[r].text}
default:
panic(`lhs is string, rhs for '+' must be a string`)
}
default:
panic("??? '+'")
}
vals = vals[:r]
case tokMinus:
l := len(vals) - 2
r := len(vals) - 1
switch vals[l].typ {
case nodeInt:
switch vals[r].typ {
case nodeInt:
vals[l] = node{typ: nodeInt, int: vals[l].int - vals[r].int}
case nodeFloat:
vals[l] = node{typ: nodeFloat, float: float64(vals[l].int) - vals[r].float}
default:
panic(`lhs is int, rhs for '+' must be an int or float`)
}
case nodeFloat:
switch vals[r].typ {
case nodeInt:
vals[l] = node{typ: nodeFloat, float: vals[l].float - float64(vals[r].int)}
case nodeFloat:
vals[l] = node{typ: nodeFloat, float: vals[l].float - vals[r].float}
default:
panic(`lhs is float, rhs for '+' must be an int or float`)
}
default:
panic("??? '+'")
}
vals = vals[:r]
case tokBang:
i := len(vals) - 1
switch vals[i].typ {
Expand Down

0 comments on commit 90eb2dc

Please sign in to comment.