Switched compiler to do lua> instead of lua files.
This commit is contained in:
parent
49adc12952
commit
28f5c31299
30
nomsu.lua
30
nomsu.lua
@ -579,13 +579,16 @@ do
|
|||||||
end
|
end
|
||||||
return tree
|
return tree
|
||||||
end,
|
end,
|
||||||
run = function(self, src, filename, vars, max_operations)
|
run = function(self, src, filename, vars, max_operations, output_file)
|
||||||
if vars == nil then
|
if vars == nil then
|
||||||
vars = { }
|
vars = { }
|
||||||
end
|
end
|
||||||
if max_operations == nil then
|
if max_operations == nil then
|
||||||
max_operations = nil
|
max_operations = nil
|
||||||
end
|
end
|
||||||
|
if output_file == nil then
|
||||||
|
output_file = nil
|
||||||
|
end
|
||||||
if src == "" then
|
if src == "" then
|
||||||
return nil, "", vars
|
return nil, "", vars
|
||||||
end
|
end
|
||||||
@ -619,6 +622,14 @@ do
|
|||||||
%s
|
%s
|
||||||
return %s;
|
return %s;
|
||||||
end);]]):format(statements or "", expr or "ret")
|
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
|
if self.debug then
|
||||||
self:writeln(tostring(colored.bright("RUNNING LUA:")) .. "\n" .. tostring(colored.blue(colored.bright(code_for_statement))))
|
self:writeln(tostring(colored.bright("RUNNING LUA:")) .. "\n" .. tostring(colored.blue(colored.bright(code_for_statement))))
|
||||||
end
|
end
|
||||||
@ -1320,14 +1331,9 @@ end)]]):format(concat(lua_bits, "\n"))
|
|||||||
end
|
end
|
||||||
if vars.filename:match(".*%.nom") then
|
if vars.filename:match(".*%.nom") then
|
||||||
if not self.skip_precompiled then
|
if not self.skip_precompiled then
|
||||||
local file = io.open(vars.filename .. ".lua", "r")
|
local file = io.open(vars.filename:gsub("%.nom", ".compiled.nom"), "r")
|
||||||
if file then
|
|
||||||
local contents = file:read('*a')
|
|
||||||
file:close()
|
|
||||||
return load(contents)()(self, vars)
|
|
||||||
end
|
end
|
||||||
end
|
local file = file or io.open(vars.filename)
|
||||||
local file = io.open(vars.filename)
|
|
||||||
if not file then
|
if not file then
|
||||||
self:error("File does not exist: " .. tostring(vars.filename))
|
self:error("File does not exist: " .. tostring(vars.filename))
|
||||||
end
|
end
|
||||||
@ -1449,7 +1455,7 @@ if arg then
|
|||||||
c.skip_precompiled = not args.flags["-O"]
|
c.skip_precompiled = not args.flags["-O"]
|
||||||
if args.input then
|
if args.input then
|
||||||
if args.flags["-c"] and not args.output then
|
if args.flags["-c"] and not args.output then
|
||||||
args.output = args.input .. ".lua"
|
args.output = args.input:gsub("%.nom", ".compiled.nom")
|
||||||
end
|
end
|
||||||
local compiled_output = nil
|
local compiled_output = nil
|
||||||
if args.flags["-p"] then
|
if args.flags["-p"] then
|
||||||
@ -1468,10 +1474,8 @@ if arg then
|
|||||||
else
|
else
|
||||||
input = io.open(args.input):read("*a")
|
input = io.open(args.input):read("*a")
|
||||||
end
|
end
|
||||||
local retval, code = c:run(input, args.input)
|
local vars = { }
|
||||||
if compiled_output then
|
local retval, code = c:run(input, args.input, vars, nil, compiled_output)
|
||||||
compiled_output:write(code)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
if args.flags["-p"] then
|
if args.flags["-p"] then
|
||||||
c.write = _write
|
c.write = _write
|
||||||
|
24
nomsu.moon
24
nomsu.moon
@ -37,7 +37,6 @@ if _VERSION == "Lua 5.1"
|
|||||||
-- type checking?
|
-- type checking?
|
||||||
-- Fix compiler bug that breaks when file ends with a block comment
|
-- Fix compiler bug that breaks when file ends with a block comment
|
||||||
-- Add compiler options for optimization level (compile-fast vs. run-fast, etc.)
|
-- 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
|
lpeg.setmaxstack 10000 -- whoa
|
||||||
{:P,:R,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
|
{:P,:R,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
|
||||||
@ -406,7 +405,7 @@ class NomsuCompiler
|
|||||||
@print_tree tree, " "
|
@print_tree tree, " "
|
||||||
return 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 src == "" then return nil, "", vars
|
||||||
if max_operations
|
if max_operations
|
||||||
timeout = ->
|
timeout = ->
|
||||||
@ -433,6 +432,11 @@ return (function(nomsu, vars)
|
|||||||
%s
|
%s
|
||||||
return %s;
|
return %s;
|
||||||
end);]])\format(statements or "", expr or "ret")
|
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
|
if @debug
|
||||||
@writeln "#{colored.bright "RUNNING LUA:"}\n#{colored.blue colored.bright(code_for_statement)}"
|
@writeln "#{colored.bright "RUNNING LUA:"}\n#{colored.blue colored.bright(code_for_statement)}"
|
||||||
lua_thunk, err = load(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)
|
return dofile(vars.filename)(@, vars)
|
||||||
if vars.filename\match(".*%.nom")
|
if vars.filename\match(".*%.nom")
|
||||||
if not @skip_precompiled -- Look for precompiled version
|
if not @skip_precompiled -- Look for precompiled version
|
||||||
file = io.open(vars.filename..".lua", "r")
|
file = io.open(vars.filename\gsub("%.nom", ".compiled.nom"), "r")
|
||||||
if file
|
file = file or io.open(vars.filename)
|
||||||
contents = file\read('*a')
|
|
||||||
file\close!
|
|
||||||
return load(contents)!(@, vars)
|
|
||||||
file = io.open(vars.filename)
|
|
||||||
if not file
|
if not file
|
||||||
@error "File does not exist: #{vars.filename}"
|
@error "File does not exist: #{vars.filename}"
|
||||||
contents = file\read('*a')
|
contents = file\read('*a')
|
||||||
@ -925,7 +925,7 @@ if arg
|
|||||||
if args.input
|
if args.input
|
||||||
-- Read a file or stdin and output either the printouts or the compiled lua
|
-- Read a file or stdin and output either the printouts or the compiled lua
|
||||||
if args.flags["-c"] and not args.output
|
if args.flags["-c"] and not args.output
|
||||||
args.output = args.input..".lua"
|
args.output = args.input\gsub("%.nom", ".compiled.nom")
|
||||||
compiled_output = nil
|
compiled_output = nil
|
||||||
if args.flags["-p"]
|
if args.flags["-p"]
|
||||||
_write = c.write
|
_write = c.write
|
||||||
@ -940,10 +940,8 @@ if arg
|
|||||||
input = if args.input == '-'
|
input = if args.input == '-'
|
||||||
io.read('*a')
|
io.read('*a')
|
||||||
else io.open(args.input)\read("*a")
|
else io.open(args.input)\read("*a")
|
||||||
retval, code = c\run(input, args.input)
|
vars = {}
|
||||||
-- Output compile lua code
|
retval, code = c\run(input, args.input, vars, nil, compiled_output)
|
||||||
if compiled_output
|
|
||||||
compiled_output\write code
|
|
||||||
if args.flags["-p"]
|
if args.flags["-p"]
|
||||||
c.write = _write
|
c.write = _write
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user