-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinterpreter.rb
164 lines (150 loc) · 4.14 KB
/
interpreter.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require "rugged"
class String
def myshellsplit
return self.scan /(?: # MATCH EITHER
" # a string literal, which begins with "
(?: # and contains
\\. # any escaped character,
| # or
[^"\\] # any character which is not " or \
)* # of which there can be any number.
" # Finally, string literals end with a ".
| # OR match a non-string instruction
\S+ # which are all composed of one or more
# non-whitespace characters.
)/x
end
end
class Stack
def initialize
@stack = []
end
def push value
if value.nil?
raise "Tried to push nil"
end
@stack.push value
end
def pop
if @stack.empty?
0
else
@stack.pop
end
end
end
class Tape
def initialize
@tape = Array.new(10000, 0)
@position = @tape.size/2
end
def write value
if value.nil?
raise "Tried to write nil"
end
@tape[@position] = value
end
def read
@tape[@position]
end
def left i
@position -= i
end
def right i
@position += i
end
end
class LegitInterpreter
def initialize path
@repo = Rugged::Repository.new(path)
@current = @repo.branches["master"].target
@stack = Stack.new
@tape = Tape.new
@debug = ENV["LEGIT_DEBUG"] == "1"
@did_jump = false
end
def run
loop do
@did_jump = false
@current.message.split("\n").first.myshellsplit.each do |command|
execute command
if @debug
p @stack
p @tape
end
end
unless @did_jump
case @current.parents.size
when 0
exit
when 1
@current = @current.parents.first
else
v = @stack.pop
v = 999 if v < 0 # FIXME
@current = @current.parents[[v, @current.parents.size-1].min]
end
end
end
end
def execute command
if @debug
hash = @current.oid[0..8]
puts "Executing #{command} (#{hash})..."
end
case command
when "get"
c = STDIN.getc
@stack.push c.nil? ? 0 : c.ord
when "put"
c = (@stack.pop % 256).chr
STDOUT.write c
when "dup"
v = @stack.pop
@stack.push v
@stack.push v
when "pop"
@stack.pop
when "add"
a = @stack.pop
b = @stack.pop
@stack.push b+a
when "sub"
a = @stack.pop
b = @stack.pop
@stack.push b-a
when "cmp"
a = @stack.pop
b = @stack.pop
@stack.push b>a ? 1 : 0
when "read"
@stack.push @tape.read
when "write"
@tape.write @stack.pop
when "left"
v = @stack.pop
@tape.left v
when "right"
v = @stack.pop
@tape.right v
when "quit"
exit
when /^\d+$/
@stack.push command.to_i
when /^\[.*\]$/
tag_name = command[1..-2]
tag = @repo.tags[tag_name]
raise "Could not jump to tag '"+tagname+"'" unless tag
@current = tag.target
@did_jump = true
when /^".*"$/
command.undump.split("").each do |c|
@stack.push c.ord
end
else
raise "Unknown command '"+command+"'"
end
end
end
i = LegitInterpreter.new(ARGV.first)
i.run