Add files via upload
parent
e37ff47180
commit
01a39fac71
|
@ -0,0 +1,258 @@
|
|||
was.register_function=function(name,t)
|
||||
was.functions[name]=t.action
|
||||
was.info[name]=t.info
|
||||
was.privs[name]=t.privs
|
||||
end
|
||||
|
||||
was.chr=function(t)
|
||||
local a=string.byte(t)
|
||||
return (a>=65 and a<=90) or (a>=97 and a<=122) or t=="." or t=="_" or t=="-"
|
||||
end
|
||||
|
||||
was.num=function(t)
|
||||
local a=string.byte(t)
|
||||
return a>=48 and a<=57
|
||||
end
|
||||
|
||||
was.symbol=function(t)
|
||||
return was.symbols:find(t)
|
||||
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
|
||||
|
||||
was.compiler=function(input_text,user)
|
||||
if type(input_text)~="string" or input_text:len()<2 then
|
||||
return
|
||||
end
|
||||
input_text=input_text .."\n"
|
||||
input_text=input_text:gsub("%(","{")
|
||||
input_text=input_text:gsub("%)","}")
|
||||
|
||||
for i=1,was.symbols:len(),1 do
|
||||
input_text=input_text:gsub("%" ..was.symbols:sub(i,i)," " .. was.symbols:sub(i,i) .." ")
|
||||
end
|
||||
|
||||
local c
|
||||
local data={}
|
||||
local output_data={}
|
||||
local n
|
||||
local chr
|
||||
local s
|
||||
local ob={type="",content=""}
|
||||
for i=1,input_text:len(),1 do
|
||||
c=input_text:sub(i,i)
|
||||
|
||||
n=was.num(c)
|
||||
chr=was.chr(c)
|
||||
s=was.symbol(c)
|
||||
--string
|
||||
if c=='"' and ob.type~="string" then
|
||||
if ob.content~="" then table.insert(data,ob) end
|
||||
ob={type="",content=""}
|
||||
ob.type="string"
|
||||
elseif c=='"' and ob.type=="string" then
|
||||
if ob.content~="" then table.insert(data,ob) end
|
||||
ob={type="",content=""}
|
||||
end
|
||||
if ob.type=="string" and c~='"' then
|
||||
ob.content=ob.content .. c
|
||||
end
|
||||
--number
|
||||
if n and ob.type=="" then
|
||||
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=""}
|
||||
end
|
||||
if ob.type=="number" then
|
||||
ob.content=ob.content .. c
|
||||
end
|
||||
--var
|
||||
if ob.type=="" and chr then
|
||||
ob.type="var"
|
||||
elseif ob.type=="var" and not chr and c~="." then
|
||||
if ob.content~="" then table.insert(data,ob) end
|
||||
ob={type="",content=""}
|
||||
end
|
||||
if ob.type=="var" then
|
||||
ob.content=ob.content .. c
|
||||
end
|
||||
--symbols
|
||||
if ob.type=="" and s then
|
||||
ob.type="symbol"
|
||||
elseif ob.type=="symbol" and not s then
|
||||
if ob.content~="" then table.insert(data,ob) end
|
||||
ob={type="",content=""}
|
||||
end
|
||||
if ob.type=="symbol" then
|
||||
ob.content=ob.content .. c
|
||||
end
|
||||
|
||||
--end of line
|
||||
if c=="\n" then
|
||||
if ob.content~="" then table.insert(data,ob) end
|
||||
table.insert(output_data,data)
|
||||
ob={type="",content=""}
|
||||
data={}
|
||||
end
|
||||
end
|
||||
local output_data2={}
|
||||
local func
|
||||
--print(dump(output_data))
|
||||
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)
|
||||
|
||||
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=="end" and ii==1 then
|
||||
--end
|
||||
data[ii].type="end state"
|
||||
|
||||
elseif data[ii+1] and data[ii].type=="var" and data[ii].content=="global" and data[ii+1].type=="var" then
|
||||
--global var
|
||||
data[ii+1].global=true
|
||||
elseif data[ii+1] and data[ii].type=="symbol" and data[ii+1].type=="symbol" then
|
||||
--2 symbols to oparator
|
||||
data[ii].content=data[ii].content .. data[ii+1].content
|
||||
table.remove(data,ii+1)
|
||||
elseif data[ii+1] and data[ii].type=="var" and data[ii+1].content=="=" and not (data[ii+2] and data[ii+2].type=="symbol") then
|
||||
--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"
|
||||
table.remove(data,ii+1)
|
||||
elseif data[ii].content=="}" then
|
||||
--)
|
||||
func=nil
|
||||
data[ii].type="bracket end"
|
||||
end
|
||||
ii=ii+1
|
||||
end
|
||||
|
||||
if func then
|
||||
return 'ERROR line '.. i ..': missing ")"'
|
||||
end
|
||||
|
||||
table.insert(output_data2,data)
|
||||
end
|
||||
|
||||
if user then
|
||||
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(user,was.privs[v.content]) then
|
||||
return 'ERROR: the function "' .. v.content ..'" requires privileges: ' .. minetest.privs_to_string(was.privs[v.content])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
user=user or ":server:"
|
||||
was.user[user]=was.user[user] or {}
|
||||
was.user[user].global={}
|
||||
|
||||
was.run(output_data2,user)
|
||||
end
|
||||
|
||||
was.run_function=function(func_name,data,VAR,i,ii)
|
||||
local d={}
|
||||
local open=0
|
||||
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" or data[i].type=="symbol" then
|
||||
table.insert(d,data[i].content)
|
||||
elseif data[i].type=="var" and VAR[data[i].content] then
|
||||
table.insert(d,VAR[data[i].content])
|
||||
elseif data[i].type=="function" and was.functions[data[i].content] then
|
||||
local re,newi=was.run_function(data[i].content,data,VAR,i+1,#data)
|
||||
i=newi
|
||||
if re then
|
||||
table.insert(d,re)
|
||||
end
|
||||
end
|
||||
i=i+1
|
||||
end
|
||||
if func_name=="if" then
|
||||
return was.functions[func_name](d),i
|
||||
else
|
||||
return was.functions[func_name](unpack(d)),i
|
||||
end
|
||||
end
|
||||
|
||||
was.run=function(input,user)
|
||||
local VAR=was.user[user].global
|
||||
local state=0
|
||||
|
||||
for index,v in ipairs(input) do
|
||||
local i=1
|
||||
while i<=#v do
|
||||
if state==0 and v[i].type=="set var" and v[i+1] then
|
||||
local ndat=v[i+1]
|
||||
if (ndat.type=="string" or ndat.type=="number" or ndat.type=="bool") and ndat.content then
|
||||
VAR[v[i].content]=ndat.content
|
||||
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)
|
||||
else
|
||||
VAR[v[i].content]=nil
|
||||
end
|
||||
|
||||
if v[i].global then
|
||||
was.user[user].global[v[i].content]=VAR[v[i].content]
|
||||
end
|
||||
|
||||
i=i+1
|
||||
elseif v[i].type=="function" and was.functions[v[i].content] then
|
||||
local a
|
||||
if state==0 then
|
||||
a=was.run_function(v[i].content,v,VAR,i+1,#v)
|
||||
end
|
||||
if v[i].content=="if" and a~=true then
|
||||
state=state+1
|
||||
end
|
||||
i=i+1
|
||||
elseif state>0 and v[i].type=="end state" then
|
||||
state=state-1
|
||||
end
|
||||
i=i+1
|
||||
end
|
||||
end
|
||||
--print(dump(VAR))
|
||||
end
|
|
@ -0,0 +1,106 @@
|
|||
was.register_function("test",{
|
||||
action=function(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9)
|
||||
print(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9)
|
||||
end
|
||||
})
|
||||
|
||||
was.register_function("pos",{
|
||||
info="to pos (n1 n2 n3)",
|
||||
action=function(n1,n2,n3)
|
||||
if type(n1)=="number" and type(n2)=="number" and type(n3)=="number" then
|
||||
return {x=n1,y=n2,z=n3}
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
was.register_function("node.set",{
|
||||
info="(pos,nodename)",
|
||||
privs={give=true,ban=true},
|
||||
action=function(pos,name)
|
||||
if was.is_string(name) and was.is_pos(pos) and minetest.registered_nodes[name] then
|
||||
minetest.set_node(pos,{name=name})
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
was.register_function("player.get_pos",{
|
||||
info="(playername)",
|
||||
action=function(name)
|
||||
if type(name)~="string" then
|
||||
return
|
||||
end
|
||||
local p=minetest.get_player_by_name(name)
|
||||
if p then
|
||||
return p:get_pos()
|
||||
end
|
||||
end
|
||||
})
|
||||
|
||||
was.register_function("if",{
|
||||
info="able oparators: and or not nor == ~= < > => =<",
|
||||
action=function(arg)
|
||||
local logic={}
|
||||
local AND
|
||||
local OR
|
||||
local NOT
|
||||
local NOR
|
||||
local li=0
|
||||
local i=2
|
||||
|
||||
if #arg<3 then
|
||||
return false
|
||||
end
|
||||
|
||||
while i<#arg do
|
||||
local a=arg[i]
|
||||
|
||||
if a=="==" then
|
||||
table.insert(logic,(arg[i-1] == arg[i+1]))
|
||||
li=li+1
|
||||
elseif a=="~=" then
|
||||
table.insert(logic,(arg[i-1] ~= arg[i+1]))
|
||||
li=li+1
|
||||
elseif a=="<" and type(arg[i-1])=="number" and type(arg[i+1])=="number" then
|
||||
table.insert(logic,(arg[i-1] < arg[i+1]))
|
||||
li=li+1
|
||||
elseif a==">" and type(arg[i-1])=="number" and type(arg[i+1])=="number" then
|
||||
table.insert(logic,(arg[i-1] > arg[i+1]))
|
||||
li=li+1
|
||||
elseif a=="<=" and type(arg[i-1])=="number" and type(arg[i+1])=="number" then
|
||||
table.insert(logic,(arg[i-1] <= arg[i+1]))
|
||||
li=li+1
|
||||
elseif a==">=" and type(arg[i-1])=="number" and type(arg[i+1])=="number" then
|
||||
table.insert(logic,(arg[i-1] >= arg[i+1]))
|
||||
li=li+1
|
||||
end
|
||||
|
||||
if li>1 and AND and not (logic[li]==true and logic[li-1]==true) then
|
||||
AND=nil
|
||||
return false
|
||||
elseif li>1 and a and OR and not (logic[li]==true or logic[li-1]==true) then
|
||||
OR=nil
|
||||
return false
|
||||
elseif li>1 and a and NOT and (logic[li]==true and logic[li-1]==true) then
|
||||
NOT=nil
|
||||
return false
|
||||
elseif li>1 and a and NOR and (logic[li]==true or logic[li-1]==true) then
|
||||
NOR=nil
|
||||
return false
|
||||
end
|
||||
|
||||
if arg[i]=="and" or arg[i]=="or" or arg[i]=="not" or arg[i]=="nor" then
|
||||
AND=arg[i]=="and"
|
||||
OR=arg[i]=="or"
|
||||
NOT=arg[i]=="not"
|
||||
NOR=arg[i]=="nor"
|
||||
end
|
||||
|
||||
i=i+2
|
||||
end
|
||||
if li<2 then
|
||||
return logic[li]==true
|
||||
else
|
||||
return true
|
||||
end
|
||||
end
|
||||
})
|
|
@ -0,0 +1,91 @@
|
|||
was={
|
||||
functions={},
|
||||
info={},
|
||||
privs={},
|
||||
user={},
|
||||
symbols="#@=?!&()[]{}%*+-/$<>|~^",
|
||||
}
|
||||
|
||||
dofile(minetest.get_modpath("was") .. "/api.lua")
|
||||
dofile(minetest.get_modpath("was") .. "/functions.lua")
|
||||
|
||||
--world action script
|
||||
--bool true false
|
||||
--number 0 123.456
|
||||
--string "asd 134"
|
||||
--var string number function var bool
|
||||
--function pos(1 2 a)
|
||||
|
||||
--was.register_function("name"{
|
||||
-- info="",
|
||||
-- privs={},
|
||||
-- action=function()
|
||||
-- end
|
||||
--})
|
||||
|
||||
|
||||
minetest.register_chatcommand("was", {
|
||||
description = "World action script gui",
|
||||
privs = {kick = true},
|
||||
func = function(name, param)
|
||||
was.gui(name)
|
||||
return true
|
||||
end,
|
||||
})
|
||||
|
||||
was.gui=function(name,msg)
|
||||
was.user[name]=was.user[name] or {text="",funcs={}}
|
||||
|
||||
local text=was.user[name].text or ""
|
||||
local funcs=""
|
||||
|
||||
for f,v in pairs(was.functions) do
|
||||
if minetest.check_player_privs(name,was.privs[f]) then
|
||||
funcs=funcs .. f ..","
|
||||
table.insert(was.user[name].funcs,f)
|
||||
end
|
||||
end
|
||||
funcs=funcs:sub(0,funcs:len()-1)
|
||||
|
||||
local gui="size[20,12]"
|
||||
.."textarea[0,1.3;17,13;text;;" .. text .. "]"
|
||||
.."textlist[16.6,1;3,12;list;" .. funcs .."]"
|
||||
.."label[0,0.6;".. minetest.colorize("#00FF00",(msg or "")) .."]"
|
||||
.."button[0,-0.2;1.3,1;run;Run]"
|
||||
.."button[1,-0.2;1.3,1;save;Save]"
|
||||
|
||||
|
||||
minetest.after(0.1, function(gui,name)
|
||||
return minetest.show_formspec(name, "was.gui",gui)
|
||||
end, gui,name)
|
||||
end
|
||||
|
||||
|
||||
minetest.register_on_player_receive_fields(function(user, form, pressed)
|
||||
if form=="was.gui" then
|
||||
local name=user:get_player_name()
|
||||
if (pressed.quit and not pressed.key_enter) or not was.user[name] then
|
||||
return
|
||||
end
|
||||
local funcs=was.user[name].funcs
|
||||
was.user[name].funcs={}
|
||||
was.user[name].text=pressed.text
|
||||
|
||||
if pressed.list and pressed.list~="IMV" then
|
||||
local n=pressed.list:gsub("CHG:","")
|
||||
local f=funcs[tonumber(n)]
|
||||
local info=was.info[f] or ""
|
||||
|
||||
if was.privs[f] then
|
||||
info=info .. "| Privs: " ..minetest.privs_to_string(was.privs[f])
|
||||
end
|
||||
minetest.close_formspec(name,form)
|
||||
was.gui(name,info)
|
||||
|
||||
elseif pressed.save then
|
||||
elseif pressed.run then
|
||||
local msg=was.compiler(pressed.text,name)
|
||||
was.gui(name,msg)
|
||||
end
|
||||
end
|
||||
end)
|
Loading…
Reference in New Issue