From 520adb365d9568b262bc9bfb4da301c1579cd9fe Mon Sep 17 00:00:00 2001 From: Finn Barber Date: Sun, 15 Sep 2024 12:38:38 +0100 Subject: [PATCH] Don't try and catch overloading errors while the type inference state is still in flux. Also remove Join in favor of overloading Append/Prepend for strings. --- src/builtins.c | 2 +- src/cognac.c | 4 ++++ src/prelude.cog | 29 +++++++---------------------- src/runtime.h | 33 +++++++++++++++++++++++++++++++++ tests/hanoi.cog | 2 +- tests/strings.cog | 2 +- 6 files changed, 47 insertions(+), 25 deletions(-) diff --git a/src/builtins.c b/src/builtins.c index c84a270..54e2f36 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -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}, diff --git a/src/cognac.c b/src/cognac.c index 15b550c..df3f367 100755 --- a/src/cognac.c +++ b/src/cognac.c @@ -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) { @@ -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) { @@ -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: diff --git a/src/prelude.cog b/src/prelude.cog index 81e9fa1..614d3bf 100644 --- a/src/prelude.cog +++ b/src/prelude.cog @@ -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. @@ -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. @@ -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 ); @@ -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 ) @@ -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 ( @@ -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 "'" ); ); ~ diff --git a/src/runtime.h b/src/runtime.h index 4e4398c..3bcdb11 100644 --- a/src/runtime.h +++ b/src/runtime.h @@ -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; } diff --git a/tests/hanoi.cog b/tests/hanoi.cog index 6aebb1f..168612a 100644 --- a/tests/hanoi.cog +++ b/tests/hanoi.cog @@ -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 ( diff --git a/tests/strings.cog b/tests/strings.cog index c639658..6cd49b8 100644 --- a/tests/strings.cog +++ b/tests/strings.cog @@ -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";