diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-10-19 18:16:15 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-10-19 18:16:15 -0700 |
| commit | ca5896b7bd8579e42a7fc9601e00512518051432 (patch) | |
| tree | af356f2a5f2ed22b580f4b19f5fce5ee15a66ef2 | |
| parent | c7994cf72043936db1a5023c8475b0b64e0aaa21 (diff) | |
Finally fixed the bullshit way that string literals were converted to lua.
| -rw-r--r-- | lib/metaprogramming.nom | 29 | ||||
| -rwxr-xr-x | nomsu.moon | 22 |
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)); @@ -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") |
