From da5d653de8920091fb689a85eabdfc2c34278b1b Mon Sep 17 00:00:00 2001 From: Stephen Dolan Date: Tue, 4 Sep 2012 20:43:40 +0100 Subject: [PATCH] Add a Boolean "not" operator. Not wholly convinced this is a good idea, maybe "not" should be a builtin function instead? 'not (.a == .b)' vs. '.a == .b | not' --- c/compile.c | 6 +++++- c/compile.h | 1 + c/lexer.l | 1 + c/parser.y | 5 +++++ c/testdata | 4 ++++ 5 files changed, 16 insertions(+), 1 deletion(-) diff --git a/c/compile.c b/c/compile.c index c4782f1d..b7f91aa3 100644 --- a/c/compile.c +++ b/c/compile.c @@ -383,7 +383,11 @@ block gen_or(block a, block b) { block_append(&code, gen_condbranch(block_join(gen_op_simple(POP), gen_op_const(LOADK, jv_true())), if_a_false)); return code; - +} + +block gen_not(block a) { + return block_join(a, gen_condbranch(gen_op_const(LOADK, jv_false()), + gen_op_const(LOADK, jv_true()))); } block gen_cond(block cond, block iftrue, block iffalse) { diff --git a/c/compile.h b/c/compile.h index 1a7c62c6..8f9526b0 100644 --- a/c/compile.h +++ b/c/compile.h @@ -29,6 +29,7 @@ block gen_assign(block expr); block gen_definedor(block a, block b); block gen_and(block a, block b); block gen_or(block a, block b); +block gen_not(block a); block gen_cond(block cond, block iftrue, block iffalse); diff --git a/c/lexer.l b/c/lexer.l index 47789d29..f2651762 100644 --- a/c/lexer.l +++ b/c/lexer.l @@ -19,6 +19,7 @@ "elif" { return ELSE_IF; } "and" { return AND; } "or" { return OR; } +"not" { return NOT; } "end" { return END; } "//" { return DEFINEDOR; } "."|"="|";"|"["|"]"|","|":"|"("|")"|"{"|"}"|"|"|"+"|"\$" { return yytext[0];} diff --git a/c/parser.y b/c/parser.y index 5b997e62..54456d09 100644 --- a/c/parser.y +++ b/c/parser.y @@ -36,6 +36,7 @@ %token END "end" %token AND "and" %token OR "or" +%token NOT "not" %right "//" %nonassoc '=' SETPIPE %nonassoc EQ @@ -111,6 +112,10 @@ Exp "and" Exp { $$ = gen_and($1, $3); } | +"not" Exp { + $$ = gen_not($2); +} | + Exp "//" Exp { $$ = gen_definedor($1, $3); } | diff --git a/c/testdata b/c/testdata index 4241c6cd..f453edd3 100644 --- a/c/testdata +++ b/c/testdata @@ -260,3 +260,7 @@ def inc(x): x |= .+1; inc(.[].a) [false,true] [false,true] [false,false] + +[.[] | not .] +[1,0,false,null,true,"hello"] +[false,false,true,true,false,false]