on master help sign in
for-range
commit12a6c63aa9ca6781bf8c32ac47766a6e45dcc39c authoriko <mail@iko.soy> dateSat 20 Jul 2024, 13:59 (2 years ago) parentc30a4fd
5 files changed+65−3
zod/luau/TODO.md+2−1 zod/luau/lib/lua/parser.hoon+36−2 zod/luau/lib/lua/printer.hoon+19 zod/luau/sur/lua.hoon+2 zod/luau/tests/foo.lua+6
zod/luau/TODO.md+2−1
@@ -1,6 +1,7 @@
11# Parser
22
3−- [ ] functioncall
3+- [x] functioncall
44- [ ] any string apart from the basic string with no escapes
55- [ ] check if int fits in int or make it a float
66- [ ] strip out comments before parsing
7+- [ ] should `print-blok` have `do`-`end`? This is fucked.
zod/luau/lib/lua/parser.hoon+36−2
@@ -298,10 +298,44 @@ apex
298298 %+ cook
299299 |= =functioncall [%func-call functioncall]
300300 parse-functioncall
301+ ::
302+ %+ cook
303+ |= =for-range [%for-range for-range]
304+ parse-for-range
305+ ==
306+++ parse-for-range
307+ %^ tnee %for-range for-range
308+ %+ cook
309+ |= [* * var=name * * * from=expr * * * to=expr by=(unit expr) * * * body=blok * *]
310+ [var from to by body]
311+ ;~ plug
312+ (jest 'for')
313+ wss
314+ parse-name
315+ ws
316+ (jest '=')
317+ ws
318+ parse-expr
319+ ws
320+ (jest ',')
321+ ws
322+ parse-expr
323+ %- punt
324+ %+ cook |=([* * * =expr] expr)
325+ ;~ plug
326+ ws
327+ (jest ',')
328+ wss
329+ parse-expr
330+ ==
331+ wss
332+ (jest 'do')
333+ wss
334+ parse-blok
335+ wss
336+ (jest 'end')
301337 ==
302338++ parse-if
303− %+ knee *if
304− |.
305339 %^ tnee %parse-if if
306340 %+ cook
307341 |= [* cond=expr * body=blok elsa=(list [cond=expr body=blok]) else=(unit blok) *]
zod/luau/lib/lua/printer.hoon+19
@@ -205,6 +205,25 @@ print
205205 (print-expr cond.stat)
206206 ==
207207 %if (print-if +.stat)
208+ %for-range (print-for-range +.stat)
209+ ==
210+:: for-range
211+::
212+++ print-for-range
213+ |= =for-range
214+ ^- tape
215+ %- zing
216+ :~
217+ "for "
218+ (trip var.for-range)
219+ " = "
220+ (print-expr from.for-range)
221+ ", "
222+ (print-expr to.for-range)
223+ ?~ by.for-range "" (zing ~[", " (print-expr u.by.for-range)])
224+ "\0ado\0a"
225+ (print-blok body.for-range)
226+ "\0aend"
208227 ==
209228:: if
210229::
zod/luau/sur/lua.hoon+2
@@ -77,8 +77,10 @@
7777 [%repeat body=blok cond=expr]
7878 [%if if]
7979 [%break ~]
80+ [%for-range for-range]
8081 [%empty ~]
8182 ==
83++$ for-range [var=name from=expr to=expr by=(unit expr) body=blok]
8284+$ if [cond=expr body=blok elsa=(list [cond=expr body=blok]) else=(unit blok)]
8385+$ functioncall
8486 $%
zod/luau/tests/foo.lua+6
@@ -76,3 +76,9 @@ if l == 3 then a = a + 1 ; assert(getlocal(2, 1) == "(temporary)")
7676elseif l == 4 then a = a + 1; assert(getlocal(2, 1) == "A")
7777else ;
7878end
79+
80+for i = 1, 100 do
81+ karlSum = karlSum + i
82+end
83+
84+for j = 100, 1, 1 do fredSum = fredSum + j end