Typedef works for simple case
This commit is contained in:
@@ -280,6 +280,7 @@ gen_ast(Ast *ast){
|
|||||||
else if(sym->type == type_type){
|
else if(sym->type == type_type){
|
||||||
gen("typedef ");
|
gen("typedef ");
|
||||||
gen_simple_decl(sym->type_val, node->name);
|
gen_simple_decl(sym->type_val, node->name);
|
||||||
|
gen(";");
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
parsing_error(node->pos, "C_Codegen: Unhandled type of constant expression");
|
parsing_error(node->pos, "C_Codegen: Unhandled type of constant expression");
|
||||||
|
|||||||
@@ -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
|
CONSTANT :: 10
|
||||||
thing := 10
|
thing := 10
|
||||||
if i := thing + cond, cond + CONSTANT
|
if i := thing + cond, cond + CONSTANT
|
||||||
|
|||||||
4
main.cpp
4
main.cpp
@@ -23,8 +23,8 @@ int main(){
|
|||||||
test_intern_table();
|
test_intern_table();
|
||||||
lex_test();
|
lex_test();
|
||||||
|
|
||||||
String result = compile_file("globals.kl"_s);
|
// String result = compile_file("globals.kl"_s);
|
||||||
// String result = compile_file("lambdas.kl"_s);
|
String result = compile_file("lambdas.kl"_s);
|
||||||
// String result = compile_file("order_independent_globals.kl"_s);
|
// String result = compile_file("order_independent_globals.kl"_s);
|
||||||
printf("%s", result.str);
|
printf("%s", result.str);
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
enum Sym_Kind{
|
enum Sym_Kind{
|
||||||
SYM_NONE,
|
SYM_NONE,
|
||||||
SYM_TYPE,
|
|
||||||
SYM_CONST,
|
SYM_CONST,
|
||||||
SYM_VAR,
|
SYM_VAR,
|
||||||
};
|
};
|
||||||
@@ -93,10 +92,11 @@ sym_new(Sym_Kind kind, Intern_String name, Ast *ast){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Sym *
|
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);
|
Sym *result = sym_new(kind, name, ast);
|
||||||
result->type = type;
|
result->type = type;
|
||||||
result->state = SYM_RESOLVED;
|
result->state = SYM_RESOLVED;
|
||||||
|
result->value = value;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,15 +110,16 @@ resolved_get(Ast *ast){
|
|||||||
function Ast_Resolved_Type *
|
function Ast_Resolved_Type *
|
||||||
resolved_type_get(Ast_Expr *ast){
|
resolved_type_get(Ast_Expr *ast){
|
||||||
Sym *result = resolved_get(ast);
|
Sym *result = resolved_get(ast);
|
||||||
assert(result->kind == SYM_TYPE);
|
assert(result->type == type_type);
|
||||||
assert(result->type);
|
assert(result->type);
|
||||||
return result->type;
|
return result->type_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
function void
|
function void
|
||||||
sym_insert_builtin_type(String name, Ast_Resolved_Type *type){
|
sym_insert_builtin_type(String name, Ast_Resolved_Type *type){
|
||||||
Intern_String string = intern_string(&pctx->interns, name);
|
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);
|
sym_insert(sym);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,19 +132,22 @@ sym_insert_builtins(){
|
|||||||
|
|
||||||
{
|
{
|
||||||
Intern_String string = intern_string(&pctx->interns, "true"_s);
|
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);
|
sym_insert(sym);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
Intern_String string = intern_string(&pctx->interns, "false"_s);
|
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);
|
sym_insert(sym);
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
Intern_String string = intern_string(&pctx->interns, "null"_s);
|
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);
|
sym_insert(sym);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,14 +188,14 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
|
|
||||||
Ast_Begin(AST_VAR, Ast_Var){
|
Ast_Begin(AST_VAR, Ast_Var){
|
||||||
Operand op = eval_decl(node);
|
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);
|
sym_insert(sym);
|
||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ast_Begin(AST_CONST, Ast_Const){
|
Ast_Begin(AST_CONST, Ast_Const){
|
||||||
Operand op = eval_decl(node);
|
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);
|
sym_insert(sym);
|
||||||
Ast_End();
|
Ast_End();
|
||||||
}
|
}
|
||||||
@@ -200,7 +204,7 @@ eval_stmt(Ast *ast, Ast_Resolved_Type *ret){
|
|||||||
switch(node->op){
|
switch(node->op){
|
||||||
case TK_Comma:{
|
case TK_Comma:{
|
||||||
Operand op = eval_expr(node->expr);
|
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);
|
sym_insert(sym);
|
||||||
}break;
|
}break;
|
||||||
invalid_default_case;
|
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){
|
Ast_Begin(AST_IDENT, Ast_Atom){
|
||||||
Sym *sym = resolve_name(node->pos, node->intern_val);
|
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
|
// @note: check if null and rewrite the expression to match the expected type
|
||||||
Operand result = {};
|
Operand result = {};
|
||||||
if(sym->type->kind == TYPE_Null){
|
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){
|
else if(sym->kind == SYM_CONST || sym->kind == SYM_VAR){
|
||||||
result.type = type_type;
|
|
||||||
result.type_val = sym->type;
|
|
||||||
sym_new_resolved(SYM_TYPE, sym->name, sym->type, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
else{
|
|
||||||
result.type = sym->type;
|
result.type = sym->type;
|
||||||
result.is_const = sym->kind == SYM_CONST ? true : false;
|
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;
|
return result;
|
||||||
Ast_End();
|
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]");
|
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);
|
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;
|
return type;
|
||||||
Ast_End();
|
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);
|
args.add(type.type_val);
|
||||||
}
|
}
|
||||||
lambda_type = type_lambda(ret_op.type_val, args);
|
lambda_type = type_lambda(ret_op.type_val, args);
|
||||||
sym_new_resolved(SYM_TYPE, {}, lambda_type, node);
|
{
|
||||||
assert(lambda_type);
|
assert(lambda_type);
|
||||||
|
Value val; val.type_val = lambda_type;
|
||||||
|
sym_new_resolved(SYM_CONST, {}, type_type, val, node);
|
||||||
|
}
|
||||||
|
|
||||||
Operand result = {type_type, true};
|
Operand result = {type_type, true};
|
||||||
result.type_val = lambda_type;
|
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);
|
S64 i = node->args.get_index(it);
|
||||||
Ast_Resolved_Type *type = args[i];
|
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);
|
sym_insert(arg_sym);
|
||||||
}
|
}
|
||||||
For(node->block->stmts){
|
For(node->block->stmts){
|
||||||
|
|||||||
Reference in New Issue
Block a user