Skip to content

Commit

Permalink
Add provisions to overload the return type also
Browse files Browse the repository at this point in the history
  • Loading branch information
StavromulaBeta committed Sep 4, 2024
1 parent 8cb9065 commit dc518b8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
{.name="block!", .calltype=call, .argc=1, .args={block}, .returns=true, .rettype=block},
{.name="boolean!", .calltype=call, .argc=1, .args={boolean},.returns=true, .rettype=boolean},

{.name="first", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=any, .overload=true, .overloads={list,string,NIL}},
{.name="rest", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=any, .overload=true, .overloads={list,string,NIL}, .overload_to_return=true},
{.name="first", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=any, .overload=true, .overloads={list,string,NIL}, .overload_returns={any, string, NIL}},
{.name="rest", .calltype=call, .argc=1, .args={any}, .returns=true, .rettype=any, .overload=true, .overloads={list,string,NIL}, .overload_returns={list, string, NIL}},
{.name="push", .calltype=call, .argc=2, .args={any, list}, .returns=true, .rettype=list},
{.name="empty?", .calltype=call, .argc=1, .args={list}, .returns=true, .rettype=boolean},
{.name="join", .calltype=call, .argc=2, .args={string, string}, .returns=true, .rettype=string},
Expand Down
8 changes: 4 additions & 4 deletions src/cognac.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func_t* make_func(ast_list_t* tree, char* name)
func->has_regs = false;
func->overload = false;
func->overloaded_to = any;
func->overload_to_return = false;
func->overload_returns[0] = NIL;
func->builtin = false;
func->name = name;
func->locals = NULL;
Expand Down Expand Up @@ -1931,8 +1931,8 @@ bool add_var_types_forwards(module_t* mod)
for ( val_list_t* vv = new_fn->args ; vv ; vv = vv->next )
if (vv->val == v->val) vv->val = make_value(t, v->val->source);
new_fn->overloaded_to = t;
if (fn->overload_to_return && new_fn->returns)
new_fn->rettype = t;
if (fn->overload_returns[0] != NIL && new_fn->returns)
new_fn->rettype = fn->overload_returns[i];
op->op->func = new_fn;
goto end;
}
Expand Down Expand Up @@ -3467,8 +3467,8 @@ word_list_t* builtins(void)
fn->overloaded_to = any;
fn->unmangled_name = b[i].name;
fn->overload = b[i].overload;
fn->overload_to_return = b[i].overload_to_return;
if (b[i].overload) memcpy(&fn->overloads, &b[i].overloads, sizeof(b[i].overloads));
if (b[i].overload) memcpy(&fn->overload_returns, &b[i].overload_returns, sizeof(b[i].overload_returns));
type_t calltype = b[i].calltype;
for (int ii = b[i].argc-1 ; ii >= 0 ; --ii)
fn->args = push_val(make_value(b[i].args[ii], NULL), fn->args);
Expand Down
4 changes: 2 additions & 2 deletions src/cognac.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ struct _builtin_t
val_type_t storagetype;
val_type_t rettype;
val_type_t overloads[10];
val_type_t overload_returns[10];
bool overload;
bool overload_to_return;
//val_type_t checks;
};

Expand Down Expand Up @@ -197,8 +197,8 @@ struct _func_t
size_t argc;
char* name;
val_type_t overloads[10];
val_type_t overload_returns[10];
val_type_t overloaded_to;
bool overload_to_return;
bool overload;
bool returns;
bool stack;
Expand Down
8 changes: 4 additions & 4 deletions src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static BOOLEAN ___ioQ(ANY);
static BOOLEAN ___zeroQ(ANY);
static ANY ___first(ANY);
static ANY ___rest(ANY);
static ANY ___first_STRING(STRING);
static STRING ___first_STRING(STRING);
static STRING ___rest_STRING(STRING);
static ANY ___first_LIST(LIST);
static LIST ___rest_LIST(LIST);
Expand Down Expand Up @@ -1493,10 +1493,10 @@ static LIST ___rest_LIST(LIST lst)
return lst->next;
}

static ANY ___first_STRING(STRING str)
static STRING ___first_STRING(STRING str)
{
if unlikely(!*str) throw_error("empty string is invalid");
return box_STRING(gc_strndup((char*)str, mblen(str, MB_CUR_MAX)));
return gc_strndup((char*)str, mblen(str, MB_CUR_MAX));
}

static STRING ___rest_STRING(STRING str)
Expand All @@ -1510,7 +1510,7 @@ static ANY ___first(ANY a)
switch(a.type)
{
case list: return ___first_LIST(a.list);
case string: return ___first_STRING(a.string);
case string: return box_STRING(___first_STRING(a.string));
default: type_error("string or list", a);
}
}
Expand Down

0 comments on commit dc518b8

Please sign in to comment.