From 70255c18cbde2a6a1c6385db541802744ab55a03 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Fri, 10 Jun 2022 19:41:10 +0200 Subject: [PATCH] Enums working --- ccodegen.cpp | 37 ++++++++++++++++++------------------- enums.kl | 16 ---------------- main.cpp | 1 + typechecking.cpp | 10 ++++++++-- 4 files changed, 27 insertions(+), 37 deletions(-) diff --git a/ccodegen.cpp b/ccodegen.cpp index 372e08c..ee73e80 100644 --- a/ccodegen.cpp +++ b/ccodegen.cpp @@ -339,6 +339,23 @@ gen_ast(Ast *ast){ BREAK(); } + CASE(ENUM, Decl){ + gen("/*enum %s{", node->name.str); + // @todo add typespec + global_indent++; + For(node->scope->decls){ + genln("%s", it->name.str); + if(it->expr){ + gen(" = "); + gen_expr(it->expr); + } + gen(","); + } + global_indent--; + genln("};*/"); + BREAK(); + } + CASE(CONST, Decl){ switch(node->type->kind){ CASE_FLOAT:{ @@ -382,25 +399,7 @@ gen_ast(Ast *ast){ // } // } // else if(sym->type_val->kind == TYPE_ENUM){ - // Ast_Enum *enu = (Ast_Enum *)sym->type_val->ast; - // assert(enu->kind == AST_ENUM); - // if(node->value->kind == AST_ENUM){ - // gen("/*enum %s{", node->name.str); - // // @todo add typespec - // global_indent++; - // For(enu->members){ - // genln("%s", it->name.str); - // gen(" = "); - // Sym *value_sym = resolved_get(it); - // gen("%d", bigint_as_signed(&value_sym->big_int_val)); - // gen(","); - // } - // global_indent--; - // genln("};*/"); - // } - // else{ - // // Type alias - // } + // // } // else{ // gen("// typedef "); diff --git a/enums.kl b/enums.kl index 6cb6c78..6c2da0f 100644 --- a/enums.kl +++ b/enums.kl @@ -1,20 +1,4 @@ -Thing :: struct - len: S64 - - Constant_String :: "Test" - Constant :: 10 - - Thing_Kind :: enum - None - Thing_Not - -test_string := Thing.Constant_String -thing: Thing -access := thing.len -test := Thing.Thing_Kind.Thing_Not -test_const := Thing.Constant - Allocator_Kind :: enum Null Arena diff --git a/main.cpp b/main.cpp index 8593a54..866595a 100644 --- a/main.cpp +++ b/main.cpp @@ -220,6 +220,7 @@ int main(int argument_count, char **arguments){ files.add("order1.kl"_s); files.add("order2.kl"_s); files.add("new_types.kl"_s); + files.add("enums.kl"_s); String result = compile_files(files); printf("%s", result.str); __debugbreak(); diff --git a/typechecking.cpp b/typechecking.cpp index 40c3ea3..6cecacf 100644 --- a/typechecking.cpp +++ b/typechecking.cpp @@ -663,11 +663,12 @@ resolve_field_access(Ast_Expr *node, Ast_Scope *context){ field_access_scope = context; Operand op = resolve_expr(node, AST_CANT_BE_NULL); + if(op.type == type_type && is_enum(op.type_val)) op.type = op.type_val; if(current) current->type = op.type; if(is_pointer(op.type)) op.type = op.type->base; type_complete(op.type); - if(next && !is_struct(op.type)){ + if(next && !is_struct(op.type) && !is_enum(op.type)){ compiler_error(node->pos, "Dot access"); } @@ -756,7 +757,11 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags){ return {}; } else if(node->op == TK_Dot){ - return resolve_field_access(node, 0); + Operand op = resolve_field_access(node, 0); + if(op.is_const){ + rewrite_into_const(node, Ast_Binary, op.value); + } + return op; } else{ Operand left = resolve_expr(node->left, AST_CANT_BE_NULL); @@ -974,6 +979,7 @@ resolve_decl(Ast_Decl *ast){ op = require_const_int(it->expr, AST_CANT_BE_NULL); value = bigint_as_signed(&op.big_int_val) + 1; } else{ + it->state = DECL_RESOLVED; op.type = untyped_int; bigint_init_signed(&op.big_int_val, value++); }