Retrofitted "%'s %" to parse as dot syntax.

This commit is contained in:
Bruce Hill 2018-04-06 16:53:30 -07:00
parent 227b944bad
commit a49e97f0e3
3 changed files with 64 additions and 54 deletions

View File

@ -11,24 +11,10 @@ immediately:
It's also critical to have parens around %obj, otherwise Lua is too dumb to
realize that {x=1}["x"] is the same as ({x=1})["x"] or that
{x=1}.x is the same as ({x=1}).x
compile [..]
parse [..]
%obj' %key, %obj's %key, %key in %obj, %key'th in %obj, %key of %obj,
%key st in %obj, %key nd in %obj, %key rd in %obj, %key th in %obj,
..to:
lua> ".."
local obj_lua = \(%obj as lua expr);
if not obj_lua:sub(-1,-1):match("[a-zA-Z)]") then
obj_lua = "("..obj_lua..")";
end
local key_lua = \(%key as lua expr);
local key_attr = (key_lua:match("'([a-zA-Z][a-zA-Z0-9]*)'")
or key_lua:match('"([a-zA-Z][a-zA-Z0-9]*)"'));
if key_attr then
return {expr=obj_lua.."."..key_attr};
elseif key_lua:sub(1,1) == "[" then
key_lua = " "..key_lua.." ";
end
return {expr=obj_lua.."["..key_lua.."]"};
..as: %obj.%key
# Comparison Operators
immediately:

View File

@ -589,6 +589,18 @@ do
end
end
return buff
elseif "IndexChain" == _exp_0 then
local bits = { }
local _list_1 = tok.value
for _index_0 = 1, #_list_1 do
local bit = _list_1[_index_0]
local nomsu = inline_expression(bit)
if not (nomsu) then
return nil
end
insert(bits, nomsu)
end
return concat(bits, ".")
elseif "List" == _exp_0 then
local bits = { }
local _list_1 = tok.value
@ -691,6 +703,8 @@ do
return nil
end
return "(..)\n " .. self:indent(nomsu)
elseif "IndexChain" == _exp_0 then
return nil
elseif "List" == _exp_0 then
local buff = "[..]"
local line = "\n "
@ -762,7 +776,7 @@ do
return nil
end
buff = buff .. (function()
if bit.type == "Var" or bit.type == "List" or bit.type == "Dict" then
if bit.type == "Var" or bit.type == "List" or bit.type == "Dict" or bit.type == "IndexChain" then
return "\\" .. nomsu
else
return "\\(" .. nomsu .. ")"
@ -967,24 +981,6 @@ do
return {
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
local lua_bits = { }
local locals = { }
@ -1186,6 +1182,24 @@ do
expr = "(" .. tostring(concat(concat_parts, "..")) .. ")"
}
end
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 "List" == _exp_0 then
local items = { }
local _list_1 = tree.value
@ -1259,7 +1273,7 @@ do
return
end
local _exp_0 = tree.type
if "List" == _exp_0 or "File" == _exp_0 or "Block" == _exp_0 or "FunctionCall" == _exp_0 or "Text" == _exp_0 then
if "List" == _exp_0 or "File" == _exp_0 or "Block" == _exp_0 or "FunctionCall" == _exp_0 or "Text" == _exp_0 or "IndexChain" == _exp_0 then
local _list_1 = tree.value
for _index_0 = 1, #_list_1 do
local v = _list_1[_index_0]
@ -1312,7 +1326,7 @@ do
return replacement
end
local _exp_0 = tree.type
if "File" == _exp_0 or "Nomsu" == _exp_0 or "Block" == _exp_0 or "List" == _exp_0 or "FunctionCall" == _exp_0 or "Text" == _exp_0 then
if "File" == _exp_0 or "Nomsu" == _exp_0 or "Block" == _exp_0 or "List" == _exp_0 or "FunctionCall" == _exp_0 or "Text" == _exp_0 or "IndexChain" == _exp_0 then
local new_values, is_changed = { }, false
for i, old_value in ipairs(tree.value) do
local new_value = type(old_value) ~= "string" and self:tree_map(old_value, fn) or nil

View File

@ -439,6 +439,13 @@ class NomsuCompiler
"("..nomsu..")"
else nomsu
return buff
when "IndexChain"
bits = {}
for bit in *tok.value
nomsu = inline_expression bit
return nil unless nomsu
insert bits, nomsu
return concat(bits, ".")
when "List"
bits = {}
for bit in *tok.value
@ -498,6 +505,8 @@ class NomsuCompiler
nomsu = expression(tok)
return nil unless nomsu
return "(..)\n "..@indent(nomsu)
when "IndexChain"
return nil
when "List"
buff = "[..]"
line = "\n "
@ -548,7 +557,7 @@ class NomsuCompiler
else
nomsu = inline_expression(bit)
return nil unless nomsu
buff ..= if bit.type == "Var" or bit.type == "List" or bit.type == "Dict"
buff ..= if bit.type == "Var" or bit.type == "List" or bit.type == "Dict" or bit.type == "IndexChain"
"\\"..nomsu
else "\\("..nomsu..")"
return buff
@ -680,20 +689,6 @@ class NomsuCompiler
--return expr:"nomsu:parse(#{repr @get_source_code(tree.value)}, #{repr @get_line_number(tree.value)}).value[1]"
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"
lua_bits = {}
locals = {}
@ -791,6 +786,21 @@ class NomsuCompiler
return expr:concat_parts[1]
else return expr:"(#{concat(concat_parts, "..")})"
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
-- TODO: improve generated code by removing parens and square brackets when possible
if i == 1
insert items, "(#{lua.expr})"
else
insert items, "[ #{lua.expr}]"
return expr:concat(items,"")
when "List"
items = {}
for item in *tree.value
@ -840,7 +850,7 @@ class NomsuCompiler
coroutine.yield(tree, depth)
return unless Types.is_node(tree)
switch tree.type
when "List", "File", "Block", "FunctionCall", "Text"
when "List", "File", "Block", "FunctionCall", "Text", "IndexChain"
for v in *tree.value
@walk_tree(v, depth+1)
when "Dict"
@ -892,7 +902,7 @@ class NomsuCompiler
if replacement != nil
return replacement
switch tree.type
when "File", "Nomsu", "Block", "List", "FunctionCall", "Text"
when "File", "Nomsu", "Block", "List", "FunctionCall", "Text", "IndexChain"
new_values, is_changed = {}, false
for i,old_value in ipairs(tree.value)
new_value = type(old_value) != "string" and @tree_map(old_value, fn) or nil