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.c
161 lines (147 loc) · 4.46 KB
/
codegen.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
/* codegen.c
Code generator
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 <assert.h>
#include "codegen.h"
#include "ast_transforms.h"
void strf(state_t*state, const char*format, ...)
{
char buf[1024];
int l;
va_list arglist;
va_start(arglist, format);
vsnprintf(buf, sizeof(buf)-1, format, arglist);
va_end(arglist);
char*s = buf;
while(*s) {
char*last = s;
while(*s && *s!='\n')
s++;
if(s>last) {
state->writer->write(state->writer, last, s-last);
}
if(*s=='\n') {
write_uint8(state->writer, '\n');
int t;
for(t=0;t<state->indent;t++) {
write_uint8(state->writer, ' ');
}
s++;
if(!*s) {
state->after_newline = 1;
return;
}
}
}
state->after_newline = 0;
}
void write_escaped_string(state_t*s, const char*p)
{
while(*p) {
switch(*p) {
case '\n':
write_uint8(s->writer, '\\');
write_uint8(s->writer, 'n');
break;
case '\r':
write_uint8(s->writer, '\\');
write_uint8(s->writer, 'r');
break;
case '\t':
write_uint8(s->writer, '\\');
write_uint8(s->writer, 't');
break;
case '"':
write_uint8(s->writer, '\\');
write_uint8(s->writer, '"');
break;
default:
if(*p>0x20 && *p<0x7f) {
write_uint8(s->writer, *p);
} else {
write_uint8(s->writer, '\\');
write_uint8(s->writer, 'x');
write_uint8(s->writer, "0123456789abcdef"[((*p)>>4)&15]);
write_uint8(s->writer, "0123456789abcdef"[((*p)&15)]);
}
break;
}
p++;
}
}
void write_node(state_t*s, node_t*n)
{
switch(node_get_opcode(n)) {
#define NODE(opcode, name) \
case opcode: \
s->codegen->write_##name(n, s); \
break;
LIST_NODES
#undef NODE
default:
assert(0);
}
}
void indent(state_t*s)
{
s->indent += 4;
if(s->after_newline) {
/* if we're on a new line, indent this line, too */
char*indent = " ";
s->writer->write(s->writer, indent, 4);
}
}
void dedent(state_t*s)
{
s->indent -= 4;
}
char*generate_code(codegen_t*codegen, model_t*m)
{
node_t*n = (node_t*)m->code;
state_t s;
s.model = m;
s.codegen = codegen;
s.indent = 0;
s.writer = growingmemwriter_new();
n = node_prepare_for_code_generation(n);
codegen->write_header(m, &s);
write_node(&s, n);
codegen->write_footer(m, &s);
write_uint8(s.writer, 0);
char*result = writer_growmemwrite_getmem(s.writer, 0);
s.writer->finish(s.writer);
return result;
}
codegen_t* codegen_default = &codegen_python;
char*model_generate_code(model_t*m, const char*language)
{
if(!language) {
return generate_code(codegen_default, m);
} else if(!strcmp(language,"python")) {
return generate_code(&codegen_python, m);
} else if(!strcmp(language,"c")) {
return generate_code(&codegen_c, m);
} else if(!strcmp(language,"c++")) {
return generate_code(&codegen_c, m);
} else if(!strcmp(language,"ruby")) {
return generate_code(&codegen_ruby, m);
} else if(!strcmp(language,"javascript")) {
return generate_code(&codegen_js, m);
} else if(!strcmp(language,"js")) {
return generate_code(&codegen_js, m);
} else {
return generate_code(codegen_default, m);
}
}