diff options
author | Eduardo Julian | 2017-12-03 16:17:42 -0400 |
---|---|---|
committer | Eduardo Julian | 2017-12-03 16:17:42 -0400 |
commit | c85449b25ca5092d576d6378c17ea2bee4c4a85f (patch) | |
tree | 1376add111f23a6595ac343932179b6c578d4c18 /new-luxc/source/luxc/lang/translation.lux | |
parent | f2d1c591814a055b48dab3a72b3cddef0d1b0974 (diff) |
- Improved statement-level macro-expansion.
Diffstat (limited to 'new-luxc/source/luxc/lang/translation.lux')
-rw-r--r-- | new-luxc/source/luxc/lang/translation.lux | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/new-luxc/source/luxc/lang/translation.lux b/new-luxc/source/luxc/lang/translation.lux index 71bef93a2..6cba6cc35 100644 --- a/new-luxc/source/luxc/lang/translation.lux +++ b/new-luxc/source/luxc/lang/translation.lux @@ -40,6 +40,7 @@ (exception: #export Macro-Expansion-Failed) (exception: #export Unrecognized-Statement) (exception: #export Invalid-Alias) +(exception: #export Invalid-Macro) (def: (process-annotations annsC) (-> Code (Meta [$.Inst Code])) @@ -72,37 +73,6 @@ (def: #export (translate translate-module aliases code) (-> (-> Text Compiler (Process Compiler)) Aliases Code (Meta Aliases)) (case code - (^code ((~ [_ (#.Symbol macro-name)]) (~+ args))) - (do macro.Monad<Meta> - [?macro (&.with-error-tracking - (macro.find-macro macro-name))] - (case ?macro - (#.Some macro) - (do @ - [expansion (: (Meta (List Code)) - (function [compiler] - (case (macroL.expand macro args compiler) - (#e.Error error) - ((&.throw Macro-Expansion-Failed error) compiler) - - output - output))) - expansion-aliases (monad.map @ (translate translate-module aliases) expansion)] - (if (dict.empty? aliases) - (loop [expansion-aliases expansion-aliases] - (case expansion-aliases - #.Nil - (wrap aliases) - - (#.Cons head tail) - (if (dict.empty? head) - (recur tail) - (wrap head)))) - (wrap aliases))) - - #.None - (&.throw Unrecognized-Statement (%code code)))) - (^code ("lux def" (~ [_ (#.Symbol ["" def-name])]) (~ valueC) (~ annsC))) (hostL.with-context def-name (&.with-fresh-type-env @@ -159,6 +129,39 @@ _ (statementT.translate-program program-args programI)] (wrap aliases)) + (^code ((~ macroC) (~+ argsC+))) + (do macro.Monad<Meta> + [[_ macroA] (&.with-scope + (&.with-type Macro + (analyse macroC))) + [_macroT _macroM _macroV] (case macroA + [_ (#.Symbol macro-name)] + (&.with-error-tracking + (macro.find-def macro-name)) + + _ + (&.throw Invalid-Macro (%code code))) + expansion (: (Meta (List Code)) + (function [compiler] + (case (macroL.expand (:! Macro _macroV) argsC+ compiler) + (#e.Error error) + ((&.throw Macro-Expansion-Failed error) compiler) + + output + output))) + expansion-aliases (monad.map @ (translate translate-module aliases) expansion)] + (wrap (if (dict.empty? aliases) + (loop [expansion-aliases expansion-aliases] + (case expansion-aliases + #.Nil + aliases + + (#.Cons head tail) + (if (dict.empty? head) + (recur tail) + head))) + aliases))) + _ (&.throw Unrecognized-Statement (%code code)))) |