More support for optional metadata and better Tuple support.
This commit is contained in:
parent
4c693d7474
commit
9564492cf1
@ -211,14 +211,13 @@ setmetatable(NOMSU_DEFS, {
|
|||||||
if type(value) == 'table' then
|
if type(value) == 'table' then
|
||||||
error(value)
|
error(value)
|
||||||
end
|
end
|
||||||
lpeg.userdata.tree_metadata[node_id] = {
|
local node = Types[key](node_id, value)
|
||||||
|
lpeg.userdata.tree_metadata[node] = {
|
||||||
start = start,
|
start = start,
|
||||||
stop = stop,
|
stop = stop,
|
||||||
filename = lpeg.userdata.filename,
|
filename = lpeg.userdata.filename,
|
||||||
source_code = lpeg.userdata.source_code
|
source_code = lpeg.userdata.source_code
|
||||||
}
|
}
|
||||||
local node = Types[key](node_id, value)
|
|
||||||
lpeg.userdata.tree_metadata[node] = lpeg.userdata.tree_metadata[node_id]
|
|
||||||
return node
|
return node
|
||||||
end
|
end
|
||||||
self[key] = make_node
|
self[key] = make_node
|
||||||
|
31
nomsu.moon
31
nomsu.moon
@ -21,9 +21,7 @@ colors = setmetatable({}, {__index:->""})
|
|||||||
colored = setmetatable({}, {__index:(_,color)-> ((msg)-> colors[color]..(msg or '')..colors.reset)})
|
colored = setmetatable({}, {__index:(_,color)-> ((msg)-> colors[color]..(msg or '')..colors.reset)})
|
||||||
{:insert, :remove, :concat} = table
|
{:insert, :remove, :concat} = table
|
||||||
|
|
||||||
_Tuple = immutable(nil)
|
Tuple = immutable(nil, {name:"Tuple", __tostring:=> "Tuple(#{concat [repr(x) for x in *@], ", "})"})
|
||||||
Tuple = (t)->
|
|
||||||
return _Tuple(table.unpack(t))
|
|
||||||
|
|
||||||
cached = (fn)->
|
cached = (fn)->
|
||||||
cache = setmetatable({}, {__mode:"k"})
|
cache = setmetatable({}, {__mode:"k"})
|
||||||
@ -59,15 +57,18 @@ lpeg.setmaxstack 10000 -- whoa
|
|||||||
{:P,:R,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
|
{:P,:R,:V,:S,:Cg,:C,:Cp,:B,:Cmt} = lpeg
|
||||||
|
|
||||||
Types = {}
|
Types = {}
|
||||||
|
type_tostring = =>
|
||||||
|
"#{@name}(#{concat [repr(x) for x in *@], ", "})"
|
||||||
for t in *{"File", "Nomsu", "Block", "List", "FunctionCall", "Text", "Dict", "Number", "Word", "Var", "Comment"}
|
for t in *{"File", "Nomsu", "Block", "List", "FunctionCall", "Text", "Dict", "Number", "Word", "Var", "Comment"}
|
||||||
Types[t] = immutable({"id","value"}, {type:t, name:t})
|
Types[t] = immutable({"id","value"}, {type:t, name:t, __tostring:type_tostring})
|
||||||
Types.DictEntry = immutable({"key","value"}, {name:"DictEntry"})
|
Types.DictEntry = immutable({"key","value"}, {name:"DictEntry"})
|
||||||
Types.is_node = (n)->
|
Types.is_node = (n)->
|
||||||
type(n) == 'userdata' and n.type
|
type(n) == 'userdata' and n.type
|
||||||
|
|
||||||
NOMSU_DEFS = with {}
|
NOMSU_DEFS = with {}
|
||||||
-- Newline supports either windows-style CR+LF or unix-style LF
|
-- Newline supports either windows-style CR+LF or unix-style LF
|
||||||
.Tuple = Tuple
|
.Tuple = (values)->
|
||||||
|
return Tuple(table.unpack(values))
|
||||||
.DictEntry = (k,v) -> Types.DictEntry(k,v)
|
.DictEntry = (k,v) -> Types.DictEntry(k,v)
|
||||||
.nl = P("\r")^-1 * P("\n")
|
.nl = P("\r")^-1 * P("\n")
|
||||||
.ws = S(" \t")
|
.ws = S(" \t")
|
||||||
@ -189,6 +190,8 @@ class NomsuCompiler
|
|||||||
:table, :assert, :dofile, :loadstring, :type, :select, :debug, :math, :io, :pairs,
|
:table, :assert, :dofile, :loadstring, :type, :select, :debug, :math, :io, :pairs,
|
||||||
:load, :ipairs,
|
:load, :ipairs,
|
||||||
}
|
}
|
||||||
|
for k,v in pairs(Types) do @environment[k] = v
|
||||||
|
@environment.Tuple = Tuple
|
||||||
@environment.ACTIONS = setmetatable({}, {__index:(key)=>
|
@environment.ACTIONS = setmetatable({}, {__index:(key)=>
|
||||||
error("Attempt to run undefined action: #{key}", 0)
|
error("Attempt to run undefined action: #{key}", 0)
|
||||||
})
|
})
|
||||||
@ -262,7 +265,7 @@ class NomsuCompiler
|
|||||||
get_line_number: cached (tree)=>
|
get_line_number: cached (tree)=>
|
||||||
metadata = @tree_metadata[tree]
|
metadata = @tree_metadata[tree]
|
||||||
unless metadata
|
unless metadata
|
||||||
error "Failed to find metatdata for tree: #{tree}", 0
|
return "<dynamically generated>"
|
||||||
unless @file_metadata[metadata.filename]
|
unless @file_metadata[metadata.filename]
|
||||||
error "Failed to find file metatdata for file: #{metadata.filename}", 0
|
error "Failed to find file metatdata for file: #{metadata.filename}", 0
|
||||||
line_starts = @file_metadata[metadata.filename].line_starts
|
line_starts = @file_metadata[metadata.filename].line_starts
|
||||||
@ -682,7 +685,8 @@ class NomsuCompiler
|
|||||||
return statements:"--"..tree.value\gsub("\n","\n--")
|
return statements:"--"..tree.value\gsub("\n","\n--")
|
||||||
|
|
||||||
when "Nomsu"
|
when "Nomsu"
|
||||||
return expr:"nomsu:parse(#{repr @get_source_code(tree.value)}, #{repr @get_line_number(tree.value)}).value[1]"
|
--return expr:"nomsu:parse(#{repr @get_source_code(tree.value)}, #{repr @get_line_number(tree.value)}).value[1]"
|
||||||
|
return expr:repr(tree.value)
|
||||||
|
|
||||||
when "Block"
|
when "Block"
|
||||||
lua_bits = {}
|
lua_bits = {}
|
||||||
@ -892,9 +896,7 @@ class NomsuCompiler
|
|||||||
else
|
else
|
||||||
new_values[i] = old_value
|
new_values[i] = old_value
|
||||||
if is_changed
|
if is_changed
|
||||||
new_tree = getmetatable(tree)(tree.id, Tuple(new_values))
|
new_tree = getmetatable(tree)(tree.id, Tuple(table.unpack(new_values)))
|
||||||
-- TODO: Maybe generate new metadata?
|
|
||||||
@tree_metadata[new_tree] = @tree_metadata[tree]
|
|
||||||
return new_tree
|
return new_tree
|
||||||
|
|
||||||
when "Dict"
|
when "Dict"
|
||||||
@ -908,9 +910,7 @@ class NomsuCompiler
|
|||||||
else
|
else
|
||||||
new_values[i] = e
|
new_values[i] = e
|
||||||
if is_changed
|
if is_changed
|
||||||
new_tree = getmetatable(tree)(tree.id, Tuple(new_values))
|
new_tree = getmetatable(tree)(tree.id, Tuple(table.unpack(new_values)))
|
||||||
-- TODO: Maybe generate new metadata?
|
|
||||||
@tree_metadata[new_tree] = @tree_metadata[tree]
|
|
||||||
return new_tree
|
return new_tree
|
||||||
when nil -- Raw table, probably from one of the .value of a multi-value tree (e.g. List)
|
when nil -- Raw table, probably from one of the .value of a multi-value tree (e.g. List)
|
||||||
error("Invalid tree: #{repr tree}")
|
error("Invalid tree: #{repr tree}")
|
||||||
@ -1013,7 +1013,10 @@ class NomsuCompiler
|
|||||||
@define_compile_action "!! code location !!", get_line_no!, ->
|
@define_compile_action "!! code location !!", get_line_no!, ->
|
||||||
tree = nomsu.compilestack[#nomsu.compilestack-1]
|
tree = nomsu.compilestack[#nomsu.compilestack-1]
|
||||||
metadata = @tree_metadata[tree]
|
metadata = @tree_metadata[tree]
|
||||||
return expr: repr("#{metadata.filename}:#{metadata.start},#{metadata.stop}")
|
if metadata
|
||||||
|
return expr: repr("#{metadata.filename}:#{metadata.start},#{metadata.stop}")
|
||||||
|
else
|
||||||
|
return expr: repr("<dynamically generated>")
|
||||||
|
|
||||||
@define_action "run file %filename", get_line_no!, (_filename)->
|
@define_action "run file %filename", get_line_no!, (_filename)->
|
||||||
return nomsu\run_file(_filename)
|
return nomsu\run_file(_filename)
|
||||||
|
Loading…
Reference in New Issue
Block a user