class Lolcode::VM

Attributes

block_level[RW]
buffer[RW]
started[RW]
verbose[RW]

Public Class Methods

new(options={}) click to toggle source
# File lib/lolcode/vm.rb, line 9
def initialize(options={})
  reset_all
  $stdout.sync = true
  $stderr.sync = true
  self.verbose = options[:verbose] || false
end

Public Instance Methods

flush() click to toggle source

Flush the buffer

# File lib/lolcode/vm.rb, line 49
def flush
  run('')
end
run(line) click to toggle source
# File lib/lolcode/vm.rb, line 16
def run(line)
  puts "[INFO] Orig line: #{line}" if self.verbose

  (start; return) if line =~ /^HAI\b.*/    # start VM
  (halt; return) if line =~ /^KTHXBYE\b.*/ # halt VM
  return unless started?

  # TODO: comma in the string shouldn't be splited
  ruby_line = line.split(',').collect do |l|
    translate(l)
  end.join(';')

  puts "[INFO] Ruby code: #{ruby_line}" if self.verbose

  self.buffer << ruby_line
  return if open_block?

  res = nil
  begin
    puts "[INFO] Eval code: #{self.buffer}" if self.verbose
    res = eval self.buffer
    reset_buffer
  rescue
    puts "[ERROR] Exec Error: #{line}"
  end

  $stdout.flush
  $stderr.flush

  res
end
started?() click to toggle source
# File lib/lolcode/vm.rb, line 53
def started?
  self.started
end

Private Instance Methods

halt() click to toggle source
# File lib/lolcode/vm.rb, line 65
def halt
  reset_all
  puts "[INFO] Now LOLCODE VM is halted" if self.verbose
end
open_block?() click to toggle source
# File lib/lolcode/vm.rb, line 82
def open_block?
  self.block_level > 0
end
reset_all() click to toggle source
# File lib/lolcode/vm.rb, line 74
def reset_all
  instance_variables.each { |v| remove_instance_variable(v) }
  self.started = false
  self.block_level = 0
  reset_buffer
end
reset_buffer() click to toggle source
# File lib/lolcode/vm.rb, line 70
def reset_buffer
  self.buffer = ""
end
start() click to toggle source
# File lib/lolcode/vm.rb, line 60
def start
  self.started = true
  puts "[INFO] Now LOLCODE VM is started" if self.verbose
end
type_of(string) click to toggle source
# File lib/lolcode/vm.rb, line 86
def type_of string
  return :string if string =~ /^\"(.*)\"$/
  return :int if string =~ /^[0-9]+$/
  return :symbol if string =~ /^\w+$/
  return :untyped
end