local Program = 
	[[ v
 >"!dlroW olleH">:v
               ^,_@
	]]

local Stack = {}
local Pointer = Vector2.new(1,1)
local PointerAngle = ">"
local Code = {}

local temp = ""
for i=1,string.len(Program) do
	if string.sub(Program,i,i) == "\n" then
		table.insert(Code,temp)
	else
		temp = temp .. string.sub(Program,i,i)
	end
end
temp = nil

function GetChar(Vec)
	print(Vec.Y..","..Vec.X)
	return string.sub(Code[Vec.Y],Vec.X,Vec.X)
end

function ChangeDir()
	if GetChar(Pointer) == ">" then
		PointerAngle = ">"
		print("going left")
	end
	if GetChar(Pointer) == "<" then
		PointerAngle = "<"
		print("going right")
	end
	if GetChar(Pointer) == "v" then
		PointerAngle = "v"
		print("going down")
	end
	if GetChar(Pointer) == "^" then
		PointerAngle = "^"
		print("going up")
	end
end

function Move()
	if PointerAngle == ">" then
		Pointer = Pointer + Vector2.new(1,0)
	elseif PointerAngle == "<" then
		Pointer = Pointer + Vector2.new(-1,0)
	elseif PointerAngle == "^" then
		Pointer = Pointer + Vector2.new(0,-1)
	elseif PointerAngle == "v" then
		Pointer = Pointer + Vector2.new(0,1)
	end
end

while true do
	print(GetChar(Pointer))
	if GetChar(Pointer) == "@" then
		break
	end
	if tonumber(GetChar(Pointer)) then
		table.insert(Stack, tonumber(GetChar(Pointer)))
	end
	if GetChar(Pointer) == ":" then
		table.insert(Stack, Stack[#Stack])
	end
	if GetChar(Pointer) == "_" then
		if Stack[#Stack] == 0 then
			PointerAngle = "<"
		else
			PointerAngle = ">"
		end
	end
	if GetChar(Pointer) == "|" then
		if Stack[#Stack] == 0 then
			PointerAngle = "v"
		else
			PointerAngle = "^"
		end
	end
	if GetChar(Pointer) == "." then
		print(Stack[#Stack])
		Stack[#Stack] = nil
	end
	if GetChar(Pointer) == "," then
		print(string.char(Stack[#Stack]))
		Stack[#Stack] = nil
	end
	if GetChar(Pointer) == "\\" then
		local temp = Stack[#Stack]
		Stack[#Stack] = Stack[#Stack-1]
		Stack[#Stack-1] = temp
		temp = nil
	end
	if GetChar(Pointer) == "$" then
		Stack[#Stack] = nil
	end
	if GetChar(Pointer) == "?" then
		local temp = {">","<","v","^"}
		PointerAngle = temp[math.random(1,4)]
	end
	if GetChar(Pointer) == "#" then
		Move()
	end
	if GetChar(Pointer) == "\"" then
		Move()
		while GetChar(Pointer) ~= "\"" do
			table.insert(Stack,string.byte(GetChar(Pointer)))
		end
	end
	if GetChar(Pointer) == "+" then
		local a = Stack[#Stack] + Stack[#Stack-1]
		for i = 1,2 do
			Stack[#Stack] = nil
		end
		Stack[#Stack] = a
	end
	if GetChar(Pointer) == "*" then
		local a = Stack[#Stack] * Stack[#Stack-1]
		for i = 1,2 do
			Stack[#Stack] = nil
		end
		Stack[#Stack] = a
	end
	if GetChar(Pointer) == "-" then
		local a = Stack[#Stack-1] - Stack[#Stack]
		for i = 1,2 do
			Stack[#Stack] = nil
		end
		Stack[#Stack] = a
	end
	if GetChar(Pointer) == "/" then
		local a = Stack[#Stack-1] - (Stack[#Stack-1] % Stack[#Stack])
		for i = 1,2 do
			Stack[#Stack] = nil
		end
		Stack[#Stack] = a
	end
	if GetChar(Pointer) == "%" then
		local a = Stack[#Stack-1] % Stack[#Stack]
		for i = 1,2 do
			Stack[#Stack] = nil
		end
		Stack[#Stack] = a
	end
	ChangeDir()
	
	Move()
	wait()
end

take this peice of shtuff:

 v
 >"!dlroW olleH">:v
                         ^,_@

there is a pointer that starts on the top right of the string, the v tells it to start going downwards, then a > to make it go left, then pushes some byte code of the string into a stack, but thats not important right now, the thing is i cant get the interpereter to make the pointer go the right way, like even when encountering the > it still goes down, and goes onto a line that does not exist, help? (the script is an interpereter for Befunge-93)

1 comment

0

Do not use string.len(str). Use #str.