diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-07 18:45:27 -0800 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2018-01-07 18:45:27 -0800 |
| commit | 568a44ef191e1f4072d379700e3b6f599150a92b (patch) | |
| tree | c58e74b5f7539a0055d72d8ff40ef6f499aef9ea /nomsu.moon | |
| parent | c92e5fbc81e57ada43f2c17792e500da5b708bee (diff) | |
Reworking some stuff so that functions only allow expressions to be
return values with either an explicit "return" statement or if they're
the only line in the function, and the line is an expression.
Diffstat (limited to 'nomsu.moon')
| -rwxr-xr-x | nomsu.moon | 28 |
1 files changed, 15 insertions, 13 deletions
@@ -342,7 +342,7 @@ class NomsuCompiler @assert tree.type == "File", "Attempt to run non-file: #{tree.type}" buffer = {} - return_value = nil + -- TODO: handle return statements in a file for statement in *tree.value if @debug @writeln "#{colored.bright "RUNNING NOMSU:"}\n#{colored.bright colored.yellow statement.src}" @@ -355,8 +355,7 @@ class NomsuCompiler code_for_statement = ([[ return (function(nomsu, vars) %s -return %s; -end);]])\format(statements or "", expr or "ret") +end);]])\format(statements or ("return "..expr..";")) if output_file if statements and #statements > 0 output_file\write "lua> \"..\"\n #{@indent statements\gsub("\\","\\\\")}\n" @@ -374,7 +373,6 @@ end);]])\format(statements or "", expr or "ret") error("Failed to compile generated code:\n#{colored.bright colored.blue colored.onblack code}\n\n#{err}\n\nProduced by statement:\n#{colored.bright colored.yellow statement.src}") run_statement = lua_thunk! ok,ret = pcall(run_statement, self, vars) - if expr then return_value = ret if not ok @errorln "#{colored.red "Error occurred in statement:"}\n#{colored.yellow statement.src}" @errorln debug.traceback! @@ -382,17 +380,15 @@ end);]])\format(statements or "", expr or "ret") if statements insert buffer, statements if expr - insert buffer, "ret = #{expr};" + insert buffer, "#{expr};" if max_operations debug.sethook! lua_code = ([[ return (function(nomsu, vars) -local ret; %s -return ret; end);]])\format(concat(buffer, "\n")) - return return_value, lua_code, vars + return nil, lua_code, vars tree_to_value: (tree, vars, filename)=> code = "return (function(nomsu, vars)\nreturn #{@tree_to_lua(tree, filename)};\nend);" @@ -546,7 +542,7 @@ end);]])\format(concat(buffer, "\n")) for line in *tree.value expr,statement = @tree_to_lua line, filename if statement then insert lua_bits, statement - if expr then insert lua_bits, "ret = #{expr};" + if expr then insert lua_bits, "#{expr};" return nil, concat(lua_bits, "\n") when "Nomsu" @@ -556,13 +552,16 @@ end);]])\format(concat(buffer, "\n")) lua_bits = {} for arg in *tree.value expr,statement = @tree_to_lua arg, filename + if #tree.value == 1 and expr and not statement + return ([[ +(function(nomsu, vars) + return %s; +end)]])\format(expr) if statement then insert lua_bits, statement - if expr then insert lua_bits, "ret = #{expr};" + if expr then insert lua_bits, "#{expr};" return ([[ (function(nomsu, vars) -local ret; %s -return ret; end)]])\format(concat(lua_bits, "\n")) when "FunctionCall" @@ -889,7 +888,7 @@ if arg parser = re.compile([[ args <- {| {:flags: flags? :} ({:input: input :} ";" ("-o;"{:output: output :} ";")?)? (";")? |} !. flags <- (({| ({flag} ";")* |}) -> set) - flag <- "-c" / "-i" / "-p" / "-O" / "--help" / "-h" + flag <- "-c" / "-i" / "-p" / "-O" / "--help" / "-h" / "-v" input <- "-" / [^;]+ output <- "-" / [^;]+ ]], {:set}) @@ -901,6 +900,9 @@ if arg c = NomsuCompiler() + if args.flags["-v"] + c.debug = true + c.skip_precompiled = not args.flags["-O"] if args.input -- Read a file or stdin and output either the printouts or the compiled lua |
