Compiling global, work on AST_LAMBDA_EXPR
This commit is contained in:
137
ccodegen.cpp
137
ccodegen.cpp
@@ -5,12 +5,19 @@ global S32 global_indent;
|
||||
global S32 is_inside_struct;
|
||||
|
||||
function void gen_ast(Ast *ast);
|
||||
function void gen_expr(Ast_Expr *ast);
|
||||
|
||||
function void
|
||||
gen_indent(){
|
||||
for(S32 i = 0; i < global_indent; i++) gen(" ");
|
||||
}
|
||||
|
||||
function void
|
||||
gen_line(Ast *node){
|
||||
if(emit_line_directives)
|
||||
genln("#line %d", node->pos->line+1);
|
||||
}
|
||||
|
||||
// @todo: Gen complicated decl
|
||||
//array 10 ( pointer (pointer array 5 int a))
|
||||
// int (*(*(a[5])))[10]
|
||||
@@ -91,6 +98,60 @@ gen_value(Value a){
|
||||
return result;
|
||||
}
|
||||
|
||||
function void
|
||||
gen_stmt_scope(Ast_Scope *scope){
|
||||
gen("{");
|
||||
global_indent++;
|
||||
For(scope->stmts) {
|
||||
gen_line(it);
|
||||
genln("");
|
||||
gen_ast(it);
|
||||
}
|
||||
global_indent--;
|
||||
genln("}");
|
||||
}
|
||||
|
||||
enum {
|
||||
ALWAYS_EMIT_VALUE = 0,
|
||||
DONT_EMIT_VALUE = 1,
|
||||
};
|
||||
|
||||
function void
|
||||
gen_var(Intern_String name, Ast_Resolved_Type *type, Ast_Expr *expr, B32 emit_value){
|
||||
gen_simple_decl(type, name);
|
||||
|
||||
if(emit_value == DONT_EMIT_VALUE){
|
||||
return;
|
||||
}
|
||||
|
||||
if(expr){
|
||||
gen(" = ");
|
||||
gen_expr(expr);
|
||||
} else { // Default zero
|
||||
if(is_numeric(type)){
|
||||
gen(" = 0");
|
||||
} else {
|
||||
gen(" = {}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
gen_lambda(Intern_String name, Ast_Lambda *lambda){
|
||||
gen_simple_decl(lambda->type->func.ret, name);
|
||||
gen("(");
|
||||
For(lambda->args){
|
||||
gen_var(it->name, it->type, 0, DONT_EMIT_VALUE);
|
||||
if(&it != (lambda->args.end() - 1)) gen(", ");
|
||||
}
|
||||
gen(")");
|
||||
|
||||
if(lambda->scope) {
|
||||
gen_stmt_scope(lambda->scope);
|
||||
}
|
||||
else gen(";");
|
||||
}
|
||||
|
||||
function void
|
||||
gen_expr(Ast_Expr *ast){
|
||||
switch(ast->kind){
|
||||
@@ -115,6 +176,11 @@ gen_expr(Ast_Expr *ast){
|
||||
BREAK();
|
||||
}
|
||||
|
||||
CASE(LAMBDA_EXPR, Lambda){
|
||||
gen_lambda({}, node);
|
||||
BREAK();
|
||||
}
|
||||
|
||||
CASE(BINARY, Binary){
|
||||
if(node->op == TK_Dot){
|
||||
gen_expr(node->left);
|
||||
@@ -172,50 +238,6 @@ gen_expr(Ast_Expr *ast){
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
gen_line(Ast *node){
|
||||
if(emit_line_directives)
|
||||
genln("#line %d", node->pos->line+1);
|
||||
}
|
||||
|
||||
function void
|
||||
gen_stmt_scope(Ast_Scope *scope){
|
||||
gen("{");
|
||||
global_indent++;
|
||||
For(scope->stmts) {
|
||||
gen_line(it);
|
||||
genln("");
|
||||
gen_ast(it);
|
||||
}
|
||||
global_indent--;
|
||||
genln("}");
|
||||
}
|
||||
|
||||
enum {
|
||||
ALWAYS_EMIT_VALUE = 0,
|
||||
DONT_EMIT_VALUE = 1,
|
||||
};
|
||||
|
||||
function void
|
||||
gen_var(Intern_String name, Ast_Resolved_Type *type, Ast_Expr *expr, B32 emit_value){
|
||||
gen_simple_decl(type, name);
|
||||
|
||||
if(emit_value == DONT_EMIT_VALUE){
|
||||
return;
|
||||
}
|
||||
|
||||
if(expr){
|
||||
gen(" = ");
|
||||
gen_expr(expr);
|
||||
} else { // Default zero
|
||||
if(is_numeric(type)){
|
||||
gen(" = 0");
|
||||
} else {
|
||||
gen(" = {}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
gen_ast(Ast *ast){
|
||||
switch(ast->kind){
|
||||
@@ -283,31 +305,10 @@ gen_ast(Ast *ast){
|
||||
}
|
||||
|
||||
CASE(LAMBDA, Decl){
|
||||
if(node->kind == AST_LAMBDA){
|
||||
if(is_flag_set(node->flags, AST_FOREIGN)){
|
||||
return;
|
||||
}
|
||||
Ast_Lambda *lambda = node->lambda;
|
||||
Ast_Resolved_Type *ret = node->type->func.ret;
|
||||
gen_simple_decl(ret, node->name);
|
||||
gen("(");
|
||||
For(lambda->args){
|
||||
gen_var(it->name, it->type, 0, DONT_EMIT_VALUE);
|
||||
if(&it != (lambda->args.end() - 1)) gen(", ");
|
||||
}
|
||||
gen(")");
|
||||
|
||||
if(lambda->scope) {
|
||||
gen_stmt_scope(lambda->scope);
|
||||
}
|
||||
else gen(";");
|
||||
}
|
||||
else{
|
||||
gen_simple_decl(node->type, node->name);
|
||||
gen(" = ");
|
||||
gen_expr((Ast_Expr *)node->expr);
|
||||
gen(";");
|
||||
if(is_flag_set(node->flags, AST_FOREIGN)){
|
||||
gen("/*foreign*/");
|
||||
}
|
||||
gen_lambda(node->name, node->lambda);
|
||||
BREAK();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user