-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathbf.cr
More file actions
95 lines (80 loc) · 1.47 KB
/
bf.cr
File metadata and controls
95 lines (80 loc) · 1.47 KB
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
require "socket"
module Op
record Inc, val : Int32
record Move, val : Int32
record Print
alias T = Inc | Move | Print | Array(Op::T)
end
class Tape
def initialize
@tape = [0]
@pos = 0
end
def get
@tape[@pos]
end
def inc(x)
@tape[@pos] += x
end
def move(x)
@pos += x
while @pos >= @tape.size
@tape << 0
end
end
end
class Program
@ops : Array(Op::T)
def initialize(code : String)
@ops = parse(code.each_char)
end
def run
_run @ops, Tape.new
end
private def _run(program, tape)
program.each do |op|
case op
when Op::Inc
tape.inc(op.val)
when Op::Move
tape.move(op.val)
when Array(Op::T)
while tape.get > 0
_run(op, tape)
end
when Op::Print
print(tape.get.chr)
end
end
end
private def parse(iterator)
res = [] of Op::T
iterator.each do |c|
op = case c
when '+'; Op::Inc.new(1)
when '-'; Op::Inc.new(-1)
when '>'; Op::Move.new(1)
when '<'; Op::Move.new(-1)
when '.'; Op::Print.new
when '['; parse(iterator)
when ']'; break
end
res << op if op
end
res
end
end
def notify(msg)
begin
TCPSocket.open("localhost", 9001) { |s|
s.puts msg
}
rescue
# standalone usage
end
end
text = File.read(ARGV[0])
pid = Process.pid
notify("Crystal\t#{pid}")
Program.new(text).run
notify("stop")