Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local run = function(s)
- local variables = {}
- for vName, vValue in string.gmatch(s, "([%a_][%a%d_]*) ?= ?([\"'%a%d_]*);") do
- variables[vName] = vValue
- end
- local functions = {}
- for fName, fOut in string.gmatch(s, "([%a_][%a%d_]*) ?=> ?([%a%d_]*);") do
- functions[fName] = function() return variables[fOut] or fOut end
- end
- local out = {}
- for fCall in string.gmatch(s, "([%a_][%a%d_]*) ?%(%);") do
- local result = type(functions[fCall]) == "function" and functions[fCall]() or type(_G[fCall]) == "function" and _G[fCall]()
- out[#out + 1] = result
- end
- for value1, comparsion, value2 in string.gmatch(s, "([%a%d_]+) ?([<>]=?) ?([%a%d_]+);?") do
- value1 = (tonumber(value1) or variables[value1] or 0)
- value2 = (tonumber(value2) or variables[value2] or 0)
- local result
- if comparsion:sub(1, 1) == "<" then
- result = value1 < value2
- else
- result = value1 > value2
- end
- if comparsion:sub(2, 2) == "=" then
- result = result or value1 == value2
- end
- out[#out + 1] = result
- end
- for value, values in string.gmatch(s, "([%a%d_]+) ?== ?%((.-)%);") do
- value = tonumber(variables[value] or value)
- if value then
- local args = {}
- for v in string.gmatch(values, "[^, ]+") do
- args[#args + 1] = tonumber(v) or tonumber(variables[v])
- end
- local bool = false
- for i = 1, #args do
- if args[i] == value then
- bool = true
- break
- end
- end
- out[#out + 1] = bool
- end
- end
- for bool, tru, fals in string.gmatch(s, "([%a_][%a%d_]*) ?%? ?([%a%d_]+) ?: ([%a%d_]+);") do
- bool = not not (variables[bool] or _G[bool])
- if bool then
- out[#out + 1] = variables[tru] or _G[tru] or "nil"
- else
- out[#out + 1] = variables[fals] or _G[fals] or "nil"
- end
- end
- for tru, fals in string.gmatch(s, "([%a_][%a%d_]*) ?%?%? ?([%a%d_]+);") do
- tru = variables[tru] or _G[tru] or tonumber(tru)
- if not not tru then
- out[#out + 1] = tru or "nil"
- else
- out[#out + 1] = variables[fals] or _G[fals] or tonumber(fals) or "nil"
- end
- end
- return out
- end
- local r = run([[
- x = 10;
- f => x;
- 10 < 20 < 30;
- 10 == (x, 20, 30);
- x ? y : 49;
- y ?? x;
- ]])
- print(table.unpack(r))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement