Skip to content

Commit

Permalink
short circuit and/or expressions in storm (#1952)
Browse files Browse the repository at this point in the history
short circuit and/or expressions in storm
  • Loading branch information
invisig0th authored Nov 11, 2020
1 parent 41ae5a2 commit 23b8a9b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
22 changes: 16 additions & 6 deletions synapse/lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,10 +2479,6 @@ async def expr_ge(x, y):
return await toint(x) >= await toint(y)
async def expr_le(x, y):
return await toint(x) <= await toint(y)
async def expr_or(x, y):
return await tobool(x) or await tobool(y)
async def expr_and(x, y):
return await tobool(x) and await tobool(y)
async def expr_prefix(x, y):
x, y = await tostr(x), await tostr(y)
return x.startswith(y)
Expand All @@ -2503,8 +2499,6 @@ async def expr_re(x, y):
'<': expr_lt,
'>=': expr_ge,
'<=': expr_le,
'or': expr_or,
'and': expr_and,
'^=': expr_prefix,
}

Expand Down Expand Up @@ -2546,6 +2540,22 @@ async def compute(self, runt, path):
parm2 = await self.kids[2].compute(runt, path)
return await self._operfunc(parm1, parm2)

class ExprOrNode(Value):
async def compute(self, runt, path):
parm1 = await self.kids[0].compute(runt, path)
if await tobool(parm1):
return True
parm2 = await self.kids[2].compute(runt, path)
return await tobool(parm2)

class ExprAndNode(Value):
async def compute(self, runt, path):
parm1 = await self.kids[0].compute(runt, path)
if not await tobool(parm1):
return False
parm2 = await self.kids[2].compute(runt, path)
return await tobool(parm2)

class TagName(Value):

def prepare(self):
Expand Down
4 changes: 2 additions & 2 deletions synapse/lib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,8 +465,8 @@ def massage_vartokn(x):
'edittagpropdel': s_ast.EditTagPropDel,
'editunivdel': s_ast.EditUnivDel,
'editunivset': s_ast.EditPropSet,
'expror': s_ast.ExprNode,
'exprand': s_ast.ExprNode,
'expror': s_ast.ExprOrNode,
'exprand': s_ast.ExprAndNode,
'exprnot': s_ast.UnaryExprNode,
'exprcmp': s_ast.ExprNode,
'exprproduct': s_ast.ExprNode,
Expand Down
8 changes: 4 additions & 4 deletions synapse/tests/test_lib_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,8 @@
'Query: [LiftProp: [Const: media:news], EditEdgeDel: [Const: refs, SubQuery: [Query: [LiftPropBy: [Const: inet:fqdn, Const: =, Const: woot.com]]]]]',
'Query: [LiftProp: [Const: media:news], EditEdgeAdd: [Const: refs, SubQuery: [Query: [LiftPropBy: [Const: inet:fqdn, Const: =, Const: woot.com]]]]]',
'Query: [CmdOper: [Const: cron, List: [Const: add, Const: --monthly, Const: -1:12:30, ArgvQuery: [Query: [LiftTag: [TagName: [Const: bar]]]]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [ExprNode: [ExprNode: [Const: 1, Const: or, Const: 1], Const: or, Const: 0]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [ExprNode: [ExprNode: [Const: 1, Const: and, Const: 1], Const: and, Const: 0]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [ExprOrNode: [ExprOrNode: [Const: 1, Const: or, Const: 1], Const: or, Const: 0]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [ExprAndNode: [ExprAndNode: [Const: 1, Const: and, Const: 1], Const: and, Const: 0]]]]',
'Query: [SetVarOper: [Const: var, Const: tag1], LiftTag: [TagName: [Const: base, VarValue: [Const: var]]]]',
'Query: [LiftProp: [Const: test:str], SetVarOper: [Const: var, Const: tag1], FiltOper: [Const: +, TagValuCond: [TagMatch: [Const: base, VarValue: [Const: var]], Const: @=, Const: 2014]]]',
'Query: [LiftProp: [Const: test:str], SetVarOper: [Const: var, Const: tag1], PivotToTags: [TagMatch: [Const: base, VarValue: [Const: var]]], isjoin=False]',
Expand Down Expand Up @@ -1014,8 +1014,8 @@
'Query: [IfStmt: [IfClause: [VarValue: [Const: foo], SubQuery: [Query: [EditTagAdd: [TagName: [Const: woot]]]]], IfClause: [DollarExpr: [ExprNode: [Const: 1, Const: -, Const: 1]], SubQuery: [Query: [EditTagAdd: [TagName: [Const: nowoot]]]]]]]',
'Query: [IfStmt: [IfClause: [VarValue: [Const: foo], SubQuery: [Query: [EditTagAdd: [TagName: [Const: woot]]]]], IfClause: [DollarExpr: [ExprNode: [Const: 1, Const: -, Const: 1]], SubQuery: [Query: [EditTagAdd: [TagName: [Const: nowoot]]]]], SubQuery: [Query: [EditTagAdd: [TagName: [Const: nonowoot]]]]]]',
'Query: [IfStmt: [IfClause: [DollarExpr: [ExprNode: [VarValue: [Const: data], Const: ~=, Const: hehe]], SubQuery: [Query: [VarEvalOper: [FuncCall: [VarDeref: [VarValue: [Const: lib], Const: print], CallArgs: [Const: yes], CallKwargs: []]]]]], SubQuery: [Query: [VarEvalOper: [FuncCall: [VarDeref: [VarValue: [Const: lib], Const: print], CallArgs: [Const: no], CallKwargs: []]]]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [ExprNode: [Const: 1, Const: or, ExprNode: [Const: 0, Const: and, Const: 0]]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [ExprNode: [UnaryExprNode: [Const: not, Const: 1], Const: and, Const: 1]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [ExprOrNode: [Const: 1, Const: or, ExprAndNode: [Const: 0, Const: and, Const: 0]]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [ExprAndNode: [UnaryExprNode: [Const: not, Const: 1], Const: and, Const: 1]]]]',
'Query: [SetVarOper: [Const: foo, DollarExpr: [UnaryExprNode: [Const: not, ExprNode: [Const: 1, Const: >, Const: 1]]]]]',
'Query: [LiftTagProp: [TagProp: [Const: baz.faz, Const: lol]]]',
'Query: [LiftFormTagProp: [FormTagProp: [Const: foo:bar, Const: baz.faz, Const: lol]]]',
Expand Down
3 changes: 3 additions & 0 deletions synapse/tests/test_lib_storm.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ async def test_lib_storm_basics(self):

self.len(1, await core.nodes('inet:ipv4=1.2.3.4', opts={'view': view1}))

self.len(0, await core.nodes('$x = $lib.null if ($x and $x > 20) { [ ps:contact=* ] }'))
self.len(1, await core.nodes('$x = $lib.null if ($lib.true or $x > 20) { [ ps:contact=* ] }'))

async def test_storm_tree(self):

async with self.getTestCore() as core:
Expand Down

0 comments on commit 23b8a9b

Please sign in to comment.