was/api.lua

422 lines
11 KiB
Lua
Raw Normal View History

2019-01-03 13:57:48 -08:00
was.register_function=function(name,t)
2019-01-12 01:35:39 -08:00
if t.depends and not minetest.get_modpath(t.depends) then return end
2019-01-03 13:57:48 -08:00
was.functions[name]=t.action
2019-01-04 02:32:59 -08:00
was.function_packed[name]=t.packed
2019-01-03 13:57:48 -08:00
was.info[name]=t.info
was.privs[name]=t.privs
end
was.register_symbol=function(symbol,f,info)
2019-01-04 03:04:00 -08:00
was.symbols[symbol]=f
was.info[symbol]=info
2019-01-04 02:32:59 -08:00
end
2019-01-08 13:56:48 -08:00
was.protected=function(pos)
if was.is_pos(pos) then
return minetest.is_protected(pos,was.userdata.name)
end
end
2019-01-03 13:57:48 -08:00
was.chr=function(t)
local a=string.byte(t)
2019-01-12 01:35:39 -08:00
return (a>=65 and a<=90) or (a>=97 and a<=122) or t=="_" --or t=="."
2019-01-03 13:57:48 -08:00
end
was.num=function(t)
local a=string.byte(t)
return a>=48 and a<=57
end
was.symbol=function(t)
2019-01-04 03:04:00 -08:00
return was.symbols_characters:find(t)
2019-01-03 13:57:48 -08:00
end
was.is_number=function(n)
return type(n)=="number"
end
was.is_string=function(s)
return type(s)=="string"
end
was.is_pos=function(pos)
return type(pos)=="table" and type(pos.x)=="number" and type(pos.y)=="number" and type(pos.z)=="number"
end
2019-01-06 13:57:21 -08:00
was.is_table=function(t)
return type(t)=="table"
end
2019-01-03 13:57:48 -08:00
2019-01-06 02:48:22 -08:00
was.ilastuserdata=function()
for i=was.userdata.index,#was.userdata.data,1 do
if not was.userdata.data[i+1] or was.userdata.data[i].type=="bracket end" then
return i
end
end
2019-01-06 02:48:22 -08:00
return 1
end
2019-01-06 02:48:22 -08:00
was.iuserdata=function(i)
local v=was.userdata.data[i]
2019-01-07 11:02:08 -08:00
if v then
2019-01-06 02:48:22 -08:00
if v.type=="var" then
v=was.userdata.var[v.content]
else
v=v.content
end
end
return v
end
2019-01-12 01:35:39 -08:00
was.compiler=function(input_text,def)
def=def or{}
if type(input_text)~="string" or input_text:len()<2 or type(def.user)~="string" or not was.is_pos(def.pos) then
2019-01-03 13:57:48 -08:00
return
end
input_text=input_text .."\n"
2019-01-06 13:57:21 -08:00
input_text=input_text:gsub("%("," { ")
input_text=input_text:gsub("%)"," } ")
input_text=input_text:gsub("%[","")
input_text=input_text:gsub("%]","")
2019-01-09 00:29:35 -08:00
input_text=input_text:gsub("%%","")
2019-01-03 13:57:48 -08:00
local c
local data={}
local output_data={}
local n
local chr
local s
local ob={type="",content=""}
2019-01-06 13:57:21 -08:00
local i=1
while i<=input_text:len() do
2019-01-03 13:57:48 -08:00
c=input_text:sub(i,i)
n=was.num(c)
chr=was.chr(c)
s=was.symbol(c)
2019-01-06 13:57:21 -08:00
2019-01-03 13:57:48 -08:00
if c=='"' and ob.type~="string" then
2019-01-06 13:57:21 -08:00
--string
2019-01-03 13:57:48 -08:00
if ob.content~="" then table.insert(data,ob) end
ob={type="",content=""}
ob.type="string"
2019-01-06 13:57:21 -08:00
c=""
2019-01-03 13:57:48 -08:00
elseif c=='"' and ob.type=="string" then
if ob.content~="" then table.insert(data,ob) end
ob={type="",content=""}
2019-01-06 13:57:21 -08:00
elseif n and ob.type=="" then
2019-01-03 13:57:48 -08:00
--number
ob.type="number"
elseif not n and ob.type=="number" and c~="." then
if ob.content~="" then table.insert(data,ob) end
ob={type="",content=""}
2019-01-06 13:57:21 -08:00
i=i-1
elseif ob.type=="" and chr then
2019-01-03 13:57:48 -08:00
--var
ob.type="var"
2019-01-12 01:35:39 -08:00
elseif ob.type=="var" and not chr and c~="." then
2019-01-03 13:57:48 -08:00
if ob.content~="" then table.insert(data,ob) end
ob={type="",content=""}
2019-01-06 13:57:21 -08:00
i=i-1
elseif ob.type=="" and s then
2019-01-03 13:57:48 -08:00
--symbols
ob.type="symbol"
elseif ob.type=="symbol" and not s then
if ob.content~="" then table.insert(data,ob) end
ob={type="",content=""}
2019-01-06 13:57:21 -08:00
i=i-1
elseif c=="\n" then
2019-01-03 13:57:48 -08:00
--end of line
if ob.content~="" then table.insert(data,ob) end
table.insert(output_data,data)
ob={type="",content=""}
data={}
end
2019-01-06 13:57:21 -08:00
if ob.type~="" then
2019-01-12 01:35:39 -08:00
if c=="." and ob.type=="var" then
ob.table=true
end
2019-01-06 13:57:21 -08:00
ob.content=ob.content .. c
end
i=i+1
2019-01-03 13:57:48 -08:00
end
local output_data2={}
local func
--print(dump(output_data))
local ifends=0
2019-01-07 09:44:10 -08:00
local nexts=0
2019-01-03 13:57:48 -08:00
for i,v in ipairs(output_data) do
local ii=1
data=v
while ii<=#v do
if data[ii].type=="number" then
--number
data[ii].content=tonumber(data[ii].content)
2019-01-07 06:16:06 -08:00
if data[ii-1] and data[ii-1].content=="-" then
data[ii].content=-data[ii].content
table.remove(data,ii-1)
ii=ii-1
end
2019-01-03 13:57:48 -08:00
elseif data[ii].type=="var" and (data[ii].content=="and" or data[ii].content=="or" or data[ii].content=="not" or data[ii].content=="nor") then
--oparator
data[ii].type="symbol"
elseif data[ii].type=="var" and (data[ii].content=="false" or data[ii].content=="true") then
--bool
data[ii].type="bool"
data[ii].content=data[ii].content=="true"
elseif data[ii].content=="elseif" or data[ii].content=="else" or (ifends>0 and data[ii].content=="end") then
--elseif, else end
data[ii].type="ifstate"
data[ii].ifstate=true
if data[ii].content=="end" then
ifends=ifends-1
end
2019-01-12 01:35:39 -08:00
-- elseif data[ii+1] and data[ii].type=="var" and data[ii].content=="global" and data[ii+1].type=="var" then
2019-01-03 13:57:48 -08:00
--global var
2019-01-12 01:35:39 -08:00
-- data[ii+1].global=true
2019-01-04 15:30:48 -08:00
elseif data[ii+1] and data[ii+2] and data[ii].type=="var" and data[ii+1].content=="=" and not was.symbols[data[ii+1].content .. data[ii+2].content] then
2019-01-03 13:57:48 -08:00
--var =
if func then
return 'ERROR line '.. i ..': set variable "' ..data[ii].content .. '" inside function'
end
data[ii].type="set var"
table.remove(data,ii+1)
elseif data[ii+1] and data[ii].type=="var" and data[ii+1].content=="{" then
--function(
if not was.functions[data[ii].content] then
return 'ERROR line '.. i ..': void function "' .. data[ii].content ..'"'
end
func=true
data[ii].type="function"
2019-01-12 01:35:39 -08:00
data[ii].table=nil
if data[ii].content=="if" then
data[ii].ifstate=true
ifends=ifends+1
2019-01-07 09:44:10 -08:00
elseif data[ii].content=="for" then
if nexts==1 then
return 'ERROR line '.. i ..': only 1 "for" at time'
end
nexts=1
data[ii].forstate=true
end
2019-01-03 13:57:48 -08:00
table.remove(data,ii+1)
elseif data[ii].content=="}" then
--)
func=nil
data[ii].type="bracket end"
2019-01-09 00:29:35 -08:00
elseif data[ii].type=="symbol" then
--symbol
if not was.symbols[data[ii].content] then
return 'ERROR line '.. i ..': "' .. data[ii].content ..'" unknown symbol'
elseif data[ii].content=="--" then
ii=#v
end
2019-01-12 01:35:39 -08:00
--for next
2019-01-07 09:44:10 -08:00
elseif data[ii].type=="var" and data[ii].content=="next" then
if nexts==0 then
return 'ERROR line '.. i ..': no "for" to return to'
end
nexts=0
data[ii].forstate=true
2019-01-12 01:35:39 -08:00
--elseif data[ii+1] and data[ii+2] and data[ii].type=="var" and data[ii+1].content=="." and data[ii+2].type=="var" then
--table
-- data[ii].table={}
-- data[ii].content=data[ii].content .. "." .. data[ii+2].content
-- table.remove(data,ii+1)
-- table.remove(data,ii+1)
--for ni=ii,#v 2 do
-- if data[ni].type=="var" and data[ni+1].content=="." then
-- data[ii].table[data[ni].content]=1
-- data[ii].content=data[ii].content .. "." .. data[ii+2].content
-- data[ni]=nil
-- ii=ii-1
-- end
--end
2019-01-03 13:57:48 -08:00
end
ii=ii+1
end
if func then
return 'ERROR line '.. i ..': missing ")"'
end
table.insert(output_data2,data)
end
2019-01-12 01:35:39 -08:00
for i,c in pairs(output_data2) do
for name,v in pairs(c) do
if v.type=="function" and was.privs[v.content] and not minetest.check_player_privs(def.user,was.privs[v.content]) then
return 'ERROR: the function "' .. v.content ..'" requires privileges: ' .. minetest.privs_to_string(was.privs[v.content])
2019-01-03 13:57:48 -08:00
end
end
2019-01-12 01:35:39 -08:00
end
if ifends>0 then
return 'ERROR: Missing ' .. ifends .. ' if "end"'
end
2019-01-07 09:44:10 -08:00
if nexts>0 then
return 'ERROR: Missing ' .. nexts .. ' for "next"'
end
2019-01-12 01:35:39 -08:00
local msg=was.run(output_data2,def)
return msg
2019-01-03 13:57:48 -08:00
end
was.run_function=function(func_name,data,VAR,i,ii)
2019-01-03 13:57:48 -08:00
local d={}
local open=0
2019-01-06 02:48:22 -08:00
was.userdata.function_name=func_name
2019-01-03 13:57:48 -08:00
while i<=ii do
if data[i].type=="bracket end" then
if open<=0 then
break
else
open=open-1
end
elseif data[i].type=="function" then
open=open+1
end
if data[i].type=="number" or data[i].type=="string" or data[i].type=="bool" then
2019-01-03 13:57:48 -08:00
table.insert(d,data[i].content)
elseif data[i].type=="symbol" and was.symbols[data[i].content] then
2019-01-06 02:48:22 -08:00
was.userdata.index=i
2019-01-12 02:12:28 -08:00
table.insert(d, was.symbols[data[i].content]() or data[i].content)
2019-01-04 02:32:59 -08:00
elseif data[i].type=="var" then
table.insert(d,VAR[data[i].content] or func_name=="if" and "!")
elseif data[i].type=="function" then
2019-01-06 02:48:22 -08:00
was.userdata.index=i
2019-01-04 02:32:59 -08:00
local re,newi=was.run_function(data[i].content,data,VAR,i+1,#data)
2019-01-03 13:57:48 -08:00
i=newi
if re then
table.insert(d,re or func_name=="if" and "!")
2019-01-03 13:57:48 -08:00
end
end
2019-01-06 02:48:22 -08:00
2019-01-03 13:57:48 -08:00
i=i+1
end
2019-01-04 02:32:59 -08:00
if was.function_packed[func_name] then
return was.functions[func_name](d),i
2019-01-03 13:57:48 -08:00
else
2019-01-04 02:32:59 -08:00
return was.functions[func_name](unpack(d)),i
2019-01-03 13:57:48 -08:00
end
end
2019-01-12 01:35:39 -08:00
was.run=function(input,def)
local VAR={} --table.copy(was.user[user].global)
for i,n in pairs(def.event) do
VAR[i]=n
end
2019-01-03 13:57:48 -08:00
local state=0
2019-01-04 15:30:48 -08:00
local elsestate=0
2019-01-07 09:44:10 -08:00
local forstate
was.userdata={
2019-01-12 01:35:39 -08:00
name=def.user,
function_name="",
index=0,
data={},
2019-01-12 01:35:39 -08:00
id=def.pos.x .." " ..def.pos.y .." " .. def.pos.z,
pos=def.pos,
print=def.print,
}
2019-01-07 09:44:10 -08:00
local index=0
while index<#input do
index=index+1
local v=input[index]
2019-01-03 13:57:48 -08:00
local i=1
while i<=#v do
was.userdata.data=v
was.userdata.index=i
2019-01-06 02:48:22 -08:00
was.userdata.var=VAR
2019-01-09 00:29:35 -08:00
if was.userdata.error then
return 'ERROR line '.. index ..': ' .. was.userdata.error
elseif v[i].forstate then
2019-01-07 09:44:10 -08:00
if v[i].content=="next" then
if forstate.i<forstate.e then
index=forstate.re
forstate.i=forstate.i+1
else
forstate=nil
end
elseif v[i].content=="for" then
local fo=was.run_function(v[i].content,v,VAR,i+1,#v)
forstate={
re=index,
i=fo.s,
e=fo.e,
}
if fo.msg then
return fo.msg
end
end
elseif v[i].ifstate then
if v[i].content=="if" then
if state==0 and was.run_function(v[i].content,v,VAR,i+1,#v)==true then
state=0
else
state=state+1
end
end
if v[i].content=="elseif" then
if state==0 then
state=1
elsestate=1
elseif state==1 and elsestate==0 and was.run_function(v[i].content,v,VAR,i+1,#v)==true then
state=0
end
elseif v[i].content=="else" then
if state==0 then
state=1
elseif state==1 and elsestate==0 then
state=0
end
elseif v[i].content=="end" then
state=state-1
if state<=0 then
state=0
elsestate=0
end
end
elseif state>0 then
elseif v[i].type=="set var" and v[i+1] then
2019-01-03 13:57:48 -08:00
local ndat=v[i+1]
if (ndat.type=="string" or ndat.type=="number" or ndat.type=="bool") then
2019-01-03 13:57:48 -08:00
VAR[v[i].content]=ndat.content
2019-01-04 03:04:00 -08:00
elseif ndat.type=="symbol" and was.symbols[ndat.content] then
VAR[v[i].content]=was.symbols[ndat.content]()
2019-01-03 13:57:48 -08:00
elseif ndat.type=="var" and VAR[ndat.content] then
VAR[v[i].content]=VAR[ndat.content]
elseif ndat.type=="function" and was.functions[ndat.content] then
VAR[v[i].content]=was.run_function(ndat.content,v,VAR,i+2,#v)
2019-01-03 13:57:48 -08:00
else
VAR[v[i].content]=nil
end
2019-01-12 01:35:39 -08:00
-- if v[i].global then
-- was.user[user].global[v[i].content]=VAR[v[i].content]
-- end
2019-01-03 13:57:48 -08:00
i=i+1
elseif v[i].type=="function" then
was.run_function(v[i].content,v,VAR,i+1,#v)
2019-01-03 13:57:48 -08:00
i=i+1
2019-01-07 11:02:08 -08:00
elseif v[i].type=="symbol" and was.symbols[v[i].content] and v[i-1] and v[i-1].type=="var" then
VAR[v[i-1].content]=was.symbols[v[i].content]()
2019-01-03 13:57:48 -08:00
end
i=i+1
end
end
was.userdata={}
2019-01-03 13:57:48 -08:00
--print(dump(VAR))
end