Created
January 9, 2017 22:41
-
-
Save exerro/1c522424689b7856cf6569bd4700abbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local function node_query_internal( query ) | |
if query.type == QUERY_ID then | |
return ("n.id=='%s'"):format( query.value ) | |
elseif query.type == QUERY_TAG then | |
return ("n:has_tag'%s'"):format( query.value ) | |
elseif query.type == QUERY_ALL then | |
return "true" | |
elseif query.type == QUERY_CLASS then | |
-- TODO: check if type_of() accepts a string | |
return ("n:type_of'%s'"):format( query.value ) | |
elseif query.type == QUERY_NEGATE then | |
local i = node_query_internal( query.value ) | |
return i == "true" and "false" or i == "false" and "true" or "not " .. i | |
elseif query.type == QUERY_ATTRIBUTES then | |
-- TODO! | |
error "NYI" | |
elseif query.type == QUERY_OPERATOR then | |
local lvalue = node_query_internal( query.lvalue ) | |
local rvalue = node_query_internal( query.rvalue ) | |
if query.operator == "&" then | |
if lvalue == "true" then return rvalue end | |
if rvalue == "true" then return lvalue end | |
if lvalue == "false" then return lvalue end | |
if rvalue == "false" then return rvalue end | |
return lvalue .. " and " .. rvalue | |
elseif query.operator == "|" then | |
if lvalue == "true" then return lvalue end | |
if rvalue == "true" then return rvalue end | |
if lvalue == "false" then return rvalue end | |
if rvalue == "false" then return lvalue end | |
return lvalue .. " or " .. rvalue | |
end | |
end | |
end | |
class "Codegen" {} | |
function Codegen.node_query( parsed_query ) | |
print( "local n=...return " .. node_query_internal( parsed_query ) ) | |
return load( "local n=...return " .. node_query_internal( parsed_query ), "query" ) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment