Skip to content

Commit

Permalink
Refactor, add comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
LavirtheWhiolet committed Dec 9, 2014
1 parent ff6669a commit c912578
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 91 deletions.
2 changes: 1 addition & 1 deletion Ruby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ To get some help:

### VERSION

It is compiled from [petooh-gpl](https://github.com/LavirtheWhiolet/petooh-gpl), commit [81ec5e52461f98d68f3bef310b4cecc8e5b24f77 on Wed Dec 10 00:24:27 2014 +0500](https://github.com/LavirtheWhiolet/petooh-gpl/commit/81ec5e52461f98d68f3bef310b4cecc8e5b24f77).
It is compiled from [petooh-gpl](https://github.com/LavirtheWhiolet/petooh-gpl), commit [384cd996a2c7246d2a9905b3e9f24c5eb941539d on Wed Dec 10 02:02:00 2014 +0500](https://github.com/LavirtheWhiolet/petooh-gpl/commit/384cd996a2c7246d2a9905b3e9f24c5eb941539d).
216 changes: 126 additions & 90 deletions Ruby/petooh.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,115 @@
#!/usr/bin/env ruby
# encoding: UTF-8

module PETOOH

#
# +program+ is an Array of PETOOH keywords (in the form of String-s).
#
# +debug+ - if true then PETOOH VM state is dumped on each execution step.
#
def run(program, debug = false)
# Init virtual machine.
mem = [0] * 16
data_ptr = 0
pc = 0 # Index of current instruction in program.
# Run!
while pc < program.size
# Log (if needed).
if debug
STDERR.puts "MEM: #{mem.join(" ")}"
STDERR.puts "PC: #{pc} (#{program[pc]})"
STDERR.puts "DP: #{data_ptr} (#{mem[data_ptr]})"
end
# Analyze current program instruction.
case program[pc]
when "Kudah"
#
data_ptr += 1
# Expand memory if needed.
if data_ptr >= mem.size
mem.concat([0] * 16)
end
#
pc += 1
when "kudah"
#
data_ptr -= 1
# Convert the machine's state from the form of
# { data_ptr = -1, mem = { -1:x, 0:y, 1:z, … } }
# to the form of
# { data_ptr = 0, mem = { 0:x, 1:y, 2:z, … } }
if data_ptr < 0
mem.unshift(0)
data_ptr = 0
end
#
pc += 1
when "Ko"
mem[data_ptr] += 1
pc += 1
when "kO"
mem[data_ptr] -= 1
pc += 1
when "Kukarek"
print mem[data_ptr].chr
pc += 1
when "Kud"
if mem[data_ptr] == 0
# Increment PC until corresponding "kud" is encountered
# (taking nested "Kud"/"kud" into account).
nest_level = 1
pc += 1
while pc < program.size and nest_level > 0
case program[pc]
when "Kud"
nest_level += 1
when "kud"
nest_level -= 1
end
pc += 1
end
else
# Just skip the instruction.
pc += 1
end
when "kud"
if mem[data_ptr] != 0
# Decrement PC until corresponding "Kud" is encountered
# (taking nested "Kud"/"kud" into account).
nest_level = 1
pc -= 1
while pc > 0 and nest_level > 0
case program[pc]
when "Kud"
nest_level -= 1
when "kud"
nest_level += 1
end
pc -= 1
end
# Position PC right to the corresponding "Kud".
pc += 1
else
# Just skip the instruction.
pc += 1
end
end
end
end

#
# reads PETOOH program from +io+ and parses it into Array of PETOOH
# keywords (in the form of String-s).
#
def parse(io)
yy_parse(io)
end

private

# Generated by Generator of Parsers of Lavir AKA self-bootstrap.



#
Expand Down Expand Up @@ -254,100 +363,27 @@ def yy_nontermh(yy_context)
val = :yy_nil
yy_char(yy_context) and yy_to_pcv(val)
end

module PETOOH

class VM

def initialize(program)
end

end

end

# End of generated code.

def run(program)
# Init.
mem = [0] * 16
data_ptr = 0
pc = 0
# Run!
while pc < program.size
if $debug
STDERR.puts "MEM: #{mem.join(" ")}"
STDERR.puts "PC: #{pc} (#{program[pc]})"
STDERR.puts "DP: #{data_ptr} (#{mem[data_ptr]})"
end
case program[pc]
when "Kudah"
data_ptr += 1
if data_ptr >= mem.size
mem.concat([0] * 16)
end
pc += 1
when "kudah"
data_ptr -= 1
if data_ptr < 0
mem.unshift(0)
data_ptr = 0
end
pc += 1
when "Ko"
mem[data_ptr] += 1
pc += 1
when "kO"
mem[data_ptr] -= 1
pc += 1
when "Kukarek"
print mem[data_ptr].chr
pc += 1
when "Kud"
if mem[data_ptr] == 0
nest_level = 1
pc += 1
while pc < program.size and nest_level > 0
case program[pc]
when "Kud"
nest_level += 1
when "kud"
nest_level -= 1
end
pc += 1
end
else
pc += 1
end
when "kud"
if mem[data_ptr] != 0
nest_level = 1
pc -= 1
while pc > 0 and nest_level > 0
case program[pc]
when "Kud"
nest_level -= 1
when "kud"
nest_level += 1
end
pc -= 1
end
pc += 1
else
pc += 1
end
end
end
end

$debug = false

case ARGV
when [], ["-h"], ["--help"]
puts <<-HELP
# Main.
if $0 == __FILE__
# Parse args.
case ARGV
when [], ["-h"], ["--help"]
puts <<-HELP
Usage: ruby #{File.basename(__FILE__)} file
HELP
else
File.open(ARGV[0]) do |io|
run(yy_parse(io))
HELP
exit
end
file = ARGV[0]
# Run!
include PETOOH
File.open(file) do |io|
program = parse(io)
run program
end
end

0 comments on commit c912578

Please sign in to comment.