-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoutput.c
259 lines (232 loc) · 7.51 KB
/
output.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
#include "output.h"
#include "class.h"
#include "debug.h"
#include "disassembly.h"
static void write_variable(FILE *fd, struct variable_definition *variable){
if (variable->read_access || variable->write_access){
if (variable->read_access == variable->write_access){
fprintf(fd, "%s ", variable->read_access);
}else{
if (variable->read_access)
fprintf(fd, "%sread ", variable->read_access);
if (variable->write_access)
fprintf(fd, "%swrite ", variable->write_access);
}
}
if (variable->constant)
fprintf(fd, "constant ");
if (variable->indirect)
fprintf(fd, "indirect ");
fprintf(fd, "%s %s", variable->type, variable->name);
if (variable->dimensions)
fprintf(fd, "%s", variable->dimensions);
if (variable->value_count && variable->initial_values){
fputs(" = ", fd);
if (variable->indirect || variable->dimensions || variable->value_count>1)
fputs("{", fd);
unsigned i;
for(i=0;i<variable->value_count;i++){
if (i>0)
fputs(", ", fd);
if (variable->initial_values[i])
fputs(variable->initial_values[i], fd);
}
if (variable->indirect || variable->dimensions || variable->value_count>1)
fputs("}", fd);
}
fprintf(fd, "\n");
}
static void write_variables(FILE *fd, int user_defined, const char *type, struct variable_definition *variable[]){
if (!variable)
return;
int found = 0;
unsigned i;
for (i=0;variable[i];i++){
if (variable[i]->user_defined == user_defined){
if (!found){
found = 1;
if (type)
fprintf(fd, "%s variables\n", type);
}
write_variable(fd, variable[i]);
}
}
if (found && type)
fprintf(fd, "end variables\n\n");
}
static int write_type_dec(FILE *fd, struct type_definition *type_def){
if (type_def->type != enum_type && type_def->type != class_type)
return 0;
// global?
if (type_def->system)
fprintf(fd, "system ");
fprintf(fd, "type %s", type_def->name);
if (type_def->type == class_type){
struct class_definition *class_def = type_def->class_definition;
if (class_def->ancestor)
fprintf(fd, " from %s", class_def->ancestor);
if (class_def->parent)
fprintf(fd, " within %s", class_def->parent);
if (class_def->autoinstantiate)
fprintf(fd, " autoinstantiate");
}else{
fprintf(fd, " enumerated");
}
fprintf(fd, "\n");
return 1;
}
static void write_forward(FILE *fd, struct class_group *group){
fprintf(fd, "forward\n");
// TODO this probably isn't quite right for nested classes, eg menu's
unsigned i;
for (i=0;i<group->type_count;i++){
if (!write_type_dec(fd, &group->types[i]))
continue;
if (group->types[i].type == enum_type){
struct enumeration *enum_def = group->types[i].enum_definition;
unsigned j;
for (j=0;j<enum_def->value_count;j++)
fprintf(fd, "%s! = %u\n", enum_def->values[j].name, enum_def->values[j].value);
}
fprintf(fd, "end type\n");
}
write_variables(fd, 0, NULL, group->global_variables);
fprintf(fd, "end forward\n\n");
}
static void write_method_header(FILE *fd, struct script_definition *script){
if (script->hidden){
// not sure what the right syntax is for this undocumented flag
fprintf(fd, "/* Deprecated */ ");
}
if (script->event_type){
fprintf(fd, "event %s %s", script->name+1, script->event_type);
return;
}else if(script->event){
if (script->return_type)
fprintf(fd, "event type %s %s(", script->return_type, script->name);
else
fprintf(fd, "event %s(", script->name);
} else if (script->return_type){
fprintf(fd, "%s function %s %s(",
script->access?script->access:"public",
script->return_type,
script->name);
}else{
fprintf(fd, "%s subroutine %s(",
script->access?script->access:"public",
script->name);
}
unsigned i;
for(i=0; i<script->argument_count; i++){
if (i>0)
fprintf(fd, ", ");
struct argument_definition *arg = script->arguments[i];
if (arg->access)
fprintf(fd, "%s ", arg->access);
fprintf(fd, "%s", arg->type);
if (arg->name)
fprintf(fd, " %s", arg->name);
if (arg->dimensions)
fprintf(fd, "%s", arg->dimensions);
}
fprintf(fd,")");
for (i=0; i<script->throws_count; i++){
if (i==0)
fprintf(fd, " throws %s", script->throws[i]);
else
fprintf(fd, ", %s", script->throws[i]);
}
if (script->rpc)
fprintf(fd, " rpcfunc");
if (script->library){
if (script->system)
fprintf(fd, " system");
fprintf(fd, " library \"%s\"", script->library);
}
if (script->external_name)
fprintf(fd, " alias for \"%s\"", script->external_name);
}
static void write_prototypes(FILE *fd, int external, struct class_definition *class_def){
int found=0;
unsigned i;
for (i=0;class_def->scripts[i];i++){
struct script_definition *script = class_def->scripts[i];
if (!script->event && external == (script->library ? 1:0)){
if (!found){
fprintf(fd, "%s prototypes\n", external?"type":"forward");
found = 1;
}
write_method_header(fd, script);
fprintf(fd,"\n");
}
}
if (found)
fprintf(fd, "end prototypes\n\n");
}
static void write_script_body(FILE *fd, struct class_group *group, struct class_definition *class_def, struct script_definition *script){
write_method_header(fd, script);
fprintf(fd, ";");
// variable declarations, skipping arguments
write_variables(fd, 0, NULL, script->local_variables + script->argument_count);
struct disassembly *code = disassemble(group, class_def, script);
if (code){
dump_statements(fd, code);
//fprintf(fd, "/* Resource table;\n");
//dump_script_resources(fd, group, script);
//fprintf(fd, "\nInstructions;\n");
//dump_pcode(fd, code);
//fprintf(fd, "*/\n");
disassembly_free(code);
}else{
dump_raw_pcode(fd, script);
}
if (script->event)
fprintf(fd, "\nend event\n\n");
else if(script->return_type)
fprintf(fd, "\nend function\n\n");
else
fprintf(fd, "\nend subroutine\n\n");
}
static void write_class(FILE *fd, struct class_group *UNUSED(group), struct type_definition *type_def){
struct class_definition *class_def = type_def->class_definition;
write_type_dec(fd, type_def);
write_variables(fd, 0, NULL, class_def->instance_variables);
unsigned i;
for (i=0;class_def->scripts[i];i++){
if (class_def->scripts[i]->event && !class_def->scripts[i]->in_ancestor){
write_method_header(fd, class_def->scripts[i]);
fprintf(fd,"\n");
}
}
fprintf(fd, "end type\n\n");
// TODO global variable goes here?
write_prototypes(fd, 1, class_def);
write_variables(fd, 1, "type", class_def->instance_variables);
write_prototypes(fd, 0, class_def);
// first the functions
for (i=0;class_def->scripts[i];i++){
struct script_definition *script = class_def->scripts[i];
if (script->implemented && !script->event){
write_script_body(fd, group, class_def, script);
}
}
// then the events
for (i=0;class_def->scripts[i];i++){
struct script_definition *script = class_def->scripts[i];
if (script->implemented && script->event){
write_script_body(fd, group, class_def,script);
}
}
}
void write_group(FILE *fd, struct class_group *group){
write_forward(fd, group);
// TODO structure definitions first, not last
write_variables(fd, 1, "shared", group->global_variables);
// this works, but it's not exactly right...
write_variables(fd, 0, "global", group->global_variables);
unsigned i;
for (i=0;i<group->type_count;i++){
if (group->types[i].type == class_type)
write_class(fd, group, &group->types[i]);
}
}