File tree Expand file tree Collapse file tree 6 files changed +92
-5
lines changed
Expand file tree Collapse file tree 6 files changed +92
-5
lines changed Original file line number Diff line number Diff line change @@ -191,6 +191,15 @@ local function format_assign(lhs_exps, assign)
191191 error " unknown assign expression"
192192end
193193
194+ -- the if statement only takes a single lhs, so we wrap in table to git to
195+ -- "assign" tuple format
196+ local function format_assign_for_if (lhs , assign )
197+ if assign then
198+ return format_assign ({lhs }, assign )
199+ end
200+ return lhs
201+ end
202+
194203local function sym (chars )
195204 return Space * chars
196205end
@@ -389,7 +398,7 @@ local build_grammar = wrap_env(function()
389398 SwitchCase = key " when" * Exp * key " then" ^- 1 * Body / mark " case" ,
390399 SwitchElse = key " else" * Body / mark " else" ,
391400
392- If = key " if" * Exp * key " then" ^- 1 * Body *
401+ If = key " if" * ( Exp * Assign ^- 1 / format_assign_for_if ) * key " then" ^- 1 * Body *
393402 ((Break * CheckIndent )^- 1 * EmptyLine ^ 0 * key " elseif" * Exp * key " then" ^- 1 * Body / mark " elseif" )^ 0 *
394403 ((Break * CheckIndent )^- 1 * EmptyLine ^ 0 * key " else" * Body / mark " else" )^- 1 / mark " if" ,
395404
Original file line number Diff line number Diff line change @@ -519,9 +519,27 @@ Statement = Transformer({
519519 end
520520 return construct_comprehension (action (exp ), clauses )
521521 end ,
522+ [" do" ] = function (self , node , ret )
523+ if ret then
524+ node [2 ] = apply_to_last (node [2 ], ret )
525+ end
526+ return node
527+ end ,
522528 [" if" ] = function (self , node , ret )
529+ smart_node (node )
530+ if ntype (node .cond ) == " assign" then
531+ local _ , assign , body = unpack (node )
532+ local name = assign [2 ][1 ]
533+ return build [" do" ]({
534+ assign ,
535+ {
536+ " if" ,
537+ name ,
538+ unpack (node , 3 )
539+ }
540+ })
541+ end
523542 if ret then
524- smart_node (node )
525543 node [' then' ] = apply_to_last (node [' then' ], ret )
526544 for i = 4 , # node do
527545 local case = node [i ]
Original file line number Diff line number Diff line change @@ -226,16 +226,31 @@ Statement = Transformer {
226226 action = action or ( exp) -> { exp}
227227 construct_comprehension action( exp) , clauses
228228
229- -- handle cascading return decorator
229+ do : ( node, ret) =>
230+ node[ 2 ] = apply_to_last node[ 2 ] , ret if ret
231+ node
232+
230233 if : ( node, ret) =>
234+ smart_node node
235+
236+ -- extract assigns in cond
237+ if ntype( node. cond) == " assign"
238+ _, assign, body = unpack node
239+ name = assign[ 2 ][ 1 ]
240+ return build[ " do" ] {
241+ assign
242+ { " if" , name, unpack node, 3 }
243+ }
244+
245+ -- handle cascading return decorator
231246 if ret
232- smart_node node
233247 -- mutate all the bodies
234248 node[ ' then' ] = apply_to_last node[ ' then' ] , ret
235249 for i = 4 , # node
236250 case = node[ i]
237251 body_idx = # node[ i]
238252 case[ body_idx] = apply_to_last case[ body_idx] , ret
253+
239254 node
240255
241256 with : ( node, ret) =>
Original file line number Diff line number Diff line change @@ -50,4 +50,19 @@ if lets go
5050elseif " just us"
5151 print " will smith" else show 5555555
5252
53+ --
54+
55+ if something = 10
56+ print something
57+ else
58+ print " else"
59+
60+ hello = if something = 10
61+ print something
62+ else
63+ print " else"
64+
65+
66+ hello = 5 + if something = 10
67+ print something
5368
Original file line number Diff line number Diff line change @@ -84,4 +84,29 @@ elseif "just us" then
8484 print (" will smith" )
8585else
8686 show (5555555 )
87- end
87+ end
88+ do
89+ local something = 10
90+ if something then
91+ print (something )
92+ else
93+ print (" else" )
94+ end
95+ end
96+ local hello
97+ do
98+ local something = 10
99+ if something then
100+ hello = print (something )
101+ else
102+ hello = print (" else" )
103+ end
104+ end
105+ hello = 5 + (function ()
106+ do
107+ local something = 10
108+ if something then
109+ return print (something )
110+ end
111+ end
112+ end )()
Original file line number Diff line number Diff line change @@ -50,3 +50,8 @@ not working right:
5050
5151* let array items in table be defined without {} when indented
5252
53+
54+ * allow dot access with names that are keywords: hello.then
55+
56+ * convert tree transformer to be depth first instead of breadth first (lazy)
57+
You can’t perform that action at this time.
0 commit comments