Skip to content

Commit

Permalink
🎨 Semantic pass
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Mar 10, 2024
1 parent a9b21a5 commit 41e555f
Show file tree
Hide file tree
Showing 26 changed files with 430 additions and 376 deletions.
2 changes: 1 addition & 1 deletion ast
2 changes: 1 addition & 1 deletion fmt
Submodule fmt updated 1 files
+1 −0 src/lint.c
2 changes: 1 addition & 1 deletion include/default_args.h
@@ -1,4 +1,4 @@
#ifndef __DEFAULT_ARGS
#define __DEFAULT_ARGS
ANN Func_Def default_args(const Env env, Func_Base *fb, Ast *acc, uint32_t max);
ANN Func_Def default_args(const Sema *sema, Func_Base *fb, Ast *acc, uint32_t max);
#endif
1 change: 1 addition & 0 deletions include/env/env.h
Expand Up @@ -57,6 +57,7 @@ ANN Value mk_class(const Env env, const Type base, const loc_t);
ANN bool compat_func(const __restrict__ Func_Def,
const __restrict__ Func_Def);
ANN Type known_type(const Env env, Type_Decl *);
ANN m_str tdpp(MemPool, SymTable*, const Type_Decl*, const bool no_color);
ANN Type prim_ref(const Env env, const Type t, const Type_Decl *td);
ANN bool env_access(const Env env, const ae_flag flag, const loc_t);
ANN bool env_storage(const Env env, ae_flag flag, const loc_t);
Expand Down
3 changes: 2 additions & 1 deletion include/env/envset.h
Expand Up @@ -28,7 +28,8 @@ ANN2(1) static inline void envset_popv(struct EnvSet *es, const Value v) {

ANN static inline bool envset_pushf(struct EnvSet *es, const Value owner) {
CHECK_B(envset_pushv(es, owner));
return env_pushv(es->env, owner);
/*return*/ env_pushv(es->env, owner);
return true;
}
ANN2(1) static inline void envset_popf(struct EnvSet *es, const Value owner) {
env_pop(es->env, es->scope);
Expand Down
1 change: 1 addition & 0 deletions include/gwiondata.h
Expand Up @@ -17,6 +17,7 @@ typedef struct GwionData_ {
threadpool_t *tpool;
bool cdoc;
bool errored;
bool color;
} GwionData;

ANN GwionData *new_gwiondata(const MemPool, const uint32_t thread_count, const uint32_t queue_size);
Expand Down
4 changes: 4 additions & 0 deletions include/sema.h
@@ -1 +1,5 @@
#ifndef __SEMA
ANN bool sema_pass(Env a, Ast *b);
ANN bool sema_variadic_class(Env, Class_Def);
ANN bool sema_variadic_func(Env, Func_Def);
#endif
16 changes: 11 additions & 5 deletions include/sema_private.h
@@ -1,12 +1,18 @@
typedef struct {
char *filename;
MemPool mp;
//Context context;
SymTable *st;
MP_Vector *tmpls;
Stmt_List *stmt_list;
struct PPArg_ *ppa;
bool error;
bool func;
bool scope;
bool handling;
bool in_variadic;
} Sema;

#ifdef __SEMA_IMPLEMENTATION__
ANN static bool sema_array_sub(Sema *a, Array_Sub b);
ANN static bool sema_tmpl(Sema *a, Tmpl *b);
ANN static bool sema_range(Sema *a, Range *b);
Expand Down Expand Up @@ -57,9 +63,8 @@ ANN static bool sema_stmt_pp(Sema *a, Stmt_PP b);
ANN static bool sema_stmt_retry(Sema *a, Stmt_Exp b);
ANN static bool sema_stmt_try(Sema *a, Stmt_Try b);
ANN static bool sema_stmt_defer(Sema *a, Stmt_Defer b);
ANN static bool sema_stmt(Sema *a, Stmt* b);
ANN static bool sema_stmt_list(Sema *a, Stmt_List b);
ANN static bool sema_func_base(Sema *a, Func_Base *b);
ANN static bool sema_stmt(Sema *a, Stmt* b, bool in_list);
ANN static bool sema_stmt_list(Sema *a, Stmt_List *b);
ANN static bool sema_func_def(Sema *a, Func_Def b);
ANN static bool sema_class_def(Sema *a, Class_Def b);
ANN static bool sema_trait_def(Sema *a, Trait_Def b);
Expand All @@ -69,4 +74,5 @@ ANN static bool sema_fptr_def(Sema *a, Fptr_Def b);
ANN static bool sema_type_def(Sema *a, Type_Def b);
ANN static bool sema_extend_def(Sema *a, Extend_Def b);
ANN static bool sema_section(Sema *a, Section *b);
ANN static bool sema_ast(Sema *a, Ast b);
ANN static bool sema_ast(Sema *a, Ast *b);
#endif
12 changes: 0 additions & 12 deletions include/spread.h

This file was deleted.

20 changes: 11 additions & 9 deletions src/emit/emit.c
Expand Up @@ -599,10 +599,10 @@ ANN static inline bool emit_exp_pop_next(const Emitter emit, Exp* e);

ANN static bool emit_range(const Emitter emit, Range *range) {
if (range->start)
CHECK_B(emit_exp_pop_next(emit, range->start));
CHECK_B(emit_exp(emit, range->start));
else emit_pushimm(emit, 0);
if (range->end)
CHECK_B(emit_exp_pop_next(emit, range->end));
CHECK_B(emit_exp(emit, range->end));
else emit_pushimm(emit, -1);
return true;
}
Expand Down Expand Up @@ -1624,12 +1624,14 @@ ANN bool emit_exp_call1(const Emitter emit, const Func f,
{
const Type t = f->value_ref->from->owner_class;
if(t && (!emit->env->curr || !isa(t, emit->env->class_def)))
//!is_new(f->def) || f->value_ref->from->owner_class->array_depth)
//if(f->value_ref->from->owner_class->array_depth)

//!is_new(f->def) || f->value_ref->from->owner_class->array_depth)

//if(f,->value_ref->from->owner_class->array_depth)
CHECK_B(emit_ensure_func(emit, f));
}
} else if(is_static)
push_func_code(emit, f);
push_func_code(emit, f);
call_finish(emit, f, size, is_static);
emit->status = status;
return true;
Expand Down Expand Up @@ -1860,7 +1862,7 @@ ANN static bool emit_exp_if(const Emitter emit, const Exp_If *exp_if) {
const m_uint nval = m_vector_size(&emit->code->live_values);
const uint16_t offset = emit->code->frame->curr_offset;
const uint16_t vcount = emit->code->frame->value_count;
CHECK_B(emit_exp_pop_next(emit, e));
CHECK_B(emit_exp(emit, e));
const m_uint nval_if = m_vector_size(&emit->code->live_values);
if(nval < nval_if) {
const m_uint diff = nval_if - nval;
Expand All @@ -1874,7 +1876,7 @@ ANN static bool emit_exp_if(const Emitter emit, const Exp_If *exp_if) {

const Instr op2 = emit_add_instr(emit, Goto);
op->m_val = emit_code_size(emit);
const bool ret = emit_exp_pop_next(emit, exp_if->else_exp);
const bool ret = emit_exp(emit, exp_if->else_exp);
const m_uint nval_else = m_vector_size(&emit->code->live_values);
if(nval < nval_else) {
const m_uint diff = nval_else - nval;
Expand Down Expand Up @@ -2022,7 +2024,7 @@ ANN static bool emit_stmt_return(const Emitter emit, const Stmt_Exp stmt) {
// if(!stmt->val->ref && tflag(stmt->val->type, tflag_compound))
// emit_local(emit, stmt->val->type);
emit->status.in_return = true;
CHECK_B(emit_exp_pop_next(emit, stmt->val));
CHECK_B(emit_exp(emit, stmt->val));
emit->status.in_return = false;
}
vector_add(&emit->code->stack_return, (vtype)emit_add_instr(emit, Goto));
Expand Down Expand Up @@ -2321,7 +2323,7 @@ ANN static bool _emit_stmt_loop(const Emitter emit, const Stmt_Loop stmt,
emit_memsetimm(emit, offset, -1);
stmt->idx.value->from->offset = offset;
}
CHECK_B(emit_exp_pop_next(emit, stmt->cond));
CHECK_B(emit_exp(emit, stmt->cond));
emit_regmove(emit, -SZ_INT);
emit_regtomem(emit, offset + !!stmt->idx.tag.sym * SZ_INT, 0);
*index = emit_code_size(emit);
Expand Down
8 changes: 5 additions & 3 deletions src/gwion.c
@@ -1,3 +1,4 @@
#include <stdbool.h>
#include <unistd.h>
#include "gwion_util.h"
#include "gwion_ast.h"
Expand Down Expand Up @@ -119,11 +120,12 @@ ANN bool gwion_ini(const Gwion gwion, CliArg *arg) {
new_passes(gwion);
CHECK_B(arg_parse(gwion, arg));
if (arg->color == COLOR_NEVER)
tcol_override_color_checks(0);
gwion->data->color = false;
else if (arg->color == COLOR_AUTO)
tcol_override_color_checks(isatty(1));
gwion->data->color = isatty(1);
else if (arg->color == COLOR_ALWAYS)
tcol_override_color_checks(1);
gwion->data->color = true;
tcol_override_color_checks(gwion->data->color);
if(!vector_size(&gwion->data->passes->vec)) {
if (!gwion->data->cdoc)
pass_default(gwion);
Expand Down
52 changes: 27 additions & 25 deletions src/lib/object_op.c
Expand Up @@ -13,6 +13,7 @@
#include "parse.h"
#include "gwi.h"
#include "tmpl_info.h"
#include "sema.h"

#undef insert_symbol

Expand Down Expand Up @@ -279,7 +280,8 @@ ANN static bool scantmpl_class_def(const Env env, struct tmpl_info *info) {
if(c->body) cdef->body = cpy_ast(env->gwion->mp, c->body);
cdef->cflag = c->cflag;
cdef->base.tmpl = mk_tmpl(env, c->base.tmpl, info->td->types);
const bool ret = scan0_class_def(env, cdef);
const bool ret = (!is_spread_tmpl(cdef->base.tmpl) || sema_variadic_class(env, cdef)) &&
scan0_class_def(env, cdef);
if ((info->ret = cdef->base.type)) {
info->ret->info->cdef = cdef;
set_tflag(info->ret, tflag_cdef);
Expand Down Expand Up @@ -374,29 +376,29 @@ static OP_EMIT(opem_cond_object) {
GWION_IMPORT(object_op) {
const Type t_error = gwi_mk_type(gwi, "@error", 0, NULL);
gwi->gwion->type[et_error] = t_error;
GWI_B(gwi_set_global_type(gwi, t_error, et_error))
GWI_B(gwi_oper_ini(gwi, "Object", "Object", NULL))
GWI_B(gwi_oper_add(gwi, opck_object_at))
GWI_B(gwi_oper_emi(gwi, opem_object_at))
GWI_B(gwi_oper_end(gwi, ":=>", NULL))
GWI_B(gwi_oper_ini(gwi, (m_str)OP_ANY_TYPE, "@Compound", NULL))
GWI_B(gwi_oper_add(gwi, opck_object_instance))
GWI_B(gwi_oper_end(gwi, "=>", NULL))
GWI_B(gwi_oper_ini(gwi, "Object", "Object", "bool"))
GWI_B(gwi_oper_end(gwi, "==", EqObject))
GWI_B(gwi_oper_end(gwi, "!=", NeqObject))
GWI_B(gwi_oper_add(gwi, opck_object_cast))
GWI_B(gwi_oper_end(gwi, "$", NULL))
GWI_B(gwi_oper_ini(gwi, NULL, "Object", "bool"))
GWI_B(gwi_oper_emi(gwi, opem_uncond_object))
GWI_B(gwi_oper_end(gwi, "@unconditional", NULL))
GWI_B(gwi_oper_emi(gwi, opem_cond_object))
GWI_B(gwi_oper_end(gwi, "@conditional", NULL))
GWI_B(gwi_oper_add(gwi, opck_unary_meta2))
GWI_B(gwi_oper_emi(gwi, opem_not_object))
GWI_B(gwi_oper_end(gwi, "!", NULL))
GWI_B(gwi_oper_ini(gwi, "@Compound", NULL, NULL))
GWI_B(gwi_oper_add(gwi, opck_struct_scan))
GWI_B(gwi_oper_end(gwi, "class", NULL))
gwi_set_global_type(gwi, t_error, et_error);
GWI_B(gwi_oper_ini(gwi, "Object", "Object", NULL))
GWI_B(gwi_oper_add(gwi, opck_object_at))
GWI_B(gwi_oper_emi(gwi, opem_object_at))
GWI_B(gwi_oper_end(gwi, ":=>", NULL))
GWI_B(gwi_oper_ini(gwi, (m_str)OP_ANY_TYPE, "@Compound", NULL))
GWI_B(gwi_oper_add(gwi, opck_object_instance))
GWI_B(gwi_oper_end(gwi, "=>", NULL))
GWI_B(gwi_oper_ini(gwi, "Object", "Object", "bool"))
GWI_B(gwi_oper_end(gwi, "==", EqObject))
GWI_B(gwi_oper_end(gwi, "!=", NeqObject))
GWI_B(gwi_oper_add(gwi, opck_object_cast))
GWI_B(gwi_oper_end(gwi, "$", NULL))
GWI_B(gwi_oper_ini(gwi, NULL, "Object", "bool"))
GWI_B(gwi_oper_emi(gwi, opem_uncond_object))
GWI_B(gwi_oper_end(gwi, "@unconditional", NULL))
GWI_B(gwi_oper_emi(gwi, opem_cond_object))
GWI_B(gwi_oper_end(gwi, "@conditional", NULL))
GWI_B(gwi_oper_add(gwi, opck_unary_meta2))
GWI_B(gwi_oper_emi(gwi, opem_not_object))
GWI_B(gwi_oper_end(gwi, "!", NULL))
GWI_B(gwi_oper_ini(gwi, "@Compound", NULL, NULL))
GWI_B(gwi_oper_add(gwi, opck_struct_scan))
GWI_B(gwi_oper_end(gwi, "class", NULL))
return true;
}
35 changes: 12 additions & 23 deletions src/parse/check.c
Expand Up @@ -15,7 +15,6 @@
#include "specialid.h"
#include "tmp_resolve.h"
#include "partial.h"
#include "spread.h"
#include "array.h"

ANN bool check_stmt(const Env env, Stmt*);
Expand Down Expand Up @@ -833,16 +832,18 @@ ANN static TmplArg_List check_template_args(const Env env, Exp_Call *exp,

if(spread) {
Exp* e = exp->args;
if(fdef->base->args)
for(uint32_t i = 0; i < fdef->base->args->len && e; i++) e = e->next;
while(e) {
TmplArg targ = {
.type = tmplarg_td,
.d = { .td = type2td(env->gwion, e->type, e->loc) }
};
mp_vector_add(env->gwion->mp, &tl, TmplArg, targ);
e = e->next;
}
const uint32_t len = fdef->base->args ? fdef->base->args->len : 0;
// if(fdef->base->args) {
for(uint32_t i = 0; i < len && e; i++) e = e->next;
while(e) {
TmplArg targ = {
.type = tmplarg_td,
.d = { .td = type2td(env->gwion, e->type, e->loc) }
};
mp_vector_add(env->gwion->mp, &tl, TmplArg, targ);
e = e->next;
}
// }
}
return tl;
}
Expand Down Expand Up @@ -1400,15 +1401,6 @@ ANN static bool check_conts(const Env env, Stmt* a, Stmt* b) {
return true;
}

ANN static inline bool for_empty(const Env env, const Stmt_For stmt) {
if (!stmt->c2 || !stmt->c2->d.stmt_exp.val)
ERR_B(stmt_self(stmt)->loc,
_("empty for loop condition..."
"...(note: explicitly use 'true' if it's the intent)"
"...(e.g., 'for(; true;){{ /*...*/ }')"));
return true;
}

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);
Expand Down Expand Up @@ -1482,7 +1474,6 @@ stmt_func_xxx(flow, Stmt_Flow, env_inline_mult(env, 1.5),
stmt_self(stmt)->stmt_type != ae_stmt_while) ||
!check_conts(env, stmt_self(stmt), stmt->body)) ? true : false)
stmt_func_xxx(for, Stmt_For, env_inline_mult(env, 1.5), !(
!for_empty(env, stmt) ||
!check_stmt(env, stmt->c1) ||
!check_flow(env, stmt->c2->d.stmt_exp.val) ||
(stmt->c3 && !check_exp(env, stmt->c3)) ||
Expand Down Expand Up @@ -2231,8 +2222,6 @@ ANN static bool recursive_type_base(const Env env, const Type t) {
return !error;
}

ANN bool check_trait_requests(const Env env, const Type t, const ID_List list, const ValueFrom *from);

ANN static bool check_class_tmpl(const Env env, const Tmpl *tmpl, const Nspc nspc) {
bool ok = true;
if(tmplarg_ntypes(tmpl->list) != tmpl->list->len) {
Expand Down
4 changes: 4 additions & 0 deletions src/parse/check_traits.c
Expand Up @@ -60,9 +60,13 @@ ANN static bool trait_inherit(const Env env, const Type t, const Func_Def req) {
const bool ret = traverse_func_def(env, cpy);
nspc_pop_type(env->gwion->mp, env->curr);
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);

// sema_ast, traverse_ast
// mp_vector_add(env->gwion->mp, &t->info->cdef->body, Section, section);
return ret;
}

Expand Down
5 changes: 3 additions & 2 deletions src/parse/func_resolve_tmpl.c
Expand Up @@ -13,7 +13,7 @@
#include "emit.h"
#include "specialid.h"
#include "tmp_resolve.h"
#include "spread.h"
#include "sema.h"

struct ResolverArgs {
const Value v;
Expand All @@ -32,7 +32,8 @@ ANN static inline Value template_get_ready(const Env env, const Value v,
ANN static inline bool
tmpl_valid(const Env env, const Func_Def fdef) {
if (safe_fflag(fdef->base->func, fflag_valid)) return true;
const bool ret = check_traverse_fdef(env, fdef);
const bool ret = (fdef->builtin || sema_variadic_func(env, fdef)) &&
check_traverse_fdef(env, fdef);
if(!fdef->base->func) free_func_def(env->gwion->mp, fdef);
return ret;
}
Expand Down

0 comments on commit 41e555f

Please sign in to comment.