This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
forked from matthiaskramm/mrscake
-
Notifications
You must be signed in to change notification settings - Fork 1
/
codegen_ruby.c
346 lines (336 loc) · 8.3 KB
/
codegen_ruby.c
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* codegen_ruby.c
Code generator for Ruby
Part of the data prediction package.
Copyright (c) 2011 Matthias Kramm <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include "codegen.h"
void ruby_write_node_block(node_t*n, state_t*s)
{
int t;
for(t=0;t<n->num_children;t++) {
if(t)
strf(s, "\n");
write_node(s, n->child[t]);
}
}
void ruby_write_node_if(node_t*n, state_t*s)
{
if(!node_terminates(n) && node_has_consumer_parent(n)) {
strf(s, "if ");
write_node(s, n->child[0]);
strf(s, " then ");
indent(s);write_node(s, n->child[1]);dedent(s);
if(!node_is_missing(n->child[2])) {
strf(s, " else ");
indent(s);write_node(s, n->child[2]);dedent(s);
}
strf(s, " end");
} else {
strf(s, "if ");
write_node(s, n->child[0]);
strf(s, " then\n");
indent(s);write_node(s, n->child[1]);dedent(s);
if(!node_is_missing(n->child[2])) {
strf(s, "\nelse\n");
indent(s);write_node(s, n->child[2]);dedent(s);
}
strf(s, "\nend");
}
}
void ruby_write_node_add(node_t*n, state_t*s)
{
int t;
for(t=0;t<n->num_children;t++) {
if(t && !node_has_minus_prefix(n->child[t])) strf(s, "+");
write_node(s, n->child[t]);
}
}
void ruby_write_node_sub(node_t*n, state_t*s)
{
int t;
for(t=0;t<n->num_children;t++) {
if(t && !node_has_minus_prefix(n->child[t])) strf(s, "-");
write_node(s, n->child[t]);
}
}
void ruby_write_node_mul(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, "*");
write_node(s, n->child[1]);
}
void ruby_write_node_div(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, "/");
write_node(s, n->child[1]);
}
void ruby_write_node_lt(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, "<");
write_node(s, n->child[1]);
}
void ruby_write_node_lte(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, "<=");
write_node(s, n->child[1]);
}
void ruby_write_node_gt(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, ">");
write_node(s, n->child[1]);
}
void ruby_write_node_gte(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, ">=");
write_node(s, n->child[1]);
}
void ruby_write_node_in(node_t*n, state_t*s)
{
strf(s, "!!");
write_node(s, n->child[1]);
strf(s, ".index(");
write_node(s, n->child[0]);
strf(s, ")");
}
void ruby_write_node_not(node_t*n, state_t*s)
{
strf(s, "!");
write_node(s, n->child[0]);
}
void ruby_write_node_neg(node_t*n, state_t*s)
{
strf(s, "-");
write_node(s, n->child[0]);
}
void ruby_write_node_exp(node_t*n, state_t*s)
{
strf(s, "Math.exp(");
write_node(s, n->child[0]);
strf(s, ")");
}
void ruby_write_node_sqr(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, "**2");
}
void ruby_write_node_abs(node_t*n, state_t*s)
{
strf(s, "(");
write_node(s, n->child[0]);
strf(s, ").abs");
}
void ruby_write_node_param(node_t*n, state_t*s)
{
if(s->model->sig->has_column_names) {
strf(s, "%s", s->model->sig->column_names[n->value.i]);
} else {
strf(s, "data[%d]", n->value.i);
}
}
void ruby_write_node_nop(node_t*n, state_t*s)
{
}
void ruby_write_constant(constant_t*c, state_t*s)
{
int t;
switch(c->type) {
case CONSTANT_FLOAT:
strf(s, "%f", c->f);
break;
case CONSTANT_INT:
case CONSTANT_CATEGORY:
strf(s, "%d", c->i);
break;
case CONSTANT_BOOL:
if(c->b)
strf(s, "true");
else
strf(s, "false");
case CONSTANT_STRING:
strf(s, ":");
strf(s, "%s", c->s);
break;
case CONSTANT_MISSING:
strf(s, "nil");
break;
case CONSTANT_MIXED_ARRAY:
case CONSTANT_INT_ARRAY:
case CONSTANT_FLOAT_ARRAY:
case CONSTANT_CATEGORY_ARRAY:
case CONSTANT_STRING_ARRAY:
strf(s, "[");
for(t=0;t<c->a->size;t++) {
if(t)
strf(s, ",");
ruby_write_constant(&c->a->entries[t], s);
}
strf(s, "]");
break;
}
}
void ruby_write_node_constant(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_bool(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_category(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_mixed_array(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_string_array(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_int_array(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_float_array(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_category_array(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_zero_int_array(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_float(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_int(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_string(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_missing(node_t*n, state_t*s)
{
ruby_write_constant(&n->value, s);
}
void ruby_write_node_getlocal(node_t*n, state_t*s)
{
strf(s, "v%d", n->value.i);
}
void ruby_write_node_setlocal(node_t*n, state_t*s)
{
strf(s, "v%d = ", n->value.i);
write_node(s, n->child[0]);
}
void ruby_write_node_inclocal(node_t*n, state_t*s)
{
strf(s, "v%d += 1", n->value.i);
}
void ruby_write_node_bool_to_float(node_t*n, state_t*s)
{
strf(s, "(");
write_node(s, n->child[0]);
strf(s, " ? 1.0 : 0.0");
strf(s, ")");
}
void ruby_write_node_equals(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, " == ");
write_node(s, n->child[1]);
}
void ruby_write_node_arg_max(node_t*n, state_t*s)
{
strf(s, "([");
int t;
for(t=0;t<n->num_children;t++) {
if(t) strf(s, ",");
write_node(s, n->child[t]);
}
strf(s, "].each.inject([]) {|i,n| [i,[n,i[1]+1]].max})[1]");
}
void ruby_write_node_arg_max_i(node_t*n, state_t*s)
{
ruby_write_node_arg_max(n, s);
}
void ruby_write_node_array_at_pos(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, "[");
write_node(s, n->child[1]);
strf(s, "]");
}
void ruby_write_node_array_at_pos_inc(node_t*n, state_t*s)
{
write_node(s, n->child[0]);
strf(s, "[");
write_node(s, n->child[1]);
strf(s, "]+=1");
}
void ruby_write_node_array_arg_max_i(node_t*n, state_t*s)
{
strf(s, "(");
write_node(s, n->child[0]);
strf(s, ".each.inject([]) {|i,n| [i,[n,i[1]+1]].max})[1]");
}
void ruby_write_node_return(node_t*n, state_t*s)
{
strf(s, "return ");
write_node(s, n->child[0]);
}
void ruby_write_node_brackets(node_t*n, state_t*s)
{
strf(s, "(");
write_node(s, n->child[0]);
strf(s, ")");
}
void ruby_write_header(model_t*model, state_t*s)
{
strf(s, "def predict(");
if(s->model->sig) {
int t;
node_t*root = (node_t*)model->code;
for(t=0;t<model->sig->num_inputs;t++) {
if(t) strf(s, ", ");
strf(s, "%s", s->model->sig->column_names[t]);
}
} else {
strf(s, "data");
}
strf(s, ")\n");
indent(s);
}
void ruby_write_footer(model_t*model, state_t*s)
{
dedent(s);
strf(s, "\nend\n");
}
codegen_t codegen_ruby = {
#define NODE(opcode, name) write_##name: ruby_write_##name,
LIST_NODES
#undef NODE
write_header: ruby_write_header,
write_footer: ruby_write_footer,
};