mirror of
https://github.com/leafo/moonscript.git
synced 2024-12-08 01:54:24 +00:00
stubbed out new compiler in moonscript
This commit is contained in:
parent
fbf2e370a6
commit
66fcb9593d
@ -12,15 +12,10 @@ local data = require "moonscript.data"
|
|||||||
-- })
|
-- })
|
||||||
|
|
||||||
local map, bind, itwos = util.map, util.bind, util.itwos
|
local map, bind, itwos = util.map, util.bind, util.itwos
|
||||||
local Stack = data.Stack
|
local Stack, ntype = data.Stack, data.ntype
|
||||||
|
|
||||||
local indent_char = " "
|
local indent_char = " "
|
||||||
|
|
||||||
function ntype(node)
|
|
||||||
if type(node) ~= "table" then return "value" end
|
|
||||||
return node[1]
|
|
||||||
end
|
|
||||||
|
|
||||||
-- number of newlines in string
|
-- number of newlines in string
|
||||||
local function num_lines(str)
|
local function num_lines(str)
|
||||||
local sum = 0
|
local sum = 0
|
||||||
|
99
moonscript/compile2.moon
Normal file
99
moonscript/compile2.moon
Normal file
@ -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()
|
||||||
|
|
@ -37,3 +37,9 @@ function Set(items)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- find out the type of a node
|
||||||
|
function ntype(node)
|
||||||
|
if type(node) ~= "table" then return "value" end
|
||||||
|
return node[1]
|
||||||
|
end
|
||||||
|
|
||||||
|
@ -5,11 +5,10 @@ local util = require"moonscript.util"
|
|||||||
|
|
||||||
require"lpeg"
|
require"lpeg"
|
||||||
|
|
||||||
local compile = require"moonscript.compile"
|
|
||||||
local dump = require"moonscript.dump"
|
local dump = require"moonscript.dump"
|
||||||
local data = require"moonscript.data"
|
local data = require"moonscript.data"
|
||||||
|
|
||||||
local ntype = compile.ntype
|
local ntype = data.ntype
|
||||||
local trim = util.trim
|
local trim = util.trim
|
||||||
|
|
||||||
local Stack = data.Stack
|
local Stack = data.Stack
|
||||||
|
Loading…
Reference in New Issue
Block a user