Enums working
This commit is contained in:
37
ccodegen.cpp
37
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 ");
|
||||
|
||||
16
enums.kl
16
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
|
||||
|
||||
1
main.cpp
1
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();
|
||||
|
||||
@@ -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++);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user