aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bitbucket@bruce-hill.com>2017-12-14 16:41:22 -0800
committerBruce Hill <bitbucket@bruce-hill.com>2017-12-14 16:41:22 -0800
commit28f5c31299d7f7bd813e3984767d27b4db011395 (patch)
treeccc8549d824858e7375d29600586558d30b8cc2f
parent49adc12952789d3a7a8e0f02b172265a9cb9aa1b (diff)
Switched compiler to do lua> instead of lua files.
-rw-r--r--nomsu.lua30
-rwxr-xr-xnomsu.moon24
2 files changed, 28 insertions, 26 deletions
diff --git a/nomsu.lua b/nomsu.lua
index d79c8ab..12a9f5d 100644
--- a/nomsu.lua
+++ b/nomsu.lua
@@ -579,13 +579,16 @@ do
end
return tree
end,
- run = function(self, src, filename, vars, max_operations)
+ run = function(self, src, filename, vars, max_operations, output_file)
if vars == nil then
vars = { }
end
if max_operations == nil then
max_operations = nil
end
+ if output_file == nil then
+ output_file = nil
+ end
if src == "" then
return nil, "", vars
end
@@ -619,6 +622,14 @@ do
%s
return %s;
end);]]):format(statements or "", expr or "ret")
+ if output_file then
+ if statements and #statements > 0 then
+ output_file:write("lua> \"..\"\n " .. tostring(self:indent(statements:gsub("\\", "\\\\"))) .. "\n")
+ end
+ if expr and #expr > 0 then
+ output_file:write("=lua \"..\"\n " .. tostring(self:indent(expr:gsub("\\", "\\\\"))) .. "\n")
+ end
+ end
if self.debug then
self:writeln(tostring(colored.bright("RUNNING LUA:")) .. "\n" .. tostring(colored.blue(colored.bright(code_for_statement))))
end
@@ -1320,14 +1331,9 @@ end)]]):format(concat(lua_bits, "\n"))
end
if vars.filename:match(".*%.nom") then
if not self.skip_precompiled then
- local file = io.open(vars.filename .. ".lua", "r")
- if file then
- local contents = file:read('*a')
- file:close()
- return load(contents)()(self, vars)
- end
+ local file = io.open(vars.filename:gsub("%.nom", ".compiled.nom"), "r")
end
- local file = io.open(vars.filename)
+ local file = file or io.open(vars.filename)
if not file then
self:error("File does not exist: " .. tostring(vars.filename))
end
@@ -1449,7 +1455,7 @@ if arg then
c.skip_precompiled = not args.flags["-O"]
if args.input then
if args.flags["-c"] and not args.output then
- args.output = args.input .. ".lua"
+ args.output = args.input:gsub("%.nom", ".compiled.nom")
end
local compiled_output = nil
if args.flags["-p"] then
@@ -1468,10 +1474,8 @@ if arg then
else
input = io.open(args.input):read("*a")
end
- local retval, code = c:run(input, args.input)
- if compiled_output then
- compiled_output:write(code)
- end
+ local vars = { }
+ local retval, code = c:run(input, args.input, vars, nil, compiled_output)
end
if args.flags["-p"] then
c.write = _write
diff --git a/nomsu.moon b/nomsu.moon
index e9907bd..ce6f554 100755
--- a/nomsu.moon
+++ b/nomsu.moon
@@ -37,7 +37,6 @@ if _VERSION == "Lua 5.1"
-- type checking?
-- Fix compiler bug that breaks when file ends with a block comment
-- Add compiler options for optimization level (compile-fast vs. run-fast, etc.)
--- Change precompiling from producing lua code to producing lua> "code" nomsu files
lpeg.setmaxstack 10000 -- whoa
{:P,:R,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
@@ -406,7 +405,7 @@ class NomsuCompiler
@print_tree tree, " "
return tree
- run: (src, filename, vars={}, max_operations=nil)=>
+ run: (src, filename, vars={}, max_operations=nil, output_file=nil)=>
if src == "" then return nil, "", vars
if max_operations
timeout = ->
@@ -433,6 +432,11 @@ return (function(nomsu, vars)
%s
return %s;
end);]])\format(statements or "", expr or "ret")
+ if output_file
+ if statements and #statements > 0
+ output_file\write "lua> \"..\"\n #{@indent statements\gsub("\\","\\\\")}\n"
+ if expr and #expr > 0
+ output_file\write "=lua \"..\"\n #{@indent expr\gsub("\\","\\\\")}\n"
if @debug
@writeln "#{colored.bright "RUNNING LUA:"}\n#{colored.blue colored.bright(code_for_statement)}"
lua_thunk, err = load(code_for_statement)
@@ -881,12 +885,8 @@ end)]])\format(concat(lua_bits, "\n"))
return dofile(vars.filename)(@, vars)
if vars.filename\match(".*%.nom")
if not @skip_precompiled -- Look for precompiled version
- file = io.open(vars.filename..".lua", "r")
- if file
- contents = file\read('*a')
- file\close!
- return load(contents)!(@, vars)
- file = io.open(vars.filename)
+ file = io.open(vars.filename\gsub("%.nom", ".compiled.nom"), "r")
+ file = file or io.open(vars.filename)
if not file
@error "File does not exist: #{vars.filename}"
contents = file\read('*a')
@@ -925,7 +925,7 @@ if arg
if args.input
-- Read a file or stdin and output either the printouts or the compiled lua
if args.flags["-c"] and not args.output
- args.output = args.input..".lua"
+ args.output = args.input\gsub("%.nom", ".compiled.nom")
compiled_output = nil
if args.flags["-p"]
_write = c.write
@@ -940,10 +940,8 @@ if arg
input = if args.input == '-'
io.read('*a')
else io.open(args.input)\read("*a")
- retval, code = c\run(input, args.input)
- -- Output compile lua code
- if compiled_output
- compiled_output\write code
+ vars = {}
+ retval, code = c\run(input, args.input, vars, nil, compiled_output)
if args.flags["-p"]
c.write = _write