Class: Lolcode::VM

Inherits:
Object
  • Object
show all
Includes:
Translator
Defined in:
lib/lolcode/vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Translator

#translate

Constructor Details

#initialize(options = {}) ⇒ VM

Returns a new instance of VM.



9
10
11
12
13
14
# File 'lib/lolcode/vm.rb', line 9

def initialize(options={})
  reset_all
  $stdout.sync = true
  $stderr.sync = true
  self.verbose = options[:verbose] || false
end

Instance Attribute Details

#block_levelObject

Returns the value of attribute block_level.



7
8
9
# File 'lib/lolcode/vm.rb', line 7

def block_level
  @block_level
end

#bufferObject

Returns the value of attribute buffer.



7
8
9
# File 'lib/lolcode/vm.rb', line 7

def buffer
  @buffer
end

#startedObject

Returns the value of attribute started.



7
8
9
# File 'lib/lolcode/vm.rb', line 7

def started
  @started
end

#verboseObject

Returns the value of attribute verbose.



7
8
9
# File 'lib/lolcode/vm.rb', line 7

def verbose
  @verbose
end

Instance Method Details

#flushObject

Flush the buffer



49
50
51
# File 'lib/lolcode/vm.rb', line 49

def flush
  run('')
end

#run(line) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/lolcode/vm.rb', line 53

def started?
  self.started
end