Skip to content

Commit

Permalink
compile flow update
Browse files Browse the repository at this point in the history
  • Loading branch information
ctxcode committed Aug 29, 2023
1 parent 5de72ba commit b8175dd
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 21 deletions.
2 changes: 1 addition & 1 deletion lib/src/type/string.ki
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use mem;
use os;

class String type:ptr track rc {
class String type:ptr rc {

/////////////////////
// Core
Expand Down
7 changes: 1 addition & 6 deletions src/build/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,10 @@ void class_generate_deref_props(Class *class) {
}
void class_generate_free(Class *class) {
//
printf("c:%p\n", class);
Func *func = class->func_free;
if (func->chunk_body)
return;

printf("0\n");
func_make_arg_decls(func);

Build *b = class->fc->b;
Expand All @@ -226,7 +224,6 @@ void class_generate_free(Class *class) {
Decl *this_decl = this_arg->decl;
Value *this = value_init(alc, v_decl, this_decl, type_class);

printf("1\n");
// Call __before_free
if (class->func_before_free) {
Value *on = vgen_fptr(alc, class->func_before_free, NULL);
Expand All @@ -236,7 +233,6 @@ void class_generate_free(Class *class) {
array_push(fscope->ast, token_init(alc, tkn_statement, fcall));
}

printf("2\n");
// Call __deref_props
if (class->func_deref_props) {
Value *on = vgen_fptr(alc, class->func_deref_props, NULL);
Expand All @@ -248,7 +244,6 @@ void class_generate_free(Class *class) {

ClassProp *propw = map_get(class->props, "_RC_WEAK");
Scope *free_scope = fscope;
printf("3\n");

if (propw) {
free_scope = scope_init(alc, sct_default, fscope, true);
Expand Down Expand Up @@ -518,7 +513,7 @@ Class *class_get_generic_class(Class *class, Array *types) {
map_set(gclass->scope->identifiers, "CLASS", idf);

// stage_2(new_fc);
new_fc->chunk = class->chunk_body;
*new_fc->chunk = *class->chunk_body;
stage_2_2_class_read_props(new_fc, gclass, false, false);
if (fc->stage_completed <= 2) {
chain_add(b->stage_2_3, new_fc);
Expand Down
28 changes: 28 additions & 0 deletions src/build/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,34 @@ void fcall_type_check(Fc *fc, Value *on, Array *values) {
}
}

Value *func_arg_get_value(Fc *fc, Arg *arg) {
//
if (arg->value) {
Value *res = al(fc->alc, sizeof(Value));
Value *default_val = arg->value;
*res = *default_val;
return res;
}
if (arg->value_chunk) {
if (arg->parsing_value) {
sprintf(fc->sbuf, "Class property has an infinite recursive value definition");
fc_error(fc);
}
arg->parsing_value = true;

Chunk original;
original = *fc->chunk;
*fc->chunk = *arg->value_chunk;
Value *res = read_value(fc, fc->alc, arg->value_chunk_scope, false, 0, false);
*fc->chunk = original;

arg->parsing_value = false;
arg->value = res;
return res;
}
return NULL;
}

void func_make_arg_decls(Func *func) {
//
Allocator *alc = func->fc->alc;
Expand Down
3 changes: 1 addition & 2 deletions src/build/stage-1-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ void stage_1_class(Fc *fc, bool is_struct) {
class->scope = scope_init(fc->alc, sct_class, fc->scope, false);
class->is_struct = is_struct;
class->is_rc = !is_struct;
class->track_ownership = !is_struct;
class->track_ownership = false;
class->def_chunk = def_chunk;

array_push(fc->classes, class);
Expand Down Expand Up @@ -264,7 +264,6 @@ void stage_1_class(Fc *fc, bool is_struct) {
bool track = false;
while (strcmp(token, "{") != 0) {
if (strcmp(token, "type") == 0) {
class->track_ownership = false;
class->is_rc = false;
tok_expect(fc, ":", true, false);
tok(fc, token, true, false);
Expand Down
2 changes: 1 addition & 1 deletion src/build/stage-2-2-props.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void stage_2_2(Fc *fc) {
if (class->is_generic_base)
continue;

fc->chunk = class->chunk_body;
*fc->chunk = *class->chunk_body;
stage_2_2_class_read_props(fc, class, false, false);
}

Expand Down
1 change: 1 addition & 0 deletions src/build/stage-2-3-post-props.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,5 +122,6 @@ void stage_2_3_circular(Build *b, Class *class) {
if (circular) {
class->is_circular = true;
class->track_ownership = true;
// printf("circular: %s\n", class->dname);
}
}
15 changes: 9 additions & 6 deletions src/build/stage-2-6-read-types.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ void stage_2_6(Fc *fc) {
printf("# Stage 2.1 : Read aliasses : %s\n", fc->path_ki);
}

for (int i = 0; i < fc->classes->length; i++) {
Class *class = array_get_index(fc->classes, i);
if (class->is_generic_base)
continue;
stage_2_6_class_default_functions(fc, class);
}
for (int i = 0; i < fc->funcs->length; i++) {
Func *func = array_get_index(fc->funcs, i);
if (!func->chunk_args)
Expand All @@ -36,6 +30,12 @@ void stage_2_6(Fc *fc) {
}
g->type = type;
}
for (int i = 0; i < fc->classes->length; i++) {
Class *class = array_get_index(fc->classes, i);
if (class->is_generic_base)
continue;
stage_2_6_class_default_functions(fc, class);
}

chain_add(b->stage_3, fc);
}
Expand All @@ -50,6 +50,8 @@ void stage_2_6_class_functions(Fc *fc, Class *class) {
Func *func = array_get_index(funcs, i);
stage_2_6_func(fc, func);
}

stage_2_6_class_default_functions(fc, class);
}

void stage_2_6_func(Fc *fc, Func *func) {
Expand Down Expand Up @@ -102,6 +104,7 @@ void stage_2_6_func(Fc *fc, Func *func) {

Arg *arg = arg_init(alc, name, type);
arg->value_chunk = val_chunk;
arg->value_chunk_scope = func->scope;
arg->type_chunk = type_chunk;

array_push(func->args, arg);
Expand Down
9 changes: 5 additions & 4 deletions src/build/stage-3-values.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ void stage_3(Fc *fc) {
Class *class = array_get_index(fc->classes, i);
if (class->is_generic_base)
continue;
if (b->verbose > 2) {
printf("> Read class values: %s\n", class->dname);
}
stage_3_class(fc, class);
}
for (int i = 0; i < fc->funcs->length; i++) {
Expand All @@ -37,6 +34,10 @@ void stage_3(Fc *fc) {

void stage_3_class(Fc *fc, Class *class) {

if (fc->b->verbose > 2) {
printf("> Read class values: %s\n", class->dname);
}

Allocator *alc = fc->alc;
Map *props = class->props;
for (int i = 0; i < props->keys->length; i++) {
Expand All @@ -46,7 +47,7 @@ void stage_3_class(Fc *fc, Class *class) {
if (!chunk)
continue;

fc->chunk = chunk;
*fc->chunk = *chunk;

Value *val = read_value(fc, alc, class->scope, false, 0, false);
val = try_convert(fc, alc, val, prop->type);
Expand Down
2 changes: 1 addition & 1 deletion src/build/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ Value *value_func_call(Allocator *alc, Fc *fc, Scope *scope, Value *on) {
while (index < argc) {
Arg *arg = array_get_index(args, index);

Value *val = arg->value;
Value *val = func_arg_get_value(fc, arg);
if (!val)
break;

Expand Down
1 change: 1 addition & 0 deletions src/headers/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ void scope_add_defer_token(Allocator *alc, Scope *scope, Token *token);
Func *func_init(Allocator *alc, Build *b);
void fcall_type_check(Fc *fc, Value *on, Array *values);
void func_make_arg_decls(Func *func);
Value *func_arg_get_value(Fc *fc, Arg *arg);

// Class
Class *class_init(Allocator *alc);
Expand Down
2 changes: 2 additions & 0 deletions src/headers/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,12 @@ struct Arg {
char *name;
Type *type;
Value *value;
Scope *value_chunk_scope;
Chunk *value_chunk;
Chunk *type_chunk;
Decl *decl;
bool is_mut;
bool parsing_value;
};
struct UsageLine {
Decl *decl;
Expand Down

0 comments on commit b8175dd

Please sign in to comment.