Enums working

This commit is contained in:
Krzosa Karol
2022-06-10 19:41:10 +02:00
parent 43fe057cf4
commit 70255c18cb
4 changed files with 27 additions and 37 deletions

View File

@@ -339,6 +339,23 @@ gen_ast(Ast *ast){
BREAK(); 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){ CASE(CONST, Decl){
switch(node->type->kind){ switch(node->type->kind){
CASE_FLOAT:{ CASE_FLOAT:{
@@ -382,25 +399,7 @@ gen_ast(Ast *ast){
// } // }
// } // }
// else if(sym->type_val->kind == TYPE_ENUM){ // 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{ // else{
// gen("// typedef "); // gen("// typedef ");

View File

@@ -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 Allocator_Kind :: enum
Null Null
Arena Arena

View File

@@ -220,6 +220,7 @@ int main(int argument_count, char **arguments){
files.add("order1.kl"_s); files.add("order1.kl"_s);
files.add("order2.kl"_s); files.add("order2.kl"_s);
files.add("new_types.kl"_s); files.add("new_types.kl"_s);
files.add("enums.kl"_s);
String result = compile_files(files); String result = compile_files(files);
printf("%s", result.str); printf("%s", result.str);
__debugbreak(); __debugbreak();

View File

@@ -663,11 +663,12 @@ resolve_field_access(Ast_Expr *node, Ast_Scope *context){
field_access_scope = context; field_access_scope = context;
Operand op = resolve_expr(node, AST_CANT_BE_NULL); 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(current) current->type = op.type;
if(is_pointer(op.type)) op.type = op.type->base; if(is_pointer(op.type)) op.type = op.type->base;
type_complete(op.type); 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"); compiler_error(node->pos, "Dot access");
} }
@@ -756,7 +757,11 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags){
return {}; return {};
} }
else if(node->op == TK_Dot){ 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{ else{
Operand left = resolve_expr(node->left, AST_CANT_BE_NULL); 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); op = require_const_int(it->expr, AST_CANT_BE_NULL);
value = bigint_as_signed(&op.big_int_val) + 1; value = bigint_as_signed(&op.big_int_val) + 1;
} else{ } else{
it->state = DECL_RESOLVED;
op.type = untyped_int; op.type = untyped_int;
bigint_init_signed(&op.big_int_val, value++); bigint_init_signed(&op.big_int_val, value++);
} }