Copying and printing the copy

This commit is contained in:
Krzosa Karol
2023-03-31 22:44:11 +02:00
parent e70d544029
commit b6835d0f6a
5 changed files with 26 additions and 9 deletions

View File

@@ -79,7 +79,6 @@ arena_push_size(Arena *a, size_t size) {
#define push_struct_copy(a, T, p) (T *)push_copy(a, p, sizeof(T)) #define push_struct_copy(a, T, p) (T *)push_copy(a, p, sizeof(T))
CORE_Static void * CORE_Static void *
push_copy(Arena *a, void *p, size_t size) { push_copy(Arena *a, void *p, size_t size) {
if (p == 0) return 0;
void *result = arena_push_size(a, size); void *result = arena_push_size(a, size);
memory_copy(result, p, size); memory_copy(result, p, size);
return result; return result;

View File

@@ -76,6 +76,7 @@ struct Array {
allocator = a; allocator = a;
data = allocate_array(a, T, size); data = allocate_array(a, T, size);
cap = size; cap = size;
len = 0;
} }
Array<T> copy(Allocator *a) { Array<T> copy(Allocator *a) {

View File

@@ -834,9 +834,15 @@ compile_to_c_code() {
String core_stringify(Ast *); String core_stringify(Ast *);
For(pctx->ordered_decls) { For(pctx->ordered_decls) {
Ast *copy = ast_copy(it, 0);
String r = core_stringify(it); String r = core_stringify(it);
printf("\n-----REAL!!!------\n");
printf("%s\n", r.str);
r = core_stringify(copy);
printf("\n-----COPY!!!------\n");
printf("%s\n", r.str); printf("%s\n", r.str);
} }
pctx->gen.reset();
#if 0 #if 0
int di = 0; int di = 0;

View File

@@ -227,6 +227,7 @@ end_of_switch:
#define ast_create_copy(parent_scope, T, ast) (T *)ast__create_copy(parent_scope, sizeof(T), ast) #define ast_create_copy(parent_scope, T, ast) (T *)ast__create_copy(parent_scope, sizeof(T), ast)
Ast *ast__create_copy(Ast_Scope *parent_scope, size_t size, Ast *ast) { Ast *ast__create_copy(Ast_Scope *parent_scope, size_t size, Ast *ast) {
if (ast == 0) return 0;
Ast *result = (Ast *)push_copy(pctx->perm, ast, size); Ast *result = (Ast *)push_copy(pctx->perm, ast, size);
result->parent_scope = parent_scope; result->parent_scope = parent_scope;
result->di = ++pctx->unique_ids; result->di = ++pctx->unique_ids;
@@ -242,9 +243,9 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
Ast_Scope *src = (Ast_Scope *)ast; Ast_Scope *src = (Ast_Scope *)ast;
Ast_Scope *dst = ast_create_copy(parent_scope, Ast_Scope, src); Ast_Scope *dst = ast_create_copy(parent_scope, Ast_Scope, src);
free_all_nodes(&dst->decls); dst->decls = {};
For(src->decls) { For(src->decls) {
Ast_Decl *copy = (Ast_Decl *)ast_copy(it, dst); Ast_Decl *copy = (Ast_Decl *)ast_copy(it, src);
add(pctx->perm, &dst->decls, copy); add(pctx->perm, &dst->decls, copy);
} }

View File

@@ -93,6 +93,7 @@ void core__stringify(Ast *ast) {
Ast_Scope *n = (Ast_Scope *)ast; Ast_Scope *n = (Ast_Scope *)ast;
global_indent += 1; global_indent += 1;
genln("@%u", n->di);
For(n->decls) { For(n->decls) {
genln(""); genln("");
core__stringify(it); core__stringify(it);
@@ -109,11 +110,12 @@ void core__stringify(Ast *ast) {
case AST_IDENT: { case AST_IDENT: {
Ast_Atom *n = (Ast_Atom *)ast; Ast_Atom *n = (Ast_Atom *)ast;
gen("%Q", n->intern_val); gen("%Q@%u", n->intern_val, n->di);
} break; } break;
case AST_VALUE: { case AST_VALUE: {
Ast_Atom *n = (Ast_Atom *)ast; Ast_Atom *n = (Ast_Atom *)ast; //@todo: proper value
gen("@%Q", n->pos->string); gen("%Q@%u", n->pos->string, n->di);
} break; } break;
case AST_INDEX: { case AST_INDEX: {
@@ -121,13 +123,13 @@ void core__stringify(Ast *ast) {
core__stringify(n->expr); core__stringify(n->expr);
gen("["); gen("[");
core__stringify(n->index); core__stringify(n->index);
gen("]"); gen("]@%u", n->di);
} break; } break;
case AST_UNARY: { case AST_UNARY: {
Ast_Unary *n = (Ast_Unary *)ast; Ast_Unary *n = (Ast_Unary *)ast;
gen("%Q", n->pos->string);
core__stringify(n->expr); core__stringify(n->expr);
gen("%Q@%u", n->pos->string, n->di);
} break; } break;
case AST_BINARY: { case AST_BINARY: {
@@ -135,6 +137,7 @@ void core__stringify(Ast *ast) {
core__stringify(n->left); core__stringify(n->left);
gen("%Q", n->pos->string); gen("%Q", n->pos->string);
core__stringify(n->right); core__stringify(n->right);
gen("@%u", n->di);
} break; } break;
case AST_CALL_ITEM: { case AST_CALL_ITEM: {
@@ -148,7 +151,7 @@ void core__stringify(Ast *ast) {
gen(" = "); gen(" = ");
} }
core__stringify(n->item); core__stringify(n->item);
gen("@%u", n->di);
} break; } break;
case AST_CALL: { case AST_CALL: {
@@ -160,6 +163,7 @@ void core__stringify(Ast *ast) {
if (!n->exprs.is_last(&it)) gen(","); if (!n->exprs.is_last(&it)) gen(",");
} }
gen(")"); gen(")");
gen("@%u", n->di);
} break; } break;
case AST_COMPOUND: { case AST_COMPOUND: {
Ast_Call *n = (Ast_Call *)ast; Ast_Call *n = (Ast_Call *)ast;
@@ -174,6 +178,7 @@ void core__stringify(Ast *ast) {
} }
global_indent -= 1; global_indent -= 1;
genln("}"); genln("}");
gen("@%u", n->di);
} break; } break;
case AST_TYPE_OF: case AST_TYPE_OF:
@@ -200,6 +205,7 @@ void core__stringify(Ast *ast) {
gen("("); gen("(");
core__stringify(n->expr); core__stringify(n->expr);
gen(")"); gen(")");
gen("@%u", n->di);
} break; } break;
case AST_SWITCH: { case AST_SWITCH: {
@@ -210,6 +216,7 @@ void core__stringify(Ast *ast) {
core__stringify(it); core__stringify(it);
} }
core__stringify(n->default_scope); core__stringify(n->default_scope);
gen("@%u", n->di);
} break; } break;
case AST_SWITCH_CASE: { case AST_SWITCH_CASE: {
@@ -219,6 +226,7 @@ void core__stringify(Ast *ast) {
core__stringify(it); core__stringify(it);
} }
core__stringify(n->scope); core__stringify(n->scope);
gen("@%u", n->di);
} break; } break;
case AST_VAR_UNPACK: { case AST_VAR_UNPACK: {
@@ -233,10 +241,12 @@ void core__stringify(Ast *ast) {
case AST_PASS: { case AST_PASS: {
genln("pass"); genln("pass");
gen("@%u", ast->di);
} break; } break;
case AST_BREAK: { case AST_BREAK: {
genln("break"); genln("break");
gen("@%u", ast->di);
} break; } break;
case AST_NAMESPACE: { case AST_NAMESPACE: {