9 type Expression interface
{}
10 type ParenExpr
struct {
20 type BinOpExpr
struct {
25 type AssocExpr
struct {
30 type UnaryExpr
struct {
41 %token
<token
> NUMBER NOT AND OR IS
52 yylex.
(*Lexer
).result
= $$
57 $$
= NumExpr
{literal
: $1.literal
}
61 $$
= UnaryExpr
{operator
: "!", right
: $2}
65 $$
= AssocExpr
{left
: $1, operator
: "&&", right
: $3}
69 $$
= ParenExpr
{SubExpr
: $2}
73 $$
= AssocExpr
{left
: $1, operator
: "||", right
: $3}
77 $$
= BinOpExpr
{left
: NumExpr
{literal
: $1.literal
}, operator
: "<", right
: NumExpr
{literal
: $3.literal
}}
81 $$
= BinOpExpr
{left
: NumExpr
{literal
: $1.literal
}, operator
: ">", right
: NumExpr
{literal
: $3.literal
}}
85 $$
= BinOpExpr
{left
: NumExpr
{literal
: $1.literal
}, operator
: "=", right
: NumExpr
{literal
: $3.literal
}}
89 $$
= BinOpExpr
{left
: NumExpr
{literal
: $1.literal
}, operator
: ">=", right
: NumExpr
{literal
: $3.literal
}}
93 $$
= BinOpExpr
{left
: NumExpr
{literal
: $1.literal
}, operator
: "<=", right
: NumExpr
{literal
: $3.literal
}}
98 Vars map
[string]interface
{}
101 func
(l
*Lexer
) Lex
(lval
*yySymType
) int {
123 if v
, ok
:= l.Vars
[lit
]; ok
{
127 lit
= strconv.Itoa
(v.
(int))
132 lval.token
= Token
{token
: tok
, literal
: lit
}
135 func
(l
*Lexer
) Error
(e
string) {
138 func EvalN
(e Expression
) int {
139 switch t
:= e.
(type
) {
141 num
, _
:= strconv.Atoi
(t.literal
)
146 func Eval
(e Expression
) bool {
147 switch t
:= e.
(type
) {
150 return Eval
(t.SubExpr
)
152 fmt.Println
(t.operator
)
153 right
:= Eval
(t.right
)
161 right
:= Eval
(t.right
)
170 left
:= EvalN
(t.left
)
171 right
:= EvalN
(t.right
)
181 fmt.Printf
("unsuported expr[%+v]", t
)
185 func Parse
(exp
string, vars map
[string]interface
{}) bool {
188 l.Init
(strings.NewReader
(exp
))
190 return Eval
(l.result
)
194 vars
:= map
[string]interface
{}{
198 f
:= Parse
("NOT (A IS B)", vars
)
199 fmt.Printf
("%t\n", f
) // false
201 t
:= Parse
("A IS B", vars
)
202 fmt.Printf
("%t\n", t
) // true