Fixed compiling to .lua files
This commit is contained in:
parent
928e78f252
commit
931ae40f40
@ -162,7 +162,7 @@ action [help %action]:
|
||||
# Compiler tools
|
||||
immediately:
|
||||
compile [run %code] to:
|
||||
Lua value "nomsu:run(\(%code as lua expr), \(=lua "tree.source"))"
|
||||
Lua value "nomsu:run(Nomsu(\(=lua "tree.source"), \(%code as lua expr)))"
|
||||
parse [enable debugging] as: lua> "nomsu.debug = true;"
|
||||
parse [disable debugging] as: lua> "nomsu.debug = false;"
|
||||
|
||||
|
54
nomsu.lua
54
nomsu.lua
@ -327,6 +327,12 @@ do
|
||||
return code:gsub("\n", "\n" .. (" "):rep(levels))
|
||||
end,
|
||||
parse = function(self, nomsu_code)
|
||||
if type(nomsu_code) == 'string' then
|
||||
_nomsu_chunk_counter = _nomsu_chunk_counter + 1
|
||||
local filename = "<nomsu chunk #" .. tostring(_nomsu_chunk_counter) .. ">.nom"
|
||||
nomsu_code = Nomsu(filename, nomsu_code)
|
||||
FILE_CACHE[filename] = nomsu_code
|
||||
end
|
||||
local userdata = {
|
||||
source_code = nomsu_code,
|
||||
indent_stack = {
|
||||
@ -340,33 +346,36 @@ do
|
||||
assert(tree, "In file " .. tostring(colored.blue(filename)) .. " failed to parse:\n" .. tostring(colored.onyellow(colored.black(nomsu_code))))
|
||||
return tree
|
||||
end,
|
||||
run = function(self, nomsu_code)
|
||||
if type(nomsu_code) == 'string' then
|
||||
_nomsu_chunk_counter = _nomsu_chunk_counter + 1
|
||||
local filename = "<nomsu chunk #" .. tostring(_nomsu_chunk_counter) .. ">.nom"
|
||||
nomsu_code = Nomsu(filename, nomsu_code)
|
||||
FILE_CACHE[filename] = nomsu_code
|
||||
run = function(self, nomsu_code, compile_fn)
|
||||
if compile_fn == nil then
|
||||
compile_fn = nil
|
||||
end
|
||||
if #nomsu_code == 0 then
|
||||
return nil
|
||||
end
|
||||
local tree = self:parse(nomsu_code, source)
|
||||
local tree = self:parse(nomsu_code)
|
||||
assert(tree, "Failed to parse: " .. tostring(nomsu_code))
|
||||
assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type))
|
||||
local lua = self:tree_to_lua(tree)
|
||||
lua:convert_to_statements()
|
||||
lua:declare_locals()
|
||||
lua:prepend("-- File: " .. tostring(source) .. "\n")
|
||||
lua:prepend("-- File: " .. tostring(nomsu_code.source or "") .. "\n")
|
||||
if compile_fn then
|
||||
compile_fn(lua)
|
||||
end
|
||||
return self:run_lua(lua)
|
||||
end,
|
||||
run_file = function(self, filename)
|
||||
run_file = function(self, filename, compile_fn)
|
||||
if compile_fn == nil then
|
||||
compile_fn = nil
|
||||
end
|
||||
local file_attributes = assert(lfs.attributes(filename), "File not found: " .. tostring(filename))
|
||||
if file_attributes.mode == "directory" then
|
||||
for short_filename in lfs.dir(filename) do
|
||||
local full_filename = filename .. '/' .. short_filename
|
||||
local attr = lfs.attributes(full_filename)
|
||||
if attr.mode ~= "directory" and short_filename:match(".*%.nom") then
|
||||
self:run_file(full_filename)
|
||||
self:run_file(full_filename, compile_fn)
|
||||
end
|
||||
end
|
||||
return
|
||||
@ -387,7 +396,7 @@ do
|
||||
if not file then
|
||||
error("File does not exist: " .. tostring(filename), 0)
|
||||
end
|
||||
return self:run(file)
|
||||
return self:run(file, compile_fn)
|
||||
else
|
||||
return error("Invalid filetype for " .. tostring(filename), 0)
|
||||
end
|
||||
@ -1350,25 +1359,24 @@ if arg and debug_getinfo(2).func ~= require then
|
||||
if args.flags["-c"] and not args.output then
|
||||
args.output = args.input:gsub("%.nom", ".lua")
|
||||
end
|
||||
local compiled_output = nil
|
||||
local compile_fn = nil
|
||||
if args.flags["-p"] then
|
||||
nomsu.environment.print = function() end
|
||||
compiled_output = io.output()
|
||||
compile_fn = function(code)
|
||||
return io.output():write("local IMMEDIATE = true;\n" .. tostring(code))
|
||||
end
|
||||
elseif args.output then
|
||||
compiled_output = io.open(args.output, 'w')
|
||||
compile_fn = function(code)
|
||||
return io.open(args.output, 'w'):write("local IMMEDIATE = true;\n" .. tostring(code))
|
||||
end
|
||||
end
|
||||
if args.input:match(".*%.lua") then
|
||||
local retval = dofile(args.input)(nomsu, { })
|
||||
dofile(args.input)(nomsu, { })
|
||||
else
|
||||
local retval, code
|
||||
if args.input == '-' then
|
||||
retval, code = nomsu:run(io.read('a'))
|
||||
if args.input == "-" then
|
||||
nomsu:run(io.read('a'), compile_fn)
|
||||
else
|
||||
retval, code = nomsu:run_file(args.input)
|
||||
end
|
||||
if compiled_output then
|
||||
compiled_output:write("local IMMEDIATE = true;\n")
|
||||
compiled_output:write(code)
|
||||
nomsu:run_file(args.input, compile_fn)
|
||||
end
|
||||
end
|
||||
if args.flags["-p"] then
|
||||
|
45
nomsu.moon
45
nomsu.moon
@ -296,6 +296,11 @@ class NomsuCompiler
|
||||
return code\gsub("\n","\n"..(" ")\rep(levels))
|
||||
|
||||
parse: (nomsu_code)=>
|
||||
if type(nomsu_code) == 'string'
|
||||
_nomsu_chunk_counter += 1
|
||||
filename = "<nomsu chunk ##{_nomsu_chunk_counter}>.nom"
|
||||
nomsu_code = Nomsu(filename, nomsu_code)
|
||||
FILE_CACHE[filename] = nomsu_code
|
||||
userdata = {
|
||||
source_code:nomsu_code, indent_stack: {""}
|
||||
}
|
||||
@ -308,30 +313,27 @@ class NomsuCompiler
|
||||
return tree
|
||||
|
||||
_nomsu_chunk_counter = 0
|
||||
run: (nomsu_code)=>
|
||||
if type(nomsu_code) == 'string'
|
||||
_nomsu_chunk_counter += 1
|
||||
filename = "<nomsu chunk ##{_nomsu_chunk_counter}>.nom"
|
||||
nomsu_code = Nomsu(filename, nomsu_code)
|
||||
FILE_CACHE[filename] = nomsu_code
|
||||
run: (nomsu_code, compile_fn=nil)=>
|
||||
if #nomsu_code == 0 then return nil
|
||||
tree = @parse(nomsu_code, source)
|
||||
tree = @parse(nomsu_code)
|
||||
assert tree, "Failed to parse: #{nomsu_code}"
|
||||
assert tree.type == "File", "Attempt to run non-file: #{tree.type}"
|
||||
lua = @tree_to_lua(tree)
|
||||
lua\convert_to_statements!
|
||||
lua\declare_locals!
|
||||
lua\prepend "-- File: #{source}\n"
|
||||
lua\prepend "-- File: #{nomsu_code.source or ""}\n"
|
||||
if compile_fn
|
||||
compile_fn(lua)
|
||||
return @run_lua(lua)
|
||||
|
||||
run_file: (filename)=>
|
||||
run_file: (filename, compile_fn=nil)=>
|
||||
file_attributes = assert(lfs.attributes(filename), "File not found: #{filename}")
|
||||
if file_attributes.mode == "directory"
|
||||
for short_filename in lfs.dir(filename)
|
||||
full_filename = filename..'/'..short_filename
|
||||
attr = lfs.attributes(full_filename)
|
||||
if attr.mode ~= "directory" and short_filename\match(".*%.nom")
|
||||
@run_file full_filename
|
||||
@run_file full_filename, compile_fn
|
||||
return
|
||||
|
||||
if filename\match(".*%.lua")
|
||||
@ -346,7 +348,7 @@ class NomsuCompiler
|
||||
file = file or FILE_CACHE[filename]
|
||||
if not file
|
||||
error("File does not exist: #{filename}", 0)
|
||||
return @run(file)
|
||||
return @run(file, compile_fn)
|
||||
else
|
||||
error("Invalid filetype for #{filename}", 0)
|
||||
|
||||
@ -945,24 +947,21 @@ if arg and debug_getinfo(2).func != require
|
||||
-- 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\gsub("%.nom", ".lua")
|
||||
compiled_output = nil
|
||||
compile_fn = nil
|
||||
if args.flags["-p"]
|
||||
nomsu.environment.print = ->
|
||||
compiled_output = io.output()
|
||||
compile_fn = (code)->
|
||||
io.output!\write("local IMMEDIATE = true;\n"..tostring(code))
|
||||
elseif args.output
|
||||
compiled_output = io.open(args.output, 'w')
|
||||
compile_fn = (code)->
|
||||
io.open(args.output, 'w')\write("local IMMEDIATE = true;\n"..tostring(code))
|
||||
|
||||
if args.input\match(".*%.lua")
|
||||
retval = dofile(args.input)(nomsu, {})
|
||||
dofile(args.input)(nomsu, {})
|
||||
else
|
||||
local retval, code
|
||||
if args.input == '-'
|
||||
retval, code = nomsu\run(io.read('a'))
|
||||
else
|
||||
retval, code = nomsu\run_file(args.input)
|
||||
if compiled_output
|
||||
compiled_output\write("local IMMEDIATE = true;\n")
|
||||
compiled_output\write(code)
|
||||
if args.input == "-"
|
||||
nomsu\run(io.read('a'), compile_fn)
|
||||
else nomsu\run_file(args.input, compile_fn)
|
||||
|
||||
if args.flags["-p"]
|
||||
nomsu.environment.print = print
|
||||
|
Loading…
Reference in New Issue
Block a user