Skip to content

Commit

Permalink
orbis: fix condition for op right associativity, add token string for…
Browse files Browse the repository at this point in the history
…matting, set precedences for operators, impl unary negation
  • Loading branch information
lithdew committed May 26, 2020
1 parent ba1b352 commit 8b4980e
Showing 1 changed file with 59 additions and 11 deletions.
70 changes: 59 additions & 11 deletions orbis/constraint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,47 @@ const (
tokLTE
tokAND
tokOR
tokPlus
tokMinus
tokNegate
tokText
tokInt
tokFloat
tokBracketStart
tokBracketEnd
)

var tokStr = [...]string{
tokEOF: "eof",
tokGT: ">",
tokGTE: ">=",
tokLT: "<",
tokLTE: "<=",
tokAND: "&",
tokOR: "|",
tokPlus: "+",
tokMinus: "-",
tokNegate: "-",
tokText: "text",
tokInt: "int",
tokFloat: "float",
tokBracketStart: "(",
tokBracketEnd: ")",
}

func (t tokType) String() string {
return tokStr[t]
}

var precedences = [...]int{
tokGT: 1,
tokGTE: 1,
tokLT: 1,
tokLTE: 1,
tokAND: 1,
tokOR: 1,
tokGT: 3,
tokGTE: 3,
tokLT: 3,
tokLTE: 3,
tokPlus: 2,
tokMinus: 2,
tokAND: 1,
tokOR: 1,
}

const eof = rune(0)
Expand Down Expand Up @@ -86,7 +113,7 @@ func isHexRune(r rune) bool {
}

func TestConstraint(t *testing.T) {
q := `<=4000`
q := `>=1&<=400|>=500&<=600`

bc := 0 // byte count
cc := 0 // char count
Expand Down Expand Up @@ -440,25 +467,40 @@ func TestConstraint(t *testing.T) {
default:
panic(`'<=' is not paired with int or float`)
}
case tokAND:
fmt.Println("Hit &!")
spew.Dump(ops, vals)
fmt.Println()
case tokOR:
fmt.Println("Hit |!")
spew.Dump(ops, vals)
fmt.Println()
}
}

for current := lex(); current.typ != tokEOF; current = lex() {
current := lex()
last := current

for current.typ != tokEOF {
fmt.Printf("%s\n", current.repr(q))

switch current.typ {
case tokText, tokInt, tokFloat:
vals = append(vals, current)
case tokBracketStart:
ops = append(ops, current)
case tokGT, tokGTE, tokLT, tokLTE:
case tokGT, tokGTE, tokLT, tokLTE, tokAND, tokOR, tokPlus, tokMinus:
if current.typ == tokMinus && last.typ != tokInt && last.typ != tokFloat && last.typ != tokText {
current.typ = tokNegate
}

for len(ops) > 0 {
op := ops[len(ops)-1]

o1 := precedences[current.typ]
o2 := precedences[op.typ]

if op.typ == tokBracketStart || o1 > o2 || o1 == o2 {
if op.typ == tokBracketStart || o1 > o2 { // (also check o1 == o2 if op is right-associative)
break
}

Expand All @@ -468,6 +510,12 @@ func TestConstraint(t *testing.T) {
}
ops = append(ops, current)
}

spew.Dump(ops, vals)
fmt.Println()

last = current
current = lex()
}

for len(ops) > 0 {
Expand All @@ -481,5 +529,5 @@ func TestConstraint(t *testing.T) {
eval(op)
}

spew.Dump(ops, vals, res)
//spew.Dump(ops, vals, res)
}

0 comments on commit 8b4980e

Please sign in to comment.