| 285 | } |
| 286 | |
| 287 | void EmitEpilogue(Parser *parser) { |
| 288 | if (parser->init_count == 0 && init_list->len() == 0 && class_vars->count() == 0) |
| 289 | return; |
| 290 | Str *modulename = parser->source->modulename; |
| 291 | TokenList *output = parser->output; |
| 292 | for (Int i = 0; i < output->len(); i++) { |
| 293 | Token &token = output->at(i); |
| 294 | if (token.sym == SymIdent && class_vars->contains(token.str)) { |
| 295 | Int j = i-1; |
| 296 | while (j >= 0 && TEST(SymsWS | BIT(SymAst), output->at(j).sym)) |
| 297 | j--; |
| 298 | if (j < 0 || !type_prefix_set->contains(output->at(j).str)) |
| 299 | token.str = class_vars->at(token.str); |
| 300 | } |
| 301 | } |
| 302 | if (parser->init_count == 0 && init_list->len() == 0) |
| 303 | return; |
| 304 | Str *init_part; |
| 305 | if (parser->c_source()) { |
| 306 | init_part = S("\n" |
| 307 | "void pSingular_init_var(const void *s, const void *t, long n);\n" |
| 308 | "void *pSingular_alloc_var(long n);\n" |
| 309 | "void pSingular_register_init(void (*f)());\n" |
| 310 | "static void pSingular_mod_init() {\n" |
| 311 | ); |
| 312 | |
| 313 | } else { |
| 314 | init_part = S("\n" |
| 315 | "extern \"C\" {\n" |
| 316 | "void pSingular_init_var(const void *s, const void *t, long n);\n" |
| 317 | "void *pSingular_alloc_var(long n);\n" |
| 318 | "void pSingular_register_init(void (*f)());\n" |
| 319 | "}\n" |
| 320 | "typedef struct {\n" |
| 321 | " void *target; void *source; long size;\n" |
| 322 | "} pSingular_var_desc;\n" |
| 323 | "static pSingular_var_desc pSingular_var_descs[%n];\n" |
| 324 | "static void pSingular_register_init_var(void *t, void *s, long n) {\n" |
| 325 | " pSingular_var_desc * p = pSingular_var_descs;\n" |
| 326 | " while (p->target) p++;\n" |
| 327 | " p->target = t; p->source = s; p->size = n;\n" |
| 328 | "}\n" |
| 329 | "static void pSingular_mod_init() {\n" |
| 330 | ); |
| 331 | } |
| 332 | for (Int i = 0; i < init_list->len(); i++) { |
| 333 | Str *var_name = init_list->at(i); |
| 334 | if (class_vars->contains(var_name)) { |
| 335 | Str *type = class_types->at(var_name); |
| 336 | if (namespaced->contains(var_name)) { |
| 337 | type = namespaced->at(var_name)->clone()->add("::")->add(type); |
| 338 | var_name = namespaced->at(var_name)->clone()->add("::")->add(var_name); |
| 339 | } |
| 340 | init_part->add(S( |
| 341 | " %s = (%c *)pSingular_alloc_var((long)sizeof(%c));\n" |
| 342 | " pSingular_init_var(%s, &%s__INIT__, (long) sizeof(%s));\n" |
| 343 | )->replace_all(S("%c"), type)->replace_all(S("%s"), var_name)); |
| 344 | } else { |