Delete old scoping code, working on any types
This commit is contained in:
88
ccodegen.cpp
88
ccodegen.cpp
@@ -3,7 +3,6 @@
|
||||
#define genln(...) do{gen("\n"); gen_indent(); gen(__VA_ARGS__); }while(0)
|
||||
global S32 global_indent;
|
||||
global S32 is_inside_struct;
|
||||
global B32 should_gen_scope_name = false;
|
||||
|
||||
function void gen_ast(Ast *ast);
|
||||
function bool gen_expr(Ast_Expr *ast, Ast_Type *type_of_var = 0);
|
||||
@@ -28,7 +27,6 @@ gen_line(Ast *node){
|
||||
function String
|
||||
string_scope_name(Allocator *a, Ast_Scope *scope){
|
||||
String string = {};
|
||||
if(!should_gen_scope_name) return string;
|
||||
if(scope->parent_scope) string = string_scope_name(a, scope->parent_scope);
|
||||
if(scope->name.str) string = string_fmt(a, "%Q%Q_", string, scope->name);
|
||||
return string;
|
||||
@@ -44,10 +42,7 @@ gen_scope_name(Ast_Scope *scope){
|
||||
function String
|
||||
unique_name(Allocator *allocator, Ast *ast){
|
||||
Scratch scratch;
|
||||
B32 temp = should_gen_scope_name;
|
||||
should_gen_scope_name = true;
|
||||
String result = string_scope_name(scratch, ast->parent_scope);
|
||||
should_gen_scope_name = temp;
|
||||
assert(result.len);
|
||||
result = string_fmt(allocator, "%Q%d", result, ast->pos->line);
|
||||
return result;
|
||||
@@ -63,20 +58,20 @@ unique_name(Allocator *allocator, Ast *ast){
|
||||
// 4 Add type name on the left side
|
||||
|
||||
function String
|
||||
string_simple_decl_prefix(Allocator *a, Ast_Type *ast, bool scope_names){
|
||||
string_simple_decl_prefix(Allocator *a, Ast_Type *ast){
|
||||
switch(ast->kind){
|
||||
case TYPE_POINTER:{
|
||||
String string = string_simple_decl_prefix(a, ast->base, scope_names);
|
||||
String string = string_simple_decl_prefix(a, ast->base);
|
||||
string = string_fmt(a, "%Q*", string);
|
||||
return string;
|
||||
}break;
|
||||
case TYPE_LAMBDA: return {}; break;
|
||||
case TYPE_ENUM:
|
||||
case TYPE_ARRAY: {
|
||||
return string_simple_decl_prefix(a, ast->base, scope_names);
|
||||
return string_simple_decl_prefix(a, ast->base);
|
||||
}break;
|
||||
case TYPE_SLICE:{
|
||||
String string = string_simple_decl_prefix(a, ast->base, true);
|
||||
String string = string_simple_decl_prefix(a, ast->base);
|
||||
string = string_fmt(a, "Slice%llu ", ast->type_id);
|
||||
return string;
|
||||
}break;
|
||||
@@ -89,7 +84,6 @@ string_simple_decl_prefix(Allocator *a, Ast_Type *ast, bool scope_names){
|
||||
auto name = constant->name;
|
||||
String string = string_fmt(a, "%Q ", name);
|
||||
String sc = {};
|
||||
if(scope_names) sc = string_scope_name(a, constant->parent_scope);
|
||||
return string_fmt(a, "%Q%Q", sc, string);
|
||||
}break;
|
||||
default: {
|
||||
@@ -101,13 +95,13 @@ string_simple_decl_prefix(Allocator *a, Ast_Type *ast, bool scope_names){
|
||||
}
|
||||
|
||||
function String
|
||||
string_simple_decl_postfix(Allocator *a, Ast_Type *ast, bool scope_names){
|
||||
string_simple_decl_postfix(Allocator *a, Ast_Type *ast){
|
||||
switch(ast->kind){
|
||||
case TYPE_POINTER:
|
||||
return string_simple_decl_postfix(a, ast->base, scope_names);
|
||||
return string_simple_decl_postfix(a, ast->base);
|
||||
break;
|
||||
case TYPE_ARRAY:{
|
||||
String result = string_simple_decl_postfix(a, ast->arr.base, scope_names);
|
||||
String result = string_simple_decl_postfix(a, ast->arr.base);
|
||||
String string = string_fmt(a, "[%d]%Q", ast->arr.size, result);
|
||||
return string;
|
||||
}break;
|
||||
@@ -120,12 +114,12 @@ string_simple_decl_postfix(Allocator *a, Ast_Type *ast, bool scope_names){
|
||||
}
|
||||
|
||||
function String
|
||||
string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}, Ast_Scope *scope = 0, bool scope_names = true){
|
||||
string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}){
|
||||
if(ast->kind == TYPE_LAMBDA) {
|
||||
String prefix = string_simple_decl_prefix(a, ast->func.ret, scope_names);
|
||||
String prefix = string_simple_decl_prefix(a, ast->func.ret);
|
||||
String string = string_fmt(a, "%Q(*%Q)(", prefix, name);
|
||||
For(ast->func.args){
|
||||
String prefix_arg = string_simple_decl_prefix(a, it, scope_names);
|
||||
String prefix_arg = string_simple_decl_prefix(a, it);
|
||||
string = string_fmt(a, "%Q%Q", string, prefix_arg);
|
||||
if(&it != ast->func.args.end() - 1)
|
||||
string = string_fmt(a, "%Q, ", string);
|
||||
@@ -134,29 +128,27 @@ string_simple_decl(Allocator *a, Ast_Type *ast, Intern_String name = {}, Ast_Sco
|
||||
return string;
|
||||
}
|
||||
else{
|
||||
String string = string_simple_decl_prefix(a, ast, scope_names);
|
||||
String string = string_simple_decl_prefix(a, ast);
|
||||
if(name.len) {
|
||||
if(scope && scope_names)
|
||||
string = string_fmt(a, "%Q%Q", string, string_scope_name(a, scope));
|
||||
string = string_fmt(a, "%Q%Q", string, name);
|
||||
}
|
||||
String postfix = string_simple_decl_postfix(a, ast, scope_names);
|
||||
String postfix = string_simple_decl_postfix(a, ast);
|
||||
string = string_fmt(a, "%Q%Q", string, postfix);
|
||||
return string;
|
||||
}
|
||||
}
|
||||
|
||||
function void
|
||||
gen_simple_decl(Ast_Type *ast, Intern_String name = {}, Ast_Scope *scope = 0, bool scope_names = true){
|
||||
gen_simple_decl(Ast_Type *ast, Intern_String name = {}){
|
||||
Scratch scratch;
|
||||
String string = string_simple_decl(scratch, ast, name, scope, scope_names);
|
||||
String string = string_simple_decl(scratch, ast, name);
|
||||
gen("%.*s", (int)string.len, string.str);
|
||||
}
|
||||
|
||||
function String
|
||||
gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name, Ast_Scope *scope, bool scope_names){
|
||||
gen_string_simple_decl(Allocator *a, Ast_Type *ast, String name){
|
||||
Scratch scratch;
|
||||
String string = string_simple_decl(scratch, ast, pctx->intern(name), scope, scope_names);
|
||||
String string = string_simple_decl(scratch, ast, pctx->intern(name));
|
||||
String result = string_copy(a, string);
|
||||
return result;
|
||||
}
|
||||
@@ -213,7 +205,7 @@ enum {
|
||||
function void
|
||||
gen_var(Ast_Decl *decl, B32 emit_value, B32 scope_names){
|
||||
if(is_flag_set(decl->flags, AST_FOREIGN)) gen("extern ");
|
||||
gen_simple_decl(decl->type, decl->name, decl->parent_scope, scope_names);
|
||||
gen_simple_decl(decl->type, decl->name);
|
||||
if(is_flag_set(decl->flags, AST_FOREIGN)) return;
|
||||
|
||||
if(emit_value == DONT_EMIT_VALUE){
|
||||
@@ -238,7 +230,7 @@ gen_lambda(Intern_String name, Ast_Lambda *lambda, B32 generate_block = true){
|
||||
if(name == pctx->intern("main"_s) || name == pctx->intern("WinMain"_s)){
|
||||
is_foreign = true;
|
||||
}
|
||||
gen_simple_decl(lambda->resolved_type->func.ret, name, lambda->parent_scope, !is_foreign);
|
||||
gen_simple_decl(lambda->resolved_type->func.ret, name);
|
||||
gen("(");
|
||||
For(lambda->args){
|
||||
gen_var(it, DONT_EMIT_VALUE, true);
|
||||
@@ -259,26 +251,30 @@ gen_expr(Ast_Expr *ast, Ast_Type *type_of_var){
|
||||
CASE(IDENT, Atom){
|
||||
if(node->resolved_decl->kind == AST_MODULE_NAMESPACE || node->resolved_decl->kind == AST_FILE_NAMESPACE)
|
||||
return false;
|
||||
B32 print_scope = true;
|
||||
if(node->resolved_decl->lambda){
|
||||
if(is_flag_set(node->resolved_decl->lambda->flags, AST_FOREIGN))
|
||||
print_scope = false;
|
||||
|
||||
if(type_of_var){
|
||||
if(is_slice(type_of_var) && is_array(node->resolved_decl->type)){
|
||||
gen("{%d, ", (int)node->resolved_decl->type->arr.size);
|
||||
gen("%Q}", node->intern_val);
|
||||
}
|
||||
else if(!is_any(ast->resolved_type) && is_any(type_of_var)){
|
||||
gen("(Any){&");
|
||||
gen_expr(ast);
|
||||
gen(", %d}", ast->resolved_type->type_id);
|
||||
}
|
||||
else goto otherwise;
|
||||
}
|
||||
if(type_of_var && is_slice(type_of_var) && is_array(node->resolved_decl->type)){
|
||||
gen("{%d, ", (int)node->resolved_decl->type->arr.size);
|
||||
if(print_scope) gen_scope_name(node->resolved_decl->parent_scope);
|
||||
gen("%Q}", node->intern_val);
|
||||
} else {
|
||||
if(print_scope) gen_scope_name(node->resolved_decl->parent_scope);
|
||||
else{
|
||||
otherwise:
|
||||
gen("%Q", node->intern_val);
|
||||
}
|
||||
|
||||
BREAK();
|
||||
}
|
||||
|
||||
CASE(VALUE, Atom){
|
||||
B32 written = gen_value(node->value);
|
||||
if(!written) {
|
||||
gen_scope_name(node->parent_scope);
|
||||
gen("%Q", node->value.intern_val);
|
||||
}
|
||||
BREAK();
|
||||
@@ -511,7 +507,6 @@ gen_ast(Ast *ast){
|
||||
|
||||
CASE(STRUCT, Decl){
|
||||
gen("struct ");
|
||||
gen_scope_name(node->parent_scope);
|
||||
gen("%Q{", node->name);
|
||||
global_indent++;
|
||||
is_inside_struct++;
|
||||
@@ -783,7 +778,6 @@ function String
|
||||
get_compilation_result(){
|
||||
generating_time_begin = os_time();
|
||||
|
||||
#if 1
|
||||
gen(R"==(
|
||||
#include <stdint.h>
|
||||
#include <math.h>
|
||||
@@ -808,23 +802,22 @@ typedef S32 Bool;
|
||||
typedef S64 Type;
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
#define assert(x) do{if(!(x))__debugbreak();}while(0)
|
||||
#define assert_msg(x,...) assert(x)
|
||||
typedef struct String{
|
||||
U8 *str;
|
||||
S64 len;
|
||||
}String;
|
||||
#define assert(x) do{if(!(x))__debugbreak();}while(0)
|
||||
#define assert_msg(x,...) assert(x)
|
||||
)==");
|
||||
|
||||
)==");
|
||||
//*(volatile int *)0 = 0;
|
||||
#endif
|
||||
// Generate struct forward decls
|
||||
For(pctx->ordered_decls){
|
||||
if(it->kind == AST_STRUCT){
|
||||
genln("typedef struct %Q %Q;", it->name, it->name);
|
||||
}
|
||||
}
|
||||
|
||||
// Generate slice and tuple types
|
||||
For(pctx->all_types){
|
||||
Scratch scratch;
|
||||
Ast_Type *type = it;
|
||||
@@ -855,7 +848,7 @@ typedef struct String{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Generate lambda forward decls
|
||||
For(pctx->ordered_decls){
|
||||
if(it->kind == AST_LAMBDA){
|
||||
genln("");
|
||||
@@ -863,12 +856,14 @@ typedef struct String{
|
||||
}
|
||||
}
|
||||
|
||||
// Generate language.kl
|
||||
for(S32 i = 0; i < pctx->base_language_ordered_decl_len; i++){
|
||||
Ast_Decl *it = pctx->ordered_decls[i];
|
||||
genln("");
|
||||
gen_ast(it);
|
||||
}
|
||||
|
||||
// Generate type info
|
||||
genln("S64 type_infos_len = %d;", pctx->all_types.len);
|
||||
genln("Type_Info *type_infos = (Type_Info[]){");
|
||||
global_indent++;
|
||||
@@ -910,6 +905,7 @@ typedef struct String{
|
||||
global_indent--;
|
||||
gen("};");
|
||||
|
||||
// Generate actual code
|
||||
for(S32 i = pctx->base_language_ordered_decl_len; i < pctx->ordered_decls.len; i++){
|
||||
Ast_Decl *it = pctx->ordered_decls[i];
|
||||
genln("");
|
||||
|
||||
Reference in New Issue
Block a user