Skip to content

Commit 6846ee5

Browse files
committed
SB05-065: Change list comprehension syntax
From [x | x <- collection, x % 2 == 0] to [x for x in collection if x % 2 == 0]
1 parent 0550d64 commit 6846ee5

File tree

8 files changed

+32
-25
lines changed

8 files changed

+32
-25
lines changed

lkql/extensions/src/lkql-evaluation.adb

+6-6
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ package body LKQL.Evaluation is
8080
function Eval_Unwrap (Ctx : Eval_Context; Node : L.Unwrap) return Primitive;
8181

8282
function Make_Comprehension_Environment_Iter
83-
(Ctx : Eval_Context; Node : L.Arrow_Assoc_List)
83+
(Ctx : Eval_Context; Node : L.List_Comp_Assoc_List)
8484
return Comprehension_Env_Iter;
8585
-- Given a List of Arrow_Assoc, return an iterator that yields the
8686
-- environments produced by this list of Arrow_Assoc in the context of a
@@ -607,7 +607,7 @@ package body LKQL.Evaluation is
607607

608608
function Environment_Iter_For_Assoc
609609
(Ctx : Eval_Context;
610-
Assoc : L.Arrow_Assoc;
610+
Assoc : L.List_Comp_Assoc;
611611
Nested : Comprehension_Env_Iter_Access)
612612
return Comprehension_Env_Iter_Access;
613613

@@ -677,16 +677,16 @@ package body LKQL.Evaluation is
677677
-----------------------------------------
678678

679679
function Make_Comprehension_Environment_Iter
680-
(Ctx : Eval_Context; Node : L.Arrow_Assoc_List)
680+
(Ctx : Eval_Context; Node : L.List_Comp_Assoc_List)
681681
return Comprehension_Env_Iter
682682
is
683683
Current_Env : Comprehension_Env_Iter_Access := null;
684684
Res : Comprehension_Env_Iter;
685685
begin
686686
for I in reverse Node.Children'Range loop
687687
declare
688-
Current_Assoc : constant L.Arrow_Assoc :=
689-
Node.Children (I).As_Arrow_Assoc;
688+
Current_Assoc : constant L.List_Comp_Assoc :=
689+
Node.Children (I).As_List_Comp_Assoc;
690690
begin
691691
Current_Env :=
692692
Environment_Iter_For_Assoc (Ctx, Current_Assoc, Current_Env);
@@ -705,7 +705,7 @@ package body LKQL.Evaluation is
705705

706706
function Environment_Iter_For_Assoc
707707
(Ctx : Eval_Context;
708-
Assoc : L.Arrow_Assoc;
708+
Assoc : L.List_Comp_Assoc;
709709
Nested : Comprehension_Env_Iter_Access)
710710
return Comprehension_Env_Iter_Access
711711
is

lkql/language/lexer.py

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Token(LexerToken):
1717
Selector = WithSymbol()
1818
Match = WithSymbol()
1919
Rec = WithSymbol()
20+
For = WithSymbol()
2021
Skip = WithSymbol()
2122
Is = WithSymbol()
2223
In = WithSymbol()
@@ -108,6 +109,7 @@ class Token(LexerToken):
108109
(Literal("selector"), Token.Selector),
109110
(Literal("match"), Token.Match),
110111
(Literal("rec"), Token.Rec),
112+
(Literal("for"), Token.For),
111113
(Literal("skip"), Token.Skip),
112114
(Literal("is"), Token.Is),
113115
(Literal("in"), Token.In),

lkql/language/parser.py

+14-10
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ class Query(Expr):
491491
pattern = Field(type=BasePattern)
492492

493493

494-
class ArrowAssoc(LKQLNode):
494+
class ListCompAssoc(LKQLNode):
495495
"""
496496
Arrow association of the form: id <- expr.
497497
This construction is meant to be used a part of a list comprehension
@@ -508,7 +508,7 @@ class ListComprehension(Expr):
508508
"""
509509

510510
expr = Field(type=Expr)
511-
generators = Field(type=ArrowAssoc.list)
511+
generators = Field(type=ListCompAssoc.list)
512512
guard = Field(type=Expr)
513513

514514

@@ -517,9 +517,11 @@ class BlockExpr(Expr):
517517
Expression of the form: val id = value; expr
518518
519519
For instance::
520+
{
520521
val x = 40;
521522
val y = 2;
522523
x + y
524+
}
523525
"""
524526

525527
vals = Field(type=ValDecl.list)
@@ -1028,13 +1030,14 @@ def patterns():
10281030
Opt("(", c(), List(G.named_arg, sep=",", empty_valid=False), ")")
10291031
),
10301032

1031-
arrow_assoc=ArrowAssoc(G.identifier, "<-", G.expr),
1032-
1033-
listcomp=ListComprehension("[", G.expr, "|",
1034-
List(G.arrow_assoc,
1035-
sep=",", empty_valid=False),
1036-
Opt(",", G.expr),
1037-
"]"),
1033+
listcomp=ListComprehension(
1034+
"[",
1035+
G.expr, "for",
1036+
List(ListCompAssoc(G.identifier, "in", G.expr),
1037+
sep=",", empty_valid=False),
1038+
Opt("if", G.expr),
1039+
"]"
1040+
),
10381041

10391042
decl=Or(G.fun_decl,
10401043
G.selector_decl,
@@ -1103,7 +1106,8 @@ def patterns():
11031106
),
11041107

11051108
block_expr=BlockExpr(
1106-
"{", c(), List(G.val_decl, sep=";", empty_valid=False), ";", G.expr, "}"
1109+
"{", c(), List(G.val_decl, sep=";", empty_valid=False), ";", G.expr,
1110+
"}"
11071111
),
11081112

11091113
val_decl=ValDecl("val", c(), G.identifier, "=", G.expr),

lkql_checker/share/lkql/too_many_dependencies.lkql

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ fun result(maxDeps) =
33
CompilationUnit(any c @ children is Identifier(any parent(depth=2) is WithClause))
44
.body = l @ LibraryItem when {
55
val semanticParent = l.item?.semantic_parent();
6-
val deps = [x | x <- c, x.referenced_decl() != semanticParent];
6+
val deps = [x for x in c if x.referenced_decl() != semanticParent];
77
deps.length > maxDeps
88
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
val subtype = select SubtypeIndication
1+
val subtypes = select SubtypeIndication
22
val objects = select ObjectDecl
3-
print([ o.image & " " & st.image | o <- objects, st <- subtype,
4-
(o.image & " " & st.image).length != 64])
3+
print([ o.image & " " & st.image
4+
for o in objects, st in subtypes
5+
if (o.image & " " & st.image).length != 64])
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
val all = select p @ _
2-
val decls = [a | a <- all, a is ObjectDecl]
2+
val decls = [a for a in all if a is ObjectDecl]
33
print(decls)
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[x + y| x <- valuesList, y <- valuesList2, x.property == "Foo"]
1+
[x + y for x in valuesList, y in valuesList2 if x.property == "Foo"]

testsuite/tests/parser/list_comprehension/test.out

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ ListComprehension
88
| |right:
99
| | Identifier: y
1010
|generators:
11-
| ArrowAssocList
12-
| | ArrowAssoc
11+
| ListCompAssocList
12+
| | ListCompAssoc
1313
| | |binding_name:
1414
| | | Identifier: x
1515
| | |coll_expr:
1616
| | | Identifier: valuesList
17-
| | ArrowAssoc
17+
| | ListCompAssoc
1818
| | |binding_name:
1919
| | | Identifier: y
2020
| | |coll_expr:

0 commit comments

Comments
 (0)