+elseif, fixed disappearing text, better if
parent
0d7eebda29
commit
707a1a851f
83
api.lua
83
api.lua
|
@ -116,7 +116,7 @@ was.compiler=function(input_text,user)
|
|||
local output_data2={}
|
||||
local func
|
||||
--print(dump(output_data))
|
||||
|
||||
local ifends=0
|
||||
for i,v in ipairs(output_data) do
|
||||
local ii=1
|
||||
data=v
|
||||
|
@ -133,12 +133,13 @@ was.compiler=function(input_text,user)
|
|||
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].content=="else" and ii==1 then
|
||||
--else
|
||||
data[ii].type="else state"
|
||||
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
|
||||
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
|
||||
|
@ -159,7 +160,11 @@ was.compiler=function(input_text,user)
|
|||
return 'ERROR line '.. i ..': void function "' .. data[ii].content ..'"'
|
||||
end
|
||||
func=true
|
||||
data[ii].type="function"
|
||||
data[ii].type="function"
|
||||
if data[ii].content=="if" then
|
||||
data[ii].ifstate=true
|
||||
ifends=ifends+1
|
||||
end
|
||||
table.remove(data,ii+1)
|
||||
elseif data[ii].content=="}" then
|
||||
--)
|
||||
|
@ -185,6 +190,11 @@ was.compiler=function(input_text,user)
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
if ifends>0 then
|
||||
return 'ERROR: Missing ' .. ifends .. ' if "end"'
|
||||
end
|
||||
|
||||
user=user or ":server:"
|
||||
was.user[user]=was.user[user] or {}
|
||||
was.user[user].global=was.user[user].global or {}
|
||||
|
@ -239,11 +249,40 @@ was.run=function(input,user)
|
|||
local elsestate=0
|
||||
was.username=user
|
||||
--print(dump(input))
|
||||
|
||||
for index,v in ipairs(input) do
|
||||
local i=1
|
||||
while i<=#v do
|
||||
if (state==0 or elsestate==1) and v[i].type=="set var" and v[i+1] then
|
||||
if 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,{var=VAR[v[i].content],variables=VAR,user=user})==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,{var=VAR[v[i].content],variables=VAR,user=user})==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
|
||||
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
|
||||
|
@ -262,27 +301,11 @@ was.run=function(input,user)
|
|||
end
|
||||
|
||||
i=i+1
|
||||
elseif v[i].type=="function" and was.functions[v[i].content] then
|
||||
local a
|
||||
if (state==0 or elsestate==1) then
|
||||
a=was.run_function(v[i].content,v,VAR,i+1,#v,{var=VAR[v[i].content],variables=VAR,user=user})
|
||||
|
||||
end
|
||||
if v[i].content=="if" and a~=true then
|
||||
state=state+1
|
||||
end
|
||||
elseif v[i].type=="function" then
|
||||
was.run_function(v[i].content,v,VAR,i+1,#v,{var=VAR[v[i].content],variables=VAR,user=user})
|
||||
i=i+1
|
||||
elseif (state==0 or elsestate==1) and v[i].type=="symbol" and was.symbols[v[i].content] then
|
||||
was.symbols[v[i].content](VAR[v[i].content],VAR,user)
|
||||
elseif state==1 and v[i].type=="else state" then
|
||||
elsestate=1
|
||||
elseif state==0 and v[i].type=="else state" then
|
||||
state=1
|
||||
elseif state>0 and v[i].type=="end state" then
|
||||
state=state-1
|
||||
if state<=0 then
|
||||
elsestate=0
|
||||
end
|
||||
elseif v[i].type=="symbol" and was.symbols[v[i].content] then
|
||||
was.symbols[v[i].content](VAR[v[i].content],VAR,user)
|
||||
end
|
||||
i=i+1
|
||||
end
|
||||
|
|
14
info.txt
14
info.txt
|
@ -18,14 +18,18 @@ global var stored in tempoary user memory
|
|||
========IF=======
|
||||
|
||||
if(a==b)
|
||||
..code..
|
||||
end
|
||||
..code..
|
||||
endif
|
||||
|
||||
if(1=="asd" or a~=b and 87.3>=c nor a<=3 not "aasd"==!)
|
||||
..code..
|
||||
..code..
|
||||
elseif(b==!)
|
||||
..code..
|
||||
elseif(b~=a not c<b)
|
||||
..code..
|
||||
else
|
||||
..code..
|
||||
end
|
||||
..code..
|
||||
endif
|
||||
|
||||
========REGISTRY=FUNCTIONS=======
|
||||
|
||||
|
|
27
init.lua
27
init.lua
|
@ -73,6 +73,12 @@ minetest.register_node("was:computer", {
|
|||
|
||||
was.gui_addnumbers=function(text)
|
||||
text=text.."\n"
|
||||
for i=1,text:len(),1 do
|
||||
if text:sub(i,i)~="\n" then
|
||||
text=text:sub(i,text:len())
|
||||
break
|
||||
end
|
||||
end
|
||||
local t=""
|
||||
for i,v in ipairs(text.split(text,"\n")) do
|
||||
t=t ..i.." " ..v .."\n"
|
||||
|
@ -80,23 +86,24 @@ was.gui_addnumbers=function(text)
|
|||
return t
|
||||
end
|
||||
|
||||
|
||||
was.gui_delnumbers=function(text)
|
||||
for i=1,text:len(),1 do
|
||||
if text:sub(i,i)~="\n" then
|
||||
text=text:sub(i,text:len())
|
||||
break
|
||||
end
|
||||
end
|
||||
local t=""
|
||||
for i,v in ipairs(text.split(text,"\n")) do
|
||||
local n,nn
|
||||
for ii=1,v:len(),1 do
|
||||
local s=string.sub(v,ii,ii)
|
||||
if not n and was.num(s)==false then
|
||||
n=true
|
||||
end
|
||||
if n and (nn or s~=" ") then
|
||||
t=t..s
|
||||
nn=true
|
||||
elseif n and not nn then
|
||||
nn=true
|
||||
if not was.num(s) then
|
||||
ii= (s==" " and ii+1) or ii
|
||||
t=t .. string.sub(v,ii,v:len()).."\n"
|
||||
break
|
||||
end
|
||||
end
|
||||
t=t.."\n"
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
|
14
register.lua
14
register.lua
|
@ -103,6 +103,20 @@ was.register_function("player.get_pos",{
|
|||
================= MISC =================
|
||||
--]]
|
||||
|
||||
was.register_function("elseif",{
|
||||
packed=true,
|
||||
info="Used with if",
|
||||
action=function(arg)
|
||||
return was.functions["if"](arg)
|
||||
end
|
||||
})
|
||||
was.register_function("else",{
|
||||
packed=true,
|
||||
info="Used with if",
|
||||
action=function(arg)
|
||||
end
|
||||
})
|
||||
|
||||
was.register_function("if",{
|
||||
packed=true,
|
||||
info="able oparators: and or not nor == ~= < > => =<",
|
||||
|
|
Loading…
Reference in New Issue