diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 66a98ab..68bbe7d 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -12,15 +12,10 @@ local data = require "moonscript.data" -- }) local map, bind, itwos = util.map, util.bind, util.itwos -local Stack = data.Stack +local Stack, ntype = data.Stack, data.ntype local indent_char = " " -function ntype(node) - if type(node) ~= "table" then return "value" end - return node[1] -end - -- number of newlines in string local function num_lines(str) local sum = 0 diff --git a/moonscript/compile2.moon b/moonscript/compile2.moon new file mode 100644 index 0000000..03b9a84 --- /dev/null +++ b/moonscript/compile2.moon @@ -0,0 +1,99 @@ + +module "moonscript.compile", package.seeall + +util = require "moonscript.util" +data = require "moonscript.data" + +import map, bind, itwos, every from util +import Stack from data + +B = {} +block_t = {__index: B} + +indent_char = " " +pretty = (lines, indent) -> + indent = indent or "" + render = (line) -> + if type(line) == "table" + indent_char..pretty(line) + else + line + + table.concat [render line for line in *lines], "\n"..indent + +block_t = {} +Block = (parent) -> + indent = parent and parent.indent + 1 or 0 + setmetatable { + lines: {}, names: {} + indent: indent, parent: parent + }, block_t + +B = + put_name: (name) => + @names[name] = true + + has_name: (name) => + if @names[name] + true + elseif @parent + @parent:has_name name + else + false + + add_lines: (lines) => + table.insert @lines, line for line in *lines + + add_line: (line) => + table.insert @lines, line + + render: => + pretty @lines, indent_char:rep @indent + +line_compile = + assign: (node) => + "hello world" + +value_compile = + exp: (node) => + "yeah" + +compiler_index = + block: (node) => + @out = Block(@out) + + @stm s for s in *node + + out = @out + @out = @out.parent + out + + name: (node) => @value node + + value: (node) => + return tostring node if type node != "table" + fn = value_compile[node[1]] + error "Failed to compile value: "..node[1] if not fn + + fn self, node + + values: (values, delim) => + delim = delim or ', ' + table.concat [@value v for v in values], delim + + stm: (node) => + fn = line_compile[node[1]] + error "Failed to compile statment: "..node[1] if not fn + + out = fn self, node + @out:add_line out if out + +block_t.__index = B + +build_compiler = -> + setmetatable {}, { __index: compiler_index } + +_M.tree = (tree) -> + compiler = build_compiler() + compiler:block(tree):render() + diff --git a/moonscript/data.lua b/moonscript/data.lua index 3dd3104..49dcde9 100644 --- a/moonscript/data.lua +++ b/moonscript/data.lua @@ -37,3 +37,9 @@ function Set(items) return self end +-- find out the type of a node +function ntype(node) + if type(node) ~= "table" then return "value" end + return node[1] +end + diff --git a/moonscript/parse.lua b/moonscript/parse.lua index c321de8..7826791 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -5,11 +5,10 @@ local util = require"moonscript.util" require"lpeg" -local compile = require"moonscript.compile" local dump = require"moonscript.dump" local data = require"moonscript.data" -local ntype = compile.ntype +local ntype = data.ntype local trim = util.trim local Stack = data.Stack