From bfb34916e31043a5027109924f246c57b871e9b9 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Sun, 7 Apr 2019 08:48:57 -0400 Subject: [PATCH] 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 --- parseManager/init.lua | 71 ++++++++++++++++++++++++++++++++++++++++-- test.dms | 34 +++++++++++++++++--- test.dmsc | Bin 830 -> 1706 bytes test.lua | 7 +++++ 4 files changed, 104 insertions(+), 8 deletions(-) diff --git a/parseManager/init.lua b/parseManager/init.lua index ac2ea94..8fccf2e 100644 --- a/parseManager/init.lua +++ b/parseManager/init.lua @@ -1083,7 +1083,63 @@ function parseManager:define(t) self.methods[i]=v end end +function parseManager:handleChoice(func) + self.choiceManager = func +end 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 func=self.chunks[func] if func then @@ -1199,7 +1255,12 @@ function parseManager:parseHeader(data) local var,inwards = dat:match("(.-)%[(.+)%]") local ind = parseManager.split(inwards,":") 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 elseif not type(self.currentENV[dat])=="table" then @@ -1276,9 +1337,13 @@ function parseManager:pairAssign(vars,vals,envF) end function parseManager:next(block,choice) if self.entry then + self.isrunning = true block = block or self.entry self.entry = nil end + if block then + self.isrunning = true + end local chunk = self.currentChunk or self.chunks[block] or self.chunks["START"] self.currentChunk=chunk if choice then @@ -1293,12 +1358,12 @@ function parseManager:next(block,choice) end local ret 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 chunk.pos=chunk.pos+1 data=chunk[chunk.pos] end - if not data then return end + if not data then self.isrunning = false return end chunk.pos=chunk.pos+1 self:debug("TYPE: "..data.Type) if data.Type=="text" then diff --git a/test.dms b/test.dms index 71e356b..0feadef 100644 --- a/test.dms +++ b/test.dms @@ -2,11 +2,14 @@ ENTRY MAIN USING extendedDefine VERSION 4.1 [MAIN]{ - test = [1,2,3,4,5,6,7,8,9,10] - t=0 - test2 = "Hello" - str = $test2[1:-2]$ - print(str) + // test = [1,2,3,4,5,6,7,8,9,10] + // t=0 + // test2 = "Hello" + // str = $test2[1:-1]$ + // print("$test2[-1:1]$") + // print(str) + // t=testfunc(1,2) + // print(t) // for i = 1,10 < // "Choice Test: $test[1]$" < // "A" setGlobalVar("t",1) @@ -16,4 +19,25 @@ VERSION 4.1 // > // ::leave:: // 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 } \ No newline at end of file diff --git a/test.dmsc b/test.dmsc index bf7f4dbfdbf805e7b8afc587ebff5dabf0e2922b..e8567f79f20d659ab9160965ada1adebc747ce8b 100644 GIT binary patch literal 1706 zcmb_d+iuf95Ovx#O#lHZ;vz1hfe5t^RB90t54_|;MbxlBcM&{*kQ;AOQ)g|~8v?(< z8~?!@A6L$J?DRrZK&>Tv?A@6;XD&Nl@I0@l=H6C3Ppa~ZW>?&s%T{hd2vx%NJ63rO zA!S*8OEi={KTX4-fz1~`O0TlYMF=6trns_wdt_hIei?TOmO=r>dyg9=-azCC^LQOh zEWS3!reDRE{_3&L)>3MCOq=l}6*ph0yTrFkNH~JP`-^`@sv}A#xOAF_^>_ z76kK{71kiw=K|g`mqHRvqG9wz_2M{@ty(v2;%#r7?B|G2u#Yg-A&zrm2Q}=JoKD>U zXhIOFB3VKanQh$gumy3PB4>gaB{noJhpe-~O$dghgggf;9V`=w3u9AA2Pd=jSxxKx$M-_g0?Zy@@wYdFEHBrH9taS7+3DnL=!<3!jkFU+R^Bc5+1qUhE)_A)Mw*~mL0v!>r<5iex<)|!TO%~s z;0}4n#@iLVwf!ieXN))4pju_&;KykOK6W^N9 l4AcGWKZEsE3QytT-NlWC8?=zcOTQ8t`6kdoM-M`ye*@Q`PUips literal 830 zcmaJVH06P6QRhEONit+md_CQ2Rdpxeu@q{ zBt8OWdL z6DhzGG?6KIf+jKzPtcNbBfWwlT3kR7hm}#qj+KtZWohRC)*A@pI$kzg<`GPT7$lZT zEMWqQSp*|NUK{*uwq+JwgmL_TmRM81KTHKy4aXva5#Ze7oew z(2KA6987O_2HKHKIMhF1z~yyU0TAsWq~%emI~so1$4z0>58_n$pUU#Scd2~M{{TvO zQpWhG{B|r&1VsQP2;1`ZM|a$Zukc@MWwG0EsmwlB?ijtGieQ M9o0Ed-F~3H0G2$9MF0Q* diff --git a/test.lua b/test.lua index 7f16e4e..a059c82 100644 --- a/test.lua +++ b/test.lua @@ -2,9 +2,15 @@ package.path="?/init.lua;lua/?/init.lua;lua/?.lua;"..package.path local bin = require("bin") local multi = require("multi") require("parseManager") +require("multi") test=parseManager:compileToFile("test.dms","test.dmsc")--load("StoryTest/init.dms") --~ test = parseManager:loadCompiled("test.dmsc") print(test:dump()) +test:define{ + external=function(self) + return multi + end +} t=test:next() while t do if t.Type=="text" then @@ -39,3 +45,4 @@ while t do t=test:next() end end +multi:mainloop()