thread:pause() ?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: thread:pause() ?

Post by Luke100000 »

How I can use string.gsub, so that only end/else/elseif will be taken, where a " " or a new line is after and before.
e.g.:

Code: Select all

variable_with_an_end = 5
will be edited into

Code: Select all

variable_with_an_ wait() end = 5
Also, this way I can't use return anywhere, I don't know if that's intended
Oh oh, I forgot returns....
It will crash if there is something after return, break too....
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: thread:pause() ?

Post by davisdude »

Code: Select all

local String = string.gsub( String, '^(.*)%s$', Function )
This will return if the string has a space at the end of it. Not sure how to make it execute the function, but probably involves loadstring
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: thread:pause() ?

Post by Robin »

Luke100000 wrote:How I can use string.gsub, so that only end/else/elseif will be taken, where a " " or a new line is after and before.
Something like:

Code: Select all

s = s:gsub("%send%s", "wait() %0"):gsub("%selse%s", "wait() %0"):gsub("%selseif%s", "wait() %0")
(Shame Lua doesn't support regexes out of the box.)

... of course I can exploit that with something like

Code: Select all

if true then end('yolo'):byte()
So better would be:

Code: Select all

s = s:gsub("([^_%w])(end[^_%w])", "%1 wait() %2"):gsub("([^_%w])(else[^_%w])", "%1 wait() %2"):gsub("([^_%w])(elseif[^_%w])", "%1 wait() %2")
I don't know how to exploit that right now. :P
Help us help you: attach a .love.
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: thread:pause() ?

Post by Luke100000 »

Level Up! :awesome:

+replace no else/end/elseif in strings any more
+only remove \n and tabs in code, not in strings

should work now.

Code: Select all

local lastTime = false
local timewhenstarted = love.timer.getTime()
local time = 0 --count the time it takes, resets after reaching sleepAfter
local checkTime = 5 --when it should stop the function

local CPUspeed = 20 --MHz, the virtual-computer
local realCPUspeed = 3 * 3200 --MHz, my computer

local sleepAfter = CPUspeed / realCPUspeed

function wait()

	local microtime = love.timer.getTime()
	if lastTime then
		time = time + ( microtime - lastTime )
		if time > sleepAfter then
			love.timer.sleep( sleepAfter * ( realCPUspeed / CPUspeed - 1 ) )
			microtime = microtime + sleepAfter * ( realCPUspeed / CPUspeed - 1 )
			time = time - sleepAfter
		end
	end
	
	lastTime = microtime
	
	if microtime - timewhenstarted > checkTime then
		if counter then
			print( "loops per second: " .. math.floor( counter / checkTime ) .. "\n" )
		else
			print("No counter added...\n")
		end
		os.exit()
	end
	
end

function transformCode(code)
	local time = love.timer.getTime()
	local functname = "wait()"
	local commandsToCheck = {"end", "else", "elseif"}
	local string = code
	
	local parts = { }
	local lastLetter = 1
	local i = 1
	while true do
		if i > #string then
			parts[#parts+1] = {text = string:sub(lastLetter, #string), text_type = "code"}
			break
		end
		if string:sub(i, i) == '"' then
			parts[#parts+1] = {text = string:sub(lastLetter, i-1), text_type = "code"}
			parts[#parts+1] = {text = string:sub(i, string:find('"', i+1)), text_type = "string"}
			lastLetter = string:find('"', i+1) + 1
			i = lastLetter
		elseif string:sub(i, i) == "'" then
			parts[#parts+1] = {text = string:sub(lastLetter, i-1), text_type = "code"}
			parts[#parts+1] = {text = string:sub(i, string:find("'", i+1)), text_type = "string"}
			lastLetter = string:find("'", i+1) + 1
			i = lastLetter
		else
			i = i + 1
		end
	end
	
	for d,s in ipairs(parts) do
		if s.text_type == "code" then
			
			--remove all \n and tabs
			s.text = s.text:gsub("\n", " ")
			s.text = s.text:gsub("\t", " ")
			
			--remove all double spaces
			while true do
				if not s.text:find("  ") then
					break
				end
				s.text = s.text:gsub("  ", " ")
			end
			
			--removes all spaces between . and a letter
			s.text = s.text:gsub("%. ", ".")
			
			--finally: add wait()
			for i,v in ipairs(commandsToCheck) do
				s.text = s.text:gsub("([^_%w])(" .. v .. "[^_%w])", "%1 wait() %2")
			end
			
		end
	end
	
	string = ""
	for d,s in ipairs(parts) do
		string = string .. s.text
	end
	
	return string, math.floor( ( love.timer.getTime() - time ) * 1000 * 1000 ) / 1000
end

local original_code = "b = 'Dont take this elseif!'\na = 0\nc = { }\nc.end = 0\nc. else = 7\ntestwithendword = '56'\nwhile true do\n    a = a + 1\nend"
--local original_code = "counter = 0\nwhile true do\n	counter = counter + 1\nend"

local code, time = transformCode( original_code .. " " )

print("")
print("#ORIGINALCODE:")
print(original_code)
print("")
print("#CODE:")
print(code)
print("")

print("########################")
print("Need " .. time .. " ms to transform code")
print("Virtual computerspeed: " .. CPUspeed .. "MHz (" .. math.floor(CPUspeed/realCPUspeed*100) .. "% of real speed")
print("Running code for " .. checkTime .. " seconds")
print("########################")
print("")

--old printing methode
--print("\nORIGINALCODE:\n" .. original_code .. "\n\nCODE:\n" .. code .. "\n\nNeed " .. time .. " ms to transform code\n")

local func, msg = loadstring( code )

if func then
	ok, msg = pcall(func)
	if msg then
		print("ERROR: " .. msg)
	end
else
	print("ERROR: " .. msg)
end

print("")
os.exit()
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: thread:pause() ?

Post by Robin »

Three problems with that:

1: comments

Code: Select all

-- whoops all code that follows is now a comment!
2: comments

Code: Select all

while something() do
 something_else()
--[[ " ]]
end
--[[ " ]]
3: multiline strings

Code: Select all

s = [[this else end gets replaced]]
EDIT: at this point, I think you should just parse the code. :)
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: aikusuuta and 9 guests