added moon script, moved parser to package

This commit is contained in:
leaf corcoran 2011-05-20 01:08:36 -07:00
parent 9a4588d107
commit dd32b5824c
3 changed files with 86 additions and 21 deletions

View File

@ -1,3 +1,4 @@
# hello
hello = 343
print hello

69
moon Executable file
View File

@ -0,0 +1,69 @@
#!/usr/bin/lua
module("moonscript", package.seeall)
require "moonscript.parse"
require "moonscript.compile"
require "util"
require "alt_getopt"
local opts, ind = alt_getopt.get_opts(arg, "hto:", { help = "h" })
local help = [[Usage: %s [options] file...
-h Print this message
-t Dump parse tree
-o fname Write output to file
]]
function print_help(err)
if err then print("Error: "..err) end
print(help:format(arg[0]))
os.exit()
end
function read_file(fname)
local f = io.open(fname)
if not f then return nil end
return f:read("*a")
end
local files = {}
for i = ind, #arg do
table.insert(files, arg[i])
end
if opts.h then print_help() end
if #files == 0 then
print_help"Missing input file"
end
local fname = files[1]
local file_str = read_file(fname)
if not file_str then
print_help("Failed to find file `"..fname.."`")
end
local tree, err = parse.string(file_str)
if not tree then
print("Parse error: "..err)
os.exit()
end
if opts.t then
print(dump.tree(tree))
os.exit()
end
local code = compile.tree(tree)
if opts.o then
io.open(opts.o, "w"):write(code.."\n")
else
print(code)
end

View File

@ -1,12 +1,12 @@
module("moonscript", package.seeall)
module("moonscript.parse", package.seeall)
require"util"
require"lpeg"
require"moonscript.compile"
require"moonscript.dump"
require"moonscript.data"
local compile = require"moonscript.compile"
local dump = require"moonscript.dump"
local data = require"moonscript.data"
local Stack = data.Stack
@ -34,7 +34,7 @@ local Num = C(R("09")^1) / tonumber * Space
local FactorOp = lpeg.C(S"+-") * Space
local TermOp = lpeg.C(S"*/%") * Space
function wrap(fn)
local function wrap(fn)
local env = getfenv(fi)
return setfenv(fn, setmetatable({}, {
@ -52,13 +52,13 @@ function wrap(fn)
}))
end
function mark(name)
local function mark(name)
return function(...)
return name, ...
end
end
function got(what)
local function got(what)
return Cmt("", function(...)
print("++ got "..what)
return true
@ -66,7 +66,7 @@ function got(what)
end
function flatten(tbl)
local function flatten(tbl)
if #tbl == 1 then
return tbl[1]
end
@ -171,6 +171,13 @@ end)
local grammar = build_grammar()
-- parse a string
-- returns tree, or nil and error message
function string(str)
local g = build_grammar()
return grammar:match(str)
end
local program = [[
if two_dads
@ -200,18 +207,6 @@ if true
]]
local tree, err = grammar:match(program)
if not tree then error(err) end
if type(tree) == "table" then
-- dump.tree(tree)
-- print""
print(compile.tree(tree))
else
print "nothing..."
end
local program3 = [[
-- hello
class Hello