-
Notifications
You must be signed in to change notification settings - Fork 11
/
ClassicInterpreter.rb
88 lines (70 loc) · 1.75 KB
/
ClassicInterpreter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'io/console'
require_relative './stack.rb'
require_relative './Flag.rb'
require_relative './BrainFlakError.rb'
require_relative './Interpreter.rb'
class ClassicInterpreter < Interpreter
def initialize(source, left_in, right_in, debug, max_cycles, ascii_mode)
super(source, left_in, right_in, debug, max_cycles)
@ascii_mode = ascii_mode
end
# Nilads ~~~~~~~~~~~~~~~~~~~~~
def round_nilad()
@current_value += 1
end
def square_nilad()
@current_value -= 1
end
def curly_nilad()
@current_value += @active_stack.pop
end
def angle_nilad()
@active_stack = @active_stack == @left ? @right : @left
end
# Open Braces ~~~~~~~~~~~~~~~~
def open_round()
@main_stack.push(['(', @current_value, @index])
@current_value = 0
end
def open_square()
@main_stack.push(['[', @current_value, @index])
@current_value = 0
end
def open_curly()
@main_stack.push(['{', 0, @index])
new_index = read_until_matching(@source, @index)
if active_stack.peek == 0 then
@main_stack.pop()
@index = new_index
end
end
def open_angle()
@main_stack.push(['<', @current_value, @index])
@current_value = 0
end
# Close Braces ~~~~~~~~~~~~~~~
def close_round()
data = main_stack.pop()
@active_stack.push(@current_value)
@current_value += data[1]
end
def close_square()
data = main_stack.pop()
if @ascii_mode
print (@current_value % 2 ** 32).chr(Encoding::UTF_8)
else
puts @current_value
end
@current_value += data[1]
end
def close_curly()
data = @main_stack.pop()
@index = data[2] - 1
@last_op = :close_curly
@current_value += data[1]
end
def close_angle()
data = main_stack.pop()
@current_value = data[1]
end
end