Mercurial > repos > icfp2014
comparison code/lmc.tp @ 39:0e1fc2b2832f
Add support for import:from to lmc
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 26 Jul 2014 15:25:41 -0700 |
parents | 6b9b21456cf4 |
children |
comparison
equal
deleted
inserted
replaced
38:6b9b21456cf4 | 39:0e1fc2b2832f |
---|---|
308 res <- parser top: code | 308 res <- parser top: code |
309 if: res { | 309 if: res { |
310 outer <- res yield | 310 outer <- res yield |
311 functions <- dict hash | 311 functions <- dict hash |
312 | 312 |
313 num <- (outer messages) length | 313 num <- 0 |
314 syms <- symbols table | 314 syms <- symbols table |
315 | 315 |
316 prog add: (inst: "DUM" #[num]) | 316 dumaddr <- prog pc |
317 prog add: (inst: "DUM" #[0]) | |
318 | |
317 slot <- 0 | 319 slot <- 0 |
318 mainArgs <- 0 | 320 mainArgs <- 0 |
319 foreach: (outer messages) :idx msg { | 321 messageGroups <- [(outer messages)] |
320 if: (msg nodeType) = (ast assignment) { | 322 while: { not: (messageGroups empty?) } do: { |
321 def <- msg assign | 323 curMessages <- messageGroups value |
322 sym <- (msg to) name | 324 messageGroups <- messageGroups tail |
323 | 325 foreach: curMessages :idx msg { |
324 if: (def nodeType) = (ast lambda) { | 326 if: (msg nodeType) = (ast assignment) { |
325 prog add: (inst: "LDF" #[sym]) | 327 num <- num + 1 |
326 functions set: sym def | 328 def <- msg assign |
327 if: sym = "main" { | 329 sym <- (msg to) name |
328 mainArgs <- (def args) length | 330 |
331 if: (def nodeType) = (ast lambda) { | |
332 prog add: (inst: "LDF" #[sym]) | |
333 functions set: sym def | |
334 if: sym = "main" { | |
335 mainArgs <- (def args) length | |
336 } | |
337 } else: { | |
338 compileExpr: def syms: syms | |
329 } | 339 } |
340 syms define: sym slot | |
341 slot <- slot + 1 | |
330 } else: { | 342 } else: { |
331 compileExpr: def syms: syms | 343 if: (msg nodeType) = (ast call) && ((msg tocall) nodeType) = (ast sym) && ( |
344 ((msg tocall) name) = "import:from" | |
345 ) { | |
346 importSyms <- (((msg args) value) els) fold: (dict hash) with: :acc sym { | |
347 acc set: (sym name) true | |
348 } | |
349 moduleName <- ((((msg args) tail) value) args) value | |
350 moduleFile <- if: (moduleName nodeType) = (ast sym) { | |
351 (moduleName name) . ".lm" | |
352 } else: { | |
353 if: ((moduleName val) endsWith?: ".lm") { | |
354 moduleName val | |
355 } else: { | |
356 (moduleName val) . ".lm" | |
357 } | |
358 } | |
359 f <- file open: moduleFile | |
360 moduleRes <- parser top: (f readAll) | |
361 if: moduleRes { | |
362 newGroup <- [] | |
363 foreach: ((moduleRes yield) messages) :idx msg { | |
364 if: (msg nodeType) = (ast assignment) { | |
365 importSyms ifget: ((msg to) name) :jnk { | |
366 newGroup <- msg | newGroup | |
367 } else: {} | |
368 } | |
369 } | |
370 messageGroups <- newGroup | messageGroups | |
371 } else: { | |
372 error: "Failed to parse module " . moduleFile . "!\n" | |
373 } | |
374 } else: { | |
375 error: "Only assignments and import:from are allowed at the top level" | |
376 } | |
332 } | 377 } |
333 syms define: sym slot | |
334 slot <- slot + 1 | |
335 } else: { | |
336 error: "Only assignments are allowed at the top level" | |
337 } | 378 } |
338 } | 379 } |
380 (((prog instructions) get: dumaddr) args) set: 0 num | |
339 after_env <- prog makeLabel: "after_env" | 381 after_env <- prog makeLabel: "after_env" |
340 prog add: (inst: "LDF" #[after_env]) | 382 prog add: (inst: "LDF" #[after_env]) |
341 prog add: (inst: "TRAP" #[num]) | 383 prog add: (inst: "TRAP" #[num]) |
342 prog setLabel: after_env | 384 prog setLabel: after_env |
343 | 385 |