Skip to content

Commit

Permalink
Add debug command
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ender committed Aug 29, 2015
1 parent 1972e0e commit 7cb42c7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -34,6 +34,7 @@ With the exception of `v` Labyrinth uses only non-letter commands. Spaces are re
### General

- `"` is no-op, but is not considered a wall. It is very useful in padding some paths of your maze layout.
- `'` is also a no-op, but can be turned into a debug command with the interpreter flag `-d`. If the flag is set, this command will print the current position, direction and the stacks to STDOUT.
- `@` is an exit of the maze: the program terminates when this command is executed.

### Arithmetic
Expand Down Expand Up @@ -86,8 +87,8 @@ The four trickiest commands are `<^>v`:

## Comments

Labyrinth doesn't have an dedicated comment syntax. However, spaces and all letters except lower-case `v` are considered walls, so you can use them freely around your code to add comments. Furthermore, you can use arbitrary characters (even recognised ones) as long as they are not reachable, e.g. by separating them from the actual program by a layer of walls. However, in this case be careful if you use the grid manipulation commands as they might bring your comments in contact with your actual program.
Labyrinth doesn't have a dedicated comment syntax. However, spaces and all letters except lower-case `v` are considered walls, so you can use them freely around your code to add comments. Furthermore, you can use arbitrary characters (even recognised ones) as long as they are not reachable, e.g. by separating them from the actual program by a layer of walls. However, in this case be careful if you use the grid manipulation commands as they might bring your comments in contact with your actual program.

## Interpreter features

The interpreter has a verbose debug mode which can be activated with the command-line flag `-d`.
The interpreter has a verbose debug mode (like an additional debug level beyond activating `'` commands) which can be switched on with the command-line flag `-D`. If this flag is set, the interpreter will print detailed diagnostic information after every tick of the program.
5 changes: 4 additions & 1 deletion examples/hello-world.lab
Expand Up @@ -6,7 +6,10 @@
4 4 ".{@
_ " {
1 }"
08_100_33"""""
08_100_33}'{""
} {
} {


Golfed versions:

Expand Down
51 changes: 33 additions & 18 deletions labyrinth.rb
Expand Up @@ -15,7 +15,7 @@ class ProgramError < Exception; end
'$' => [:bit_xor],
'%' => [:mod],
'&' => [:bit_and],
#'\'' => ,
'\'' => [:debug],
'(' => [:dec],
')' => [:inc],
'*' => [:mul],
Expand Down Expand Up @@ -56,12 +56,12 @@ class ProgramError < Exception; end

OPERATORS.default = [:wall]

def self.run(src, debug_flag=false)
new(src, debug_flag).run
def self.run(src, debug_level=0)
new(src, debug_level).run
end

def initialize(src, debug_flag=false)
@debug = debug_flag
def initialize(src, debug_level=false)
@debug_level = debug_level

@grid = parse(src)
@height = @grid.size
Expand All @@ -78,17 +78,17 @@ def initialize(src, debug_flag=false)

def run
loop do
puts "\nTick #{@tick}:" if @debug
p @ip if @debug
puts "\nTick #{@tick}:" if @debug_level > 1
p @ip if @debug_level > 1
cmd = cell @ip
p cmd if @debug
p cmd if @debug_level > 1
if cmd[0] == :terminate
break
end
process cmd
puts @main*' ' + ' | ' + @aux.reverse*' ' if @debug
puts @main*' ' + ' | ' + @aux.reverse*' ' if @debug_level > 1
@dir = get_new_dir
p @dir if @debug
p @dir if @debug_level > 1
@ip += @dir.vec

@tick += 1
Expand Down Expand Up @@ -266,7 +266,7 @@ def process cmd
end
end

@grid.each{|l| p l} if @debug
@grid.each{|l| p l} if @debug_level > 1
when :rotate_east
offset = pop_main
@grid[(y+offset) % @height].rotate!(-1)
Expand All @@ -278,7 +278,7 @@ def process cmd
end
end

@grid.each{|l| p l} if @debug
@grid.each{|l| p l} if @debug_level > 1
when :rotate_north
offset = pop_main
grid = @grid.transpose
Expand All @@ -292,7 +292,7 @@ def process cmd
end
end

@grid.each{|l| p l} if @debug
@grid.each{|l| p l} if @debug_level > 1
when :rotate_south
offset = pop_main
grid = @grid.transpose
Expand All @@ -306,13 +306,20 @@ def process cmd
end
end

@grid.each{|l| p l} if @debug
@grid.each{|l| p l} if @debug_level > 1

# Others
when :terminate
raise '[BUG] Received :terminate. This shouldn\'t happen.'
when :nop
# Nop(e)
when :debug
if @debug_level > 0
puts
puts "Position: #{@ip.pretty}"
puts "Direction: #{@dir.class.name}"
puts "Main [ #{@main*' '} | #{@aux.reverse*' '} ] Auxiliary"
end
end
end

Expand All @@ -325,7 +332,7 @@ def get_new_dir
neighbors << dir if cell(@ip + dir.vec)[0] != :wall
end

p neighbors if @debug
p neighbors if @debug_level > 1

case neighbors.size
when 0
Expand Down Expand Up @@ -392,9 +399,17 @@ def read_byte
end
end

debug_flag = ARGV[0] == "-d"
if debug_flag
case ARGV[0]
when "-d"
debug_level = 1
when "-D"
debug_level = 2
else
debug_level = 0
end

if debug_level > 0
ARGV.shift
end

Labyrinth.run(ARGF.read, debug_flag)
Labyrinth.run(ARGF.read, debug_level)
2 changes: 1 addition & 1 deletion point2d.rb
Expand Up @@ -26,6 +26,6 @@ def to_s
end

def pretty
"(% d,% d)" % [@x, @y]
"(%d,%d)" % [@x, @y]
end
end

0 comments on commit 7cb42c7

Please sign in to comment.