Skip to content

Commit

Permalink
Don't try and catch overloading errors while the type inference state…
Browse files Browse the repository at this point in the history
… is still in flux. Also remove Join in favor of overloading Append/Prepend for strings.
  • Loading branch information
StavromulaBeta committed Sep 15, 2024
1 parent 635b264 commit 520adb3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
{.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={any}, .returns=true, .rettype=boolean, .overload=true, .overloads={list, string, table, NIL}},
{.name="join", .calltype=call, .argc=2, .args={string, string}, .returns=true, .rettype=string},
{.name="append", .calltype=call, .argc=2, .args={any, any}, .returns=true, .rettype=any, .overload=true, .overloads={string, list, NIL}, .overload_returns={string, list, NIL}},
{.name="substring", .calltype=call, .argc=3, .args={number, number, string}, .returns=true, .rettype=string},
{.name="regex", .calltype=call, .argc=2, .args={string, string}, .returns=true, .rettype=boolean},
{.name="regex-match", .calltype=call, .argc=2, .args={string, string}, .returns=true, .rettype=boolean, .stack=true},
Expand Down
4 changes: 4 additions & 0 deletions src/cognac.c
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,7 @@ bool add_var_types_forwards(module_t* mod)
op->op->func = new_fn;
goto end;
}
/*
char buf[100] = "expected ";
for (int i = 0 ; fn->overloads[i] != NIL ; ++i)
{
Expand All @@ -1969,6 +1970,7 @@ bool add_var_types_forwards(module_t* mod)
strcat(buf, "\ngot ");
strcat(buf, print_val_type(t));
throw_error(buf, op->op->where);
*/
}
else if (fn->overload && v->val->type != any && v->val->type != t)
{
Expand All @@ -1985,9 +1987,11 @@ bool add_var_types_forwards(module_t* mod)
}
if (fn->stack) assert(registers->len == 0);
if (fn->returns)
{
push_register_front(
make_register(fn->rettype, op),
registers);
}
break;
}
case bind:
Expand Down
29 changes: 7 additions & 22 deletions src/prelude.cog
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ Builds a string from a block parameter and prints it to standard output, without
Puts ( "The square of 10 is " * Twin 10 "\n");
```
~
Def Puts ( Put Fold ( Join Show ) from "" over Reverse List);
Def Puts ( Put Fold ( Prepend Show ) from "" over Reverse List);

~
Builds a string from a block parameter and prints it to standard output, with a newline.
Expand All @@ -223,7 +223,7 @@ Builds a string from a block parameter and prints it to standard output, with a
Puts ( "The square of 10 is " * Twin 10);
```
~
Def Prints ( Print Fold ( Join Show ) from "" over Reverse List);
Def Prints ( Print Fold ( Prepend Show ) from "" over Reverse List);

~
Takes a block parameter `Predicate` and a list `L`. Applies `Predicate` to each element in `L`. Returns a list containing only the elements where `Predicate` evaluated to True.
Expand Down Expand Up @@ -335,26 +335,11 @@ Def None (
);

~
Takes two list parameters and returns a new list created by joining the first list onto the end of the second list.

```
Print Append List (4 5 6) to List (1 2 3);
```
~
Def Append (
Let L2;
Let L1;
L2;
For each in Reverse L1 (
Push;
)
);

~
Takes two list parameters and returns a new list created by joining the second list onto the end of the first list.
Takes two list or string parameters and returns a new list/string created by joining the second list/string onto the end of the first list/string.

```
Print Prepend List (1 2 3) to List (4 5 6);
Print Prepend "hello" to "world";
```
~
Def Prepend ( Swap ; Append );
Expand Down Expand Up @@ -477,7 +462,7 @@ Def Index (
Let N be Of (Integer?);
Let L;

When < 0 N ( Error Join "Invalid index " Show N );
When < 0 N ( Error Prepend "Invalid index " Show N );
When Empty? L ( Error "Index is beyond the end" );

Do If Zero? N ( return First element of L )
Expand All @@ -497,7 +482,7 @@ Def Range (

When > End Start ~~ TODO? maybe we could have this create a reverse range.
(
Error Join Join Join "Invalid range " Show Start "..." Show End;
Error Prepend Prepend Prepend "Invalid range " Show Start "..." Show End;
);

Def Range-helper (
Expand All @@ -523,7 +508,7 @@ Def Assert (
Let Assertion be String!;
Let Result be Boolean!;

Unless Result ( Error Join Join "Failed assertion: '" Assertion "'" );
Unless Result ( Error Prepend Prepend "Failed assertion: '" Assertion "'" );
);

~
Expand Down
33 changes: 33 additions & 0 deletions src/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -2729,6 +2729,39 @@ static BOOLEAN ___regexHmatch(STRING reg_str, STRING str)
return found != REG_NOMATCH;
}

static LIST ___append_LIST(LIST l1, LIST l2)
{
if (!l2) return l1;
else return ___push(___first_LIST(l2), ___append_LIST(l1, ___rest_LIST(l2)));
}

static STRING ___append_STRING(STRING s1, STRING s2)
{
size_t len1 = strlen(s1);
size_t len2 = strlen(s2);
char* output = gc_malloc(len1 + len2 + 1);
strcpy(output, s2);
strcpy(output+len2, s1);
output[len1+len2] = '\0';
return (STRING)output;
}

static ANY ___append(ANY a1, ANY a2)
{
cognate_type t = type_of(a1);
switch (t)
{
case LIST_TYPE:
return box_LIST(___append_LIST(unbox_LIST(a1), unbox_LIST(a2)));
case STRING_TYPE:
return box_STRING(___append_STRING(unbox_STRING(a1), unbox_STRING(a2)));
default: type_error("List or String", a1);
}
#ifdef __TINYC__
return NIL;
#endif
}

static BOOLEAN ___numberQ_NUMBER(NUMBER _) { return true; }
static BOOLEAN ___numberQ_LIST(LIST _) { return false; }
static BOOLEAN ___numberQ_BOX(BOX _) { return false; }
Expand Down
2 changes: 1 addition & 1 deletion tests/hanoi.cog
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Def Concatenate as ( Fold ( Join Show ) from "" over Reverse List );
Def Concatenate as ( Fold ( Prepend Show ) from "" over Reverse List );

Def Move discs as (

Expand Down
2 changes: 1 addition & 1 deletion tests/strings.cog
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Print If == "Hello world ☺" Join Join the strings "Hello" " world" " ☺"
Print If == "Hello world ☺" Prepend Prepend the strings "Hello" " world" " ☺"
"PASS: Converting list of strings to combined string"
else
"FAIL: Converting list of characters to combined string";
Expand Down

0 comments on commit 520adb3

Please sign in to comment.