Skip to content

Commit

Permalink
🔥 update
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Feb 25, 2024
1 parent 41a1465 commit 30af8f4
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 71 deletions.
2 changes: 1 addition & 1 deletion ast
2 changes: 1 addition & 1 deletion fmt
1 change: 1 addition & 0 deletions include/env/type.h
Expand Up @@ -58,6 +58,7 @@ struct Type_ {
uint16_t weight;
ae_flag flag;
enum tflag tflag;
bool error;
};

REF_FUNC(Type, type)
Expand Down
2 changes: 1 addition & 1 deletion include/looper.h
Expand Up @@ -16,7 +16,7 @@ struct Looper {
struct Vector_ unroll_v;
Instr instr;
// };
struct EachIdx_ *idx;
Var_Decl *idx;
bool init;
};
#endif
8 changes: 3 additions & 5 deletions src/clean.c
Expand Up @@ -161,23 +161,21 @@ ANN static void clean_stmt_for(Clean *a, Stmt_For b) {
--a->scope;
}

ANN static void clean_idx(Clean *a, struct EachIdx_ *b) {
if (b->var.value) value_remref(b->var.value, a->gwion);
}

ANN static void clean_stmt_each(Clean *a, Stmt_Each b) {
++a->scope;
clean_exp(a, b->exp);
clean_stmt(a, b->body);
// use clean var_decl?
if (b->var.value) mp_free(a->gwion->mp, Value, b->var.value);
if (b->idx) clean_idx(a, b->idx);
if (b->idx.value) clean_var_decl(a, &b->idx);
--a->scope;
}

ANN static void clean_stmt_loop(Clean *a, Stmt_Loop b) {
++a->scope;
clean_exp(a, b->cond);
clean_stmt(a, b->body);
if(b->idx.value) clean_var_decl(a, &b->idx);
--a->scope;
}

Expand Down
8 changes: 2 additions & 6 deletions src/compile.c
Expand Up @@ -118,7 +118,7 @@ ANN static inline bool passes(struct Gwion_ *gwion, struct Compiler *c) {
if(isa(v->type, gwion->type[et_class])) {
const Type t = (Type)v->d.ptr;
type_addref(t);
mk_class(gwion->env, t, (loc_t) {});
mk_class(gwion->env, t, t->info->value->from->loc);
nspc_add_type(gwion->env->curr, insert_symbol(gwion->st, v->name), t);
} else
valid_value(gwion->env, insert_symbol(gwion->st, v->name), v);
Expand All @@ -136,13 +136,9 @@ ANN static inline bool passes(struct Gwion_ *gwion, struct Compiler *c) {
return ret;
}

static pos_t pos = { 1 , 1 };
void gwion_set_default_pos(const pos_t _pos) {
pos = _pos;
}
ANN static inline bool _check(struct Gwion_ *gwion, struct Compiler *c) {
struct AstGetter_ arg = {c->name, c->file, gwion->st, .ppa = gwion->ppa};
CHECK_B((c->ast = parse_pos(&arg, pos)));
CHECK_B((c->ast = parse(&arg)));
gwion->env->name = c->name;
return passes(gwion, c);
}
Expand Down
27 changes: 13 additions & 14 deletions src/emit/emit.c
Expand Up @@ -117,7 +117,7 @@ static const f_instr allocmember[4] = {RegPushImm, RegPushImm2, _staticmemset_,
static const f_instr allocword[4] = {AllocWord, AllocWord2, AllocWord3,
RegPushMem4};

ANN static bool emit_class_def(const Emitter, const Class_Def);
ANN static bool emit_class_def(const Emitter, const Class_Def);
ANN static bool emit_cdef(const Emitter, const Type);

ANN static bool ensure_emit(const Emitter emit, const Type t) {
Expand Down Expand Up @@ -919,8 +919,7 @@ ANN static bool struct_finish(const Emitter emit, const Exp_Decl *decl) {
const Type t = decl->type;
const bool emit_addr = exp_getvar(exp_self(decl));
if(GET_FLAG(decl->var.vd.value, late) || GET_FLAG(decl->var.td, late)) {
if(!emit_addr)
decl_expand(emit, t);
if(!emit_addr) decl_expand(emit, t);
return true;
}
if(!decl->args && !tflag(t, tflag_ctor) && emit_addr)
Expand Down Expand Up @@ -2246,21 +2245,21 @@ ANN static bool _emit_stmt_each(const Emitter emit, const Stmt_Each stmt,
emit_memsetimm(emit, key_offset, -1);
stmt->var.value->from->offset = val_offset;
//value_addref(stmt->v);
_nspc_add_value(emit->env->curr, stmt->tag.sym, stmt->var.value);
_nspc_add_value(emit->env->curr, stmt->var.tag.sym, stmt->var.value);
emit_debug(emit, stmt->var.value);
if (stmt->idx) {
stmt->idx->var.value->from->offset = key_offset;
_nspc_add_value(emit->env->curr, stmt->idx->var.tag.sym, stmt->idx->var.value);
if (stmt->idx.tag.sym) {
stmt->idx.value->from->offset = key_offset;
_nspc_add_value(emit->env->curr, stmt->idx.tag.sym, stmt->var.value);
//value_addref(stmt->idx->v);
emit_debug(emit, stmt->idx->var.value);
emit_debug(emit, stmt->idx.value);
}
struct Looper loop = {.exp = stmt->exp,
.stmt = stmt->body,
.offset = arr_offset,
.n = n,
.roll = each_op,
.unroll = each_op,
.idx = stmt->idx,
.idx = stmt->idx.tag.sym ? &stmt->idx : NULL,
.init = false
};
if (n) {
Expand Down Expand Up @@ -2296,7 +2295,7 @@ ANN static bool emit_stmt_each(const Emitter emit, const Stmt_Each stmt) {
m_uint end_pc = 0;
const bool ret = _emit_stmt_each(emit, stmt, &end_pc);
emit_pop_stack(emit, end_pc);
nspc_pop_value(emit->gwion->mp, emit->env->curr);
nspc_pop_value(emit->gwion->mp, emit->env->curr);
emit->status.unroll = 0;
return ret;
}
Expand All @@ -2322,21 +2321,21 @@ ANN static bool _emit_stmt_loop(const Emitter emit, const Stmt_Loop stmt,
emit->status.unroll = 0;
}
const m_uint offset = emit_local(emit, emit->gwion->type[et_int]);
if (stmt->idx) {
if (stmt->idx.tag.sym) {
emit_memsetimm(emit, emit_local(emit, emit->gwion->type[et_int]), 0);
emit_memsetimm(emit, offset, -1);
stmt->idx->var.value->from->offset = offset;
stmt->idx.value->from->offset = offset;
}
CHECK_B(emit_exp_pop_next(emit, stmt->cond));
emit_regmove(emit, -SZ_INT);
emit_regtomem(emit, offset + !!stmt->idx * SZ_INT, 0);
emit_regtomem(emit, offset + !!stmt->idx.tag.sym * SZ_INT, 0);
*index = emit_code_size(emit);
struct Looper loop = {.exp = stmt->cond,
.stmt = stmt->body,
.offset = offset,
.n = n,
.roll =
!stmt->idx ? stmt_loop_roll : stmt_loop_roll_idx};
!stmt->idx.tag.sym ? stmt_loop_roll : stmt_loop_roll_idx};
CHECK_B(looper_run(emit, &loop));
const Instr _goto = emit_add_instr(emit, Goto);
_goto->m_val = *index;
Expand Down
4 changes: 2 additions & 2 deletions src/import/import_fdef.c
Expand Up @@ -59,7 +59,7 @@ ANEW ANN static Func_Def import_fdef(const Gwi gwi, ImportCK *ck) {
}

ANN static bool section_fdef(const Gwi gwi, const Func_Def fdef) {
Section section = MK_SECTION(func, func_def, fdef);
Section section = MK_SECTION(func, func_def, fdef, gwi->loc);
gwi_body(gwi, &section);
return true;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ ANN static Fptr_Def import_fptr(const Gwi gwi) {
}

ANN static bool section_fptr(const Gwi gwi, const Fptr_Def fdef) {
Section section = MK_SECTION(fptr, fptr_def, fdef);
Section section = MK_SECTION(fptr, fptr_def, fdef, gwi->loc);
gwi_body(gwi, &section);
return true;
}
Expand Down
1 change: 1 addition & 0 deletions src/import/import_internals.c
Expand Up @@ -38,6 +38,7 @@ ANN static bool run_with_doc(const Gwi gwi, bool (*f)(const Gwi)) {
gwi->gwfmt = &gwfmter;
const bool ret = f(gwi);
fprintf(stdout, "%s", ls.text.str);
free_mstr(gwi->gwion->mp, ls.text.str);
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion src/import/import_item.c
Expand Up @@ -20,7 +20,7 @@ ANN bool gwi_item_ini(const Gwi gwi, const restrict m_str type,
ANN static bool gwi_item_tmpl(const Gwi gwi) {
Stmt_List slist = new_mp_vector(gwi->gwion->mp, Stmt, 1);
mp_vector_set(slist, Stmt, 0, MK_STMT_EXP(gwi->loc, gwi->ck->exp));
Section section = MK_SECTION(stmt, stmt_list, slist);
Section section = MK_SECTION(stmt, stmt_list, slist, gwi->loc);
gwi_body(gwi, &section);
mp_free2(gwi->gwion->mp, sizeof(ImportCK), gwi->ck);
gwi->ck = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/import/import_prim.c
Expand Up @@ -230,7 +230,7 @@ ANN bool gwi_primitive(const Gwi gwi, const m_str name, const m_uint size, const
free_prim_def(gwi->gwion->mp, pdef);
return ret;
}
Section section = MK_SECTION(primitive, prim_def, pdef);
Section section = MK_SECTION(primitive, prim_def, pdef, gwi->loc);
gwi_body(gwi, &section);
return true;
}
10 changes: 5 additions & 5 deletions src/lib/array.c
Expand Up @@ -807,24 +807,24 @@ static OP_CHECK(opck_array_scan) {
= ts->t != t_array ? ts->t : known_type(env, mp_vector_at(ts->td->types, TmplArg, 0)->d.td));
if (base->size == 0) {
gwerr_basic("Can't use type of size 0 as array base", NULL, NULL,
"/dev/null", (loc_t) {}, 0);
env->context->name, ts->td->tag.loc, 0);
env_set_error(env, true);
return env->gwion->type[et_error];
}
if (tflag(base, tflag_ref)) {
gwerr_basic("Can't use ref types as array base", NULL, NULL, "/dev/null",
(loc_t) {}, 0);
gwerr_basic("Can't use ref types as array base", NULL, NULL,
env->context->name, ts->td->tag.loc, 0);
env_set_error(env, true);
return env->gwion->type[et_error];
}
const Symbol sym = array_sym(env, array_base_simple(base), base->array_depth + 1);
const Type type = nspc_lookup_type1(base->info->value->from->owner, sym);
if (type) return type;
const Class_Def cdef = cpy_class_def(env->gwion->mp, c);
cdef->base.ext = type2td(env->gwion, t_array, (loc_t){});
cdef->base.ext = type2td(env->gwion, t_array, t_array->info->value->from->loc);
cdef->base.tag.sym = sym;
cdef->base.tmpl->call = new_mp_vector(env->gwion->mp, TmplArg, 1);
TmplArg arg = {.type = tmplarg_td, .d = {.td = type2td(env->gwion, base, (loc_t){})} };
TmplArg arg = {.type = tmplarg_td, .d = {.td = type2td(env->gwion, base, base->info->value->from->loc)} };
mp_vector_set(cdef->base.tmpl->call, TmplArg, 0, arg);
const Context ctx = env->context;
env->context = base->info->value->from->ctx;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/dict.c
Expand Up @@ -517,10 +517,10 @@ static INSTR(DictEachIdx) {
static OP_EMIT(opem_dict_each) {
Looper *loop = (Looper *)data;
HMapInfo *const hinfo = (HMapInfo*)loop->exp->type->nspc->class_data;
if(loop->idx && !loop->init) loop->idx->var.value->from->offset = emit_localn(emit, hinfo->key);
const Instr instr = emit_add_instr(emit, !loop->idx ? DictEach : DictEachIdx);
if(loop->idx && !loop->init) loop->idx->value->from->offset = emit_localn(emit, hinfo->key);
const Instr instr = emit_add_instr(emit, !loop->idx->tag.sym ? DictEach : DictEachIdx);
instr->m_val2 = loop->offset;
if(loop->idx) instr->m_val = loop->idx->var.value->from->offset;
if(loop->idx) instr->m_val = loop->idx->value->from->offset;
if(loop->n)instr->m_val2 += SZ_INT;
const Instr go = emit_add_instr(emit, BranchNeqInt);
if(!loop->n) loop->instr = go;
Expand Down
29 changes: 16 additions & 13 deletions src/parse/check.c
Expand Up @@ -1351,7 +1351,7 @@ ANN Type check_exp(const Env env, Exp* exp) {
do {
if(curr->type) continue;
if(curr->poison) continue;
if(!(curr->type = check_exp_func[curr->exp_type](env, &curr->d))) {
if(!(curr->type = check_exp_func[curr->exp_type](env, &curr->d)) || curr->type->error) {
POISON_NODE(ok, env, curr);
continue;
}
Expand Down Expand Up @@ -1405,20 +1405,20 @@ ANN static inline bool for_empty(const Env env, const Stmt_For stmt) {
return true;
}

ANN static void check_idx(const Env env, const Type base, struct EachIdx_ *const idx) {
idx->var.value = new_value(env, base, idx->var.tag);
valid_value(env, idx->var.tag.sym, idx->var.value);
SET_FLAG(idx->var.value, const);
ANN static void check_idx(const Env env, const Type base, Var_Decl *const idx) {
idx->value = new_value(env, base, idx->tag);
valid_value(env, idx->tag.sym, idx->value);
SET_FLAG(idx->value, const);
}

/** sets for the key expression value
with eg type *int* for an array or the *Key* type of a Dict **/
ANN static bool check_each_idx(const Env env, Exp* exp, struct EachIdx_ *const idx) {
ANN static bool check_each_idx(const Env env, Exp* exp, Var_Decl *const idx) {
struct Op_Import opi = {
.lhs = exp->type,
.op = insert_symbol("@each_idx"),
.data = (m_uint)exp,
.loc = idx->var.tag.loc
.loc = idx->tag.loc
};
DECL_B(const Type, t, = op_check(env, &opi));
check_idx(env, t, idx);
Expand All @@ -1439,16 +1439,16 @@ ANN static Type check_each_val(const Env env, Exp* exp) {

ANN static bool do_stmt_each(const Env env, const Stmt_Each stmt) {
CHECK_B(check_exp(env, stmt->exp));
if (stmt->idx)
CHECK_B(check_each_idx(env, stmt->exp, stmt->idx));
if (stmt->idx.tag.sym)
CHECK_B(check_each_idx(env, stmt->exp, &stmt->idx));
DECL_B(const Type, ret, = check_each_val(env, stmt->exp));
stmt->var.value = new_value(env, ret, stmt->tag);
valid_value(env, stmt->tag.sym, stmt->var.value);
stmt->var.value = new_value(env, ret, stmt->var.tag);
valid_value(env, stmt->var.tag.sym, stmt->var.value);
return check_conts(env, stmt_self(stmt), stmt->body);
}

ANN static bool do_stmt_repeat(const Env env, const Stmt_Loop stmt) {
if (stmt->idx) check_idx(env, env->gwion->type[et_int], stmt->idx);
if (stmt->idx.tag.sym) check_idx(env, env->gwion->type[et_int], &stmt->idx);
return check_conts(env, stmt_self(stmt), stmt->body);
}

Expand Down Expand Up @@ -2076,6 +2076,7 @@ ANN static bool check_trait_def(const Env env, const Trait_Def pdef) {
}

ANN bool check_fptr_def(const Env env, const Fptr_Def fptr) {
if(fptr->cdef->base.type->error) return false;
if(GET_FLAG(fptr->cdef, global)) env_push_global(env);
const bool ret = check_class_def(env, fptr->cdef);
if(GET_FLAG(fptr->cdef, global)) env_pop(env, 0);
Expand Down Expand Up @@ -2232,8 +2233,10 @@ ANN static bool check_class_tmpl(const Env env, const Tmpl *tmpl, const Nspc nsp
for(uint32_t i = 0; i < tmpl->list->len; i++) {
const TmplArg targ = *mp_vector_at(tmpl->call, TmplArg, i);
if(likely(targ.type == tmplarg_td)) continue;
if(!check_exp(env, targ.d.exp))
if(!check_exp(env, targ.d.exp)) {
POISON_NODE(ok, env, targ.d.exp);
continue;
}
const Specialized spec = *mp_vector_at(tmpl->list, Specialized, i);
const Value v = new_value(env, targ.d.exp->type, MK_TAG(spec.tag.sym, targ.d.exp->loc));
valuefrom(env, v->from);
Expand Down
2 changes: 1 addition & 1 deletion src/parse/check_traits.c
Expand Up @@ -59,7 +59,7 @@ ANN static bool trait_inherit(const Env env, const Type t, const Func_Def req) {
if(global) SET_FLAG(cpy->base, global);
const bool ret = traverse_func_def(env, cpy);
nspc_pop_type(env->gwion->mp, env->curr);
Section section = MK_SECTION(func, func_def, cpy);
Section section = MK_SECTION(func, func_def, cpy, t->info->value->from->loc);
if(!env->context->extend)
env->context->extend = new_mp_vector(env->gwion->mp, Section, 0);
mp_vector_add(env->gwion->mp, &env->context->extend, Section, section);
Expand Down
2 changes: 1 addition & 1 deletion src/parse/default_arg.c
Expand Up @@ -62,7 +62,7 @@ ANN Func_Def default_args(const Env env, Func_Base *fb, Ast *acc, uint32_t max)
? std_code(env, fb, max)
: new_code(env, fb, max);
const Func_Def fdef = new_func_def(env->gwion->mp, base, code);
Section section = MK_SECTION(func, func_def, fdef);
Section section = MK_SECTION(func, func_def, fdef, fb->tag.loc);
mp_vector_add(env->gwion->mp, acc, Section, section);
return fdef;
}
2 changes: 1 addition & 1 deletion src/parse/scan0.c
Expand Up @@ -64,7 +64,7 @@ ANN bool scan0_fptr_def(const Env env, const Fptr_Def fptr) {
insert_symbol("func"), args, ae_flag_static | ae_flag_private, loc);
const Func_Def fdef = new_func_def(env->gwion->mp, fbase, NULL);
Ast body = new_mp_vector(env->gwion->mp, Section, 1);
mp_vector_set(body, Section, 0, MK_SECTION(func, func_def, fdef));
mp_vector_set(body, Section, 0, MK_SECTION(func, func_def, fdef, loc));
Type_Decl* td = new_type_decl(env->gwion->mp, insert_symbol(env->gwion->type[et_closure]->name), loc);
const Class_Def cdef = new_class_def(env->gwion->mp, ae_flag_final, fptr->base->tag, td, body);
if(global) SET_FLAG(cdef, global);
Expand Down

0 comments on commit 30af8f4

Please sign in to comment.