Functions rework in progress

Currently working of functions.
Right now external hooking are not working. Some times they work great, other times it is hilighy glitched.

Internal functions work as expected and really well now. However when called outside things tend to go wrong
This commit is contained in:
Ryan Ward 2019-04-07 08:48:57 -04:00
parent 3af881e1a9
commit bfb34916e3
4 changed files with 104 additions and 8 deletions

View File

@ -1083,7 +1083,63 @@ function parseManager:define(t)
self.methods[i]=v self.methods[i]=v
end end
end end
function parseManager:handleChoice(func)
self.choiceManager = func
end
function parseManager:Invoke(func,vars,...) function parseManager:Invoke(func,vars,...)
if not self.isrunning then
self.isrunning = true
local t=self:next()
local name=func
local func=self.chunks[func]
if func then
if func.type:sub(1,1)=="f" then
local returndata={}
self.fArgs={...}
if #self.fArgs==0 then
self.fArgs={self.lastCall}
end
push(self.stack,{chunk=self.currentChunk,pos=self.currentChunk.pos,env=self.currentENV,vars=vars})
self.currentENV = self:newENV()
self.methods.JUMP(self,name)
-- return
else
self:pushError("Attempt to call a non function block!",name)
end
else
self:pushError("Attempt to call a non existing function!",name)
end
while t do
if t.Type=="text" then
io.write(t.text)
io.read()
t=self:next()
elseif t.Type=="condition" then
t=self:next()
elseif t.Type=="assignment" then
t=self:next()
elseif t.Type=="label" then
t=self:next()
elseif t.Type=="method" then
t=self:next()
elseif t.Type=="choice" then
if self.choiceManager then
t=self:choiceManager(t)
else
t=self:next(nil,math.random(1,#t[1]))
end
elseif t.Type=="end" then
if t.text=="leaking" then
t=self:next()
end
elseif t.Type=="error" then
error(t.text)
else
t=self:next()
end
end
return
end
local name=func local name=func
local func=self.chunks[func] local func=self.chunks[func]
if func then if func then
@ -1199,7 +1255,12 @@ function parseManager:parseHeader(data)
local var,inwards = dat:match("(.-)%[(.+)%]") local var,inwards = dat:match("(.-)%[(.+)%]")
local ind = parseManager.split(inwards,":") local ind = parseManager.split(inwards,":")
if #ind==2 then if #ind==2 then
return self.currentENV[dat:match("(.-)%[")]:sub(ind[1],ind[2]) local str = self.currentENV[dat:match("(.-)%[")]
if tonumber(ind[1])<0 and tonumber(ind[2])>0 then
return str:reverse():sub(math.abs(tonumber(ind[1])),-math.abs(tonumber(ind[2])))
else
return str:sub(ind[1],ind[2])
end
end end
end end
elseif not type(self.currentENV[dat])=="table" then elseif not type(self.currentENV[dat])=="table" then
@ -1276,9 +1337,13 @@ function parseManager:pairAssign(vars,vals,envF)
end end
function parseManager:next(block,choice) function parseManager:next(block,choice)
if self.entry then if self.entry then
self.isrunning = true
block = block or self.entry block = block or self.entry
self.entry = nil self.entry = nil
end end
if block then
self.isrunning = true
end
local chunk = self.currentChunk or self.chunks[block] or self.chunks["START"] local chunk = self.currentChunk or self.chunks[block] or self.chunks["START"]
self.currentChunk=chunk self.currentChunk=chunk
if choice then if choice then
@ -1293,12 +1358,12 @@ function parseManager:next(block,choice)
end end
local ret local ret
local data=chunk[chunk.pos] local data=chunk[chunk.pos]
if not data then return end if not data then self.isrunning = false return end
if data.Type=="label" then if data.Type=="label" then
chunk.pos=chunk.pos+1 chunk.pos=chunk.pos+1
data=chunk[chunk.pos] data=chunk[chunk.pos]
end end
if not data then return end if not data then self.isrunning = false return end
chunk.pos=chunk.pos+1 chunk.pos=chunk.pos+1
self:debug("TYPE: "..data.Type) self:debug("TYPE: "..data.Type)
if data.Type=="text" then if data.Type=="text" then

View File

@ -2,11 +2,14 @@ ENTRY MAIN
USING extendedDefine USING extendedDefine
VERSION 4.1 VERSION 4.1
[MAIN]{ [MAIN]{
test = [1,2,3,4,5,6,7,8,9,10] // test = [1,2,3,4,5,6,7,8,9,10]
t=0 // t=0
test2 = "Hello" // test2 = "Hello"
str = $test2[1:-2]$ // str = $test2[1:-1]$
print(str) // print("$test2[-1:1]$")
// print(str)
// t=testfunc(1,2)
// print(t)
// for i = 1,10 < // for i = 1,10 <
// "Choice Test: $test[1]$" < // "Choice Test: $test[1]$" <
// "A" setGlobalVar("t",1) // "A" setGlobalVar("t",1)
@ -16,4 +19,25 @@ VERSION 4.1
// > // >
// ::leave:: // ::leave::
// print("t = $t$ i = $i$") // print("t = $t$ i = $i$")
val = 0
multi = external()
multi:newTLoop(testfunc,1)
// multi:newTLoop(testfunc2)
// alarm = multi:newAlarm(3)
// alarm:OnRing(testfunc)
print("Hooked function!")
}
[testfunc:function()]{
val = val + 1
print("Called $val$ times!")
return True
}
[testfunc2:function()]{
"Test"<
"1" print("A")
"2" print("B")
"3" print("C")
>
// print("Yo whats up!")
return True
} }

BIN
test.dmsc

Binary file not shown.

View File

@ -2,9 +2,15 @@ package.path="?/init.lua;lua/?/init.lua;lua/?.lua;"..package.path
local bin = require("bin") local bin = require("bin")
local multi = require("multi") local multi = require("multi")
require("parseManager") require("parseManager")
require("multi")
test=parseManager:compileToFile("test.dms","test.dmsc")--load("StoryTest/init.dms") test=parseManager:compileToFile("test.dms","test.dmsc")--load("StoryTest/init.dms")
--~ test = parseManager:loadCompiled("test.dmsc") --~ test = parseManager:loadCompiled("test.dmsc")
print(test:dump()) print(test:dump())
test:define{
external=function(self)
return multi
end
}
t=test:next() t=test:next()
while t do while t do
if t.Type=="text" then if t.Type=="text" then
@ -39,3 +45,4 @@ while t do
t=test:next() t=test:next()
end end
end end
multi:mainloop()