-
Notifications
You must be signed in to change notification settings - Fork 0
/
mywhitespace.rb
157 lines (141 loc) · 3.34 KB
/
mywhitespace.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
class Tokenizer
@@imps = {
" " => :stack,
"\t " => :arithmatic,
"\t\t" => :heap,
"\n" => :flow,
"\t\n" => :io
}
@@command = {
:stack => {
" " => :push,
"\n " => :dup,
"\n\t" => :swap,
"\n\n" => :discard
},
:arithmatic => {
" " => :add,
" \t" => :sub,
" \n" => :mul,
"\t " => :div,
"\t\t" => :mod,
},
:heap => {
" " => :store,
"\t" => :retrive,
},
:flow => {
" " => :label,
" \t" => :call,
" \n" => :jump,
"\t " => :jz,
"\t\t" => :jn,
"\t\n" => :ret,
"\n\n" => :exit,
},
:io => {
" " => :outchar,
" \t" =>:outnum ,
"\t " => :readchar,
"\t\t" => :readnum,
}
}
@@parameter = [:push, :label, :call, :jump, :jz, :jn]
attr_reader :tokens
def initialize
@tokens = []
@program = ARGF.read.tr("^ \t\n", "")
tokenize
# p @tokens
end
def tokenize
while @program != ""
#imp
if @program =~ /\A(#{@@imps.keys.join('|')})/
imp = @@imps[$1]
# p imp
@program.sub!(/\A(#{@@imps.keys.join('|')})/,"")
else
raise Exception, "undifined imp"
end
#command
if @program =~ /\A(#{@@command[imp].keys.join('|')})/
command = @@command[imp][$1]
# p command
@program.sub!(/\A(#{@@command[imp].keys.join('|')})/,"")
#parameter
if @@parameter.include?(command)
if $' =~ /\A([ \t]+)\n/
parameter = eval("0b#{$1.tr(" \t", '01')}")
@program.sub!(/\A([ \t]+)\n/,"")
else
raise Exception, "undifined parameters"
end
end
else
raise Exception, "undifined command"
end
@tokens << [imp,command,parameter]
end
end
end
class Executor
def initialize(tokens)
@tokens = tokens
end
def run
@pc = 0
@stack = []
@heap = {}
@call = []
loop do
imp, cmd, prm = @tokens[@pc]
@pc += 1
exit if @tokens.count < @pc
case cmd
when :push then @stack.push prm
when :dup then @stack.push @stack[-1]
when :swap then @stack[-1],@stack[-2] = @stack[-2], @stack[-1]
when :discard then @stack.pop
when :add then math('+')
when :sub then math('-')
when :mul then math('*')
when :div then math('/')
when :mod then math('%')
when :store
value = @stack.pop
address = @stack.pop
@heap[address] = value
when :retrive then @stack.push @heap[@stack.pop]
when :label
when :call
@call.push(@pc)
jump(prm)
when :jump then jump(prm)
when :jz then jump(prm) if @stack.pop == 0
when :jn then jump(prm) if @stack.pop < 0
when :ret then @pc = @call.pop
when :exit then exit
when :outchar then print @stack.pop.chr
when :outnum then print @stack.pop
when :readchar then @heap[@stack.pop] = $stdin.gets
when :randum then @heap[@stack.pop] = $sdin.gets.to_i
else raise Exception, 'Executor faild'
end
end
end
def math(op)
b = @stack.pop
a = @stack.pop
@stack.push eval("a #{op} b")
end
def jump(label)
@tokens.each_with_index do |token,i|
if token == [:flow, :label, label]
@pc = i
break
end
end
end
end
Executor.new(Tokenizer.new.tokens).run