Skip to content

Commit 648aa45

Browse files
committed
Fixed up the path expressions
1 parent 322dd59 commit 648aa45

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

examples/pe.pql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
x = [[1,2], [3,4], [5,6]]
3+
y = {"a": [1,2,3], "b": [2,3,4]}
4+
5+
print("x./", list(x./))
6+
print("x./ ./", list(x ./ ./))
7+
print("x.//", list(x.//))
8+
9+
print("y./", list(y./))
10+
print("y./ ./", list(y ./ ./))
11+
print("y.//", list(y.//))
12+
13+
print("x{ 1 in item}", list(x{1 in item}))
14+
print('y{ key == "b"}', dict(y{key=="b"}))

src/pythonql/Executor.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,49 @@ def PQChildPath (coll):
2525

2626
# Implement a descendents path on some collection or map
2727
def PQDescPath(coll):
28-
stack = [coll]
28+
stack = []
29+
if isList(coll):
30+
stack = [i for i in coll]
31+
elif isMap(coll):
32+
stack = list(coll.values())
2933
while stack:
3034
i = stack.pop()
35+
yield i
3136
if isList(i):
32-
[stack.push(j) for j in i[1:]]
37+
[stack.append(j) for j in i[1:]]
38+
if isList(i[0]):
39+
stack.extend([ci for ci in i[0]])
40+
elif isMap(i[0]):
41+
stack.extend(i[0].values())
3342
yield i[0]
34-
if isMap(i):
43+
elif isMap(i):
3544
keys = list(i.keys())
36-
[stack.push(i[j]) for j in keys[1:]]
45+
[stack.append(i[j]) for j in keys[1:]]
3746
yield i[keys[0]]
3847

48+
#Implements a predicate step on a collection
49+
def PQPred(coll, pred):
50+
if isList(coll):
51+
return PQPred_list(coll,pred)
52+
elif isMap(coll):
53+
return PQPred_map(coll,pred)
54+
55+
def PQPred_list(coll,pred):
56+
lcs = locals()
57+
for i in coll:
58+
lcs.update({'item':i})
59+
if eval(pred,globals(),lcs):
60+
yield i
61+
62+
def PQPred_map(coll,pred):
63+
lcs = locals()
64+
result = {}
65+
for (k,v) in coll.items():
66+
lcs.update({'key':k, 'value':v})
67+
if eval(pred,globals(),lcs):
68+
result[k] = v
69+
return result
70+
3971
# create a table with an empty tuple
4072
def emptyTuple(schema):
4173
return PQTuple([None] * len(schema), schema)

src/pythonql/parser/Preprocessor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ def get_path_expression_terminals(tree,parser):
120120

121121
for c in children[1:]:
122122
if isChildStep(c,parser):
123-
result = mk_tok([ "child_path", "(", result, ")" ])
123+
result = mk_tok([ "PQChildPath", "(", result, ")" ])
124124
elif isDescStep(c,parser):
125-
result = mk_tok([ "desc_path", "(", result, ")"])
125+
result = mk_tok([ "PQDescPath", "(", result, ")"])
126126
elif isPredStep(c,parser):
127127
condition = mk_tok([ '"""', get_all_terminals(c,parser)[1:-1], '"""'])
128-
result = mk_tok([ "pred_path", "(", result, ",", condition, ")"])
128+
result = mk_tok([ "PQPred", "(", result, ",", condition, ")"])
129129

130130
return result
131131

0 commit comments

Comments
 (0)