Typedef works for simple case

This commit is contained in:
Krzosa Karol
2022-05-27 15:18:07 +02:00
parent 8875b61970
commit 79aa52e726
4 changed files with 33 additions and 26 deletions

View File

@@ -280,6 +280,7 @@ gen_ast(Ast *ast){
else if(sym->type == type_type){
gen("typedef ");
gen_simple_decl(sym->type_val, node->name);
gen(";");
}
else{
parsing_error(node->pos, "C_Codegen: Unhandled type of constant expression");

View File

@@ -7,7 +7,9 @@ plus some cleanups before adding more complications to the code
*/
if_stmt :: (cond: int): int
type :: int
if_stmt :: (cond: int): type
CONSTANT :: 10
thing := 10
if i := thing + cond, cond + CONSTANT

View File

@@ -23,8 +23,8 @@ int main(){
test_intern_table();
lex_test();
String result = compile_file("globals.kl"_s);
// String result = compile_file("lambdas.kl"_s);
// String result = compile_file("globals.kl"_s);
String result = compile_file("lambdas.kl"_s);
// String result = compile_file("order_independent_globals.kl"_s);
printf("%s", result.str);

View File

@@ -3,7 +3,6 @@
enum Sym_Kind{
SYM_NONE,
SYM_TYPE,
SYM_CONST,
SYM_VAR,
};
@@ -93,10 +92,11 @@ sym_new(Sym_Kind kind, Intern_String name, Ast *ast){
}
function Sym *
sym_new_resolved(Sym_Kind kind, Intern_String name, Ast_Resolved_Type *type, Ast *ast){
sym_new_resolved(Sym_Kind kind, Intern_String name, Ast_Resolved_Type *type, Value value, Ast *ast){
Sym *result = sym_new(kind, name, ast);
result->type = type;
result->state = SYM_RESOLVED;
result->value = value;
return result;
}
@@ -110,15 +110,16 @@ resolved_get(Ast *ast){
function Ast_Resolved_Type *
resolved_type_get(Ast_Expr *ast){
Sym *result = resolved_get(ast);
assert(result->kind == SYM_TYPE);
assert(result->type == type_type);
assert(result->type);
return result->type;
return result->type_val;
}
function void
sym_insert_builtin_type(String name, Ast_Resolved_Type *type){
Intern_String string = intern_string(&pctx->interns, name);
Sym *sym = sym_new_resolved(SYM_TYPE, string, type, &empty_decl);
Value val; val.type_val = type;
Sym *sym = sym_new_resolved(SYM_CONST, string, type_type, val, &empty_decl);
sym_insert(sym);
}
@@ -131,19 +132,22 @@ sym_insert_builtins(){
{
Intern_String string = intern_string(&pctx->interns, "true"_s);
Sym *sym = sym_new_resolved(SYM_CONST, string, type_bool, &empty_decl);
Value val; val.int_val = 1;
Sym *sym = sym_new_resolved(SYM_CONST, string, type_bool, val, &empty_decl);
sym_insert(sym);
}
{
Intern_String string = intern_string(&pctx->interns, "false"_s);
Sym *sym = sym_new_resolved(SYM_CONST, string, type_bool, &empty_decl);
Value val; val.int_val = 0;
Sym *sym = sym_new_resolved(SYM_CONST, string, type_bool, val, &empty_decl);
sym_insert(sym);
}
{
Intern_String string = intern_string(&pctx->interns, "null"_s);
Sym *sym = sym_new_resolved(SYM_CONST, string, type_null, &empty_decl);
Value val; val.int_val = 0;
Sym *sym = sym_new_resolved(SYM_CONST, string, type_null, val, &empty_decl);
sym_insert(sym);
}
}
@@ -184,14 +188,14 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){
Ast_Begin(AST_VAR, Ast_Var){
Operand op = eval_decl(node);
Sym *sym = sym_new_resolved(SYM_VAR, node->name, op.type, node);
Sym *sym = sym_new_resolved(SYM_VAR, node->name, op.type, op.value, node);
sym_insert(sym);
Ast_End();
}
Ast_Begin(AST_CONST, Ast_Const){
Operand op = eval_decl(node);
Sym *sym = sym_new_resolved(SYM_CONST, node->name, op.type, node);
Sym *sym = sym_new_resolved(SYM_CONST, node->name, op.type, op.value, node);
sym_insert(sym);
Ast_End();
}
@@ -200,7 +204,7 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){
switch(node->op){
case TK_Comma:{
Operand op = eval_expr(node->expr);
Sym *sym = sym_new_resolved(SYM_VAR, node->ident->intern_val, op.type, node);
Sym *sym = sym_new_resolved(SYM_VAR, node->ident->intern_val, op.type, op.value, node);
sym_insert(sym);
}break;
invalid_default_case;
@@ -254,6 +258,7 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_comple
Ast_Begin(AST_IDENT, Ast_Atom){
Sym *sym = resolve_name(node->pos, node->intern_val);
// @cleanup: due to Value being a union this portion probably can get cleaned
// @note: check if null and rewrite the expression to match the expected type
Operand result = {};
if(sym->type->kind == TYPE_Null){
@@ -287,17 +292,13 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_comple
}
}
else if(sym->kind == SYM_TYPE){
result.type = type_type;
result.type_val = sym->type;
sym_new_resolved(SYM_TYPE, sym->name, sym->type, node);
}
else{
else if(sym->kind == SYM_CONST || sym->kind == SYM_VAR){
result.type = sym->type;
result.is_const = sym->kind == SYM_CONST ? true : false;
result.int_val = sym->int_val;
result.value = sym->value;
sym_new_resolved(SYM_CONST, sym->name, sym->type, sym->value, node);
}
else invalid_codepath;
return result;
Ast_End();
@@ -311,7 +312,7 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_comple
if(expr.type != type_int) parsing_error(node->pos, "Array index requires type [Int]");
type.type_val = type_array(type.type_val, expr.int_val);
sym_new_resolved(SYM_TYPE, {}, type.type_val, node);
sym_new_resolved(SYM_CONST, {}, type_type, type.value, node);
return type;
Ast_End();
}
@@ -329,8 +330,11 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_comple
args.add(type.type_val);
}
lambda_type = type_lambda(ret_op.type_val, args);
sym_new_resolved(SYM_TYPE, {}, lambda_type, node);
{
assert(lambda_type);
Value val; val.type_val = lambda_type;
sym_new_resolved(SYM_CONST, {}, type_type, val, node);
}
Operand result = {type_type, true};
result.type_val = lambda_type;
@@ -342,7 +346,7 @@ eval_expr(Ast_Expr *ast, Ast_Resolved_Type *expected_type, Sym *lambda_to_comple
S64 i = node->args.get_index(it);
Ast_Resolved_Type *type = args[i];
Sym *arg_sym = sym_new_resolved(SYM_VAR, it[0]->name, type, it[0]);
Sym *arg_sym = sym_new_resolved(SYM_VAR, it[0]->name, type, {}, it[0]);
sym_insert(arg_sym);
}
For(node->block->stmts){