-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.rb
209 lines (185 loc) · 5.07 KB
/
backend.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
=begin
idris *.idr -o out.qb --codegen qb --cg-opt "--javaName" --cg-opt "--symemu"
requirements:
- Ruby 2.7
=end
require 'stringio'
require 'optparse'
def literal_map(kind, x)
case kind
when 'float'
x.to_f
when "int", "bigInt"
x.to_i
when "char", "string"
x
when "bool"
x == "1" ? true : false
when "unit"
nil
when "symbol"
x.to_sym
else
raise TypeError::new
end
end
def code_list_to_string(prefix, io, xs)
if xs.is_a? Array
next_prefix = prefix + " "
xs.each {|x|
code_list_to_string(next_prefix, io, x)
}
io.write("\n") unless xs.empty?
return
else
raise Exception::new(xs) unless xs.is_a? String
io.write prefix
io.write(xs)
io.write("\n")
end
end
class Generate
def ExternalCall(name, args)
args = args.join(',')
return "$__RTS.#{name}(#{args})"
end
def ExternalVar(name)
return "$__RTS.#{name}"
end
def Var(name)
return name
end
def Call(name, args)
args = args.join(',')
return "#{name}(#{args})"
end
def Defun(name, args, body)
args = args.join(',')
return ["def #{name}(#{args})", [].concat(*body), "end"]
end
def Introduction(n)
return ["#{n} = nil"]
end
def Update(n, exp)
raise Exception::new("error") unless exp.is_a? String
return ["#{n} = #{exp}"]
end
def Constant(n)
return n.inspect
end
def Switch(var, xs, body)
ret = []
i = 0
xs.each do |cc, stmts|
head = i == 0 ? "if" : "elsif"
ret.push "#{head} #{var} == #{cc}"
raise Exception::new("error") unless stmts.is_a? Array
ret.push [].concat(*stmts)
i += 1
end
ret.push "else"
raise Exception::new("error") unless body.is_a? Array
ret.push [].concat(*body)
ret.push("end")
return ret
end
def If(cond, t, e)
raise Exception::new("error") unless cond.is_a? String
raise Exception::new("error") unless t.is_a? Array
raise Exception::new("error") unless e.is_a? Array
return ["if #{cond}", [].concat(*t), "else", [].concat(*e), "end"]
end
def EffectExpr(exp)
raise Exception::new("error") unless exp.is_a? String
return [exp]
end
def Return(exp)
return ["return #{exp}"]
end
end
def read_and_gen(io)
generate = Generate::new
ctor_stack = []
obj_stack = []
left_stack = []
left = 1
loop do
while left > 0
line = io.readline(newline: '\n')
pats = line.split
if pats.empty?
next
end
dispatch = pats[0]
left = left - 1
case dispatch
when "constructor"
cons = pats[1]
n = pats[2].to_i
left_stack.push left
left = n
ctor_stack.push [(generate.method cons), n]
when "literal"
kind = pats[1]
length = pats[2].to_i
buf = io.read(length)
io.readline(newline: '\n')
obj_stack.push literal_map(kind, buf)
when "list"
n = pats[1].to_i
left_stack.push left
left = n
ctor_stack.push [nil, n]
else
raise Exception::new("malformed qb format")
end
end
return obj_stack[0] if ctor_stack.empty?
ctor, n = ctor_stack.pop
args = []
n.times {
args.push obj_stack.pop
}
args.reverse!
v = if ctor == nil
args
else
ctor.(*args)
end
obj_stack.push v
left = left_stack.pop
end
end
=begin
=end
def main(filename, out)
File.open(filename) do |file|
code_secs = [].concat(*read_and_gen(file))
if out.downcase == 'std'
STDOUT.write("require_relative 'rts'\n")
STDOUT.write("$__RTS = RTS::new\n")
code_secs.each do |code_sec|
code_list_to_string('', STDOUT, code_sec)
end
else
File.open(out, "w") do |wfile|
wfile.write("require_relative 'rts'\n")
wfile.write("$__RTS = RTS::new\n")
code_secs.each do |code_sec|
code_list_to_string('', wfile, code_sec)
end
end
end
end
end
options = {}
OptionParser.new do |opts|
opts.banner = "QB to Ruby Compiler"
opts.on("f FILENAME", "--filename=FILENAME", "input filename") do |v|
options[:filename] = v
end
opts.on("-o OUT", "--out=OUT", "output") do |v|
options[:out] = v
end
end.parse!
main(options[:filename], options[:out])