aboutsummaryrefslogtreecommitdiff
path: root/compile.c
diff options
context:
space:
mode:
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c44
1 files changed, 2 insertions, 42 deletions
diff --git a/compile.c b/compile.c
index 651312c5..86799345 100644
--- a/compile.c
+++ b/compile.c
@@ -7,6 +7,7 @@
#include "ast.h"
#include "builtins/string.h"
#include "compile.h"
+#include "enums.h"
#include "structs.h"
#include "environment.h"
#include "typecheck.h"
@@ -771,48 +772,7 @@ CORD compile(env_t *env, ast_t *ast)
return CORD_EMPTY;
}
case EnumDef: {
- auto def = Match(ast, EnumDef);
- CORD_appendf(&env->code->typedefs, "typedef struct %s_s %s_t;\n", def->name, def->name);
- CORD enum_def = CORD_all("struct ", def->name, "_s {\n"
- "\tenum {");
- for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
- CORD_appendf(&enum_def, "$tag$%s$%s = %ld", def->name, tag->name, tag->value);
- if (tag->next) enum_def = CORD_cat(enum_def, ", ");
- }
- enum_def = CORD_cat(enum_def, "} $tag;\n"
- "union {\n");
- for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
- (void)compile(env, WrapAST(ast, StructDef, .name=heap_strf("%s$%s", def->name, tag->name), .fields=tag->fields));
- enum_def = CORD_all(enum_def, def->name, "$", tag->name, "_t ", tag->name, ";\n");
- CORD_appendf(&env->code->typedefs, "#define %s__%s(...) ((%s_t){$tag$%s$%s, .%s={__VA_ARGS__}})\n",
- def->name, tag->name, def->name, def->name, tag->name, tag->name);
- }
- enum_def = CORD_cat(enum_def, "};\n};\n");
- env->code->typecode = CORD_cat(env->code->typecode, enum_def);
-
- CORD cord_func = CORD_all("static CORD ", def->name, "__as_str(", def->name, "_t *obj, bool use_color) {\n",
- "\tif (!obj) return \"", def->name, "\";\n",
- "\tswitch (obj->$tag) {\n");
- for (tag_ast_t *tag = def->tags; tag; tag = tag->next) {
- cord_func = CORD_all(cord_func, "\tcase $tag$", def->name, "$", tag->name, ": return CORD_all(use_color ? \"\\x1b[36;1m",
- def->name, ".", tag->name, "\\x1b[m(\" : \"", def->name, ".", tag->name, "(\"");
-
- for (arg_ast_t *field = tag->fields; field; field = field->next) {
- type_t *field_t = parse_type_ast(env, field->type);
- CORD field_str = expr_as_string(env, CORD_all("obj->", tag->name, ".", field->name), field_t, "use_color");
- CORD_appendf(&cord_func, ", \"%s=\", %r", field->name, field_str);
- if (field->next) CORD_appendf(&cord_func, ", \", \"");
- }
- CORD_appendf(&cord_func, ", \")\");\n");
- }
- CORD_appendf(&cord_func, "\t}\n}\n");
- env->code->funcs = CORD_cat(env->code->funcs, cord_func);
-
- // Typeinfo:
- CORD_appendf(&env->code->typedefs, "typedef struct { TypeInfo type; } %s_namespace_t;\n", def->name);
- CORD_appendf(&env->code->typedefs, "extern %s_namespace_t %s;\n", def->name, def->name);
- CORD_appendf(&env->code->typeinfos, "public %s_namespace_t %s = {{.tag=CustomInfo, .CustomInfo={.as_str=(void*)%s__as_str}}};\n", def->name, def->name, def->name);
-
+ compile_enum_def(env, ast);
return CORD_EMPTY;
}
case DocTest: {