Initial working version.

This commit is contained in:
Bruce Hill 2018-04-06 16:45:51 -07:00
parent 9109a090eb
commit 227b944bad
3 changed files with 49 additions and 7 deletions

View File

@ -69,7 +69,8 @@ local _list_0 = {
"Number", "Number",
"Word", "Word",
"Var", "Var",
"Comment" "Comment",
"IndexChain"
} }
for _index_0 = 1, #_list_0 do for _index_0 = 1, #_list_0 do
local t = _list_0[_index_0] local t = _list_0[_index_0]
@ -185,7 +186,7 @@ setmetatable(NOMSU_DEFS, {
make_node = function(start, value, stop) make_node = function(start, value, stop)
node_id = node_id + 1 node_id = node_id + 1
if type(value) == 'table' then if type(value) == 'table' then
error(value) error("Not a tuple: " .. tostring(repr(value)))
end end
local node = Types[key](node_id, value) local node = Types[key](node_id, value)
lpeg.userdata.tree_metadata[node] = { lpeg.userdata.tree_metadata[node] = {
@ -966,6 +967,24 @@ do
return { return {
expr = repr(tree.value) expr = repr(tree.value)
} }
elseif "IndexChain" == _exp_0 then
local items = { }
for i, item in ipairs(tree.value) do
local lua = self:tree_to_lua(item)
if not (lua.expr) then
local line = self:get_line_number(item)
local src = self:get_source_code(item)
error(tostring(line) .. ": Cannot index " .. tostring(colored.yellow(src)) .. ", since it's not an expression.", 0)
end
if i == 1 then
insert(items, "(" .. tostring(lua.expr) .. ")")
else
insert(items, "[ " .. tostring(lua.expr) .. "]")
end
end
return {
expr = concat(items, "")
}
elseif "Block" == _exp_0 then elseif "Block" == _exp_0 then
local lua_bits = { } local lua_bits = { }
local locals = { } local locals = { }
@ -1798,6 +1817,7 @@ if arg and debug.getinfo(2).func ~= require then
end end
return os.exit(false, true) return os.exit(false, true)
end end
xpcall(run, err_hand) local ldt = require('ldt')
ldt.guard(run)
end end
return NomsuCompiler return NomsuCompiler

View File

@ -51,7 +51,7 @@ Types = {}
type_tostring = => type_tostring = =>
"#{@name}(#{concat [repr(x) for x in *@], ", "})" "#{@name}(#{concat [repr(x) for x in *@], ", "})"
Tuple = immutable(nil, {name:"Tuple"}) Tuple = immutable(nil, {name:"Tuple"})
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", "IndexChain"}
Types[t] = immutable({"id","value"}, {type:t, name:t, __tostring:type_tostring}) 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)->
@ -125,7 +125,7 @@ node_id = 0
setmetatable(NOMSU_DEFS, {__index:(key)=> setmetatable(NOMSU_DEFS, {__index:(key)=>
make_node = (start, value, stop)-> make_node = (start, value, stop)->
node_id = node_id + 1 node_id = node_id + 1
if type(value) == 'table' then error(value)-- = Tuple(value) if type(value) == 'table' then error("Not a tuple: #{repr value}")-- = Tuple(value)
node = Types[key](node_id, value) node = Types[key](node_id, value)
lpeg.userdata.tree_metadata[node] = { lpeg.userdata.tree_metadata[node] = {
:start,:stop,filename:lpeg.userdata.filename,source_code:lpeg.userdata.source_code :start,:stop,filename:lpeg.userdata.filename,source_code:lpeg.userdata.source_code
@ -680,6 +680,20 @@ class NomsuCompiler
--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) return expr:repr(tree.value)
when "IndexChain"
items = {}
for i, item in ipairs(tree.value)
lua = @tree_to_lua item
unless lua.expr
line = @get_line_number(item)
src = @get_source_code(item)
error "#{line}: Cannot index #{colored.yellow src}, since it's not an expression.", 0
if i == 1
insert items, "(#{lua.expr})"
else
insert items, "[ #{lua.expr}]"
return expr:concat(items,"")
when "Block" when "Block"
lua_bits = {} lua_bits = {}
locals = {} locals = {}
@ -1145,6 +1159,8 @@ if arg and debug.getinfo(2).func != require
-- Note: xpcall has a slightly different API in Lua <=5.1 vs. >=5.2, but this works -- Note: xpcall has a slightly different API in Lua <=5.1 vs. >=5.2, but this works
-- for both APIs -- for both APIs
xpcall(run, err_hand) ldt = require 'ldt'
ldt.guard run
--xpcall(run, err_hand)
return NomsuCompiler return NomsuCompiler

View File

@ -18,9 +18,15 @@ indented_block (Block):
inline_nomsu (Nomsu): "\" inline_expression inline_nomsu (Nomsu): "\" inline_expression
indented_nomsu (Nomsu): "\" expression indented_nomsu (Nomsu): "\" expression
inline_expression: index_chain (IndexChain):
{| noindex_inline_expression ("." ((({} ({|{%operator / (!number plain_word)}|} -> Tuple) {}) -> Text) / noindex_inline_expression))+ |} -> Tuple
noindex_inline_expression:
number / variable / inline_text / inline_list / inline_dict / inline_nomsu number / variable / inline_text / inline_list / inline_dict / inline_nomsu
/ ("(" %ws* (inline_functioncall / inline_expression) %ws* ")") / ("(" %ws* (inline_functioncall / inline_expression) %ws* ")")
inline_expression:
index_chain / noindex_inline_expression
indented_expression: indented_expression:
indented_text / indented_nomsu / indented_list / indented_dict / indented_block indented_text / indented_nomsu / indented_list / indented_dict / indented_block
expression: expression: