mirror of
https://github.com/minetest-mods/mesecons.git
synced 2025-07-01 15:20:23 +02:00
Fixes formatting.
This commit is contained in:
@ -1,8 +1,8 @@
|
|||||||
-- ______
|
-- ______
|
||||||
-- |
|
-- |
|
||||||
-- |
|
-- |
|
||||||
-- | __ ___ _ __ _ _
|
-- | __ ___ _ __ _ _
|
||||||
-- | | | | | |\ | | |_| | | | | |_ |_|
|
-- | | | | | |\ | | |_| | | | | |_ |_|
|
||||||
-- |___| |______ |__| | \| | | \ |__| |_ |_ |_ |\
|
-- |___| |______ |__| | \| | | \ |__| |_ |_ |_ |\
|
||||||
-- |
|
-- |
|
||||||
-- |
|
-- |
|
||||||
@ -55,8 +55,8 @@ local function update_real_port_states(pos, rule_name, new_state)
|
|||||||
L[i] = n % 2
|
L[i] = n % 2
|
||||||
n = math.floor(n / 2)
|
n = math.floor(n / 2)
|
||||||
end
|
end
|
||||||
-- (0,-1) (-1,0) (1,0) (0,1)
|
-- (0,-1) (-1,0) (1,0) (0,1)
|
||||||
local pos_to_side = { 4, 1, nil, 3, 2 }
|
local pos_to_side = { 4, 1, nil, 3, 2 }
|
||||||
if rule_name.x == nil then
|
if rule_name.x == nil then
|
||||||
for _, rname in ipairs(rule_name) do
|
for _, rname in ipairs(rule_name) do
|
||||||
local port = pos_to_side[rname.x + (2 * rname.z) + 3]
|
local port = pos_to_side[rname.x + (2 * rname.z) + 3]
|
||||||
@ -331,16 +331,16 @@ local function create_environment(pos, mem, event)
|
|||||||
env[name] = _G[name]
|
env[name] = _G[name]
|
||||||
end
|
end
|
||||||
|
|
||||||
env.pcall=function(...)
|
env.pcall=function(...)
|
||||||
local pcr={pcall(...)}
|
local pcr={pcall(...)}
|
||||||
if not pcr[1] then
|
if not pcr[1] then
|
||||||
if pcr[2]~=timeout_error then
|
if pcr[2] ~= timeout_error then
|
||||||
error(pcr[2])
|
error(pcr[2])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return unpack(pcr)
|
return unpack(pcr)
|
||||||
end
|
end
|
||||||
|
|
||||||
return env
|
return env
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -352,70 +352,75 @@ end
|
|||||||
|
|
||||||
--A VERY minimalistic lexer, does what it needs to for this job and no more.
|
--A VERY minimalistic lexer, does what it needs to for this job and no more.
|
||||||
local function lexLua(code)
|
local function lexLua(code)
|
||||||
local lexElements={}
|
local lexElements = {}
|
||||||
--Find keywords, whitespace, strings, and then everything else is "cleanup"
|
--Find keywords, whitespace, strings, and then everything else is "cleanup"
|
||||||
|
|
||||||
--Keywords and numbers.
|
--Keywords and numbers.
|
||||||
function lexElements.keyword(str)
|
function lexElements.keyword(str)
|
||||||
return str:match("^[%w_]+")
|
return str:match("^[%w_]+")
|
||||||
end
|
end
|
||||||
function lexElements.whitespace(str)
|
function lexElements.whitespace(str)
|
||||||
return str:match("^%s+")
|
return str:match("^[\r \t]+")
|
||||||
end
|
end
|
||||||
--Unimplemented stuff goes here.
|
--Unimplemented stuff goes here.
|
||||||
function lexElements.cleanup(str)
|
function lexElements.cleanup(str)
|
||||||
return str:match("^.")
|
return str:match("^.")
|
||||||
end
|
end
|
||||||
function lexElements.string(str)
|
function lexElements.string(str)
|
||||||
--Now parse a string.
|
--Now parse a string.
|
||||||
local quoteType=str:sub(1,1)
|
local quoteType = str:sub(1,1)
|
||||||
if quoteType~="\"" and quoteType~="\'" then return nil end
|
if quoteType ~= "\"" and quoteType ~= "\'" then return nil end
|
||||||
local inEscape=true
|
local inEscape = true
|
||||||
local rstr=""
|
local rstr = ""
|
||||||
for i=1,str:len() do
|
for i=1, str:len() do
|
||||||
local c=str:sub(i,i)
|
local c = str:sub(i,i)
|
||||||
rstr=rstr..c
|
rstr = rstr..c
|
||||||
if inEscape then
|
if inEscape then
|
||||||
inEscape=false
|
inEscape = false
|
||||||
else
|
else
|
||||||
if c==quoteType then return rstr end
|
if c == quoteType then return rstr end
|
||||||
if c=="\\" then inEscape=true end
|
if c == "\\" then inEscape = true end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return nil --unfinished string
|
return nil --unfinished string
|
||||||
end
|
end
|
||||||
function lexElements.blockcomment(str)
|
function lexElements.blockcomment(str)
|
||||||
local a=str:match("^%-%-%[%[")
|
local a = str:match("^%-%-%[%[")
|
||||||
if not a then return nil end
|
if not a then return nil end
|
||||||
local s=str:find("%-%-%]%]")
|
local s = str:find("%-%-%]%]")
|
||||||
if not s then return nil end
|
if not s then return nil end
|
||||||
return str:sub(1,s+3)
|
return str:sub(1,s+3)
|
||||||
end
|
end
|
||||||
function lexElements.linecomment(str)
|
function lexElements.linecomment(str)
|
||||||
return str:match("^%-%-[^\r\n]+")
|
return str:match("^%-%-[^\r\n]+")
|
||||||
end
|
end
|
||||||
local lexElementsOrder={"keyword","whitespace","string","blockcomment","linecomment","cleanup"}
|
-- Note: EOL is not reliable for linecounting purposes, but keeps the code better formatted, if anything
|
||||||
local lexResults={}
|
function lexElements.eol()
|
||||||
while code:len()>0 do
|
return str:match("^[\n]+")
|
||||||
--Because break doesn't exist.
|
end
|
||||||
local function parseElem()
|
local lexElementsOrder = {"keyword", "eol", "whitespace",
|
||||||
for _,v in ipairs(lexElementsOrder) do
|
"string", "blockcomment", "linecomment", "cleanup"}
|
||||||
local t,e=lexElements[v](code)
|
local lexResults = {}
|
||||||
if t then
|
while code:len() > 0 do
|
||||||
code=code:sub(t:len()+1)
|
--Because break doesn't exist.
|
||||||
table.insert(lexResults,{v,t})
|
local function parseElem()
|
||||||
return nil
|
for _,v in ipairs(lexElementsOrder) do
|
||||||
end
|
local t, e=lexElements[v](code)
|
||||||
if e then
|
if t then
|
||||||
return e
|
code=code:sub(t:len() + 1)
|
||||||
end
|
table.insert(lexResults,{v, t})
|
||||||
end
|
return nil
|
||||||
return "no match"
|
end
|
||||||
end
|
if e then
|
||||||
local err=parseElem()
|
return e
|
||||||
if err then return nil,"Lexer Error:"..err..": "..code:sub(1,32) end
|
end
|
||||||
end
|
end
|
||||||
return lexResults
|
return "no match"
|
||||||
|
end
|
||||||
|
local err=parseElem()
|
||||||
|
if err then return nil, "Lexer Error:"..err..": "..code:sub(1, 32) end
|
||||||
|
end
|
||||||
|
return lexResults
|
||||||
end
|
end
|
||||||
|
|
||||||
local function code_prohibited(code)
|
local function code_prohibited(code)
|
||||||
@ -424,40 +429,41 @@ local function code_prohibited(code)
|
|||||||
if not jit then
|
if not jit then
|
||||||
return code
|
return code
|
||||||
end
|
end
|
||||||
--Find the following constructs, and place dummies where (dummy) is:
|
--Find the following constructs, and place dummies where (dummy) is:
|
||||||
--while ... do (dummy)
|
--while ... do (dummy)
|
||||||
--for ... do (dummy)
|
--for ... do (dummy)
|
||||||
--repeat (dummy)
|
--repeat (dummy)
|
||||||
|
|
||||||
-- This only exists in Lua 5.2
|
-- This only exists in Lua 5.2
|
||||||
--(dummy) goto
|
--(dummy) goto
|
||||||
|
|
||||||
local lexed,err=lexLua(code)
|
local lexed, err=lexLua(code)
|
||||||
local rcode=""
|
local rcode = ""
|
||||||
if err then
|
if err then
|
||||||
return nil,"Couldn't lex code:"..err
|
return nil, "Couldn't lex code:"..err
|
||||||
end
|
end
|
||||||
for _,v in ipairs(lexed) do
|
for _, v in ipairs(lexed) do
|
||||||
--remove useless stuff since we're going over it anyway.
|
--remove/reduce useless stuff since we're going over it anyway.
|
||||||
if v[1]=="whitespace" then v[2]="\r\n" end
|
if v[1] == "whitespace" then v[2] = " " end
|
||||||
if v[1]=="blockcomment" then v[2]="" end
|
if v[1] == "eol" then v[2] = "\r\n" end -- Windows users may like the \r
|
||||||
if v[1]=="linecomment" then v[2]="" end
|
if v[1] == "blockcomment" then v[2] = "" end
|
||||||
--Code injection so we can safely use every feature in Lua. The only purpose of the lexer is to filter stuff that would break this, really.
|
if v[1] == "linecomment" then v[2] = "" end
|
||||||
--(the old method would stop you from using keywords in strings.)
|
--Code injection so we can safely use every feature in Lua. The only purpose of the lexer is to filter stuff that would break this, really.
|
||||||
--You can also replace this with coroutine.yield, but that's best left to a computercraft-style mod.
|
--(the old method would stop you from using keywords in strings.)
|
||||||
local dummy="(function() end)()"
|
--You can also replace this with coroutine.yield, but that's best left to a computercraft-style mod.
|
||||||
if v[2]=="do" then
|
local dummy = "(function() end)()"
|
||||||
v[2]="do "..dummy
|
if v[2] == "do" then
|
||||||
end
|
v[2] = "do "..dummy
|
||||||
if v[2]=="repeat" then
|
end
|
||||||
v[2]="repeat "..dummy
|
if v[2] == "repeat" then
|
||||||
end
|
v[2] = "repeat "..dummy
|
||||||
if v[2]=="goto" then
|
end
|
||||||
v[2]=dummy.." goto"
|
if v[2] == "goto" then
|
||||||
end
|
v[2] = dummy.." goto"
|
||||||
rcode=rcode..v[2]
|
end
|
||||||
end
|
rcode = rcode..v[2]
|
||||||
return rcode
|
end
|
||||||
|
return rcode
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -506,8 +512,8 @@ local function run(pos, event)
|
|||||||
-- Load code & mem from meta
|
-- Load code & mem from meta
|
||||||
local mem = load_memory(meta)
|
local mem = load_memory(meta)
|
||||||
local code = meta:get_string("code")
|
local code = meta:get_string("code")
|
||||||
local err
|
local err
|
||||||
code,err = code_prohibited(code)
|
code, err = code_prohibited(code)
|
||||||
if not code then return err end
|
if not code then return err end
|
||||||
|
|
||||||
-- Create environment
|
-- Create environment
|
||||||
|
Reference in New Issue
Block a user