Skip to content

Commit

Permalink
Implement and, or, !=
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jun 27, 2017
1 parent 9b8da4f commit a5a3ad2
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 153 deletions.
9 changes: 9 additions & 0 deletions expressions/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ var evaluatorTests = []struct {
{"1 == 1.0", true},
{"1 < 2.0", true},
{"1.0 < 2", true},

{"1 != 1", false},
{"1 != 2", true},

{"true and false", false},
{"true and true", true},
{"true and true and true", true},
{"false or false", false},
{"false or true", true},
}

var evaluatorTestContext = NewContext(map[string]interface{}{
Expand Down
30 changes: 26 additions & 4 deletions expressions/expressions.y
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ func init() {
f func(Context) interface{}
loopmods LoopModifiers
}
%type <f> expr rel expr1 loop
%type <f> expr rel expr1 expr2 loop
%type<loopmods> loop_modifiers
%token <val> LITERAL
%token <name> IDENTIFIER KEYWORD RELATION
%token ASSIGN LOOP
%token EQ FOR IN
%token EQ NEQ FOR IN AND OR CONTAINS
%left '.' '|'
%left '<' '>'
%%
start:
rel ';' { yylex.(*lexer).val = $1 }
expr2 ';' { yylex.(*lexer).val = $1 }
| ASSIGN IDENTIFIER '=' expr1 ';' {
name, expr := $2, $4
yylex.(*lexer).val = func(ctx Context) interface{} {
Expand Down Expand Up @@ -110,7 +110,13 @@ rel:
return generics.Equal(a, b)
}
}
| expr '<' expr {
| expr NEQ expr {
fa, fb := $1, $3
$$ = func(ctx Context) interface{} {
a, b := fa(ctx), fb(ctx)
return !generics.Equal(a, b)
}
}| expr '<' expr {
fa, fb := $1, $3
$$ = func(ctx Context) interface{} {
a, b := fa(ctx), fb(ctx)
Expand All @@ -125,3 +131,19 @@ rel:
}
}
;

expr2:
rel
| expr2 AND rel {
fa, fb := $1, $3
$$ = func(ctx Context) interface{} {
return generics.IsTrue(fa(ctx)) && generics.IsTrue(fb(ctx))
}
}
| expr2 OR rel {
fa, fb := $1, $3
$$ = func(ctx Context) interface{} {
return generics.IsTrue(fa(ctx)) || generics.IsTrue(fb(ctx))
}
}
;
Loading

0 comments on commit a5a3ad2

Please sign in to comment.