Skip to content

Commit

Permalink
Fix that silly bug by bodging add_var_types_forwards,backwards
Browse files Browse the repository at this point in the history
respectively
  • Loading branch information
StavromulaBeta committed Dec 23, 2023
1 parent 48655d9 commit acada74
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 39 deletions.
2 changes: 2 additions & 0 deletions examples/hanoi.cog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
~~ TODO Prints broken

~~ Towers of Hanoi in Cognate

Def Move discs as (
Expand Down
2 changes: 2 additions & 0 deletions examples/prime.cog
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
~~ TODO prelude break this oop

Def Factor (Zero? Modulo Swap);

Def Primes (
Expand Down
24 changes: 12 additions & 12 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@
{.name="and", .calltype=call, .argc=2, .args={boolean, boolean}, .returns=true, .rettype=boolean},
{.name="xor", .calltype=call, .argc=2, .args={boolean, boolean}, .returns=true, .rettype=boolean},
{.name="not", .calltype=call, .argc=1, .args={boolean}, .returns=true, .rettype=boolean},
{.name="number?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean, /*.checks=number*/},
{.name="io?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean, /*.checks=io*/},
{.name="symbol?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean, /*.checks=symbol*/},
{.name="integer?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean},
{.name="zero?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean},
{.name="any?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean},
{.name="list?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean, /*.checks=list*/},
{.name="string?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean, /*.checks=string*/},
{.name="block?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean, /*.checks=block*/},
{.name="boolean?", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=boolean, /*.checks=boolean*/},
{.name="number?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean, /*.checks=number*/},
{.name="io?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean, /*.checks=io*/},
{.name="symbol?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean, /*.checks=symbol*/},
{.name="integer?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean},
{.name="zero?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean},
{.name="any?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean},
{.name="list?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean, /*.checks=list*/},
{.name="string?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean, /*.checks=string*/},
{.name="block?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean, /*.checks=block*/},
{.name="boolean?", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=boolean, /*.checks=boolean*/},
{.name="number!", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="symbol!", .calltype=call, .argc=1, .args={symbol}, .returns=true, .rettype=symbol},
{.name="io!", .calltype=call, .argc=1, .args={io}, .returns=true, .rettype=io},
{.name="integer!", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="zero!", .calltype=call, .argc=1, .args={number}, .returns=true, .rettype=number},
{.name="any!", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=any},
{.name="any!", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=any},
{.name="list!", .calltype=call, .argc=1, .args={list}, .returns=true, .rettype=list},
{.name="string!", .calltype=call, .argc=1, .args={string}, .returns=true, .rettype=string},
{.name="block!", .calltype=call, .argc=1, .args={block}, .returns=true, .rettype=block},
Expand Down Expand Up @@ -74,7 +74,7 @@
{.name="precompute", .calltype=call, .argc=1, .args={block}, .returns=true, .rettype=block},
{.name="wait", .calltype=call, .argc=1, .args={number}, .returns=false},
{.name="stop", .calltype=call, .argc=0, .returns=false},
{.name="show", .calltype=call, .argc=1, .args={dispatch}, .returns=true, .rettype=string},
{.name="show", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=string},
{.name="stack", .calltype=call, .returns=true, .rettype=list, .stack=true},
{.name="clear", .calltype=call, .argc=0, .stack=true},
{.name="length", .calltype=call, .argc=1, .args={list}, .returns=true, .rettype=number},
Expand Down
58 changes: 36 additions & 22 deletions src/cognac.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ const char* print_val_type(val_type_t type)
case box: return "box";
case io: return "io";
case NIL: return "NIL";
case dispatch: return "dispatch";
case strong_any: return "strong_any";
}
return NULL;
}
Expand All @@ -701,7 +701,7 @@ const char* c_val_type(val_type_t type)
case any: return "ANY";
case box: return "BOX";
case io: return "IO";
case dispatch: __builtin_trap();
case strong_any:
case NIL: __builtin_trap();
}
return NULL;
Expand Down Expand Up @@ -1664,6 +1664,7 @@ bool add_var_types_forwards(module_t* mod)
registers);
break;
}

case literal:
push_register_front(
make_register(op->op->literal->type, op),
Expand Down Expand Up @@ -1693,10 +1694,12 @@ bool add_var_types_forwards(module_t* mod)
case ret:
{
val_type_t t = pop_register_front(registers)->type;
if (t != any && func->func->rettype != t && !func->func->branch)
if (func->func->rettype != strong_any && t != any && func->func->rettype != t && !func->func->branch)
{
if (func->func->rettype != any) type_error(func->func->rettype, t, op->op->where); // type error
func->func->rettype = t;
if (func->func->rettype != any)
func->func->rettype = strong_any;
else
func->func->rettype = t;
changed = 1;
}
break;
Expand All @@ -1722,7 +1725,7 @@ bool add_var_types_forwards(module_t* mod)
for ( val_list_t* v = fn->args ; v ; v = v->next )
{
val_type_t t = pop_register_front(registers)->type;
if (fn->unique && v->val->type == any && t != any)
if (v->val->type != strong_any && fn->unique && v->val->type == any && t != any)
// yay we're allowed to mess with it
{
v->val->type = t; // kinda dodgy
Expand All @@ -1739,10 +1742,12 @@ bool add_var_types_forwards(module_t* mod)
case bind:
{
val_type_t t = pop_register_front(registers)->type;
if (t != any && op->op->word->val->type != t) // Early use needs to be bound to undefined symbol
if (op->op->word->val->type != strong_any && t != any && op->op->word->val->type != t) // Early use needs to be bound to undefined symbol
{
if (op->op->word->val->type != any) type_error(op->op->word->val->type, t, op->op->where); // type error
op->op->word->val->type = t;
if (op->op->word->val->type != any)
op->op->word->val->type = strong_any;
else
op->op->word->val->type = t;
changed = 1;
}
break;
Expand Down Expand Up @@ -1788,11 +1793,12 @@ bool add_var_types_backwards(module_t* mod)
if (r->type != any)
for (func_list_t* ff = op->op->funcs ; ff ; ff = ff->next)
{
if (ff->func->tentative_rettype != r->type)
if (ff->func->tentative_rettype != strong_any && ff->func->tentative_rettype != r->type)
{
if (ff->func->tentative_rettype != any)
type_error(r->type, ff->func->tentative_rettype, op->op->where);
ff->func->tentative_rettype = r->type;
ff->func->tentative_rettype = strong_any;
else
ff->func->tentative_rettype = r->type;
changed = 1;
}
}
Expand All @@ -1807,10 +1813,12 @@ bool add_var_types_backwards(module_t* mod)
case var:
{
val_type_t t = pop_register_front(registers)->type;
if (t != any && op->op->word->val->type != t)
if (op->op->word->val->type != strong_any && t != any && op->op->word->val->type != t)
{
if (op->op->word->val->type != any) type_error(op->op->word->val->type, t, op->op->where); // type error
op->op->word->val->type = t;
if (op->op->word->val->type != any)
op->op->word->val->type = strong_any;
else
op->op->word->val->type = t;
changed = true;
}
}
Expand Down Expand Up @@ -1851,11 +1859,12 @@ bool add_var_types_backwards(module_t* mod)
if (fn->returns)
{
reg_t* r = pop_register_front(registers);
if (fn->tentative_rettype != r->type && r->type != any)
if (fn->tentative_rettype != strong_any && fn->tentative_rettype != r->type && r->type != any)
{
if (fn->tentative_rettype != any)
type_error(r->type, fn->tentative_rettype, op->op->where);
fn->tentative_rettype = r->type;
fn->tentative_rettype = strong_any;
else
fn->tentative_rettype = r->type;
changed = 1;
}
}
Expand All @@ -1878,10 +1887,12 @@ bool add_var_types_backwards(module_t* mod)
for (val_list_t* v = func->func->args; v ; v = v->next)
{
val_type_t t = args->val->type;
if (t != any && v->val->type != t && !func->func->branch)
if (v->val->type != strong_any && t != any && v->val->type != t && !func->func->branch)
{
if (v->val->type != any) type_error(v->val->type, t, op->op->where);
v->val->type = t;
if (v->val->type != any)
v->val->type = strong_any;
else
v->val->type = t;
changed = 1;
}
args = args->next;
Expand All @@ -1902,6 +1913,9 @@ void add_var_types(module_t* mod)
changed |= add_var_types_forwards(mod);
changed |= add_var_types_backwards(mod);
}
for (func_list_t* func = mod->funcs ; func ; func = func->next)
for (word_list_t* w = func->func->locals ; w ; w = w->next)
if (w->word->val->type == strong_any) w->word->val->type = any;
}

void add_typechecks(module_t* mod)
Expand Down Expand Up @@ -3062,7 +3076,7 @@ int main(int argc, char** argv)
compute_stack,
add_registers,
//shorten_references,
inline_values,
//inline_values,
compute_variables,
resolve_early_use,
determine_unique_calls,
Expand Down
4 changes: 3 additions & 1 deletion src/cognac.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ typedef enum _val_type_t
box,
io,
any,
dispatch,
strong_any,
} val_type_t;

struct _ptr_assoc_t
Expand Down Expand Up @@ -117,6 +117,7 @@ struct _builtin_t
type_t calltype;
val_type_t storagetype;
val_type_t rettype;
//val_type_t checks;
};

struct _val_list_t
Expand Down Expand Up @@ -183,6 +184,7 @@ struct _func_t
func_list_t* calls;
val_list_t* args;
val_type_t rettype;
val_type_t checks;
val_type_t tentative_rettype;
size_t argc;
char* name;
Expand Down
2 changes: 0 additions & 2 deletions src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,6 @@ static LIST ___map(BLOCK, LIST);
static LIST ___filter(BLOCK, LIST);
static LIST ___range(NUMBER, NUMBER);
static ANY ___index(NUMBER, LIST);
static void ___puts(BLOCK);
static void ___prints(BLOCK);
static BLOCK ___precompute(BLOCK);
static void ___wait(NUMBER);
static LIST ___split(STRING, STRING);
Expand Down
4 changes: 2 additions & 2 deletions tests/dispatch.cog
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Let N be "PASS: dynamic dispatch";

Do If Number? N (Print N)
else (Print + 1 N);
Do If Number? N (Print + 1 N)
else (Print N);

0 comments on commit acada74

Please sign in to comment.