1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 17:59:30 +00:00
candran/lua-parser/scope.lua
Reuh 2a1e293aa5 Candran 0.2
Changed a LOT. Notable changes:
* Removed decorators, as they're not that useful, unless rewriting most
Lua libraries API.
* Added functions parameters default values.
* Added Lua 5.3 stuff and building to Lua 5.1.
* Remplaced the LuaMinify parser by lua-parser. It now requires some
non-Lua dependencies (LPeg) unfortunately, but it's waaaaaay easier to
handle. Code should be adaptable to any Metalua-like AST generator
anyway.
* The generated code now look like shit, and comment are stripped,
because the parser ignore them. Oh well.
* Changed a few things in the preprocessor environment.
* Nobody will read this commit message I guess. If you did, create an
issue saying "I love pineapple flavored bread".
2017-08-06 18:45:52 +02:00

74 lines
1.3 KiB
Lua

--[[
This module implements functions that handle scoping rules
]]
local scope = {}
function scope.lineno (s, i)
if i == 1 then return 1, 1 end
local l, lastline = 0, ""
s = s:sub(1, i) .. "\n"
for line in s:gmatch("[^\n]*[\n]") do
l = l + 1
lastline = line
end
local c = lastline:len() - 1
return l, c ~= 0 and c or 1
end
function scope.new_scope (env)
if not env.scope then
env.scope = 0
else
env.scope = env.scope + 1
end
local scope = env.scope
env.maxscope = scope
env[scope] = {}
env[scope]["label"] = {}
env[scope]["local"] = {}
env[scope]["goto"] = {}
end
function scope.begin_scope (env)
env.scope = env.scope + 1
end
function scope.end_scope (env)
env.scope = env.scope - 1
end
function scope.new_function (env)
if not env.fscope then
env.fscope = 0
else
env.fscope = env.fscope + 1
end
local fscope = env.fscope
env["function"][fscope] = {}
end
function scope.begin_function (env)
env.fscope = env.fscope + 1
end
function scope.end_function (env)
env.fscope = env.fscope - 1
end
function scope.begin_loop (env)
if not env.loop then
env.loop = 1
else
env.loop = env.loop + 1
end
end
function scope.end_loop (env)
env.loop = env.loop - 1
end
function scope.insideloop (env)
return env.loop and env.loop > 0
end
return scope