Created
August 18, 2014 13:26
-
-
Save rejax/993b7ff273af0833b63b to your computer and use it in GitHub Desktop.
i don't know what this even is to be honest
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 meta = {} | |
comet = setmetatable( {}, meta ) | |
comet.Operators = {} | |
comet.Objects = {} | |
comet.Checks = {} | |
comet.Sources = {} | |
comet.Objects["players"] = player.GetAll | |
comet.Objects["entities"] = ents.GetAll | |
comet.Operators["="] = function( ... ) | |
local c1, c2 = comet.GetChecks( ... ) | |
return c1 == c2 | |
end | |
comet.Operators["!"] = function( ... ) | |
local c1, c2 = comet.GetChecks( ... ) | |
return c1 ~= c2 | |
end | |
comet.Operators["~"] = function( ... ) | |
local c1, c2 = comet.GetChecks( ... ) | |
local kind = type( c1 ) | |
if kind == "string" then | |
local str = tostring( c2 ) | |
return c1:lower():find( str:lower() ) | |
elseif kind == "number" then | |
local num = tonumber( c2 ) | |
if not num then return false end | |
return math.floor( c1 ) == math.floor( num ) | |
else | |
return c1 == c2 | |
end | |
end | |
comet.Checks["type"] = function( obj ) | |
return type( obj ) | |
end | |
comet.Checks["valid"] = function( obj ) | |
return IsValid( obj ) | |
end | |
comet.Sources["from"] = function( from ) | |
local method | |
if from:find( "%." ) then | |
local lib, func = unpack( ("."):Explode( from ) ) | |
method = _G[lib][func] | |
else | |
method = comet.locals[from] or _G[from] | |
end | |
assert( method, "comet - invalid method" ) | |
if type( method ) == "function" then | |
return method() | |
else | |
return method | |
end | |
end | |
function comet.GetChecks( obj, check, against ) | |
if against:find( "^'(.+)'$" ) then | |
local _, _, a = against:find( "^'(.+)'$" ) | |
against = a | |
else | |
local method = comet.locals[against] or _G[against] | |
if type( method ) == "function" then | |
against = method() | |
else | |
against = method | |
end | |
end | |
local kind = type( obj ) | |
local num = tonumber( against ) | |
if num ~= nil then against = num end | |
if kind ~= "number" and kind ~= "string" and kind ~= "nil" then | |
if comet.Checks[check] then | |
return comet.Checks[check]( obj, check ), against | |
else | |
return obj[check]( obj ), against | |
end | |
else | |
return check, against | |
end | |
end | |
function comet.GetBlocks( str ) | |
local str_tab = (" "):Explode( str:find( ";$" ) and str or str .. ";" ) | |
local blocks = {} | |
local last_block = 1 | |
for k, str in pairs( str_tab ) do | |
if str:find( ";" ) then | |
local block = (table.concat( str_tab, " ", last_block, k )):gsub( ";", "" ) | |
table.insert( blocks, block ) | |
last_block = k + 1 | |
end | |
end | |
return blocks | |
end | |
function comet.GetFunctions( iter ) | |
local funcs = {} | |
local addfunc | |
local highestkey = 1 | |
for i, word in pairs( iter ) do | |
local op = word:lower() | |
if #funcs == 0 or addfunc then | |
table.insert( funcs, word ) | |
addfunc = false | |
highestkey = i + 1 | |
continue | |
end | |
addfunc = op == "and" | |
end | |
return funcs, highestkey | |
end | |
local clause_op = "where" | |
local return_all = { ["*"] = true, ["all"] = true } | |
local returns = { ["return"] = true, ["select"] = true } | |
local get_obj = "obj" | |
local select_var = "$" | |
function comet.GetObjects( outs ) | |
local concat = table.concat( outs, " " ) | |
local a_clause = concat:lower():find( clause_op ) | |
local objects = {} | |
if comet.Sources[outs[1]] then | |
local tab = comet.Sources[outs[1]]( outs[2] ) | |
objects = table.Copy( tab ) | |
else | |
for i, val in pairs( outs ) do | |
for n, f in pairs( comet.Objects ) do | |
if val:find( n ) then | |
for _, val in pairs( f() ) do table.insert( objects, val ) end | |
end | |
end | |
end | |
end | |
if a_clause then | |
local clause = concat:sub( a_clause + clause_op:len() + 1 ):gsub( " ", "" ) | |
local operator = clause:gsub( "%w", "" ):sub( 1, 1 ) | |
local pos = clause:find( operator ) | |
local check = clause:sub( 1, pos - 1 ) | |
local against = clause:sub( pos + 1 ) | |
local pattern = "%b[]" | |
local kill = ".-%[" | |
if against:find( pattern ) then | |
local function strip( s ) return s:sub( 2, s:len() - 1 ) end | |
for str in against:gmatch( pattern ) do | |
local name = str:gsub( "%b[]", strip ) | |
local ret = comet.locals[name] or _G[name] | |
assert( ret, "comet - invalid pattern" ) | |
against = against:gsub( name, ret ) | |
end | |
against = against:gsub( "%b[]", strip ) | |
end | |
local _objects = {} | |
local func = comet.Operators[operator] | |
for k, obj in pairs( objects ) do | |
local c = check | |
if check == get_obj then c = obj end | |
local ok = func( obj, c, against ) | |
if ok then | |
table.insert( _objects, obj ) | |
end | |
end | |
objects = _objects | |
end | |
return objects | |
end | |
function comet.RunFunc( obj, func ) | |
if _G[func] then | |
_G[func]( obj ) | |
elseif comet.locals[func] then | |
comet.locals[func]( obj ) | |
end | |
end | |
function comet.RunBlock( str ) | |
local iter = (" "):Explode( str ) | |
local out = {} | |
local key | |
out.funcs, key = comet.GetFunctions( iter ) | |
out.limit = iter[key]:lower() | |
if not return_all[out.limit] then out.limit = tonumber( out.limit ) end | |
comet.return1 = out.limit == 1 | |
local exec = {} | |
for i = key + 1, #iter do | |
table.insert( exec, iter[i] ) | |
end | |
out.objects = comet.GetObjects( exec ) | |
for k, obj in pairs( out.objects ) do | |
if not return_all[out.limit] and k > out.limit then break end | |
for _, func in pairs( out.funcs ) do | |
if returns[func] then | |
table.insert( comet.returns, obj ) | |
continue | |
end | |
comet.RunFunc( obj, func ) | |
end | |
end | |
end | |
function comet.GetLocals() | |
local level = 1 | |
local locals = {} | |
while true do | |
local name, value = debug.getlocal( 3, level ) | |
if not name then break end | |
locals[name] = value | |
level = level + 1 | |
end | |
return locals | |
end | |
function comet.Run( str ) | |
comet.returns = {} | |
comet.locals = comet.GetLocals() | |
comet.return1 = false | |
local blocks = comet.GetBlocks( str ) | |
for _, block in pairs( blocks ) do | |
comet.RunBlock( block ) | |
end | |
if comet.return1 then | |
return comet.returns[1] | |
else | |
return comet.returns | |
end | |
end | |
function meta:__call( str ) return comet.Run( str ) end | |
--[[ | |
callbacks amount method [where clause] | |
eg | |
[print] [all] [from player.GetAll] [where Nick='rejax'] | |
]]-- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment