diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2022-04-04 21:08:50 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2022-04-04 21:08:50 -0700 |
commit | f0f66890cc79338265ccd384288b73080ff2c939 (patch) | |
tree | 9c4496cd9be50f59b7940a74ba6e9cac4bcf744f /cppawk-include/cons-priv.h | |
parent | fc1c70004db5ebf8e4124ee1bc655ad0358e8a6f (diff) | |
download | cppawk-f0f66890cc79338265ccd384288b73080ff2c939.tar.gz cppawk-f0f66890cc79338265ccd384288b73080ff2c939.tar.bz2 cppawk-f0f66890cc79338265ccd384288b73080ff2c939.zip |
start test cases for <cons.h> material.
Small documentation tweak. Numerous bugfixes as a result of
testing: null, endp, stringp, symbolp, box, unbox, cdr,
equal all found to have some issues.
Diffstat (limited to 'cppawk-include/cons-priv.h')
-rw-r--r-- | cppawk-include/cons-priv.h | 60 |
1 files changed, 30 insertions, 30 deletions
diff --git a/cppawk-include/cons-priv.h b/cppawk-include/cons-priv.h index 1e32624..271d840 100644 --- a/cppawk-include/cons-priv.h +++ b/cppawk-include/cons-priv.h @@ -134,17 +134,16 @@ function __atom(__obj) function __null(__obj) { - return __obj == __nil + return __obj == __nil && __obj != 0 } -#define __null(obj) ((obj) == __nil) function __endp(__obj) { - if (__obj == __nil) + if (__obj == __nil && __obj != 0) return 1 - if (__consp(__obj)) - return __nil - __error("endp: a proper list ends with nil, not %s", __obj) + if (__consp(__obj)) + return 0 + __error("endp: a proper list ends with nil, not %s", __obj) } function __bs_esc(raw) @@ -161,15 +160,14 @@ function __numberp(__obj) function __stringp(__obj) { - return __typecode(obj) == "T" + return __typecode(__obj) == "T" } -#define __stringp(obj) (substr(obj, 1, 1) == "T") +#define __stringp(obj) (__typecode(obj) == "T") function __symbolp(__obj) { - return __typecode(obj) ~ /^S?$/ + return __obj != 0 && __typecode(__obj) ~ /^S?$/ } -#define __symbolp(obj) (substr(obj, 1, 1) ~ /^S?$/) function __box(__raw, __check, @@ -181,18 +179,19 @@ function __box(__raw, if (__present(__check)) __error("box; excess argument %s", __check) - __case (__typecode(__raw)) { - __of ("T", "S", "C") - __cret ("T" __raw) // represent as unescaped string - __otherwise - __cret (__raw) - } + if (__numberp(__raw)) + return __raw; + + return "T" __raw; } function __unbox(__obj, __check, __case_temps) { + if (!__present(__obj)) + __error("unbox: missing or undefined argument") + if (__present(__check)) __error("unbox; excess argument %s", __check) @@ -247,14 +246,14 @@ function __cdr(__cell, __col = match(__cell, /:/) __com = match(__cell, /,/) if (__col == 0 || __com == 0 || __col <= __com) - __error("car: %s has a malformed cons header", __cell) + __error("cdr: %s has a malformed cons header", __cell) __alen = substr(__cell, 2, __com - 2 + 1) __dlen = substr(__cell, __com + 1, __col - __com) return substr(__cell, __col + 1 + __alen, __dlen) } else if (__null(__cell)) { return __nil } else { - __error("car: %s isn't a cons", __cell) + __error("cdr: %s isn't a cons", __cell) } } @@ -354,6 +353,15 @@ function __keys(__array, return __list_end(__tmp) } + +function __equal(__obj1, __obj2) +{ + if (__obj1 == __obj2) + return 1; + return __slow_equal(__obj1, __obj2) +} +#define __equal(obj1, obj2) ((obj1) == (obj2) ? 1 : __slow_equal(obj1, obj2)) + function __slow_equal(__obj1, __obj2, __tc1, __tc2, __case_temps) { @@ -364,7 +372,7 @@ function __slow_equal(__obj1, __obj2, __of ("CC") __cret (__equal(__car(__obj1), __car(__obj2)) && __equal(__cdr(__obj1), __cdr(__obj2))) - __matching (/[TSC][TSC]/) + __matching (/[UTSC][UTSC]/) __cret (0); } @@ -374,21 +382,13 @@ function __slow_equal(__obj1, __obj2, if (__tc2 == "T") return __obj1 == __unbox(__obj2); - if (__numberp(__obj1)) - return __obj1 + 0 == __obj2 + if (__numberp(__obj1) && __numberp(__obj2)) { + return __obj1 + 0 == __obj2 + 0 + } return 0; } -function __equal(__obj1, __obj2) -{ - if (__obj1 == __obj2) - return 1; - return __slow_equal(__obj1, __obj2) -} - -#define __equal(obj1, obj2) ((obj1) == (obj2) ? 1 : __slow_equal(obj1, obj2)) - function __pack(__stk, __item) { return length(__item) ":" __item __stk |