Talk:Switchy
Interpreter
I have created a Switchy interpreter in ruby:
stack = []
labels = {}
flip = false
counter = 0
print "#{Dir.pwd} filepath> "
program = File.open(gets.delete("\n"),"r").read.split(//)
while counter < program.count
if program[counter] == "("
lbl = ""
counter+=1
while program[counter] != ")"
lbl+=program[counter]
counter += 1
end
labels[lbl] = counter
end
counter+=1
end
counter = 0
while counter < program.count
case program[counter]
when '#'
stack << (flip ? stack.pop - stack.pop : stack.pop + stack.pop)
when '*'
(flip ? stack.pop : stack.push(1))
when "'"
(flip ? (print stack[stack.count-1].chr) : stack << gets.delete("\n")[0])
when "["
lbl = ""
counter+=1
while program[counter] != "]"
lbl+=program[counter]
counter += 1
end
counter = labels[lbl]
end
((program[counter]=~/[\#|\*|\']/)==0 ? begin flip=!flip;stack.reverse! end : flip=flip)
counter+=1
end
I think it works, but I have only tested the cat program with it. OriginalOldMan (talk) 17:42, 25 October 2013 (UTC)
Actually, I don't believe the cat program listed on the Switchy page is correct due to the fact that the stack is reversed every cycle. OriginalOldMan (talk) 22:19, 25 October 2013 (UTC)
Dang, you're right. I'll make a fixed cat program. Poolala (talk) 01:42, 26 October 2013 (UTC)
You could add a no-op to make a cat program possible, or there could be a 'Switchy++' with a no-op (of course the no-op would have to trigger a clock cycle). OriginalOldMan (talk) 02:53, 26 October 2013 (UTC)
The reason I made this language was so every program would be difficult to write, and a noop would make it too easy IMO. Poolala (talk) 03:58, 26 October 2013 (UTC)
True. OriginalOldMan (talk) 04:22, 26 October 2013 (UTC)
Does Switchy allow you to jump to labels further forward in the program than the current position? If so, I will need to change the interpreter. OriginalOldMan (talk) 05:56, 26 October 2013 (UTC)
Yes. Poolala (talk) 06:31, 26 October 2013 (UTC)
Compuational Class
It's not Turing complete. You can't get more than one item on the stack at a time.
-A program can't start with an addition(# with flip=0) because the stack starts with 0 items.
-Therefor it must start with a push 1(* with flip=0) or input(' with flip=0) (or a jump that leads to one of these two things).
-The stack now must either have a single 1 or the first input, and flip=1.
-The next action can't be a subtraction(# with flip=1) because the stack only has 1 item.
-Therefor it must either pop(* with flip=1) or output(' with flip=1) (or a jump that leads to one of these two things)
-Both of these pop from the stack, so our stack of either 1 or the first input is now empty again.
-The cycle keeps repeating until the code ends
JeffryThunderStrike (talk) 19:35, 24 October 2017 (UTC)
Be bold in editing pages --StellatedHexahedron (talk) 15:27, 25 October 2017 (UTC)