aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/metaprogramming.nom29
-rwxr-xr-xnomsu.moon22
2 files changed, 35 insertions, 16 deletions
diff --git a/lib/metaprogramming.nom b/lib/metaprogramming.nom
index d167fef..4feae46 100644
--- a/lib/metaprogramming.nom
+++ b/lib/metaprogramming.nom
@@ -16,13 +16,22 @@ lua> ".."
rule [escaped parse %shorthand as %longhand] =:
lua> ".."
|local aliases = nomsu:get_stubs(nomsu:typecheck(vars, "shorthand", "List").value);
- |if #vars.longhand.value ~= 1 then;
- | nomsu:error("Expected only 1 line to parse to, but got "..tostring(#vars.longhand.value));
- |end;
- |local template = nomsu:typecheck(vars, "longhand", "Thunk").value[1];
+ |local template = nomsu:typecheck(vars, "longhand", "Thunk").value;
|local function parsing_as(nomsu, vars)
- | local replacement = nomsu:replaced_vars(template, vars);
- | return nomsu:tree_to_lua(replacement);
+ # Single expression/statement
+ | if #template == 1 then;
+ | local replacement = nomsu:replaced_vars(template[1], vars);
+ | return nomsu:tree_to_lua(replacement);
+ | end
+ # Multiple statements
+ | local lua_bits = {};
+ | for _,bit in ipairs(template) do;
+ | bit = nomsu:replaced_vars(bit, vars);
+ | local expr, statement = nomsu:tree_to_lua(bit);
+ | if statement then; table.insert(lua_bits, statement); end;
+ | if expr then; table.insert(lua_bits, "ret = "..expr..";"); end;
+ | end;
+ | return nil, table.concat(lua_bits, "\\n");
|end;
|nomsu:defmacro(aliases, parsing_as, template.src);
escaped parse \[parse %shorthand as %longhand] as \: escaped parse \%shorthand as \%longhand
@@ -56,10 +65,10 @@ compile [repr %obj] to:
compile [type %obj, type of %obj] to:
"type(\(%obj as lua))"
-parse [lua do> %block] as: lua> ".."
- |do;
- | \(%block)
- |end;
+parse [lua do> %block] as:
+ lua> "do;"
+ lua> %block
+ lua> "end;"
rule [%tree as lua statement] =:
lua do> ".."
|local _,statement = nomsu:tree_to_lua(\(%tree));
diff --git a/nomsu.moon b/nomsu.moon
index 0642392..45099d0 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -600,18 +600,28 @@ end)]])\format(concat(lua_bits, "\n"))
initialize_core: =>
-- Sets up some core functionality
+ nomsu_string_as_lua = (code)=>
+ concat_parts = {}
+ for bit in *code.value
+ if type(bit) == "string"
+ insert concat_parts, bit
+ else
+ expr, statement = @tree_to_lua bit
+ if statement
+ @error "Cannot use [[#{bit.src}]] as a string interpolation value, since it's not an expression."
+ insert concat_parts, expr
+ return concat(concat_parts)
+
-- Uses named local functions to help out callstack readability
lua_code = (vars)=>
- inner_vars = setmetatable({}, {__index:(_,key)-> "vars[#{repr(key)}]"})
- lua = @tree_to_value(vars.code, inner_vars)
+ lua = nomsu_string_as_lua(@, vars.code)
return nil, lua
- @defmacro "lua > %code", lua_code
+ @defmacro "lua> %code", lua_code
lua_value = (vars)=>
- inner_vars = setmetatable({}, {__index:(_,key)-> "vars[#{repr(key)}]"})
- lua = @tree_to_value(vars.code, inner_vars)
+ lua = nomsu_string_as_lua(@, vars.code)
return lua, nil
- @defmacro "= lua %code", lua_value
+ @defmacro "=lua %code", lua_value
run_file = (vars)=>
if vars.filename\match(".*%.lua")