-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
89 lines (80 loc) · 2.22 KB
/
Copy pathparse.go
File metadata and controls
89 lines (80 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"strings"
"slices"
)
func Tokenise(exp string) []string {
return strings.Split(exp, " ")
}
func Parse(tokens []string) Expression {
level := make([]int, len(tokens))
for lvl, i := 0, 0; i < len(tokens); i++ {
switch tokens[i] {
case "(": {
lvl++
level[i] = lvl
}
case ")": {
level[i] = lvl
lvl--
}
default:
level[i] = lvl
}
}
// remove enclosing parenthesis if they exist
remParenth := tokens[0] == "(" && tokens[len(tokens)-1] == ")"
if remParenth {
for i, val := range level {
if 0 < i && i < len(level)-1 && val < level[0] {
remParenth = false
}
}
}
if remParenth {
tokens = tokens[1:len(tokens)-1]
level = level[1:len(level)-1]
}
op, pos := 0, 0
minLvl := slices.Min(level)
for i, val := range tokens {
opNum := OpStrToNum(val)
if opNum == 0 || level[i] > minLvl {
continue
}
if val == _dimp {
op, pos = OpStrToNum(_dimp), i
break
} else if val == _imp && op < opNum {
op, pos = OpStrToNum(_imp), i
} else if val == _or && op < opNum {
op, pos = OpStrToNum(_or), i
} else if val == _and && op < opNum {
op, pos = OpStrToNum(_and), i
} else if val == _not && op < opNum {
op, pos = OpStrToNum(_not), i
}
}
var exp Expression
if op == 0 {
exp = Expression{nil, nil, tokens[0], _term}
} else if op == 1 {
rightExp := Parse(tokens[pos+1:])
exp = Expression{nil, &rightExp, _not, _unary}
} else if op == 2 {
leftExp, rightExp := Parse(tokens[:pos]), Parse(tokens[pos+1:])
exp = Expression{&leftExp, &rightExp, _and, _binary}
} else if op == 3 {
leftExp, rightExp := Parse(tokens[:pos]), Parse(tokens[pos+1:])
exp = Expression{&leftExp, &rightExp, _or, _binary}
} else if op == 4 {
leftExp, rightExp := Parse(tokens[:pos]), Parse(tokens[pos+1:])
exp = Expression{&leftExp, &rightExp, _imp, _binary}
} else if op == 5 {
leftExp, rightExp := Parse(tokens[:pos]), Parse(tokens[pos+1:])
exp = Expression{&Expression{&leftExp, &rightExp, _imp, _binary},
&Expression{&rightExp, &leftExp, _imp, _binary},
_and, _binary}
}
return exp
}