All pastes #456951 Raw Edit

Unnamed

public lua v1 · immutable
#456951 ·published 2007-04-25 02:17 UTC
rendered paste body
PBLock = {}function PBLock:New()	local o = {}	for k,v in pairs(self) do o[k]=v end	o.vars = {}	o.block = {}	return oendfunction PBLock:SetBlock(block)	self.block = blockendfunction PBLock:Run()	poolString(false)	return self:Interp()endfunction PBLock:Interp(block)	local b = block or self.block	for k,v in ipairs(b) do		local chk = self:DoChunk(v)		if type(chk) == "number" then			return chk		elseif chk == false then			return 		end --if the type is neither a number nor it flailed, we loop again	end	return false --there was nothing to returnendfunction PBLock:CheckVals(...)	for _,v in pairs(arg) do		if v == false then			return false		end	end	return trueendfunction PBLock:DoChunk(chnk,ident)	local ident = ident or 0	local value	--chnk[1] should always be the OP name	if type(chnk) ~= "table" then		return chnk --chnk is a CONSTANT value, we don't care about it	end		--here we're checking for OPCODEs in the giant craptackular table	local buf = string.rep(" ",ident*2)..poolString(chnk)..":"	for k,v in pairs(chnk) do		buf=buf.." "..poolString(v)	end	print(buf)	if chnk[1] == "VARSET" then		self.vars[chnk[2]] = self:DoChunk(chnk[3],ident+1)	elseif chnk[1] == "VARGET" then		if self.vars[chnk[2]] == nil then			print(chnk[2].." was not set! Exiting")			return false		else			value = self.vars[chnk[2]]		end	elseif chnk[1] == "MUL" then		local x,y		x = self:DoChunk(chnk[2],ident+1)		y = self:DoChunk(chnk[3],ident+1)		if not self:CheckVals(x,y) then return false end		value = x*y	elseif chnk[1] == "DIV" then		local x,y		x = self:DoChunk(chnk[2],ident+1)		y = self:DoChunk(chnk[3],ident+1)		if not self:CheckVals(x,y) then return false end		value = x/y	elseif chnk[1] == "ADD" then		local x,y		x = self:DoChunk(chnk[2],ident+1)		y = self:DoChunk(chnk[3],ident+1)		if not self:CheckVals(x,y) then return false end		value = x+y	elseif chnk[1] == "SUB" then		local x,y		x = self:DoChunk(chnk[2],ident+1)		y = self:DoChunk(chnk[3],ident+1)		if not self:CheckVals(x,y) then return false end		value = x-y	elseif chnk[1] == "EXP" then		local x,y		x = self:DoChunk(chnk[2],ident+1)		y = self:DoChunk(chnk[3],ident+1)		if not self:CheckVals(x,y) then return false end		value = x^y	elseif chnk[1] == "CALL" then		local fnc,val		fnc = chnk[2] --this should always be a string		val = self:DoChunk(chnk[3],ident+1)		if not self:CheckVals(val) then return false end		value = math[fnc](val)	elseif chnk[1] == "GET" then		local fnc,val		fnc = chnk[2] --this should always be a string		value = math[fnc]	else		print("Bad OPCODE: "..chnk[1].." Exiting")		return false	end			if value then			if type(value) == "number" then				print(string.rep(" ",(ident*2))..poolString(chnk):gsub("%$","%%").."="..value)			end						return value		else			--print("VARSET or Error")			return nil		endendpStr = {}function poolString(str)	if type(str) ~= "table" then return tostring(str) end	for k,v in pairs(pStr) do		if v==tostring(str) then			return "$"..tostring(k)		end	end	table.insert(pStr,tostring(str))	return "$"..tostring(#pStr)end--TestmPB = PBLock:New()--[[local x,y = 76,65mBlock = {	{"VARSET","X",x},	{"VARSET","Y",y},	{"VARSET","Z",		{"ADD",			{"VARGET","X"},			{"VARGET","Y"},		}	},	{"VARSET","TEST",	{"DIV",		{"VARGET","Z"},		{"SUB",			{"VARGET","X"},			{"VARGET","Y"},		}	},	},	{"CALL","sqrt",		{"VARGET","TEST"}	},}]]--[[--Hur hur gets PimBlock = {	{"VARSET","PI",		{"GET","pi"}	},	{"VARGET","PI"}}]]--[[local x = 81mBlock = {	{"VARSET","X",x},	{"CALL","sqrt",		{"VARGET","X"},	},}]]local x,y = 2,5mBlock = {	{"VARSET","X",x},	{"VARSET","Y",y},	{"VARSET","Z",		{"ADD",			{"VARGET","X"},			{"VARGET","Y"},		}	},	{"VARSET","A",		{"CALL","sqrt",			{"VARGET","Z"},		},	},	{"VARSET","B",		{"EXP",			{"VARGET","A"},			2,		}	},	{"SUB",		{"VARGET","B"},		{"VARGET","Y"},	},}mPB:SetBlock(mBlock)print(mPB:Run())