klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

commit 7e3c12424c595583a28bf2292d6b8d22fbe24f9d
parent 3bd353ef9eedff9c6ad5614d036db34f2629bf57
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Tue, 13 Dec 2011 21:04:58 -0300

Refactor: in c style, use all spaces (no tabs), k&r style, 4 space indenting. in kernel style, use 2 space indenting.

Diffstat:
Msrc/kenvironment.h | 2+-
Msrc/kerror.c | 34+++++++++++++++++-----------------
Msrc/kerror.h | 42+++++++++++++++++++++---------------------
Msrc/keval.c | 124++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgbooleans.c | 92++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgbytevectors.c | 182++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgc.c | 682++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgc.h | 30+++++++++++++++---------------
Msrc/kgchars.c | 78+++++++++++++++++++++++++++++++++++++++---------------------------------------
Msrc/kgcombiners.c | 192++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgcontinuations.c | 88++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgcontrol.c | 358++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgencapsulations.c | 8++++----
Msrc/kgenv_mut.c | 144++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgenvironments.c | 346++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgeqp.c | 14+++++++-------
Msrc/kgequalp.c | 14+++++++-------
Msrc/kgerrors.c | 8++++----
Msrc/kgffi.c | 112++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kghelpers.c | 1478++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kghelpers.h | 408++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgkd_vars.c | 2+-
Msrc/kgkeywords.c | 2+-
Msrc/kgks_vars.c | 4++--
Msrc/kgnumbers.c | 2340++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgpair_mut.c | 440++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgpairs_lists.c | 798++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgports.c | 530++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgpromises.c | 64++++++++++++++++++++++++++++++++--------------------------------
Msrc/kgstrings.c | 324++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kgsymbols.c | 2+-
Msrc/kgsystem.c | 68++++++++++++++++++++++++++++++++++----------------------------------
Msrc/kgvectors.c | 182++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kinteger.c | 86++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kinteger.h | 56++++++++++++++++++++++++++++----------------------------
Msrc/kkeyword.c | 42+++++++++++++++++++++---------------------
Msrc/klisp.c | 436++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/klisp.h | 48++++++++++++++++++++++++------------------------
Msrc/klispconf.h | 166++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kmem.c | 66+++++++++++++++++++++++++++++++++---------------------------------
Msrc/kmem.h | 22+++++++++++-----------
Msrc/kmodule.c | 2+-
Msrc/kobject.c | 28++++++++++++++--------------
Msrc/kobject.h | 212++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/koperative.c | 6+++---
Msrc/koperative.h | 2+-
Msrc/kpair.c | 8++++----
Msrc/kpair.h | 2+-
Msrc/kport.c | 90++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kport.h | 6+++---
Msrc/krational.c | 504++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/krational.h | 130++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kread.c | 1040++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kreal.c | 484++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kreal.h | 4++--
Msrc/krepl.c | 136++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kstate.c | 272++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kstate.h | 80++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kstring.c | 114++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kstring.h | 4++--
Msrc/ksymbol.c | 98++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/ksymbol.h | 2+-
Msrc/ksystem.c | 2+-
Msrc/ksystem.posix.c | 2+-
Msrc/ksystem.win32.c | 2+-
Msrc/ktable.c | 440++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/ktable.h | 2+-
Msrc/ktoken.c | 1380++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/ktoken.h | 14+++++++-------
Msrc/kwrite.c | 1040++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/kwrite.h | 2+-
Asrc/tab_to_4spaces.sed | 9+++++++++
Asrc/tab_to_8spaces.sed | 9+++++++++
Msrc/tests/booleans.k | 44++++++++++++++++++++++----------------------
Msrc/tests/bytevectors.k | 44++++++++++++++++++++++----------------------
Msrc/tests/characters.k | 2+-
Msrc/tests/check.k | 664++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/tests/combiners.k | 214++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/tests/control.k | 316++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/tests/encapsulations.k | 20+++++++++-----------
Msrc/tests/environment-mutation.k | 2+-
Msrc/tests/environments.k | 440++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/tests/eq-equal.k | 232++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/tests/error.k | 64++++++++++++++++++++++++++++++++--------------------------------
Msrc/tests/keyed-variables.k | 46+++++++++++++++++++++++-----------------------
Msrc/tests/keywords.k | 6+++---
Msrc/tests/memory-ports.k | 10+++++-----
Msrc/tests/numbers.k | 44++++++++++++++++++++++----------------------
Msrc/tests/pair-mutation.k | 70+++++++++++++++++++++++++++++++++++-----------------------------------
Msrc/tests/pairs-and-lists.k | 134++++++++++++++++++++++++++++++++++++++++---------------------------------------
Msrc/tests/ports.k | 96++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/tests/promises.k | 50+++++++++++++++++++++++++-------------------------
Msrc/tests/strings.k | 110++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/tests/symbols.k | 8++++----
Msrc/tests/system.k | 4++--
Msrc/tests/test-helpers.k | 60++++++++++++++++++++++++++++++------------------------------
Msrc/tests/vectors.k | 50+++++++++++++++++++++++++-------------------------
97 files changed, 9494 insertions(+), 9476 deletions(-)

diff --git a/src/kenvironment.h b/src/kenvironment.h @@ -19,7 +19,7 @@ bool kbinds(klisp_State *K, TValue env, TValue sym); /* keyed dynamic vars */ /* GC: Assumes parents, key & val are rooted */ TValue kmake_keyed_static_env(klisp_State *K, TValue parent, TValue key, - TValue val); + TValue val); TValue kget_keyed_static_var(klisp_State *K, TValue env, TValue key); /* environments with hashtable bindings */ diff --git a/src/kerror.c b/src/kerror.c @@ -16,7 +16,7 @@ /* GC: assumes all objs passed are rooted */ TValue klispE_new(klisp_State *K, TValue who, TValue cont, TValue msg, - TValue irritants) + TValue irritants) { Error *new_error = klispM_new(K, Error); @@ -33,7 +33,7 @@ TValue klispE_new(klisp_State *K, TValue who, TValue cont, TValue msg, } TValue klispE_new_with_errno_irritants(klisp_State *K, const char *service, - int errnum, TValue irritants) + int errnum, TValue irritants) { TValue error_description = klispE_describe_errno(K, service, errnum); krooted_tvs_push(K, error_description); @@ -58,7 +58,7 @@ void klispE_free(klisp_State *K, Error *error) void clear_buffers(klisp_State *K) { /* These shouldn't cause GC, but just in case do them first, - an object may be protected in tvs or vars */ + an object may be protected in tvs or vars */ ks_sclear(K); ks_tbclear(K); K->shared_dict = KNIL; @@ -70,10 +70,10 @@ void clear_buffers(klisp_State *K) /* ** Throw a simple error obj with: ** { -** who: current operative/continuation, -** cont: current continuation, -** message: msg, -** irritants: () +** who: current operative/continuation, +** cont: current continuation, +** message: msg, +** irritants: () ** } */ /* GC: assumes all objs passed are rooted */ @@ -82,8 +82,8 @@ void klispE_throw_simple(klisp_State *K, char *msg) TValue error_msg = kstring_new_b_imm(K, msg); krooted_tvs_push(K, error_msg); TValue error_obj = - klispE_new(K, K->next_obj, K->curr_cont, error_msg, KNIL); - /* clear buffer shouldn't cause GC, but just in case... */ + klispE_new(K, K->next_obj, K->curr_cont, error_msg, KNIL); + /* clear buffer shouldn't cause GC, but just in case... */ krooted_tvs_push(K, error_obj); clear_buffers(K); /* this pops both error_msg & error_obj */ /* call_cont protects error from gc */ @@ -93,10 +93,10 @@ void klispE_throw_simple(klisp_State *K, char *msg) /* ** Throw an error obj with: ** { -** who: current operative/continuation, -** cont: current continuation, -** message: msg, -** irritants: irritants +** who: current operative/continuation, +** cont: current continuation, +** message: msg, +** irritants: irritants ** } */ /* GC: assumes all objs passed are rooted */ @@ -107,8 +107,8 @@ void klispE_throw_with_irritants(klisp_State *K, char *msg, TValue irritants) TValue error_msg = kstring_new_b_imm(K, msg); krooted_tvs_push(K, error_msg); TValue error_obj = - klispE_new(K, K->next_obj, K->curr_cont, error_msg, irritants); - /* clear buffer shouldn't cause GC, but just in case... */ + klispE_new(K, K->next_obj, K->curr_cont, error_msg, irritants); + /* clear buffer shouldn't cause GC, but just in case... */ krooted_tvs_push(K, error_obj); clear_buffers(K); /* this pops both error_msg & error_obj */ /* call_cont protects error from gc */ @@ -119,7 +119,7 @@ void klispE_throw_system_error_with_irritants( klisp_State *K, const char *service, int errnum, TValue irritants) { TValue error_obj = klispE_new_with_errno_irritants(K, service, errnum, - irritants); + irritants); krooted_tvs_push(K, error_obj); clear_buffers(K); kcall_cont(K, K->system_error_cont, error_obj); @@ -200,7 +200,7 @@ TValue klispE_describe_errno(klisp_State *K, const char *service, int errnum) { const char *code = NULL; int tabsize = sizeof(symbolic_error_codes) / - sizeof(symbolic_error_codes[0]); + sizeof(symbolic_error_codes[0]); if (0 <= errnum && errnum < tabsize) code = symbolic_error_codes[errnum]; if (code == NULL) diff --git a/src/kerror.h b/src/kerror.h @@ -16,9 +16,9 @@ #include "kpair.h" /* for klist */ TValue klispE_new(klisp_State *K, TValue who, TValue cont, TValue msg, - TValue irritants); + TValue irritants); TValue klispE_new_with_errno_irritants(klisp_State *K, const char *service, - int errnum, TValue irritants); + int errnum, TValue irritants); void klispE_free(klisp_State *K, Error *error); @@ -29,35 +29,35 @@ void klispE_throw_system_error_with_irritants( klisp_State *K, const char *service, int errnum, TValue irritants); /* the objects should be rooted */ -#define klispE_new_simple_with_errno_irritants(K__, service__, ...) \ - ({ \ - int errnum__ = errno; \ - TValue ls__ = klist(K__, __VA_ARGS__); \ - krooted_tvs_push(K__, ls__); \ +#define klispE_new_simple_with_errno_irritants(K__, service__, ...) \ + ({ \ + int errnum__ = errno; \ + TValue ls__ = klist(K__, __VA_ARGS__); \ + krooted_tvs_push(K__, ls__); \ TValue err__ = klispE_new_with_errno_irritants(K__, service__, \ - errnum__, ls__); \ - krooted_tvs_pop(K__); \ - err__; \ + errnum__, ls__); \ + krooted_tvs_pop(K__); \ + err__; \ }) /* evaluates K__ more than once */ /* the objects should be rooted */ -#define klispE_throw_simple_with_irritants(K__, msg__, ...) \ +#define klispE_throw_simple_with_irritants(K__, msg__, ...) \ { TValue ls__ = klist(K__, __VA_ARGS__); \ - krooted_tvs_push(K__, ls__); \ - /* the pop is implicit in throw_with_irritants */ \ - klispE_throw_with_irritants(K__, msg__, ls__); } + krooted_tvs_push(K__, ls__); \ + /* the pop is implicit in throw_with_irritants */ \ + klispE_throw_with_irritants(K__, msg__, ls__); } /* the objects should be rooted */ -#define klispE_throw_errno_with_irritants(K__, service__, ...) \ - { \ - int errnum__ = errno; \ - TValue ls__ = klist(K__, __VA_ARGS__); \ - krooted_tvs_push(K__, ls__); \ +#define klispE_throw_errno_with_irritants(K__, service__, ...) \ + { \ + int errnum__ = errno; \ + TValue ls__ = klist(K__, __VA_ARGS__); \ + krooted_tvs_push(K__, ls__); \ klispE_throw_system_error_with_irritants(K__, service__, errnum__, ls__); \ - } + } -#define klispE_throw_errno_simple(K__, service__) \ +#define klispE_throw_errno_simple(K__, service__) \ klispE_throw_system_error_with_irritants(K__, service__, errno, KNIL); TValue klispE_describe_errno(klisp_State *K, const char *service, int errnum); diff --git a/src/keval.c b/src/keval.c @@ -43,17 +43,17 @@ void do_eval_ls(klisp_State *K) /* save the result of last evaluation and continue with next pair */ kset_car(apair, obj); if (ttisnil(rest)) { - /* argument evaluation complete */ - /* this is necessary to recreate the cycle in operand list */ - kset_cdr(apair, tail); - kapply_cc(K, combiner); + /* argument evaluation complete */ + /* this is necessary to recreate the cycle in operand list */ + kset_cdr(apair, tail); + kapply_cc(K, combiner); } else { - /* more arguments need to be evaluated */ - /* GC: all objects are rooted at this point */ - TValue new_cont = kmake_continuation(K, kget_cc(K), do_eval_ls, 4, - rest, env, tail, combiner); - kset_cc(K, new_cont); - ktail_eval(K, kcar(rest), env); + /* more arguments need to be evaluated */ + /* GC: all objects are rooted at this point */ + TValue new_cont = kmake_continuation(K, kget_cc(K), do_eval_ls, 4, + rest, env, tail, combiner); + kset_cc(K, new_cont); + ktail_eval(K, kcar(rest), env); } } @@ -61,8 +61,8 @@ void do_eval_ls(klisp_State *K) inline void clear_ls_marks(TValue ls) { while (ttispair(ls) && kis_marked(ls)) { - kunmark(ls); - ls = kcdr(ls); + kunmark(ls); + ls = kcdr(ls); } } @@ -77,24 +77,24 @@ inline TValue make_arg_ls(klisp_State *K, TValue operands, TValue *tail) TValue rem_op = kcdr(operands); while(ttispair(rem_op) && kis_unmarked(rem_op)) { - TValue new_pair = kcons(K, kcar(rem_op), KNIL); - kset_mark(rem_op, new_pair); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - rem_op = kcdr(rem_op); + TValue new_pair = kcons(K, kcar(rem_op), KNIL); + kset_mark(rem_op, new_pair); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + rem_op = kcdr(rem_op); } krooted_tvs_pop(K); if (ttispair(rem_op)) { - /* cyclical list */ - *tail = kget_mark(rem_op); + /* cyclical list */ + *tail = kget_mark(rem_op); } else if (ttisnil(rem_op)) { - *tail = KNIL; + *tail = KNIL; } else { - clear_ls_marks(operands); - klispE_throw_simple(K, "Not a list in applicative combination"); - return KINERT; + clear_ls_marks(operands); + klispE_throw_simple(K, "Not a list in applicative combination"); + return KINERT; } clear_ls_marks(operands); return arg_ls; @@ -116,38 +116,38 @@ void do_combine(klisp_State *K) switch(ttype(obj)) { case K_TAPPLICATIVE: { - if (ttisnil(operands)) { - /* no arguments => no evaluation, just call the operative */ - /* NOTE: the while is needed because it may be multiply wrapped */ - while(ttisapplicative(obj)) - obj = tv2app(obj)->underlying; - ktail_call_si(K, obj, operands, env, si); - } else if (ttispair(operands)) { - /* make a copy of the operands (for storing arguments) */ - TValue tail; - TValue arg_ls = make_arg_ls(K, operands, &tail); - krooted_tvs_push(K, arg_ls); - TValue comb_cont = kmake_continuation(K, kget_cc(K), do_combine, - 3, arg_ls, env, si); - - krooted_tvs_pop(K); /* already in cont */ - krooted_tvs_push(K, comb_cont); - TValue els_cont = - kmake_continuation(K, comb_cont, do_eval_ls, 4, arg_ls, env, - tail, tv2app(obj)->underlying); - kset_cc(K, els_cont); - krooted_tvs_pop(K); - ktail_eval(K, kcar(arg_ls), env); - } else { - klispE_throw_simple(K, "Not a list in applicative combination"); - return; - } + if (ttisnil(operands)) { + /* no arguments => no evaluation, just call the operative */ + /* NOTE: the while is needed because it may be multiply wrapped */ + while(ttisapplicative(obj)) + obj = tv2app(obj)->underlying; + ktail_call_si(K, obj, operands, env, si); + } else if (ttispair(operands)) { + /* make a copy of the operands (for storing arguments) */ + TValue tail; + TValue arg_ls = make_arg_ls(K, operands, &tail); + krooted_tvs_push(K, arg_ls); + TValue comb_cont = kmake_continuation(K, kget_cc(K), do_combine, + 3, arg_ls, env, si); + + krooted_tvs_pop(K); /* already in cont */ + krooted_tvs_push(K, comb_cont); + TValue els_cont = + kmake_continuation(K, comb_cont, do_eval_ls, 4, arg_ls, env, + tail, tv2app(obj)->underlying); + kset_cc(K, els_cont); + krooted_tvs_pop(K); + ktail_eval(K, kcar(arg_ls), env); + } else { + klispE_throw_simple(K, "Not a list in applicative combination"); + return; + } } case K_TOPERATIVE: - ktail_call_si(K, obj, operands, env, si); + ktail_call_si(K, obj, operands, env, si); default: - klispE_throw_simple(K, "Not a combiner in combiner position"); - return; + klispE_throw_simple(K, "Not a combiner in combiner position"); + return; } } @@ -165,19 +165,19 @@ void keval_ofn(klisp_State *K) switch(ttype(obj)) { case K_TPAIR: { - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_combine, 3, kcdr(obj), - denv, ktry_get_si(K, obj)); - kset_cc(K, new_cont); - ktail_eval(K, kcar(obj), denv); - break; + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_combine, 3, kcdr(obj), + denv, ktry_get_si(K, obj)); + kset_cc(K, new_cont); + ktail_eval(K, kcar(obj), denv); + break; } case K_TSYMBOL: - /* error handling happens in kget_binding */ - kapply_cc(K, kget_binding(K, denv, obj)); - break; + /* error handling happens in kget_binding */ + kapply_cc(K, kget_binding(K, denv, obj)); + break; default: - kapply_cc(K, obj); + kapply_cc(K, obj); } } diff --git a/src/kgbooleans.c b/src/kgbooleans.c @@ -58,12 +58,12 @@ void andp(klisp_State *K) TValue res = KTRUE; TValue tail = ptree; while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - if (kis_false(first)) { - res = KFALSE; - break; - } + TValue first = kcar(tail); + tail = kcdr(tail); + if (kis_false(first)) { + res = KFALSE; + break; + } } kapply_cc(K, res); } @@ -83,12 +83,12 @@ void orp(klisp_State *K) TValue res = KFALSE; TValue tail = ptree; while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - if (kis_true(first)) { - res = KTRUE; - break; - } + TValue first = kcar(tail); + tail = kcdr(tail); + if (kis_true(first)) { + res = KTRUE; + break; + } } kapply_cc(K, res); } @@ -121,41 +121,41 @@ void do_Sandp_Sorp(klisp_State *K) TValue denv = xparams[3]; if (!ttisboolean(obj)) { - klispE_throw_simple_with_irritants(K, "expected boolean", 1, - obj); - return; + klispE_throw_simple_with_irritants(K, "expected boolean", 1, + obj); + return; } else if (ttisnil(ls) || tv_equal(obj, term_bool)) { - /* in both cases the value to be returned is obj: - if there are no more operands it is obvious otherwise, if - the termination bool is found: - $and? returns #f when it finds #f and $or? returns #t when it - finds #t */ - kapply_cc(K, obj); + /* in both cases the value to be returned is obj: + if there are no more operands it is obvious otherwise, if + the termination bool is found: + $and? returns #f when it finds #f and $or? returns #t when it + finds #t */ + kapply_cc(K, obj); } else { - TValue first = kcar(ls); - TValue tail = kcdr(ls); - /* This is the important part of tail context + bool check */ - if (!ttisnil(tail) || !kis_bool_check_cont(kget_cc(K))) { - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_Sandp_Sorp, - 4, sname, term_bool, tail, denv); - /* - ** Mark as a bool checking cont this is needed in the last operand - ** to allow both tail recursive behaviour and boolean checking. - ** While it is not necessary if this is not the last operand it - ** avoids a continuation in the last evaluation of the inner form - ** in the common use of - ** ($and?/$or? ($or?/$and? ...) ...) - */ - kset_bool_check_cont(new_cont); - kset_cc(K, new_cont); + TValue first = kcar(ls); + TValue tail = kcdr(ls); + /* This is the important part of tail context + bool check */ + if (!ttisnil(tail) || !kis_bool_check_cont(kget_cc(K))) { + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_Sandp_Sorp, + 4, sname, term_bool, tail, denv); + /* + ** Mark as a bool checking cont this is needed in the last operand + ** to allow both tail recursive behaviour and boolean checking. + ** While it is not necessary if this is not the last operand it + ** avoids a continuation in the last evaluation of the inner form + ** in the common use of + ** ($and?/$or? ($or?/$and? ...) ...) + */ + kset_bool_check_cont(new_cont); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, ls)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, ls)); #endif - } - ktail_eval(K, first, denv); + } + ktail_eval(K, first, denv); } } @@ -176,11 +176,11 @@ void Sandp_Sorp(klisp_State *K) /* This will work even if ls is empty */ krooted_tvs_push(K, ls); TValue new_cont = kmake_continuation(K, kget_cc(K), do_Sandp_Sorp, 4, - sname, term_bool, ls, denv); + sname, term_bool, ls, denv); krooted_tvs_pop(K); /* there's no need to mark it as bool checking, no evaluation is done in the dynamic extent of this cont, no need for - source info either */ + source info either */ kset_cc(K, new_cont); kapply_cc(K, knegp(term_bool)); /* pass dummy value to start */ } @@ -199,7 +199,7 @@ void kinit_booleans_ground_env(klisp_State *K) /* 4.1.1 boolean? */ add_applicative(K, ground_env, "boolean?", typep, 2, symbol, - i2tv(K_TBOOLEAN)); + i2tv(K_TBOOLEAN)); /* 6.1.1 not? */ add_applicative(K, ground_env, "not?", notp, 0); /* 6.1.2 and? */ diff --git a/src/kgbytevectors.c b/src/kgbytevectors.c @@ -91,19 +91,19 @@ void make_bytevector(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_al1tp(K, ptree, "exact integer", keintegerp, tv_s, - maybe_byte); + maybe_byte); uint8_t fill = 0; if (get_opt_tpar(K, maybe_byte, "u8", ttisu8)) { - fill = ivalue(maybe_byte); + fill = ivalue(maybe_byte); } if (knegativep(tv_s)) { - klispE_throw_simple(K, "negative size"); - return; + klispE_throw_simple(K, "negative size"); + return; } else if (!ttisfixint(tv_s)) { - klispE_throw_simple(K, "size is too big"); - return; + klispE_throw_simple(K, "size is too big"); + return; } TValue new_bytevector = kbytevector_new_sf(K, ivalue(tv_s), fill); kapply_cc(K, new_bytevector); @@ -134,19 +134,19 @@ void bytevector_u8_ref(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "bytevector", ttisbytevector, bytevector, - "exact integer", keintegerp, tv_i); + "exact integer", keintegerp, tv_i); if (!ttisfixint(tv_i)) { - /* TODO show index */ - klispE_throw_simple(K, "index out of bounds"); - return; + /* TODO show index */ + klispE_throw_simple(K, "index out of bounds"); + return; } int32_t i = ivalue(tv_i); if (i < 0 || i >= kbytevector_size(bytevector)) { - /* TODO show index */ - klispE_throw_simple(K, "index out of bounds"); - return; + /* TODO show index */ + klispE_throw_simple(K, "index out of bounds"); + return; } TValue res = i2tv(kbytevector_buf(bytevector)[i]); @@ -163,23 +163,23 @@ void bytevector_u8_setB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_3tp(K, ptree, "bytevector", ttisbytevector, bytevector, - "exact integer", keintegerp, tv_i, "u8", ttisu8, tv_byte); + "exact integer", keintegerp, tv_i, "u8", ttisu8, tv_byte); if (!ttisfixint(tv_i)) { - /* TODO show index */ - klispE_throw_simple(K, "index out of bounds"); - return; + /* TODO show index */ + klispE_throw_simple(K, "index out of bounds"); + return; } else if (kbytevector_immutablep(bytevector)) { - klispE_throw_simple(K, "immutable bytevector"); - return; + klispE_throw_simple(K, "immutable bytevector"); + return; } int32_t i = ivalue(tv_i); if (i < 0 || i >= kbytevector_size(bytevector)) { - /* TODO show index */ - klispE_throw_simple(K, "index out of bounds"); - return; + /* TODO show index */ + klispE_throw_simple(K, "index out of bounds"); + return; } kbytevector_buf(bytevector)[i] = (uint8_t) ivalue(tv_byte); @@ -201,10 +201,10 @@ void bytevector_copy(klisp_State *K) TValue new_bytevector; /* the if isn't strictly necessary but it's clearer this way */ if (tv_equal(bytevector, K->empty_bytevector)) { - new_bytevector = bytevector; + new_bytevector = bytevector; } else { - new_bytevector = kbytevector_new_bs(K, kbytevector_buf(bytevector), - kbytevector_size(bytevector)); + new_bytevector = kbytevector_new_bs(K, kbytevector_buf(bytevector), + kbytevector_size(bytevector)); } kapply_cc(K, new_bytevector); } @@ -219,21 +219,21 @@ void bytevector_copyB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "bytevector", ttisbytevector, bytevector1, - "bytevector", ttisbytevector, bytevector2); + "bytevector", ttisbytevector, bytevector2); if (kbytevector_immutablep(bytevector2)) { - klispE_throw_simple(K, "immutable destination bytevector"); - return; + klispE_throw_simple(K, "immutable destination bytevector"); + return; } else if (kbytevector_size(bytevector1) > kbytevector_size(bytevector2)) { - klispE_throw_simple(K, "destination bytevector is too small"); - return; + klispE_throw_simple(K, "destination bytevector is too small"); + return; } if (!tv_equal(bytevector1, bytevector2) && - !tv_equal(bytevector1, K->empty_bytevector)) { - memcpy(kbytevector_buf(bytevector2), - kbytevector_buf(bytevector1), - kbytevector_size(bytevector1)); + !tv_equal(bytevector1, K->empty_bytevector)) { + memcpy(kbytevector_buf(bytevector2), + kbytevector_buf(bytevector1), + kbytevector_size(bytevector1)); } kapply_cc(K, KINERT); } @@ -249,40 +249,40 @@ void bytevector_copy_partial(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_3tp(K, ptree, "bytevector", ttisbytevector, bytevector, - "exact integer", keintegerp, tv_start, - "exact integer", keintegerp, tv_end); + "exact integer", keintegerp, tv_start, + "exact integer", keintegerp, tv_end); if (!ttisfixint(tv_start) || ivalue(tv_start) < 0 || - ivalue(tv_start) > kbytevector_size(bytevector)) { - /* TODO show index */ - klispE_throw_simple(K, "start index out of bounds"); - return; + ivalue(tv_start) > kbytevector_size(bytevector)) { + /* TODO show index */ + klispE_throw_simple(K, "start index out of bounds"); + return; } int32_t start = ivalue(tv_start); if (!ttisfixint(tv_end) || ivalue(tv_end) < 0 || - ivalue(tv_end) > kbytevector_size(bytevector)) { - klispE_throw_simple(K, "end index out of bounds"); - return; + ivalue(tv_end) > kbytevector_size(bytevector)) { + klispE_throw_simple(K, "end index out of bounds"); + return; } int32_t end = ivalue(tv_end); if (start > end) { - /* TODO show indexes */ - klispE_throw_simple(K, "end index is smaller than start index"); - return; + /* TODO show indexes */ + klispE_throw_simple(K, "end index is smaller than start index"); + return; } int32_t size = end - start; TValue new_bytevector; /* the if isn't strictly necessary but it's clearer this way */ if (size == 0) { - new_bytevector = K->empty_bytevector; + new_bytevector = K->empty_bytevector; } else { - new_bytevector = kbytevector_new_bs(K, kbytevector_buf(bytevector) - + start, size); + new_bytevector = kbytevector_new_bs(K, kbytevector_buf(bytevector) + + start, size); } kapply_cc(K, new_bytevector); } @@ -297,64 +297,64 @@ void bytevector_copy_partialB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_al3tp(K, ptree, "bytevector", ttisbytevector, bytevector1, - "exact integer", keintegerp, tv_start, - "exact integer", keintegerp, tv_end, - rest); + "exact integer", keintegerp, tv_start, + "exact integer", keintegerp, tv_end, + rest); /* XXX: this will send wrong error msgs (bad number of arg) */ bind_2tp(K, rest, - "bytevector", ttisbytevector, bytevector2, - "exact integer", keintegerp, tv_start2); + "bytevector", ttisbytevector, bytevector2, + "exact integer", keintegerp, tv_start2); if (!ttisfixint(tv_start) || ivalue(tv_start) < 0 || - ivalue(tv_start) > kbytevector_size(bytevector1)) { - /* TODO show index */ - klispE_throw_simple(K, "start index out of bounds"); - return; + ivalue(tv_start) > kbytevector_size(bytevector1)) { + /* TODO show index */ + klispE_throw_simple(K, "start index out of bounds"); + return; } int32_t start = ivalue(tv_start); if (!ttisfixint(tv_end) || ivalue(tv_end) < 0 || - ivalue(tv_end) > kbytevector_size(bytevector1)) { - klispE_throw_simple(K, "end index out of bounds"); - return; + ivalue(tv_end) > kbytevector_size(bytevector1)) { + klispE_throw_simple(K, "end index out of bounds"); + return; } int32_t end = ivalue(tv_end); if (start > end) { - /* TODO show indexes */ - klispE_throw_simple(K, "end index is smaller than start index"); - return; + /* TODO show indexes */ + klispE_throw_simple(K, "end index is smaller than start index"); + return; } int32_t size = end - start; if (kbytevector_immutablep(bytevector2)) { - klispE_throw_simple(K, "immutable destination bytevector"); - return; + klispE_throw_simple(K, "immutable destination bytevector"); + return; } if (!ttisfixint(tv_start2) || ivalue(tv_start2) < 0 || - ivalue(tv_start2) > kbytevector_size(bytevector2)) { - klispE_throw_simple(K, "to index out of bounds"); - return; + ivalue(tv_start2) > kbytevector_size(bytevector2)) { + klispE_throw_simple(K, "to index out of bounds"); + return; } int32_t start2 = ivalue(tv_start2); int64_t end2 = (int64_t) start2 + size; if ((end2 > INT32_MAX) || - (((int32_t) end2) > kbytevector_size(bytevector2))) { - klispE_throw_simple(K, "not enough space in destination"); - return; + (((int32_t) end2) > kbytevector_size(bytevector2))) { + klispE_throw_simple(K, "not enough space in destination"); + return; } if (size > 0) { - memcpy(kbytevector_buf(bytevector2) + start2, - kbytevector_buf(bytevector1) + start, - size); + memcpy(kbytevector_buf(bytevector2) + start2, + kbytevector_buf(bytevector1) + start, + size); } kapply_cc(K, KINERT); } @@ -369,17 +369,17 @@ void bytevector_u8_fillB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "bytevector", ttisbytevector, bytevector, - "u8", ttisu8, tv_byte); + "u8", ttisu8, tv_byte); if (kbytevector_immutablep(bytevector)) { - klispE_throw_simple(K, "immutable bytevector"); - return; + klispE_throw_simple(K, "immutable bytevector"); + return; } uint32_t size = kbytevector_size(bytevector); uint8_t *buf = kbytevector_buf(bytevector); while(size-- > 0) { - *buf++ = (uint8_t) ivalue(tv_byte); + *buf++ = (uint8_t) ivalue(tv_byte); } kapply_cc(K, KINERT); } @@ -398,10 +398,10 @@ void bytevector_to_immutable_bytevector(klisp_State *K) TValue res_bytevector; if (kbytevector_immutablep(bytevector)) { /* this includes the empty bytevector */ - res_bytevector = bytevector; + res_bytevector = bytevector; } else { - res_bytevector = kbytevector_new_bs_imm(K, kbytevector_buf(bytevector), - kbytevector_size(bytevector)); + res_bytevector = kbytevector_new_bs_imm(K, kbytevector_buf(bytevector), + kbytevector_size(bytevector)); } kapply_cc(K, res_bytevector); } @@ -412,7 +412,7 @@ void kinit_bytevectors_ground_env(klisp_State *K) TValue ground_env = K->ground_env; TValue symbol, value; - /* + /* ** This section is not in the report. The bindings here are ** taken from the r7rs scheme draft and should not be considered standard. ** They are provided in the meantime to allow programs to use byte vectors. @@ -420,12 +420,12 @@ void kinit_bytevectors_ground_env(klisp_State *K) /* ??.1.1? bytevector? */ add_applicative(K, ground_env, "bytevector?", typep, 2, symbol, - i2tv(K_TBYTEVECTOR)); + i2tv(K_TBYTEVECTOR)); /* ??.? immutable-bytevector?, mutable-bytevector? */ add_applicative(K, ground_env, "immutable-bytevector?", ftypep, 2, symbol, - p2tv(kimmutable_bytevectorp)); + p2tv(kimmutable_bytevectorp)); add_applicative(K, ground_env, "mutable-bytevector?", ftypep, 2, symbol, - p2tv(kmutable_bytevectorp)); + p2tv(kmutable_bytevectorp)); /* ??.1.? bytevector */ add_applicative(K, ground_env, "bytevector", bytevector, 0); /* ??.1.? list->bytevector */ @@ -441,7 +441,7 @@ void kinit_bytevectors_ground_env(klisp_State *K) add_applicative(K, ground_env, "bytevector-u8-ref", bytevector_u8_ref, 0); /* ??.1.5? bytevector-u8-set! */ add_applicative(K, ground_env, "bytevector-u8-set!", bytevector_u8_setB, - 0); + 0); /* ??.1.?? bytevector-copy */ add_applicative(K, ground_env, "bytevector-copy", bytevector_copy, 0); @@ -450,17 +450,17 @@ void kinit_bytevectors_ground_env(klisp_State *K) /* ??.1.?? bytevector-copy-partial */ add_applicative(K, ground_env, "bytevector-copy-partial", - bytevector_copy_partial, 0); + bytevector_copy_partial, 0); /* ??.1.?? bytevector-copy-partial! */ add_applicative(K, ground_env, "bytevector-copy-partial!", - bytevector_copy_partialB, 0); + bytevector_copy_partialB, 0); /* ??.?? bytevector-u8-fill! */ add_applicative(K, ground_env, "bytevector-u8-fill!", - bytevector_u8_fillB, 0); + bytevector_u8_fillB, 0); /* ??.1.?? bytevector->immutable-bytevector */ add_applicative(K, ground_env, "bytevector->immutable-bytevector", - bytevector_to_immutable_bytevector, 0); + bytevector_to_immutable_bytevector, 0); } diff --git a/src/kgc.c b/src/kgc.c @@ -33,9 +33,9 @@ #define maskmarks cast(uint16_t, ~(bitmask(BLACKBIT)|WHITEBITS)) -#define makewhite(g,x) \ - ((x)->gch.gct = cast(uint16_t, \ - ((x)->gch.gct & maskmarks) | klispC_white(g))) +#define makewhite(g,x) \ + ((x)->gch.gct = cast(uint16_t, \ + ((x)->gch.gct & maskmarks) | klispC_white(g))) #define white2gray(x) reset2bits((x)->gch.gct, WHITE0BIT, WHITE1BIT) #define black2gray(x) resetbit((x)->gch.gct, BLACKBIT) @@ -48,24 +48,24 @@ #define markfinalized(u) l_setbit((u)->gct, FINALIZEDBIT) /* klisp: NOT USED YET */ -#define KEYWEAK bitmask(KEYWEAKBIT) -#define VALUEWEAK bitmask(VALUEWEAKBIT) +#define KEYWEAK bitmask(KEYWEAKBIT) +#define VALUEWEAK bitmask(VALUEWEAKBIT) /* this one is klisp specific */ -#define markvaluearray(k, a, s) ({ \ - TValue *array_ = (a); \ - int32_t size_ = (s); \ - for(int32_t i_ = 0; i_ < size_; i_++, array_++) { \ - TValue mva_obj_ = *array_; \ - markvalue(k, mva_obj_); \ - }}) +#define markvaluearray(k, a, s) ({ \ + TValue *array_ = (a); \ + int32_t size_ = (s); \ + for(int32_t i_ = 0; i_ < size_; i_++, array_++) { \ + TValue mva_obj_ = *array_; \ + markvalue(k, mva_obj_); \ + }}) #define markvalue(k,o) { checkconsistency(o); \ - if (iscollectable(o) && iswhite(gcvalue(o))) \ - reallymarkobject(k,gcvalue(o)); } + if (iscollectable(o) && iswhite(gcvalue(o))) \ + reallymarkobject(k,gcvalue(o)); } #define markobject(k,t) { if (iswhite(obj2gco(t))) \ - reallymarkobject(k, obj2gco(t)); } + reallymarkobject(k, obj2gco(t)); } #define setthreshold(g) (g->GCthreshold = (g->estimate/100) * g->gcpause) @@ -73,7 +73,7 @@ static void removeentry (Node *n) { klisp_assert(ttisfree(gval(n))); if (iscollectable(gkey(n)->this))/* dead key; remove it */ - gkey(n)->this = gc2deadkey(gcvalue(gkey(n)->this)); + gkey(n)->this = gc2deadkey(gcvalue(gkey(n)->this)); } static void reallymarkobject (klisp_State *K, GCObject *o) @@ -87,17 +87,17 @@ static void reallymarkobject (klisp_State *K, GCObject *o) /* klisp: keep this around just in case we add it later */ #if 0 case LUA_TUSERDATA: { - Table *mt = gco2u(o)->metatable; - gray2black(o); /* udata are never gray */ - if (mt) markobject(g, mt); - markobject(g, gco2u(o)->env); - return; + Table *mt = gco2u(o)->metatable; + gray2black(o); /* udata are never gray */ + if (mt) markobject(g, mt); + markobject(g, gco2u(o)->env); + return; } #endif case K_TBIGRAT: /* the n & d are copied in the bigrat, not pointed to */ case K_TBIGINT: - gray2black(o); /* bigint & bigrats are never gray */ - break; + gray2black(o); /* bigint & bigrats are never gray */ + break; case K_TPAIR: case K_TSYMBOL: case K_TKEYWORD: @@ -115,13 +115,13 @@ static void reallymarkobject (klisp_State *K, GCObject *o) case K_TFPORT: case K_TMPORT: case K_TMODULE: - o->gch.gclist = K->gray; - K->gray = o; - break; + o->gch.gclist = K->gray; + K->gray = o; + break; default: - /* shouldn't happen */ - fprintf(stderr, "Unknown GCObject type (in GC mark): %d\n", type); - abort(); + /* shouldn't happen */ + fprintf(stderr, "Unknown GCObject type (in GC mark): %d\n", type); + abort(); } } @@ -131,11 +131,11 @@ static void reallymarkobject (klisp_State *K, GCObject *o) static void marktmu (global_State *g) { GCObject *u = g->tmudata; if (u) { - do { - u = u->gch.next; - makewhite(g, u); /* may be marked, if left from previous GC */ - reallymarkobject(g, u); - } while (u != g->tmudata); + do { + u = u->gch.next; + makewhite(g, u); /* may be marked, if left from previous GC */ + reallymarkobject(g, u); + } while (u != g->tmudata); } } @@ -146,26 +146,26 @@ size_t klispC_separateudata (lua_State *L, int all) { GCObject **p = &g->mainthread->next; GCObject *curr; while ((curr = *p) != NULL) { - if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) - p = &curr->gch.next; /* don't bother with them */ - else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { - markfinalized(gco2u(curr)); /* don't need finalization */ - p = &curr->gch.next; - } - else { /* must call its gc method */ - deadmem += sizeudata(gco2u(curr)); - markfinalized(gco2u(curr)); - *p = curr->gch.next; - /* link `curr' at the end of `tmudata' list */ - if (g->tmudata == NULL) /* list is empty? */ + if (!(iswhite(curr) || all) || isfinalized(gco2u(curr))) + p = &curr->gch.next; /* don't bother with them */ + else if (fasttm(L, gco2u(curr)->metatable, TM_GC) == NULL) { + markfinalized(gco2u(curr)); /* don't need finalization */ + p = &curr->gch.next; + } + else { /* must call its gc method */ + deadmem += sizeudata(gco2u(curr)); + markfinalized(gco2u(curr)); + *p = curr->gch.next; + /* link `curr' at the end of `tmudata' list */ + if (g->tmudata == NULL) /* list is empty? */ /* creates a circular list */ - g->tmudata = curr->gch.next = curr; - else { - curr->gch.next = g->tmudata->gch.next; - g->tmudata->gch.next = curr; - g->tmudata = curr; - } - } + g->tmudata = curr->gch.next = curr; + else { + curr->gch.next = g->tmudata->gch.next; + g->tmudata->gch.next = curr; + g->tmudata = curr; + } + } } return deadmem; } @@ -179,30 +179,30 @@ static int32_t traversetable (klisp_State *K, Table *h) { int32_t weakvalue = ktable_has_weak_values(tv)? 1 : 0; if (weakkey || weakvalue) { /* is really weak? */ - h->gct &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ - h->gct |= cast(uint16_t, (weakkey << KEYWEAKBIT) | - (weakvalue << VALUEWEAKBIT)); - h->gclist = K->weak; /* must be cleared after GC, ... */ - K->weak = obj2gco(h); /* ... so put in the appropriate list */ + h->gct &= ~(KEYWEAK | VALUEWEAK); /* clear bits */ + h->gct |= cast(uint16_t, (weakkey << KEYWEAKBIT) | + (weakvalue << VALUEWEAKBIT)); + h->gclist = K->weak; /* must be cleared after GC, ... */ + K->weak = obj2gco(h); /* ... so put in the appropriate list */ } if (weakkey && weakvalue) return 1; if (!weakvalue) { - i = h->sizearray; - while (i--) - markvalue(K, h->array[i]); + i = h->sizearray; + while (i--) + markvalue(K, h->array[i]); } i = sizenode(h); while (i--) { - Node *n = gnode(h, i); - klisp_assert(ttype(gkey(n)->this) != K_TDEADKEY || - ttisfree(gval(n))); - if (ttisfree(gval(n))) - removeentry(n); /* remove empty entries */ - else { - klisp_assert(!ttisfree(gkey(n)->this)); - if (!weakkey) markvalue(K, gkey(n)->this); - if (!weakvalue) markvalue(K, gval(n)); - } + Node *n = gnode(h, i); + klisp_assert(ttype(gkey(n)->this) != K_TDEADKEY || + ttisfree(gval(n))); + if (ttisfree(gval(n))) + removeentry(n); /* remove empty entries */ + else { + klisp_assert(!ttisfree(gkey(n)->this)); + if (!weakkey) markvalue(K, gkey(n)->this); + if (!weakvalue) markvalue(K, gval(n)); + } } return weakkey || weakvalue; } @@ -216,18 +216,18 @@ static void traverseproto (global_State *g, Proto *f) { int i; if (f->source) stringmark(f->source); for (i=0; i<f->sizek; i++) /* mark literals */ - markvalue(g, &f->k[i]); + markvalue(g, &f->k[i]); for (i=0; i<f->sizeupvalues; i++) { /* mark upvalue names */ - if (f->upvalues[i]) - stringmark(f->upvalues[i]); + if (f->upvalues[i]) + stringmark(f->upvalues[i]); } for (i=0; i<f->sizep; i++) { /* mark nested protos */ - if (f->p[i]) - markobject(g, f->p[i]); + if (f->p[i]) + markobject(g, f->p[i]); } for (i=0; i<f->sizelocvars; i++) { /* mark local-variable names */ - if (f->locvars[i].varname) - stringmark(f->locvars[i].varname); + if (f->locvars[i].varname) + stringmark(f->locvars[i].varname); } } @@ -244,7 +244,7 @@ static int32_t propagatemark (klisp_State *K) { gray2black(o); /* all types have si pointers */ if (o->gch.si != NULL) { - markobject(K, o->gch.si); + markobject(K, o->gch.si); } uint8_t type = o->gch.tt; @@ -252,95 +252,95 @@ static int32_t propagatemark (klisp_State *K) { /* case K_TBIGRAT: case K_TBIGINT: bigints & bigrats are never gray */ case K_TPAIR: { - Pair *p = cast(Pair *, o); - markvalue(K, p->mark); - markvalue(K, p->car); - markvalue(K, p->cdr); - return sizeof(Pair); + Pair *p = cast(Pair *, o); + markvalue(K, p->mark); + markvalue(K, p->car); + markvalue(K, p->cdr); + return sizeof(Pair); } case K_TSYMBOL: { - Symbol *s = cast(Symbol *, o); - markvalue(K, s->str); - return sizeof(Symbol); + Symbol *s = cast(Symbol *, o); + markvalue(K, s->str); + return sizeof(Symbol); } case K_TKEYWORD: { - Keyword *k = cast(Keyword *, o); - markvalue(K, k->str); - return sizeof(Keyword); + Keyword *k = cast(Keyword *, o); + markvalue(K, k->str); + return sizeof(Keyword); } case K_TSTRING: { - String *s = cast(String *, o); - markvalue(K, s->mark); - return sizeof(String) + (s->size + 1 * sizeof(char)); + String *s = cast(String *, o); + markvalue(K, s->mark); + return sizeof(String) + (s->size + 1 * sizeof(char)); } case K_TENVIRONMENT: { - Environment *e = cast(Environment *, o); - markvalue(K, e->mark); - markvalue(K, e->parents); - markvalue(K, e->bindings); - markvalue(K, e->keyed_node); - markvalue(K, e->keyed_parents); - return sizeof(Environment); + Environment *e = cast(Environment *, o); + markvalue(K, e->mark); + markvalue(K, e->parents); + markvalue(K, e->bindings); + markvalue(K, e->keyed_node); + markvalue(K, e->keyed_parents); + return sizeof(Environment); } case K_TCONTINUATION: { - Continuation *c = cast(Continuation *, o); - markvalue(K, c->mark); - markvalue(K, c->parent); - markvalue(K, c->comb); - markvaluearray(K, c->extra, c->extra_size); - return sizeof(Continuation) + sizeof(TValue) * c->extra_size; + Continuation *c = cast(Continuation *, o); + markvalue(K, c->mark); + markvalue(K, c->parent); + markvalue(K, c->comb); + markvaluearray(K, c->extra, c->extra_size); + return sizeof(Continuation) + sizeof(TValue) * c->extra_size; } case K_TOPERATIVE: { - Operative *op = cast(Operative *, o); - markvaluearray(K, op->extra, op->extra_size); - return sizeof(Operative) + sizeof(TValue) * op->extra_size; + Operative *op = cast(Operative *, o); + markvaluearray(K, op->extra, op->extra_size); + return sizeof(Operative) + sizeof(TValue) * op->extra_size; } case K_TAPPLICATIVE: { - Applicative *a = cast(Applicative *, o); - markvalue(K, a->underlying); - return sizeof(Applicative); + Applicative *a = cast(Applicative *, o); + markvalue(K, a->underlying); + return sizeof(Applicative); } case K_TENCAPSULATION: { - Encapsulation *e = cast(Encapsulation *, o); - markvalue(K, e->key); - markvalue(K, e->value); - return sizeof(Encapsulation); + Encapsulation *e = cast(Encapsulation *, o); + markvalue(K, e->key); + markvalue(K, e->value); + return sizeof(Encapsulation); } case K_TPROMISE: { - Promise *p = cast(Promise *, o); - markvalue(K, p->node); - return sizeof(Promise); + Promise *p = cast(Promise *, o); + markvalue(K, p->node); + return sizeof(Promise); } case K_TTABLE: { - Table *h = cast(Table *, o); - if (traversetable(K, h)) /* table is weak? */ - black2gray(o); /* keep it gray */ - return sizeof(Table) + sizeof(TValue) * h->sizearray + - sizeof(Node) * sizenode(h); + Table *h = cast(Table *, o); + if (traversetable(K, h)) /* table is weak? */ + black2gray(o); /* keep it gray */ + return sizeof(Table) + sizeof(TValue) * h->sizearray + + sizeof(Node) * sizenode(h); } case K_TERROR: { - Error *e = cast(Error *, o); - markvalue(K, e->who); - markvalue(K, e->cont); - markvalue(K, e->msg); - markvalue(K, e->irritants); - return sizeof(Error); + Error *e = cast(Error *, o); + markvalue(K, e->who); + markvalue(K, e->cont); + markvalue(K, e->msg); + markvalue(K, e->irritants); + return sizeof(Error); } case K_TBYTEVECTOR: { - Bytevector *b = cast(Bytevector *, o); - markvalue(K, b->mark); - return sizeof(Bytevector) + b->size * sizeof(uint8_t); + Bytevector *b = cast(Bytevector *, o); + markvalue(K, b->mark); + return sizeof(Bytevector) + b->size * sizeof(uint8_t); } case K_TFPORT: { - FPort *p = cast(FPort *, o); - markvalue(K, p->filename); - return sizeof(FPort); + FPort *p = cast(FPort *, o); + markvalue(K, p->filename); + return sizeof(FPort); } case K_TMPORT: { - MPort *p = cast(MPort *, o); - markvalue(K, p->filename); - markvalue(K, p->buf); - return sizeof(MPort); + MPort *p = cast(MPort *, o); + markvalue(K, p->filename); + markvalue(K, p->buf); + return sizeof(MPort); } case K_TVECTOR: { Vector *v = cast(Vector *, o); @@ -349,15 +349,15 @@ static int32_t propagatemark (klisp_State *K) { return sizeof(Vector) + v->sizearray * sizeof(TValue); } case K_TMODULE: { - Module *m = cast(Module *, o); - markvalue(K, m->env); - markvalue(K, m->exp_list); - return sizeof(Module); + Module *m = cast(Module *, o); + markvalue(K, m->env); + markvalue(K, m->exp_list); + return sizeof(Module); } default: - fprintf(stderr, "Unknown GCObject type (in GC propagate): %d\n", - type); - abort(); + fprintf(stderr, "Unknown GCObject type (in GC propagate): %d\n", + type); + abort(); } } @@ -381,14 +381,14 @@ static int32_t iscleared (TValue o, int iskey) { if (!iscollectable(o)) return 0; #if 0 /* klisp: strings may be mutable... */ if (ttisstring(o)) { - stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ - return 0; + stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */ + return 0; } #endif return iswhite(gcvalue(o)); /* klisp: keep around for later - || (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o)))); + || (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o)))); */ } @@ -398,27 +398,27 @@ static int32_t iscleared (TValue o, int iskey) { */ static void cleartable (GCObject *l) { while (l) { - Table *h = (Table *) (l); - int32_t i = h->sizearray; - klisp_assert(testbit(h->gct, VALUEWEAKBIT) || - testbit(h->gct, KEYWEAKBIT)); - if (testbit(h->gct, VALUEWEAKBIT)) { - while (i--) { - TValue *o = &h->array[i]; - if (iscleared(*o, 0)) /* value was collected? */ - *o = KFREE; /* remove value */ - } - } - i = sizenode(h); - while (i--) { - Node *n = gnode(h, i); - if (!ttisfree(gval(n)) && /* non-empty entry? */ - (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { - gval(n) = KFREE; /* remove value ... */ - removeentry(n); /* remove entry from table */ - } - } - l = h->gclist; + Table *h = (Table *) (l); + int32_t i = h->sizearray; + klisp_assert(testbit(h->gct, VALUEWEAKBIT) || + testbit(h->gct, KEYWEAKBIT)); + if (testbit(h->gct, VALUEWEAKBIT)) { + while (i--) { + TValue *o = &h->array[i]; + if (iscleared(*o, 0)) /* value was collected? */ + *o = KFREE; /* remove value */ + } + } + i = sizenode(h); + while (i--) { + Node *n = gnode(h, i); + if (!ttisfree(gval(n)) && /* non-empty entry? */ + (iscleared(key2tval(n), 1) || iscleared(gval(n), 0))) { + gval(n) = KFREE; /* remove value ... */ + removeentry(n); /* remove entry from table */ + } + } + l = h->gclist; } } @@ -427,93 +427,93 @@ static void freeobj (klisp_State *K, GCObject *o) { uint8_t type = o->gch.tt; switch (type) { case K_TBIGINT: { - mp_int_free(K, (Bigint *)o); - break; + mp_int_free(K, (Bigint *)o); + break; } case K_TBIGRAT: { - mp_rat_free(K, (Bigrat *)o); - break; + mp_rat_free(K, (Bigrat *)o); + break; } case K_TPAIR: - klispM_free(K, (Pair *)o); - break; + klispM_free(K, (Pair *)o); + break; case K_TSYMBOL: - /* symbols are in the string/symbol table */ - /* The string will be freed before/after */ - /* symbols with no source info are in the string/symbol table */ - if (ttisnil(ktry_get_si(K, gc2sym(o)))) - K->strt.nuse--; - klispM_free(K, (Symbol *)o); - break; + /* symbols are in the string/symbol table */ + /* The string will be freed before/after */ + /* symbols with no source info are in the string/symbol table */ + if (ttisnil(ktry_get_si(K, gc2sym(o)))) + K->strt.nuse--; + klispM_free(K, (Symbol *)o); + break; case K_TKEYWORD: - /* keywords are in the string table */ - /* The string will be freed before/after */ - K->strt.nuse--; - klispM_free(K, (Keyword *)o); - break; + /* keywords are in the string table */ + /* The string will be freed before/after */ + K->strt.nuse--; + klispM_free(K, (Keyword *)o); + break; case K_TSTRING: - /* immutable strings are in the string/symbol table */ - if (kstring_immutablep(gc2str(o))) - K->strt.nuse--; - klispM_freemem(K, o, sizeof(String)+o->str.size+1); - break; + /* immutable strings are in the string/symbol table */ + if (kstring_immutablep(gc2str(o))) + K->strt.nuse--; + klispM_freemem(K, o, sizeof(String)+o->str.size+1); + break; case K_TENVIRONMENT: - klispM_free(K, (Environment *)o); - break; + klispM_free(K, (Environment *)o); + break; case K_TCONTINUATION: - klispM_freemem(K, o, sizeof(Continuation) + - o->cont.extra_size * sizeof(TValue)); - break; + klispM_freemem(K, o, sizeof(Continuation) + + o->cont.extra_size * sizeof(TValue)); + break; case K_TOPERATIVE: - klispM_freemem(K, o, sizeof(Operative) + - o->op.extra_size * sizeof(TValue)); - break; + klispM_freemem(K, o, sizeof(Operative) + + o->op.extra_size * sizeof(TValue)); + break; case K_TAPPLICATIVE: - klispM_free(K, (Applicative *)o); - break; + klispM_free(K, (Applicative *)o); + break; case K_TENCAPSULATION: - klispM_free(K, (Encapsulation *)o); - break; + klispM_free(K, (Encapsulation *)o); + break; case K_TPROMISE: - klispM_free(K, (Promise *)o); - break; + klispM_free(K, (Promise *)o); + break; case K_TTABLE: - klispH_free(K, (Table *)o); - break; + klispH_free(K, (Table *)o); + break; case K_TERROR: - klispE_free(K, (Error *)o); - break; + klispE_free(K, (Error *)o); + break; case K_TBYTEVECTOR: - /* immutable bytevectors are in the string/symbol table */ - if (kbytevector_immutablep(gc2str(o))) - K->strt.nuse--; - klispM_freemem(K, o, sizeof(Bytevector)+o->bytevector.size); - break; + /* immutable bytevectors are in the string/symbol table */ + if (kbytevector_immutablep(gc2str(o))) + K->strt.nuse--; + klispM_freemem(K, o, sizeof(Bytevector)+o->bytevector.size); + break; case K_TFPORT: - /* first close the port to free the FILE structure. - This works even if the port was already closed, - it is important that this don't throw errors, because - the mechanism used in error handling would crash at this - point */ - kclose_port(K, gc2fport(o)); - klispM_free(K, (FPort *)o); - break; + /* first close the port to free the FILE structure. + This works even if the port was already closed, + it is important that this don't throw errors, because + the mechanism used in error handling would crash at this + point */ + kclose_port(K, gc2fport(o)); + klispM_free(K, (FPort *)o); + break; case K_TMPORT: - /* memory ports (string & bytevector) don't need to be closed - explicitly */ - klispM_free(K, (MPort *)o); - break; + /* memory ports (string & bytevector) don't need to be closed + explicitly */ + klispM_free(K, (MPort *)o); + break; case K_TVECTOR: klispM_freemem(K, o, sizeof(Vector) + sizeof(TValue) * o->vector.sizearray); break; case K_TMODULE: - klispM_free(K, (Module *)o); - break; + klispM_free(K, (Module *)o); + break; default: - /* shouldn't happen */ - fprintf(stderr, "Unknown GCObject type (in GC free): %d\n", - type); - abort(); + /* shouldn't happen */ + fprintf(stderr, "Unknown GCObject type (in GC free): %d\n", + type); + abort(); } } @@ -527,17 +527,17 @@ static GCObject **sweeplist (klisp_State *K, GCObject **p, uint32_t count) GCObject *curr; int deadmask = otherwhite(K); while ((curr = *p) != NULL && count-- > 0) { - if ((curr->gch.gct ^ WHITEBITS) & deadmask) { /* not dead? */ - klisp_assert(!isdead(K, curr) || testbit(curr->gch.gct, FIXEDBIT)); - makewhite(K, curr); /* make it white (for next cycle) */ - p = &curr->gch.next; - } else { /* must erase `curr' */ - klisp_assert(isdead(K, curr) || deadmask == bitmask(SFIXEDBIT)); - *p = curr->gch.next; - if (curr == K->rootgc) /* is the first element of the list? */ - K->rootgc = curr->gch.next; /* adjust first */ - freeobj(K, curr); - } + if ((curr->gch.gct ^ WHITEBITS) & deadmask) { /* not dead? */ + klisp_assert(!isdead(K, curr) || testbit(curr->gch.gct, FIXEDBIT)); + makewhite(K, curr); /* make it white (for next cycle) */ + p = &curr->gch.next; + } else { /* must erase `curr' */ + klisp_assert(isdead(K, curr) || deadmask == bitmask(SFIXEDBIT)); + *p = curr->gch.next; + if (curr == K->rootgc) /* is the first element of the list? */ + K->rootgc = curr->gch.next; /* adjust first */ + freeobj(K, curr); + } } return p; } @@ -546,12 +546,12 @@ static void checkSizes (klisp_State *K) { /* check size of string/symbol hash */ if (K->strt.nuse < cast(uint32_t , K->strt.size/4) && K->strt.size > MINSTRTABSIZE*2) - klispS_resize(K, K->strt.size/2); /* table is too big */ + klispS_resize(K, K->strt.size/2); /* table is too big */ #if 0 /* not used in klisp */ /* check size of buffer */ if (luaZ_sizebuffer(&g->buff) > LUA_MINBUFFER*2) { /* buffer too big? */ - size_t newsize = luaZ_sizebuffer(&g->buff) / 2; - luaZ_resizebuffer(L, &g->buff, newsize); + size_t newsize = luaZ_sizebuffer(&g->buff) / 2; + luaZ_resizebuffer(L, &g->buff, newsize); } #endif } @@ -564,24 +564,24 @@ static void GCTM (lua_State *L) { const TValue *tm; /* remove udata from `tmudata' */ if (o == g->tmudata) /* last element? */ - g->tmudata = NULL; + g->tmudata = NULL; else - g->tmudata->gch.next = udata->uv.next; + g->tmudata->gch.next = udata->uv.next; udata->uv.next = g->mainthread->next; /* return it to `root' list */ g->mainthread->next = o; makewhite(g, o); tm = fasttm(L, udata->uv.metatable, TM_GC); if (tm != NULL) { - lu_byte oldah = L->allowhook; - lu_mem oldt = g->GCthreshold; - L->allowhook = 0; /* stop debug hooks during GC tag method */ - g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */ - setobj2s(L, L->top, tm); - setuvalue(L, L->top+1, udata); - L->top += 2; - luaD_call(L, L->top - 2, 0); - L->allowhook = oldah; /* restore hooks */ - g->GCthreshold = oldt; /* restore threshold */ + lu_byte oldah = L->allowhook; + lu_mem oldt = g->GCthreshold; + L->allowhook = 0; /* stop debug hooks during GC tag method */ + g->GCthreshold = 2*g->totalbytes; /* avoid GC steps */ + setobj2s(L, L->top, tm); + setuvalue(L, L->top+1, udata); + L->top += 2; + luaD_call(L, L->top - 2, 0); + L->allowhook = oldah; /* restore hooks */ + g->GCthreshold = oldt; /* restore threshold */ } } @@ -591,7 +591,7 @@ static void GCTM (lua_State *L) { */ void klispC_callGCTM (lua_State *L) { while (G(L)->tmudata) - GCTM(L); + GCTM(L); } #endif @@ -600,11 +600,11 @@ void klispC_callGCTM (lua_State *L) { void klispC_freeall (klisp_State *K) { /* mask to collect all elements */ K->currentwhite = WHITEBITS | bitmask(SFIXEDBIT); /* in klisp this may not be - necessary */ + necessary */ sweepwholelist(K, &K->rootgc); /* free all keyword/symbol/string/bytevectors lists */ for (int32_t i = 0; i < K->strt.size; i++) - sweepwholelist(K, &K->strt.hash[i]); + sweepwholelist(K, &K->strt.hash[i]); } @@ -662,7 +662,7 @@ static void markroot (klisp_State *K) { /* the area protecting variables is an array of type TValue *[] */ TValue **ptr = K->rooted_vars_buf; for (int i = 0, top = K->rooted_vars_top; i < top; i++, ptr++) { - markvalue(K, **ptr); + markvalue(K, **ptr); } K->gcstate = GCSpropagate; @@ -703,52 +703,52 @@ static void atomic (klisp_State *K) { static int32_t singlestep (klisp_State *K) { switch (K->gcstate) { case GCSpause: { - markroot(K); /* start a new collection */ - return 0; + markroot(K); /* start a new collection */ + return 0; } case GCSpropagate: { - if (K->gray) - return propagatemark(K); - else { /* no more `gray' objects */ - atomic(K); /* finish mark phase */ - return 0; - } + if (K->gray) + return propagatemark(K); + else { /* no more `gray' objects */ + atomic(K); /* finish mark phase */ + return 0; + } } case GCSsweepstring: { - uint32_t old = K->totalbytes; - sweepwholelist(K, &K->strt.hash[K->sweepstrgc++]); - if (K->sweepstrgc >= K->strt.size) /* nothing more to sweep? */ - K->gcstate = GCSsweep; /* end sweep-string phase */ - klisp_assert(old >= K->totalbytes); - K->estimate -= old - K->totalbytes; - return GCSWEEPCOST; + uint32_t old = K->totalbytes; + sweepwholelist(K, &K->strt.hash[K->sweepstrgc++]); + if (K->sweepstrgc >= K->strt.size) /* nothing more to sweep? */ + K->gcstate = GCSsweep; /* end sweep-string phase */ + klisp_assert(old >= K->totalbytes); + K->estimate -= old - K->totalbytes; + return GCSWEEPCOST; } case GCSsweep: { - uint32_t old = K->totalbytes; - K->sweepgc = sweeplist(K, K->sweepgc, GCSWEEPMAX); - if (*K->sweepgc == NULL) { /* nothing more to sweep? */ - checkSizes(K); - K->gcstate = GCSfinalize; /* end sweep phase */ - } - klisp_assert(old >= K->totalbytes); - K->estimate -= old - K->totalbytes; - return GCSWEEPMAX*GCSWEEPCOST; + uint32_t old = K->totalbytes; + K->sweepgc = sweeplist(K, K->sweepgc, GCSWEEPMAX); + if (*K->sweepgc == NULL) { /* nothing more to sweep? */ + checkSizes(K); + K->gcstate = GCSfinalize; /* end sweep phase */ + } + klisp_assert(old >= K->totalbytes); + K->estimate -= old - K->totalbytes; + return GCSWEEPMAX*GCSWEEPCOST; } case GCSfinalize: { #if 0 /* keep around */ - if (g->tmudata) { - GCTM(L); - if (g->estimate > GCFINALIZECOST) - g->estimate -= GCFINALIZECOST; - return GCFINALIZECOST; - } - else { + if (g->tmudata) { + GCTM(L); + if (g->estimate > GCFINALIZECOST) + g->estimate -= GCFINALIZECOST; + return GCFINALIZECOST; + } + else { #endif - K->gcstate = GCSpause; /* end collection */ - K->gcdept = 0; - return 0; + K->gcstate = GCSpause; /* end collection */ + K->gcdept = 0; + return 0; #if 0 - } + } #endif } default: klisp_assert(0); return 0; @@ -760,69 +760,69 @@ void klispC_step (klisp_State *K) { int32_t lim = (GCSTEPSIZE/100) * K->gcstepmul; if (lim == 0) - lim = (UINT32_MAX-1)/2; /* no limit */ + lim = (UINT32_MAX-1)/2; /* no limit */ K->gcdept += K->totalbytes - K->GCthreshold; do { - lim -= singlestep(K); - if (K->gcstate == GCSpause) - break; + lim -= singlestep(K); + if (K->gcstate == GCSpause) + break; } while (lim > 0); if (K->gcstate != GCSpause) { - if (K->gcdept < GCSTEPSIZE) { - K->GCthreshold = K->totalbytes + GCSTEPSIZE; - /* - lim/g->gcstepmul;*/ - } else { - K->gcdept -= GCSTEPSIZE; - K->GCthreshold = K->totalbytes; - } + if (K->gcdept < GCSTEPSIZE) { + K->GCthreshold = K->totalbytes + GCSTEPSIZE; + /* - lim/g->gcstepmul;*/ + } else { + K->gcdept -= GCSTEPSIZE; + K->GCthreshold = K->totalbytes; + } } else { - klisp_assert(K->totalbytes >= K->estimate); - setthreshold(K); + klisp_assert(K->totalbytes >= K->estimate); + setthreshold(K); } } void klispC_fullgc (klisp_State *K) { - if (K->gcstate <= GCSpropagate) { - /* reset sweep marks to sweep all elements (returning them to white) */ - K->sweepstrgc = 0; - K->sweepgc = &K->rootgc; - /* reset other collector lists */ - K->gray = NULL; - K->grayagain = NULL; - K->weak = NULL; - K->gcstate = GCSsweepstring; - } - klisp_assert(K->gcstate != GCSpause && K->gcstate != GCSpropagate); - /* finish any pending sweep phase */ - while (K->gcstate != GCSfinalize) { - klisp_assert(K->gcstate == GCSsweepstring || K->gcstate == GCSsweep); - singlestep(K); - } - markroot(K); - while (K->gcstate != GCSpause) { - singlestep(K); - } - setthreshold(K); + if (K->gcstate <= GCSpropagate) { + /* reset sweep marks to sweep all elements (returning them to white) */ + K->sweepstrgc = 0; + K->sweepgc = &K->rootgc; + /* reset other collector lists */ + K->gray = NULL; + K->grayagain = NULL; + K->weak = NULL; + K->gcstate = GCSsweepstring; + } + klisp_assert(K->gcstate != GCSpause && K->gcstate != GCSpropagate); + /* finish any pending sweep phase */ + while (K->gcstate != GCSfinalize) { + klisp_assert(K->gcstate == GCSsweepstring || K->gcstate == GCSsweep); + singlestep(K); + } + markroot(K); + while (K->gcstate != GCSpause) { + singlestep(K); + } + setthreshold(K); } /* TODO: make all code using mutation to call these, - this is actually the only thing that is missing for an incremental - garbage collector! - IMPORTANT: a call to maybe a different but similar function should be - made before assigning to a GC guarded variable, or pushed in a GC -guarded stack! */ + this is actually the only thing that is missing for an incremental + garbage collector! + IMPORTANT: a call to maybe a different but similar function should be + made before assigning to a GC guarded variable, or pushed in a GC + guarded stack! */ void klispC_barrierf (klisp_State *K, GCObject *o, GCObject *v) { klisp_assert(isblack(o) && iswhite(v) && !isdead(K, v) && !isdead(K, o)); klisp_assert(K->gcstate != GCSfinalize && K->gcstate != GCSpause); klisp_assert(o->gch.tt != K_TTABLE); /* must keep invariant? */ if (K->gcstate == GCSpropagate) - reallymarkobject(K, v); /* restore invariant */ + reallymarkobject(K, v); /* restore invariant */ else /* don't mind */ - makewhite(K, o); /* mark as white just to avoid other barriers */ + makewhite(K, o); /* mark as white just to avoid other barriers */ } void klispC_barrierback (klisp_State *K, Table *t) { diff --git a/src/kgc.h b/src/kgc.h @@ -25,7 +25,7 @@ #define GCSfinalize 4 /* NOTE: unlike in lua the gc flags have 16 bits in klisp, - so resetbits is slightly different */ + so resetbits is slightly different */ /* ** some useful bit tricks @@ -69,8 +69,8 @@ #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) -#define iswhite(x) test2bits((x)->gch.gct, WHITE0BIT, WHITE1BIT) -#define isblack(x) testbit((x)->gch.gct, BLACKBIT) +#define iswhite(x) test2bits((x)->gch.gct, WHITE0BIT, WHITE1BIT) +#define isblack(x) testbit((x)->gch.gct, BLACKBIT) #define isgray(x) (!isblack(x) && !iswhite(x)) @@ -85,23 +85,23 @@ #define klispC_white(K) cast(uint16_t, (K)->currentwhite & WHITEBITS) -#define klispC_checkGC(K) { \ - if (K->totalbytes >= K->GCthreshold) \ - klispC_step(K); } +#define klispC_checkGC(K) { \ + if (K->totalbytes >= K->GCthreshold) \ + klispC_step(K); } -#define klispC_barrier(K,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ - klispC_barrierf(K,obj2gco(p),gcvalue(v)); } +#define klispC_barrier(K,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ + klispC_barrierf(K,obj2gco(p),gcvalue(v)); } -#define klispC_barriert(K,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ - klispC_barrierback(K,t); } +#define klispC_barriert(K,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ + klispC_barrierback(K,t); } -#define klispC_objbarrier(K,p,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ - klispC_barrierf(K,obj2gco(p),obj2gco(o)); } +#define klispC_objbarrier(K,p,o) \ + { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ + klispC_barrierf(K,obj2gco(p),obj2gco(o)); } -#define klispC_objbarriert(K,t,o) \ - { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) klispC_barrierback(K,t); } +#define klispC_objbarriert(K,t,o) \ + { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) klispC_barrierback(K,t); } /* size_t klispC_separateudata (klisp_State *K, int all); */ /* void klispC_callGCTM (klisp_State *K); */ diff --git a/src/kgchars.c b/src/kgchars.c @@ -56,15 +56,15 @@ void kinteger_to_char(klisp_State *K) bind_1tp(K, ptree, "exact integer", ttiseinteger, itv); if (ttisbigint(itv)) { - klispE_throw_simple(K, "integer out of ASCII range [0 - 127]"); - return; + klispE_throw_simple(K, "integer out of ASCII range [0 - 127]"); + return; } int32_t i = ivalue(itv); /* for now only allow ASCII */ if (i < 0 || i > 127) { - klispE_throw_simple(K, "integer out of ASCII range [0 - 127]"); - return; + klispE_throw_simple(K, "integer out of ASCII range [0 - 127]"); + return; } kapply_cc(K, ch2tv((char) i)); } @@ -114,11 +114,11 @@ void char_digitp(klisp_State *K) int base = 10; /* default */ if (get_opt_tpar(K, basetv, "base [2-36]", ttisbase)) { - base = ivalue(basetv); + base = ivalue(basetv); } char ch = tolower(chvalue(chtv)); bool b = (isdigit(ch) && (ch - '0') < base) || - (isalpha(ch) && (ch - 'a' + 10) < base); + (isalpha(ch) && (ch - 'a' + 10) < base); kapply_cc(K, b2tv(b)); } @@ -136,19 +136,19 @@ void char_to_digit(klisp_State *K) int base = 10; /* default */ if (get_opt_tpar(K, basetv, "base [2-36]", ttisbase)) { - base = ivalue(basetv); + base = ivalue(basetv); } char ch = tolower(chvalue(chtv)); int digit = 0; if (isdigit(ch) && (ch - '0') < base) - digit = ch - '0'; + digit = ch - '0'; else if (isalpha(ch) && (ch - 'a' + 10) < base) - digit = ch - 'a' + 10; + digit = ch - 'a' + 10; else { - klispE_throw_simple_with_irritants(K, "Not a digit in this base", - 2, ch2tv(ch), i2tv(base)); - return; + klispE_throw_simple_with_irritants(K, "Not a digit in this base", + 2, ch2tv(ch), i2tv(base)); + return; } kapply_cc(K, i2tv(digit)); } @@ -167,19 +167,19 @@ void digit_to_char(klisp_State *K) int base = 10; /* default */ if (get_opt_tpar(K, basetv, "base [2-36]", ttisbase)) { - base = ivalue(basetv); + base = ivalue(basetv); } if (ttisbigint(digittv) || ivalue(digittv) < 0 || - ivalue(digittv) >= base) { - klispE_throw_simple_with_irritants(K, "Not a digit in this base", - 2, digittv, i2tv(base)); - return; + ivalue(digittv) >= base) { + klispE_throw_simple_with_irritants(K, "Not a digit in this base", + 2, digittv, i2tv(base)); + return; } int digit = ivalue(digittv); char ch = digit <= 9? - '0' + digit : - 'a' + (digit - 10); + '0' + digit : + 'a' + (digit - 10); kapply_cc(K, ch2tv(ch)); } @@ -198,59 +198,59 @@ void kinit_chars_ground_env(klisp_State *K) /* 14.1.1? char? */ add_applicative(K, ground_env, "char?", typep, 2, symbol, - i2tv(K_TCHAR)); + i2tv(K_TCHAR)); /* 14.1.2? char-alphabetic?, char-numeric?, char-whitespace? */ /* unlike in r5rs these take an arbitrary number of chars (even cyclical list) */ add_applicative(K, ground_env, "char-alphabetic?", ftyped_predp, 3, - symbol, p2tv(kcharp), p2tv(kchar_alphabeticp)); + symbol, p2tv(kcharp), p2tv(kchar_alphabeticp)); add_applicative(K, ground_env, "char-numeric?", ftyped_predp, 3, - symbol, p2tv(kcharp), p2tv(kchar_numericp)); + symbol, p2tv(kcharp), p2tv(kchar_numericp)); add_applicative(K, ground_env, "char-whitespace?", ftyped_predp, 3, - symbol, p2tv(kcharp), p2tv(kchar_whitespacep)); + symbol, p2tv(kcharp), p2tv(kchar_whitespacep)); /* 14.1.3? char-upper-case?, char-lower-case? */ /* unlike in r5rs these take an arbitrary number of chars (even cyclical list) */ add_applicative(K, ground_env, "char-upper-case?", ftyped_predp, 3, - symbol, p2tv(kcharp), p2tv(kchar_upper_casep)); + symbol, p2tv(kcharp), p2tv(kchar_upper_casep)); add_applicative(K, ground_env, "char-lower-case?", ftyped_predp, 3, - symbol, p2tv(kcharp), p2tv(kchar_lower_casep)); + symbol, p2tv(kcharp), p2tv(kchar_lower_casep)); /* 14.1.4? char->integer, integer->char */ add_applicative(K, ground_env, "char->integer", kchar_to_integer, 0); add_applicative(K, ground_env, "integer->char", kinteger_to_char, 0); /* 14.1.4? char-upcase, char-downcase, char-titlecase, char-foldcase */ add_applicative(K, ground_env, "char-upcase", kchar_change_case, 1, - p2tv(toupper)); + p2tv(toupper)); add_applicative(K, ground_env, "char-downcase", kchar_change_case, 1, - p2tv(tolower)); + p2tv(tolower)); add_applicative(K, ground_env, "char-titlecase", kchar_change_case, 1, - p2tv(toupper)); + p2tv(toupper)); add_applicative(K, ground_env, "char-foldcase", kchar_change_case, 1, - p2tv(tolower)); + p2tv(tolower)); /* 14.2.1? char=? */ add_applicative(K, ground_env, "char=?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_eqp)); + symbol, p2tv(kcharp), p2tv(kchar_eqp)); /* 14.2.2? char<?, char<=?, char>?, char>=? */ add_applicative(K, ground_env, "char<?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_ltp)); + symbol, p2tv(kcharp), p2tv(kchar_ltp)); add_applicative(K, ground_env, "char<=?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_lep)); + symbol, p2tv(kcharp), p2tv(kchar_lep)); add_applicative(K, ground_env, "char>?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_gtp)); + symbol, p2tv(kcharp), p2tv(kchar_gtp)); add_applicative(K, ground_env, "char>=?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_gep)); + symbol, p2tv(kcharp), p2tv(kchar_gep)); /* 14.2.3? char-ci=? */ add_applicative(K, ground_env, "char-ci=?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_ci_eqp)); + symbol, p2tv(kcharp), p2tv(kchar_ci_eqp)); /* 14.2.4? char-ci<?, char-ci<=?, char-ci>?, char-ci>=? */ add_applicative(K, ground_env, "char-ci<?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_ci_ltp)); + symbol, p2tv(kcharp), p2tv(kchar_ci_ltp)); add_applicative(K, ground_env, "char-ci<=?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_ci_lep)); + symbol, p2tv(kcharp), p2tv(kchar_ci_lep)); add_applicative(K, ground_env, "char-ci>?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_ci_gtp)); + symbol, p2tv(kcharp), p2tv(kchar_ci_gtp)); add_applicative(K, ground_env, "char-ci>=?", ftyped_bpredp, 3, - symbol, p2tv(kcharp), p2tv(kchar_ci_gep)); + symbol, p2tv(kcharp), p2tv(kchar_ci_gep)); /* 14.2.? char-digit?, char->digit, digit->char */ add_applicative(K, ground_env, "char-digit?", char_digitp, 0); add_applicative(K, ground_env, "char->digit", char_to_digit, 0); diff --git a/src/kgcombiners.c b/src/kgcombiners.c @@ -63,16 +63,16 @@ void Svau(klisp_State *K) TValue new_op = kmake_operative(K, do_vau, 4, vptree, vpenv, vbody, denv); - #if KTRACK_SI +#if KTRACK_SI /* save as source code info the info from the expression whose evaluation got us here */ TValue si = kget_csi(K); if (!ttisnil(si)) { - krooted_tvs_push(K, new_op); - kset_source_info(K, new_op, si); - krooted_tvs_pop(K); + krooted_tvs_push(K, new_op); + kset_source_info(K, new_op, si); + krooted_tvs_pop(K); } - #endif +#endif krooted_tvs_pop(K); krooted_tvs_pop(K); @@ -107,29 +107,29 @@ void do_vau(klisp_State *K) match(K, env, op_ptree, ptree); if (!ttisignore(penv)) - kadd_binding(K, env, penv, denv); + kadd_binding(K, env, penv, denv); /* keep env in stack in case a cont has to be constructed */ if (ttisnil(body)) { - krooted_tvs_pop(K); - kapply_cc(K, KINERT); + krooted_tvs_pop(K); + kapply_cc(K, KINERT); } else { - /* this is needed because seq continuation doesn't check for - nil sequence */ - TValue tail = kcdr(body); - if (ttispair(tail)) { - TValue new_cont = kmake_continuation(K, kget_cc(K), - do_seq, 2, tail, env); - kset_cc(K, new_cont); + /* this is needed because seq continuation doesn't check for + nil sequence */ + TValue tail = kcdr(body); + if (ttispair(tail)) { + TValue new_cont = kmake_continuation(K, kget_cc(K), + do_seq, 2, tail, env); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, body)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, body)); #endif - } - krooted_tvs_pop(K); - ktail_eval(K, kcar(body), env); + } + krooted_tvs_pop(K); + ktail_eval(K, kcar(body), env); } } @@ -145,16 +145,16 @@ void wrap(klisp_State *K) bind_1tp(K, ptree, "combiner", ttiscombiner, comb); TValue new_app = kwrap(K, comb); - #if KTRACK_SI +#if KTRACK_SI /* save as source code info the info from the expression whose evaluation got us here */ TValue si = kget_csi(K); if (!ttisnil(si)) { - krooted_tvs_push(K, new_app); - kset_source_info(K, new_app, si); - krooted_tvs_pop(K); + krooted_tvs_push(K, new_app); + kset_source_info(K, new_app, si); + krooted_tvs_pop(K); } - #endif +#endif kapply_cc(K, new_app); } @@ -194,19 +194,19 @@ void Slambda(klisp_State *K) krooted_tvs_push(K, vbody); TValue new_app = kmake_applicative(K, do_vau, 4, vptree, KIGNORE, vbody, - denv); - #if KTRACK_SI + denv); +#if KTRACK_SI /* save as source code info the info from the expression whose evaluation got us here, both for the applicative and the underlying combiner */ TValue si = kget_csi(K); if (!ttisnil(si)) { - krooted_tvs_push(K, new_app); - kset_source_info(K, new_app, si); - kset_source_info(K, kunwrap(new_app), si); - krooted_tvs_pop(K); + krooted_tvs_push(K, new_app); + kset_source_info(K, new_app, si); + kset_source_info(K, kunwrap(new_app), si); + krooted_tvs_pop(K); } - #endif +#endif krooted_tvs_pop(K); krooted_tvs_pop(K); @@ -224,12 +224,12 @@ void apply(klisp_State *K) UNUSED(xparams); bind_al2tp(K, ptree, - "applicative", ttisapplicative, app, - "any", anytype, obj, - maybe_env); + "applicative", ttisapplicative, app, + "any", anytype, obj, + maybe_env); TValue env = (get_opt_tpar(K, maybe_env, "environment", ttisenvironment))? - maybe_env : kmake_empty_environment(K); + maybe_env : kmake_empty_environment(K); krooted_tvs_push(K, env); TValue expr = kcons(K, kunwrap(app), obj); @@ -309,32 +309,32 @@ void do_map(klisp_State *K) /* this case is used to kick start the mapping of both the acyclic and cyclic part, avoiding code duplication */ if (!dummyp) { - TValue np = kcons(K, obj, KNIL); - kset_cdr(last_pair, np); - last_pair = np; + TValue np = kcons(K, obj, KNIL); + kset_cdr(last_pair, np); + last_pair = np; } if (n == 0) { /* pass the rest of the list and last pair for cycle handling */ - kapply_cc(K, kcons(K, ls, last_pair)); + kapply_cc(K, kcons(K, ls, last_pair)); } else { - /* copy the ptree to avoid problems with mutation */ - /* XXX: no check necessary, could just use copy_list if there - was such a procedure */ - TValue first_ptree = check_copy_list(K, kcar(ls), false, NULL, NULL); - ls = kcdr(ls); - n = n-1; - krooted_tvs_push(K, first_ptree); - /* have to unwrap the applicative to avoid extra evaluation of first */ - TValue new_expr = kcons(K, kunwrap(app), first_ptree); - krooted_tvs_push(K, new_expr); - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_map, 6, app, - ls, last_pair, i2tv(n), denv, KFALSE); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - kset_cc(K, new_cont); - ktail_eval(K, new_expr, denv); + /* copy the ptree to avoid problems with mutation */ + /* XXX: no check necessary, could just use copy_list if there + was such a procedure */ + TValue first_ptree = check_copy_list(K, kcar(ls), false, NULL, NULL); + ls = kcdr(ls); + n = n-1; + krooted_tvs_push(K, first_ptree); + /* have to unwrap the applicative to avoid extra evaluation of first */ + TValue new_expr = kcons(K, kunwrap(app), first_ptree); + krooted_tvs_push(K, new_expr); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_map, 6, app, + ls, last_pair, i2tv(n), denv, KFALSE); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + kset_cc(K, new_cont); + ktail_eval(K, new_expr, denv); } } @@ -361,16 +361,16 @@ void do_map_cycle(klisp_State *K) /* this continuation will close the cycle and return the list */ TValue encycle_cont = - kmake_continuation(K, kget_cc(K), do_map_encycle, 2, - dummy, last_apair); + kmake_continuation(K, kget_cc(K), do_map_encycle, 2, + dummy, last_apair); krooted_tvs_push(K, encycle_cont); /* schedule the mapping of the elements of the cycle, signal dummyp = true to avoid creating a pair for the inert value passed to the first continuation */ TValue new_cont = - kmake_continuation(K, encycle_cont, do_map, 6, app, ls, - last_apair, i2tv(cpairs), denv, KTRUE); + kmake_continuation(K, encycle_cont, do_map, 6, app, ls, + last_apair, i2tv(cpairs), denv, KTRUE); klisp_assert(ttisenvironment(denv)); krooted_tvs_pop(K); @@ -391,8 +391,8 @@ void map(klisp_State *K) bind_al1tp(K, ptree, "applicative", ttisapplicative, app, lss); if (ttisnil(lss)) { - klispE_throw_simple(K, "no lists"); - return; + klispE_throw_simple(K, "no lists"); + return; } /* get the metrics of the ptree of each call to app and @@ -401,13 +401,13 @@ void map(klisp_State *K) int32_t res_pairs, res_apairs, res_cpairs; map_for_each_get_metrics(K, lss, &app_apairs, &app_cpairs, - &res_apairs, &res_cpairs); + &res_apairs, &res_cpairs); app_pairs = app_apairs + app_cpairs; res_pairs = res_apairs + res_cpairs; /* create the list of parameters to app */ lss = map_for_each_transpose(K, lss, app_apairs, app_cpairs, - res_apairs, res_cpairs); + res_apairs, res_cpairs); /* ASK John: the semantics when this is mixed with continuations, isn't all that great..., but what are the expectations considering @@ -421,9 +421,9 @@ void map(klisp_State *K) krooted_tvs_push(K, dummy); TValue ret_cont = (res_cpairs == 0)? - kmake_continuation(K, kget_cc(K), do_map_ret, 1, dummy) - : kmake_continuation(K, kget_cc(K), do_map_cycle, 4, - app, dummy, i2tv(res_cpairs), denv); + kmake_continuation(K, kget_cc(K), do_map_ret, 1, dummy) + : kmake_continuation(K, kget_cc(K), do_map_cycle, 4, + app, dummy, i2tv(res_cpairs), denv); krooted_tvs_push(K, ret_cont); @@ -431,8 +431,8 @@ void map(klisp_State *K) signal dummyp = true to avoid creating a pair for the inert value passed to the first continuation */ TValue new_cont = - kmake_continuation(K, ret_cont, do_map, 6, app, lss, dummy, - i2tv(res_apairs), denv, KTRUE); + kmake_continuation(K, ret_cont, do_map, 6, app, lss, dummy, + i2tv(res_apairs), denv, KTRUE); krooted_tvs_pop(K); krooted_tvs_pop(K); @@ -472,7 +472,7 @@ void do_array_map_ret(klisp_State *K) TValue ls = kcdr(xparams[0]); TValue (*list_to_array)(klisp_State *K, TValue array, int32_t size) = - pvalue(xparams[1]); + pvalue(xparams[1]); int32_t length = ivalue(xparams[2]); /* This will also avoid some problems with continuations @@ -499,14 +499,14 @@ void array_map(klisp_State *K) TValue list_to_array_tv = xparams[0]; TValue (*array_to_list)(klisp_State *K, TValue array, int32_t *size) = - pvalue(xparams[1]); + pvalue(xparams[1]); bind_al1tp(K, ptree, "applicative", ttisapplicative, app, lss); /* check that lss is a non empty list, and copy it */ if (ttisnil(lss)) { - klispE_throw_simple(K, "no arguments after applicative"); - return; + klispE_throw_simple(K, "no arguments after applicative"); + return; } int32_t app_pairs, app_apairs, app_cpairs; @@ -526,21 +526,21 @@ void array_map(klisp_State *K) /* all array will produce acyclic lists */ for(int32_t i = 1 /* jump over first */; i < app_pairs; ++i) { - head = kcar(tail); - int32_t pairs; - ls = array_to_list(K, head, &pairs); - /* in klisp all arrays should have the same length */ - if (pairs != res_pairs) { - klispE_throw_simple(K, "arguments of different length"); - return; - } - kset_car(tail, ls); - tail = kcdr(tail); + head = kcar(tail); + int32_t pairs; + ls = array_to_list(K, head, &pairs); + /* in klisp all arrays should have the same length */ + if (pairs != res_pairs) { + klispE_throw_simple(K, "arguments of different length"); + return; + } + kset_car(tail, ls); + tail = kcdr(tail); } /* create the list of parameters to app */ lss = map_for_each_transpose(K, lss, app_apairs, app_cpairs, - res_pairs, 0); /* cycle pairs is always 0 */ + res_pairs, 0); /* cycle pairs is always 0 */ /* ASK John: the semantics when this is mixed with continuations, isn't all that great..., but what are the expectations considering @@ -556,16 +556,16 @@ void array_map(klisp_State *K) krooted_tvs_push(K, dummy); TValue ret_cont = - kmake_continuation(K, kget_cc(K), do_array_map_ret, 3, dummy, - list_to_array_tv, i2tv(res_pairs)); + kmake_continuation(K, kget_cc(K), do_array_map_ret, 3, dummy, + list_to_array_tv, i2tv(res_pairs)); krooted_tvs_push(K, ret_cont); /* schedule the mapping of the elements of the acyclic part. signal dummyp = true to avoid creating a pair for the inert value passed to the first continuation */ TValue new_cont = - kmake_continuation(K, ret_cont, do_map, 6, app, lss, dummy, - i2tv(res_pairs), denv, KTRUE); + kmake_continuation(K, ret_cont, do_map, 6, app, lss, dummy, + i2tv(res_pairs), denv, KTRUE); krooted_tvs_pop(K); krooted_tvs_pop(K); @@ -588,10 +588,10 @@ void kinit_combiners_ground_env(klisp_State *K) /* 4.10.1 operative? */ add_applicative(K, ground_env, "operative?", typep, 2, symbol, - i2tv(K_TOPERATIVE)); + i2tv(K_TOPERATIVE)); /* 4.10.2 applicative? */ add_applicative(K, ground_env, "applicative?", typep, 2, symbol, - i2tv(K_TAPPLICATIVE)); + i2tv(K_TAPPLICATIVE)); /* 4.10.3 $vau */ /* 5.3.1 $vau */ add_operative(K, ground_env, "$vau", Svau, 0); @@ -607,14 +607,14 @@ void kinit_combiners_ground_env(klisp_State *K) add_applicative(K, ground_env, "map", map, 0); /* 5.9.? string-map, vector-map, bytevector-map */ add_applicative(K, ground_env, "string-map", array_map, 2, - p2tv(list_to_string_h), p2tv(string_to_list_h)); + p2tv(list_to_string_h), p2tv(string_to_list_h)); add_applicative(K, ground_env, "vector-map", array_map, 2, - p2tv(list_to_vector_h), p2tv(vector_to_list_h)); + p2tv(list_to_vector_h), p2tv(vector_to_list_h)); add_applicative(K, ground_env, "bytevector-map", array_map, 2, - p2tv(list_to_bytevector_h), p2tv(bytevector_to_list_h)); + p2tv(list_to_bytevector_h), p2tv(bytevector_to_list_h)); /* 6.2.1 combiner? */ add_applicative(K, ground_env, "combiner?", ftypep, 2, symbol, - p2tv(kcombinerp)); + p2tv(kcombinerp)); } /* init continuation names */ diff --git a/src/kgcontinuations.c b/src/kgcontinuations.c @@ -72,16 +72,16 @@ void extend_continuation(klisp_State *K) UNUSED(xparams); bind_al2tp(K, ptree, - "continuation", ttiscontinuation, cont, - "applicative", ttisapplicative, app, - maybe_env); + "continuation", ttiscontinuation, cont, + "applicative", ttisapplicative, app, + maybe_env); TValue env = (get_opt_tpar(K, maybe_env, "environment", ttisenvironment))? - maybe_env : kmake_empty_environment(K); + maybe_env : kmake_empty_environment(K); krooted_tvs_push(K, env); TValue new_cont = kmake_continuation(K, cont, - do_extended_cont, 2, app, env); + do_extended_cont, 2, app, env); krooted_tvs_pop(K); kapply_cc(K, new_cont); } @@ -96,24 +96,24 @@ void guard_continuation(klisp_State *K) UNUSED(xparams); bind_3tp(K, ptree, "any", anytype, entry_guards, - "continuation", ttiscontinuation, cont, - "any", anytype, exit_guards); + "continuation", ttiscontinuation, cont, + "any", anytype, exit_guards); entry_guards = check_copy_guards(K, "guard-continuation: entry guards", - entry_guards); + entry_guards); krooted_tvs_push(K, entry_guards); exit_guards = check_copy_guards(K, "guard-continuation: exit guards", - exit_guards); + exit_guards); krooted_tvs_push(K, exit_guards); TValue outer_cont = kmake_continuation(K, cont, do_pass_value, - 2, entry_guards, denv); + 2, entry_guards, denv); krooted_tvs_push(K, outer_cont); /* mark it as an outer continuation */ kset_outer_cont(outer_cont); TValue inner_cont = kmake_continuation(K, outer_cont, - do_pass_value, 2, exit_guards, denv); + do_pass_value, 2, exit_guards, denv); /* mark it as an outer continuation */ kset_inner_cont(inner_cont); @@ -137,7 +137,7 @@ void continuation_applicative(klisp_State *K) UNUSED(denv); bind_1tp(K, ptree, "continuation", - ttiscontinuation, cont); + ttiscontinuation, cont); /* cont_app is from kstate, it handles dynamic vars & interceptions */ TValue app = kmake_applicative(K, cont_app, 1, cont); @@ -165,7 +165,7 @@ void apply_continuation(klisp_State *K) UNUSED(denv); bind_2tp(K, ptree, "continuation", ttiscontinuation, - cont, "any", anytype, obj); + cont, "any", anytype, obj); /* kcall_cont is from kstate, it handles dynamic vars & interceptions */ @@ -184,34 +184,34 @@ void Slet_cc(klisp_State *K) bind_al1tp(K, ptree, "symbol", ttissymbol, sym, objs); if (ttisnil(objs)) { - /* we don't even bother creating the environment */ - kapply_cc(K, KINERT); + /* we don't even bother creating the environment */ + kapply_cc(K, KINERT); } else { - TValue new_env = kmake_environment(K, denv); + TValue new_env = kmake_environment(K, denv); - /* add binding may allocate, protect env, - keep in stack until continuation is allocated */ - krooted_tvs_push(K, new_env); - kadd_binding(K, new_env, sym, kget_cc(K)); + /* add binding may allocate, protect env, + keep in stack until continuation is allocated */ + krooted_tvs_push(K, new_env); + kadd_binding(K, new_env, sym, kget_cc(K)); - /* the list of instructions is copied to avoid mutation */ - /* MAYBE: copy the evaluation structure, ASK John */ - TValue ls = check_copy_list(K, objs, false, NULL, NULL); + /* the list of instructions is copied to avoid mutation */ + /* MAYBE: copy the evaluation structure, ASK John */ + TValue ls = check_copy_list(K, objs, false, NULL, NULL); krooted_tvs_push(K, ls); - /* this is needed because seq continuation doesn't check for - nil sequence */ - TValue tail = kcdr(ls); - if (ttispair(tail)) { - TValue new_cont = kmake_continuation(K, kget_cc(K), - do_seq, 2, tail, new_env); - kset_cc(K, new_cont); - } + /* this is needed because seq continuation doesn't check for + nil sequence */ + TValue tail = kcdr(ls); + if (ttispair(tail)) { + TValue new_cont = kmake_continuation(K, kget_cc(K), + do_seq, 2, tail, new_env); + kset_cc(K, new_cont); + } - krooted_tvs_pop(K); + krooted_tvs_pop(K); krooted_tvs_pop(K); - ktail_eval(K, kcar(ls), new_env); + ktail_eval(K, kcar(ls), new_env); } } @@ -232,7 +232,7 @@ void kgexit(klisp_State *K) TValue obj = ptree; if (!get_opt_tpar(K, obj, "any", anytype)) - obj = KINERT; + obj = KINERT; /* TODO: look out for guards and dynamic variables */ /* should be probably handled in kcall_cont() */ @@ -247,38 +247,38 @@ void kinit_continuations_ground_env(klisp_State *K) /* 7.1.1 continuation? */ add_applicative(K, ground_env, "continuation?", typep, 2, symbol, - i2tv(K_TCONTINUATION)); + i2tv(K_TCONTINUATION)); /* 7.2.2 call/cc */ add_applicative(K, ground_env, "call/cc", call_cc, 0); /* 7.2.3 extend-continuation */ add_applicative(K, ground_env, "extend-continuation", extend_continuation, - 0); + 0); /* 7.2.4 guard-continuation */ add_applicative(K, ground_env, "guard-continuation", guard_continuation, - 0); + 0); /* 7.2.5 continuation->applicative */ add_applicative(K, ground_env, "continuation->applicative", - continuation_applicative, 0); + continuation_applicative, 0); /* 7.2.6 root-continuation */ klisp_assert(ttiscontinuation(K->root_cont)); add_value(K, ground_env, "root-continuation", - K->root_cont); + K->root_cont); /* 7.2.7 error-continuation */ klisp_assert(ttiscontinuation(K->error_cont)); add_value(K, ground_env, "error-continuation", - K->error_cont); + K->error_cont); /* 7.3.1 apply-continuation */ add_applicative(K, ground_env, "apply-continuation", apply_continuation, - 0); + 0); /* 7.3.2 $let/cc */ add_operative(K, ground_env, "$let/cc", Slet_cc, - 0); + 0); /* 7.3.3 guard-dynamic-extent */ add_applicative(K, ground_env, "guard-dynamic-extent", - guard_dynamic_extent, 0); + guard_dynamic_extent, 0); /* 7.3.4 exit */ add_applicative(K, ground_env, "exit", kgexit, - 0); + 0); } /* init continuation names */ diff --git a/src/kgcontrol.c b/src/kgcontrol.c @@ -43,8 +43,8 @@ void Sif(klisp_State *K) bind_3p(K, ptree, test, cons_c, alt_c); TValue new_cont = - kmake_continuation(K, kget_cc(K), do_select_clause, - 3, denv, cons_c, alt_c); + kmake_continuation(K, kget_cc(K), do_select_clause, + 3, denv, cons_c, alt_c); /* ** Mark as a bool checking cont, not necessary but avoids a continuation ** in the last evaluation in the common use of ($if ($or?/$and? ...) ...) @@ -65,12 +65,12 @@ void do_select_clause(klisp_State *K) ** xparams[2]: alternative clause */ if (ttisboolean(obj)) { - TValue denv = xparams[0]; - TValue clause = bvalue(obj)? xparams[1] : xparams[2]; - ktail_eval(K, clause, denv); + TValue denv = xparams[0]; + TValue clause = bvalue(obj)? xparams[1] : xparams[2]; + ktail_eval(K, clause, denv); } else { - klispE_throw_simple(K, "test is not a boolean"); - return; + klispE_throw_simple(K, "test is not a boolean"); + return; } } @@ -84,29 +84,29 @@ void Ssequence(klisp_State *K) UNUSED(xparams); if (ttisnil(ptree)) { - kapply_cc(K, KINERT); + kapply_cc(K, KINERT); } else { - /* the list of instructions is copied to avoid mutation */ - /* MAYBE: copy the evaluation structure, ASK John */ - TValue ls = check_copy_list(K, ptree, false, NULL, NULL); - /* this is needed because seq continuation doesn't check for - nil sequence */ - /* TODO this could be at least in an inlineable function to - allow used from $lambda, $vau, $let family, load, etc */ - TValue tail = kcdr(ls); - if (ttispair(tail)) { - krooted_tvs_push(K, ls); - TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, - tail, denv); - kset_cc(K, new_cont); + /* the list of instructions is copied to avoid mutation */ + /* MAYBE: copy the evaluation structure, ASK John */ + TValue ls = check_copy_list(K, ptree, false, NULL, NULL); + /* this is needed because seq continuation doesn't check for + nil sequence */ + /* TODO this could be at least in an inlineable function to + allow used from $lambda, $vau, $let family, load, etc */ + TValue tail = kcdr(ls); + if (ttispair(tail)) { + krooted_tvs_push(K, ls); + TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, + tail, denv); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, ls)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, ls)); #endif - krooted_tvs_pop(K); - } - ktail_eval(K, kcar(ls), denv); + krooted_tvs_pop(K); + } + ktail_eval(K, kcar(ls), denv); } } @@ -123,7 +123,7 @@ void Ssequence(klisp_State *K) */ /* GC: assumes clauses is rooted, uses dummy 1 & 2 */ TValue split_check_cond_clauses(klisp_State *K, TValue clauses, - TValue *bodies) + TValue *bodies) { TValue cars = kcons(K, KNIL, KNIL); krooted_vars_push(K, &cars); @@ -137,38 +137,38 @@ TValue split_check_cond_clauses(klisp_State *K, TValue clauses, int32_t count = 0; while(ttispair(tail) && !kis_marked(tail)) { - ++count; - TValue first = kcar(tail); - if (!ttispair(first)) { - unmark_list(K, clauses); - klispE_throw_simple(K, "bad structure in clauses"); - return KNIL; - } + ++count; + TValue first = kcar(tail); + if (!ttispair(first)) { + unmark_list(K, clauses); + klispE_throw_simple(K, "bad structure in clauses"); + return KNIL; + } - TValue new_car = kcons(K, kcar(first), KNIL); - kset_cdr(last_car_pair, new_car); - last_car_pair = new_car; - /* bodies have to be checked later */ - TValue new_cdr = kcons(K, kcdr(first), KNIL); - kset_cdr(last_cdr_pair, new_cdr); - last_cdr_pair = new_cdr; - - kset_mark(tail, kcons(K, new_car, new_cdr)); - tail = kcdr(tail); + TValue new_car = kcons(K, kcar(first), KNIL); + kset_cdr(last_car_pair, new_car); + last_car_pair = new_car; + /* bodies have to be checked later */ + TValue new_cdr = kcons(K, kcdr(first), KNIL); + kset_cdr(last_cdr_pair, new_cdr); + last_cdr_pair = new_cdr; + + kset_mark(tail, kcons(K, new_car, new_cdr)); + tail = kcdr(tail); } /* complete the cycles before unmarking */ if (ttispair(tail)) { - TValue mark = kget_mark(tail); - kset_cdr(last_car_pair, kcar(mark)); - kset_cdr(last_cdr_pair, kcdr(mark)); + TValue mark = kget_mark(tail); + kset_cdr(last_car_pair, kcar(mark)); + kset_cdr(last_cdr_pair, kcdr(mark)); } unmark_list(K, clauses); if (!ttispair(tail) && !ttisnil(tail)) { - klispE_throw_simple(K, "expected list (clauses)"); - return KNIL; + klispE_throw_simple(K, "expected list (clauses)"); + return KNIL; } /* @@ -179,10 +179,10 @@ TValue split_check_cond_clauses(klisp_State *K, TValue clauses, */ tail = kcdr(cdrs); while(count--) { - TValue first = kcar(tail); - TValue copy = check_copy_list(K, first, false, NULL, NULL); - kset_car(tail, copy); - tail = kcdr(tail); + TValue first = kcar(tail); + TValue copy = check_copy_list(K, first, false, NULL, NULL); + kset_car(tail, copy); + tail = kcdr(tail); } *bodies = kcdr(cdrs); @@ -209,48 +209,48 @@ void do_cond(klisp_State *K) TValue denv = xparams[3]; if (!ttisboolean(obj)) { - klispE_throw_simple(K, "test evaluated to a non boolean value"); - return; + klispE_throw_simple(K, "test evaluated to a non boolean value"); + return; } else if (bvalue(obj)) { - if (ttisnil(this_body)) { - kapply_cc(K, KINERT); - } else { - TValue tail = kcdr(this_body); - if (ttispair(tail)) { - TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, - tail, denv); - kset_cc(K, new_cont); + if (ttisnil(this_body)) { + kapply_cc(K, KINERT); + } else { + TValue tail = kcdr(this_body); + if (ttispair(tail)) { + TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, + tail, denv); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, this_body)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, this_body)); #endif - } - ktail_eval(K, kcar(this_body), denv); - } + } + ktail_eval(K, kcar(this_body), denv); + } } else { - /* check next clause if there is any*/ - if (ttisnil(tests)) { - kapply_cc(K, KINERT); - } else { - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_cond, 4, - kcar(bodies), kcdr(tests), kcdr(bodies), - denv); - /* - ** Mark as a bool checking cont, not necessary but avoids a - ** continuation in the last evaluation in the common use of - ** ($cond ... (($or?/$and? ...) ...) ...) - */ - kset_bool_check_cont(new_cont); - kset_cc(K, new_cont); + /* check next clause if there is any*/ + if (ttisnil(tests)) { + kapply_cc(K, KINERT); + } else { + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_cond, 4, + kcar(bodies), kcdr(tests), kcdr(bodies), + denv); + /* + ** Mark as a bool checking cont, not necessary but avoids a + ** continuation in the last evaluation in the common use of + ** ($cond ... (($or?/$and? ...) ...) ...) + */ + kset_bool_check_cont(new_cont); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, tests)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, tests)); #endif - ktail_eval(K, kcar(tests), denv); - } + ktail_eval(K, kcar(tests), denv); + } } } @@ -270,18 +270,18 @@ void Scond(klisp_State *K) TValue obj; if (ttisnil(tests)) { - obj = KINERT; + obj = KINERT; } else { - /* pass a dummy body and a #f to the $cond continuation to - avoid code repetition here */ - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_cond, 4, - KNIL, tests, bodies, denv); - /* there is no need to mark this continuation with bool check - because it is just a dummy, no evaluation happens in its - dynamic extent, no need for source info either */ - kset_cc(K, new_cont); - obj = KFALSE; + /* pass a dummy body and a #f to the $cond continuation to + avoid code repetition here */ + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_cond, 4, + KNIL, tests, bodies, denv); + /* there is no need to mark this continuation with bool check + because it is just a dummy, no evaluation happens in its + dynamic extent, no need for source info either */ + kset_cc(K, new_cont); + obj = KFALSE; } krooted_tvs_pop(K); @@ -311,24 +311,24 @@ void do_for_each(klisp_State *K) if (n == 0) { /* return inert as the final result to for-each */ - kapply_cc(K, KINERT); + kapply_cc(K, KINERT); } else { - /* copy the ptree to avoid problems with mutation */ - /* XXX: no check necessary, could just use copy_list if there - was such a procedure */ - TValue first_ptree = check_copy_list(K, kcar(ls), false, NULL, NULL); - krooted_tvs_push(K, first_ptree); - ls = kcdr(ls); - n = n-1; - - /* have to unwrap the applicative to avoid extra evaluation of first */ - TValue new_expr = kcons(K, kunwrap(app), first_ptree); - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_for_each, 4, - app, ls, i2tv(n), denv); - krooted_tvs_pop(K); - kset_cc(K, new_cont); - ktail_eval(K, new_expr, denv); + /* copy the ptree to avoid problems with mutation */ + /* XXX: no check necessary, could just use copy_list if there + was such a procedure */ + TValue first_ptree = check_copy_list(K, kcar(ls), false, NULL, NULL); + krooted_tvs_push(K, first_ptree); + ls = kcdr(ls); + n = n-1; + + /* have to unwrap the applicative to avoid extra evaluation of first */ + TValue new_expr = kcons(K, kunwrap(app), first_ptree); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_for_each, 4, + app, ls, i2tv(n), denv); + krooted_tvs_pop(K); + kset_cc(K, new_cont); + ktail_eval(K, new_expr, denv); } } @@ -344,8 +344,8 @@ void for_each(klisp_State *K) bind_al1tp(K, ptree, "applicative", ttisapplicative, app, lss); if (ttisnil(lss)) { - klispE_throw_simple(K, "no lists"); - return; + klispE_throw_simple(K, "no lists"); + return; } /* get the metrics of the ptree of each call to app and @@ -354,21 +354,21 @@ void for_each(klisp_State *K) int32_t res_pairs, res_apairs, res_cpairs; map_for_each_get_metrics(K, lss, &app_apairs, &app_cpairs, - &res_apairs, &res_cpairs); + &res_apairs, &res_cpairs); app_pairs = app_apairs + app_cpairs; res_pairs = res_apairs + res_cpairs; /* create the list of parameters to app */ lss = map_for_each_transpose(K, lss, app_apairs, app_cpairs, - res_apairs, res_cpairs); + res_apairs, res_cpairs); krooted_tvs_push(K, lss); /* schedule all elements at once, the cycle is just ignored, this will also return #inert once done. */ TValue new_cont = - kmake_continuation(K, kget_cc(K), do_for_each, 4, app, lss, - i2tv(res_pairs), denv); + kmake_continuation(K, kget_cc(K), do_for_each, 4, app, lss, + i2tv(res_pairs), denv); kset_cc(K, new_cont); krooted_tvs_pop(K); /* this will be a nop */ @@ -388,14 +388,14 @@ void array_for_each(klisp_State *K) */ TValue (*array_to_list)(klisp_State *K, TValue array, int32_t *size) = - pvalue(xparams[0]); + pvalue(xparams[0]); bind_al1tp(K, ptree, "applicative", ttisapplicative, app, lss); /* check that lss is a non empty list, and copy it */ if (ttisnil(lss)) { - klispE_throw_simple(K, "no arguments after applicative"); - return; + klispE_throw_simple(K, "no arguments after applicative"); + return; } int32_t app_pairs, app_apairs, app_cpairs; @@ -414,21 +414,21 @@ void array_for_each(klisp_State *K) kset_car(lss, ls); /* save the first */ /* all array will produce acyclic lists */ for(int32_t i = 1 /* jump over first */; i < app_pairs; ++i) { - head = kcar(tail); - int32_t pairs; - ls = array_to_list(K, head, &pairs); - /* in klisp all arrays should have the same length */ - if (pairs != res_pairs) { - klispE_throw_simple(K, "arguments of different length"); - return; - } - kset_car(tail, ls); - tail = kcdr(tail); + head = kcar(tail); + int32_t pairs; + ls = array_to_list(K, head, &pairs); + /* in klisp all arrays should have the same length */ + if (pairs != res_pairs) { + klispE_throw_simple(K, "arguments of different length"); + return; + } + kset_car(tail, ls); + tail = kcdr(tail); } /* create the list of parameters to app */ lss = map_for_each_transpose(K, lss, app_apairs, app_cpairs, - res_pairs, 0); /* cycle pairs is always 0 */ + res_pairs, 0); /* cycle pairs is always 0 */ /* ASK John: the semantics when this is mixed with continuations, isn't all that great..., but what are the expectations considering @@ -440,8 +440,8 @@ void array_for_each(klisp_State *K) /* schedule all elements at once, this will also return #inert once done. */ TValue new_cont = - kmake_continuation(K, kget_cc(K), do_for_each, 4, app, lss, - i2tv(res_pairs), denv); + kmake_continuation(K, kget_cc(K), do_for_each, 4, app, lss, + i2tv(res_pairs), denv); kset_cc(K, new_cont); krooted_tvs_pop(K); /* this will be a nop */ @@ -469,45 +469,45 @@ void do_Swhen_Sunless(klisp_State *K) #endif if (!ttisboolean(obj)) { - klispE_throw_simple(K, "test is not a boolean"); - return; + klispE_throw_simple(K, "test is not a boolean"); + return; } if (bvalue(obj) == cond && !ttisnil(ls)) { - /* only contruct the #inert returning continuation if the - current continuation is not of the same type */ - if (!kis_inert_ret_cont(kget_cc(K))) { - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_return_value, 1, KINERT); - /* mark it, so that it can be detected as inert throwing cont */ - kset_inert_ret_cont(new_cont); - kset_cc(K, new_cont); + /* only contruct the #inert returning continuation if the + current continuation is not of the same type */ + if (!kis_inert_ret_cont(kget_cc(K))) { + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_return_value, 1, KINERT); + /* mark it, so that it can be detected as inert throwing cont */ + kset_inert_ret_cont(new_cont); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the whole form */ - kset_source_info(K, new_cont, si); + /* put the source info of the whole form */ + kset_source_info(K, new_cont, si); #endif - } - /* this is needed because seq continuation doesn't check for - nil sequence */ - /* TODO this could be at least in an inlineable function to - allow used from $lambda, $vau, $let family, load, etc */ - TValue tail = kcdr(ls); - if (ttispair(tail)) { - krooted_tvs_push(K, ls); - TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, - tail, denv); - kset_cc(K, new_cont); + } + /* this is needed because seq continuation doesn't check for + nil sequence */ + /* TODO this could be at least in an inlineable function to + allow used from $lambda, $vau, $let family, load, etc */ + TValue tail = kcdr(ls); + if (ttispair(tail)) { + krooted_tvs_push(K, ls); + TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, + tail, denv); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, ls)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, ls)); #endif - krooted_tvs_pop(K); - } - ktail_eval(K, kcar(ls), denv); + krooted_tvs_pop(K); + } + ktail_eval(K, kcar(ls), denv); } else { - /* either the test failed or the body was nil */ - kapply_cc(K, KINERT); + /* either the test failed or the body was nil */ + kapply_cc(K, KINERT); } } @@ -533,9 +533,9 @@ void Swhen_Sunless(klisp_State *K) /* prepare the continuation that will check the test result and do the evaluation */ TValue si = K->next_si; /* this is the source info of the whole - $when/$unless form */ + $when/$unless form */ TValue new_cont = kmake_continuation(K, kget_cc(K), do_Swhen_Sunless, - 4, tv_cond, ls, denv, si); + 4, tv_cond, ls, denv, si); krooted_tvs_pop(K); /* ** Mark as a bool checking cont, not necessary but avoids a continuation @@ -555,7 +555,7 @@ void kinit_control_ground_env(klisp_State *K) /* 4.5.1 inert? */ add_applicative(K, ground_env, "inert?", typep, 2, symbol, - i2tv(K_TINERT)); + i2tv(K_TINERT)); /* 4.5.2 $if */ add_operative(K, ground_env, "$if", Sif, 0); /* 5.1.1 $sequence */ @@ -566,16 +566,16 @@ void kinit_control_ground_env(klisp_State *K) add_applicative(K, ground_env, "for-each", for_each, 0); /* 6.9.? string-for-each, vector-for-each, bytevector-for-each */ add_applicative(K, ground_env, "string-for-each", array_for_each, 1, - p2tv(string_to_list_h)); + p2tv(string_to_list_h)); add_applicative(K, ground_env, "vector-for-each", array_for_each, 1, - p2tv(vector_to_list_h)); + p2tv(vector_to_list_h)); add_applicative(K, ground_env, "bytevector-for-each", array_for_each, 1, - p2tv(bytevector_to_list_h)); + p2tv(bytevector_to_list_h)); /* ?.? */ add_operative(K, ground_env, "$when", Swhen_Sunless, 1, - b2tv(true)); + b2tv(true)); add_operative(K, ground_env, "$unless", Swhen_Sunless, 1, - b2tv(false)); + b2tv(false)); } /* init continuation names */ diff --git a/src/kgencapsulations.c b/src/kgencapsulations.c @@ -57,9 +57,9 @@ void enc_unwrap(klisp_State *K) TValue key = xparams[0]; if (!kis_encapsulation_type(enc, key)) { - klispE_throw_simple(K, "object doesn't belong to this " - "encapsulation type"); - return; + klispE_throw_simple(K, "object doesn't belong to this " + "encapsulation type"); + return; } TValue obj = kget_enc_val(enc); kapply_cc(K, obj); @@ -103,5 +103,5 @@ void kinit_encapsulations_ground_env(klisp_State *K) /* 8.1.1 make-encapsulation-type */ add_applicative(K, ground_env, "make-encapsulation-type", - make_encapsulation_type, 0); + make_encapsulation_type, 0); } diff --git a/src/kgenv_mut.c b/src/kgenv_mut.c @@ -45,8 +45,8 @@ void SdefineB(klisp_State *K) krooted_tvs_push(K, dptree); TValue new_cont = kmake_continuation(K, kget_cc(K), - do_match, 3, dptree, denv, - def_sym); + do_match, 3, dptree, denv, + def_sym); kset_cc(K, new_cont); krooted_tvs_pop(K); ktail_eval(K, expr, denv); @@ -87,8 +87,8 @@ void SsetB(klisp_State *K) krooted_tvs_push(K, formals); TValue new_cont = - kmake_continuation(K, kget_cc(K), do_set_eval_obj, 4, - sname, formals, eval_exp, denv); + kmake_continuation(K, kget_cc(K), do_set_eval_obj, 4, + sname, formals, eval_exp, denv); kset_cc(K, new_cont); krooted_tvs_pop(K); @@ -113,17 +113,17 @@ void do_set_eval_obj(klisp_State *K) TValue denv = xparams[3]; if (!ttisenvironment(obj)) { - klispE_throw_simple(K, "bad type from first " - "operand evaluation (expected environment)"); - return; + klispE_throw_simple(K, "bad type from first " + "operand evaluation (expected environment)"); + return; } else { - TValue env = obj; + TValue env = obj; - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_match, 3, - formals, env, sname); - kset_cc(K, new_cont); - ktail_eval(K, eval_exp, denv); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_match, 3, + formals, env, sname); + kset_cc(K, new_cont); + ktail_eval(K, eval_exp, denv); } } @@ -133,11 +133,11 @@ inline void unmark_maybe_symbol_list(klisp_State *K, TValue ls) { UNUSED(K); while(ttispair(ls) && kis_marked(ls)) { - TValue first = kcar(ls); - if (ttissymbol(first)) - kunmark_symbol(first); - kunmark(ls); - ls = kcdr(ls); + TValue first = kcar(ls); + if (ttissymbol(first)) + kunmark_symbol(first); + kunmark(ls); + ls = kcdr(ls); } } @@ -156,33 +156,33 @@ TValue check_copy_symbol_list(klisp_State *K, TValue obj) TValue last_pair = slist; while(ttispair(tail) && !kis_marked(tail)) { - /* even if there is a type error continue checking the structure */ - TValue first = kcar(tail); - if (ttissymbol(first)) { - repeated_errorp |= kis_symbol_marked(first); - kmark_symbol(first); - } else { - type_errorp = true; - } - kmark(tail); - - TValue new_pair = kcons(K, first, KNIL); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - - tail = kcdr(tail); + /* even if there is a type error continue checking the structure */ + TValue first = kcar(tail); + if (ttissymbol(first)) { + repeated_errorp |= kis_symbol_marked(first); + kmark_symbol(first); + } else { + type_errorp = true; + } + kmark(tail); + + TValue new_pair = kcons(K, first, KNIL); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + + tail = kcdr(tail); } unmark_maybe_symbol_list(K, obj); if (!ttisnil(tail)) { - klispE_throw_simple(K, "expected finite list"); - return KNIL; + klispE_throw_simple(K, "expected finite list"); + return KNIL; } else if (type_errorp) { - klispE_throw_simple(K, "bad operand type (expected list of " - "symbols)"); - return KNIL; + klispE_throw_simple(K, "bad operand type (expected list of " + "symbols)"); + return KNIL; } else if (repeated_errorp) { - klispE_throw_simple(K, "repeated symbols"); + klispE_throw_simple(K, "repeated symbols"); } krooted_vars_pop(K); return kcdr(slist); @@ -203,16 +203,16 @@ void do_import(klisp_State *K) TValue denv = xparams[2]; if (!ttisenvironment(obj)) { - klispE_throw_simple(K, "bad type from first " - "operand evaluation (expected environment)"); - return; + klispE_throw_simple(K, "bad type from first " + "operand evaluation (expected environment)"); + return; } else { - TValue env = obj; - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_match, 3, - symbols, denv, sname); - kset_cc(K, new_cont); - ktail_eval(K, kcons(K, K->list_app, symbols), env); + TValue env = obj; + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_match, 3, + symbols, denv, sname); + kset_cc(K, new_cont); + ktail_eval(K, kcons(K, K->list_app, symbols), env); } } @@ -239,39 +239,39 @@ void SprovideB(klisp_State *K) /* this will copy the bindings from new_env to denv */ krooted_tvs_push(K, new_env); TValue import_cont = - kmake_continuation(K, kget_cc(K), do_import, 3, - sname, symbols, denv); + kmake_continuation(K, kget_cc(K), do_import, 3, + sname, symbols, denv); kset_cc(K, import_cont); /* this implicitly roots import_cont */ /* this will ignore the last value and pass the env to the above continuation */ TValue ret_exp_cont = - kmake_continuation(K, import_cont, do_return_value, - 1, new_env); + kmake_continuation(K, import_cont, do_return_value, + 1, new_env); kset_cc(K, ret_exp_cont); /* this implicitly roots ret_exp_cont */ if (ttisnil(body)) { - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - kapply_cc(K, KINERT); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + kapply_cc(K, KINERT); } else { - /* this is needed because seq continuation doesn't check for - nil sequence */ - TValue tail = kcdr(body); - if (ttispair(tail)) { - TValue new_cont = kmake_continuation(K, kget_cc(K), - do_seq, 2, tail, new_env); - kset_cc(K, new_cont); + /* this is needed because seq continuation doesn't check for + nil sequence */ + TValue tail = kcdr(body); + if (ttispair(tail)) { + TValue new_cont = kmake_continuation(K, kget_cc(K), + do_seq, 2, tail, new_env); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, body)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, body)); #endif - } - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - ktail_eval(K, kcar(body), new_env); + } + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + ktail_eval(K, kcar(body), new_env); } } @@ -310,7 +310,7 @@ void SimportB(klisp_State *K) krooted_tvs_push(K, symbols); TValue new_cont = kmake_continuation(K, kget_cc(K), do_import, 3, - sname, symbols, denv); + sname, symbols, denv); kset_cc(K, new_cont); krooted_tvs_pop(K); ktail_eval(K, env_expr, denv); diff --git a/src/kgenvironments.c b/src/kgenvironments.c @@ -48,7 +48,7 @@ void eval(klisp_State *K) UNUSED(xparams); bind_2tp(K, ptree, "any", anytype, expr, - "environment", ttisenvironment, env); + "environment", ttisenvironment, env); /* TODO: track source code info */ ktail_eval(K, expr, env); } @@ -65,27 +65,27 @@ void make_environment(klisp_State *K) TValue new_env; if (ttisnil(ptree)) { - new_env = kmake_empty_environment(K); - kapply_cc(K, new_env); + new_env = kmake_empty_environment(K); + kapply_cc(K, new_env); } else if (ttispair(ptree) && ttisnil(kcdr(ptree))) { - /* special common case of one parent, don't keep a list */ - TValue parent = kcar(ptree); - if (ttisenvironment(parent)) { - new_env = kmake_environment(K, parent); - kapply_cc(K, new_env); - } else { - klispE_throw_simple(K, "not an environment in " - "parent list"); - return; - } + /* special common case of one parent, don't keep a list */ + TValue parent = kcar(ptree); + if (ttisenvironment(parent)) { + new_env = kmake_environment(K, parent); + kapply_cc(K, new_env); + } else { + klispE_throw_simple(K, "not an environment in " + "parent list"); + return; + } } else { - /* this is the general case, copy the list but without the - cycle if there is any */ - TValue parents = check_copy_env_list(K, ptree); - krooted_tvs_push(K, parents); - new_env = kmake_environment(K, parents); - krooted_tvs_pop(K); - kapply_cc(K, new_env); + /* this is the general case, copy the list but without the + cycle if there is any */ + TValue parents = check_copy_env_list(K, ptree); + krooted_tvs_push(K, parents); + new_env = kmake_environment(K, parents); + krooted_tvs_pop(K); + kapply_cc(K, new_env); } } @@ -105,7 +105,7 @@ void make_environment(klisp_State *K) /* GC: assume bindings is rooted */ TValue split_check_let_bindings(klisp_State *K, TValue bindings, - TValue *exprs, bool starp) + TValue *exprs, bool starp) { TValue cars = kcons(K, KNIL, KNIL); krooted_vars_push(K, &cars); @@ -117,54 +117,54 @@ TValue split_check_let_bindings(klisp_State *K, TValue bindings, TValue tail = bindings; while(ttispair(tail) && !kis_marked(tail)) { - kmark(tail); - TValue first = kcar(tail); - if (!ttispair(first) || !ttispair(kcdr(first)) || - !ttisnil(kcddr(first))) { - unmark_list(K, bindings); - klispE_throw_simple(K, "bad structure in bindings"); - return KNIL; - } + kmark(tail); + TValue first = kcar(tail); + if (!ttispair(first) || !ttispair(kcdr(first)) || + !ttisnil(kcddr(first))) { + unmark_list(K, bindings); + klispE_throw_simple(K, "bad structure in bindings"); + return KNIL; + } - TValue new_car = kcons(K, kcar(first), KNIL); - kset_cdr(last_car_pair, new_car); - last_car_pair = new_car; - TValue new_cadr = kcons(K, kcadr(first), KNIL); - kset_cdr(last_cadr_pair, new_cadr); - last_cadr_pair = new_cadr; - - tail = kcdr(tail); + TValue new_car = kcons(K, kcar(first), KNIL); + kset_cdr(last_car_pair, new_car); + last_car_pair = new_car; + TValue new_cadr = kcons(K, kcadr(first), KNIL); + kset_cdr(last_cadr_pair, new_cadr); + last_cadr_pair = new_cadr; + + tail = kcdr(tail); } unmark_list(K, bindings); if (!ttispair(tail) && !ttisnil(tail)) { - klispE_throw_simple(K, "expected list"); - return KNIL; + klispE_throw_simple(K, "expected list"); + return KNIL; } else if(ttispair(tail)) { - klispE_throw_simple(K, "expected finite list"); - return KNIL; + klispE_throw_simple(K, "expected finite list"); + return KNIL; } else { - TValue res; - if (starp) { - /* all bindings are consider individual ptrees in these 'let's, - replace each ptree with its copy (after checking of course) */ - tail = kcdr(cars); - while(!ttisnil(tail)) { - TValue first = kcar(tail); - TValue copy = check_copy_ptree(K, first, KIGNORE); - kset_car(tail, copy); - tail = kcdr(tail); - } - res = kcdr(cars); - } else { - /* all bindings are consider one ptree in these 'let's */ - res = check_copy_ptree(K, kcdr(cars), KIGNORE); - } - *exprs = kcdr(cadrs); - krooted_vars_pop(K); - krooted_vars_pop(K); - return res; + TValue res; + if (starp) { + /* all bindings are consider individual ptrees in these 'let's, + replace each ptree with its copy (after checking of course) */ + tail = kcdr(cars); + while(!ttisnil(tail)) { + TValue first = kcar(tail); + TValue copy = check_copy_ptree(K, first, KIGNORE); + kset_car(tail, copy); + tail = kcdr(tail); + } + res = kcdr(cars); + } else { + /* all bindings are consider one ptree in these 'let's */ + res = check_copy_ptree(K, kcdr(cars), KIGNORE); + } + *exprs = kcdr(cadrs); + krooted_vars_pop(K); + krooted_vars_pop(K); + return res; } } @@ -198,34 +198,34 @@ void do_let(klisp_State *K) match(K, env, ptree, obj); if (ttisnil(bindings)) { - if (ttisnil(body)) { - kapply_cc(K, KINERT); - } else { - /* this is needed because seq continuation doesn't check for - nil sequence */ - TValue tail = kcdr(body); - if (ttispair(tail)) { - TValue new_cont = kmake_continuation(K, kget_cc(K), - do_seq, 2, tail, env); - kset_cc(K, new_cont); + if (ttisnil(body)) { + kapply_cc(K, KINERT); + } else { + /* this is needed because seq continuation doesn't check for + nil sequence */ + TValue tail = kcdr(body); + if (ttispair(tail)) { + TValue new_cont = kmake_continuation(K, kget_cc(K), + do_seq, 2, tail, env); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, body)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, body)); #endif - } - ktail_eval(K, kcar(body), env); - } + } + ktail_eval(K, kcar(body), env); + } } else { - TValue new_env = kmake_environment(K, env); - krooted_tvs_push(K, new_env); - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - kcar(bindings), kcdr(bindings), kcdr(exprs), - new_env, b2tv(recp), body); - krooted_tvs_pop(K); - kset_cc(K, new_cont); - ktail_eval(K, kcar(exprs), recp? new_env : env); + TValue new_env = kmake_environment(K, env); + krooted_tvs_push(K, new_env); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + kcar(bindings), kcdr(bindings), kcdr(exprs), + new_env, b2tv(recp), body); + krooted_tvs_pop(K); + kset_cc(K, new_cont); + ktail_eval(K, kcar(exprs), recp? new_env : env); } } @@ -255,8 +255,8 @@ void Slet(klisp_State *K) TValue new_env = kmake_environment(K, denv); krooted_tvs_push(K, new_env); TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - bptree, KNIL, KNIL, new_env, b2tv(false), body); + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + bptree, KNIL, KNIL, new_env, b2tv(false), body); kset_cc(K, new_cont); TValue expr = kcons(K, K->list_app, exprs); @@ -283,20 +283,20 @@ void do_bindsp(klisp_State *K) int32_t count = ivalue(xparams[1]); if (!ttisenvironment(obj)) { - klispE_throw_simple(K, "expected environment as first argument"); - return; + klispE_throw_simple(K, "expected environment as first argument"); + return; } TValue env = obj; TValue res = KTRUE; while(count--) { - TValue first = kcar(symbols); - symbols = kcdr(symbols); + TValue first = kcar(symbols); + symbols = kcdr(symbols); - if (!kbinds(K, env, first)) { - res = KFALSE; - break; - } + if (!kbinds(K, env, first)) { + res = KFALSE; + break; + } } kapply_cc(K, res); @@ -319,7 +319,7 @@ void Sbindsp(klisp_State *K) krooted_tvs_push(K, symbols); TValue new_cont = kmake_continuation(K, kget_cc(K), do_bindsp, - 2, symbols, i2tv(count)); + 2, symbols, i2tv(count)); krooted_tvs_pop(K); kset_cc(K, new_cont); ktail_eval(K, env_expr, denv); @@ -379,30 +379,30 @@ void SletS(klisp_State *K) krooted_tvs_push(K, new_env); if (ttisnil(bptree)) { - /* same as $let */ - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - bptree, KNIL, KNIL, new_env, b2tv(false), body); - kset_cc(K, new_cont); - - TValue expr = kcons(K, K->list_app, exprs); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - ktail_eval(K, expr, denv); + /* same as $let */ + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + bptree, KNIL, KNIL, new_env, b2tv(false), body); + kset_cc(K, new_cont); + + TValue expr = kcons(K, K->list_app, exprs); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + ktail_eval(K, expr, denv); } else { - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - kcar(bptree), kcdr(bptree), kcdr(exprs), - new_env, b2tv(false), body); - kset_cc(K, new_cont); - - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - ktail_eval(K, kcar(exprs), denv); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + kcar(bptree), kcdr(bptree), kcdr(exprs), + new_env, b2tv(false), body); + kset_cc(K, new_cont); + + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + ktail_eval(K, kcar(exprs), denv); } } @@ -432,8 +432,8 @@ void Sletrec(klisp_State *K) krooted_tvs_push(K, new_env); TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - bptree, KNIL, KNIL, new_env, b2tv(true), body); + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + bptree, KNIL, KNIL, new_env, b2tv(true), body); kset_cc(K, new_cont); TValue expr = kcons(K, K->list_app, exprs); @@ -471,31 +471,31 @@ void SletrecS(klisp_State *K) krooted_tvs_push(K, new_env); if (ttisnil(bptree)) { - /* same as $letrec */ - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - bptree, KNIL, KNIL, new_env, b2tv(true), body); - kset_cc(K, new_cont); - - TValue expr = kcons(K, K->list_app, exprs); - - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - ktail_eval(K, expr, new_env); + /* same as $letrec */ + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + bptree, KNIL, KNIL, new_env, b2tv(true), body); + kset_cc(K, new_cont); + + TValue expr = kcons(K, K->list_app, exprs); + + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + ktail_eval(K, expr, new_env); } else { - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - kcar(bptree), kcdr(bptree), kcdr(exprs), - new_env, b2tv(true), body); - kset_cc(K, new_cont); - - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - ktail_eval(K, kcar(exprs), new_env); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + kcar(bptree), kcdr(bptree), kcdr(exprs), + new_env, b2tv(true), body); + kset_cc(K, new_cont); + + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + ktail_eval(K, kcar(exprs), new_env); } } @@ -519,14 +519,14 @@ void do_let_redirect(klisp_State *K) TValue body = xparams[4]; if (!ttisenvironment(obj)) { - klispE_throw_simple(K, "expected environment"); - return; + klispE_throw_simple(K, "expected environment"); + return; } TValue new_env = kmake_environment(K, obj); krooted_tvs_push(K, new_env); TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - bptree, KNIL, KNIL, new_env, b2tv(false), body); + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + bptree, KNIL, KNIL, new_env, b2tv(false), body); kset_cc(K, new_cont); krooted_tvs_pop(K); @@ -559,8 +559,8 @@ void Slet_redirect(klisp_State *K) krooted_tvs_push(K, eexpr); TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let_redirect, 5, sname, - bptree, eexpr, denv, body); + kmake_continuation(K, kget_cc(K), do_let_redirect, 5, sname, + bptree, eexpr, denv, body); kset_cc(K, new_cont); krooted_tvs_pop(K); @@ -600,8 +600,8 @@ void Slet_safe(klisp_State *K) TValue new_env = kmake_environment(K, K->ground_env); krooted_tvs_push(K, new_env); TValue new_cont = - kmake_continuation(K, kget_cc(K), do_let, 7, sname, - bptree, KNIL, KNIL, new_env, b2tv(false), body); + kmake_continuation(K, kget_cc(K), do_let, 7, sname, + bptree, KNIL, KNIL, new_env, b2tv(false), body); kset_cc(K, new_cont); TValue expr = kcons(K, K->list_app, exprs); @@ -626,7 +626,7 @@ void Sremote_eval(klisp_State *K) bind_2p(K, ptree, obj, env_exp); TValue new_cont = kmake_continuation(K, kget_cc(K), - do_remote_eval, 1, obj); + do_remote_eval, 1, obj); kset_cc(K, new_cont); ktail_eval(K, env_exp, denv); @@ -639,12 +639,12 @@ void do_remote_eval(klisp_State *K) TValue obj = K->next_value; klisp_assert(ttisnil(K->next_env)); if (!ttisenvironment(obj)) { - klispE_throw_simple(K, "bad type from second operand " - "evaluation (expected environment)"); - return; + klispE_throw_simple(K, "bad type from second operand " + "evaluation (expected environment)"); + return; } else { - TValue eval_exp = xparams[0]; - ktail_eval(K, eval_exp, obj); + TValue eval_exp = xparams[0]; + ktail_eval(K, eval_exp, obj); } } @@ -682,7 +682,7 @@ void Sbindings_to_environment(klisp_State *K) krooted_tvs_push(K, new_env); TValue new_cont = kmake_continuation(K, kget_cc(K), - do_b_to_env, 2, bptree, new_env); + do_b_to_env, 2, bptree, new_env); kset_cc(K, new_cont); TValue expr = kcons(K, K->list_app, exprs); @@ -716,7 +716,7 @@ void eval_string(klisp_State *K) UNUSED(denv); bind_2tp(K, ptree, "string", ttisstring, str, - "environment", ttisenvironment, env); + "environment", ttisenvironment, env); /* create a continuation for better stack traces in case of error */ @@ -728,17 +728,17 @@ void eval_string(klisp_State *K) TValue obj = kread_from_port(K, port, true); /* read mutable pairs */ if (ttiseof(obj)) { - klispE_throw_simple_with_irritants(K, "No object found in string", 1, - str); - return; + klispE_throw_simple_with_irritants(K, "No object found in string", 1, + str); + return; } krooted_tvs_push(K, obj); TValue second_obj = kread_from_port(K, port, true); krooted_tvs_pop(K); if (!ttiseof(second_obj)) { - klispE_throw_simple_with_irritants(K, "More than one object found " - "in string", 1, str); - return; + klispE_throw_simple_with_irritants(K, "More than one object found " + "in string", 1, str); + return; } kapply_cc(K, obj); } @@ -751,10 +751,10 @@ void kinit_environments_ground_env(klisp_State *K) /* 4.8.1 environment? */ add_applicative(K, ground_env, "environment?", typep, 2, symbol, - i2tv(K_TENVIRONMENT)); + i2tv(K_TENVIRONMENT)); /* 4.8.2 ignore? */ add_applicative(K, ground_env, "ignore?", typep, 2, symbol, - i2tv(K_TIGNORE)); + i2tv(K_TIGNORE)); /* 4.8.3 eval */ add_applicative(K, ground_env, "eval", eval, 0); /* 4.8.4 make-environment */ @@ -765,10 +765,10 @@ void kinit_environments_ground_env(klisp_State *K) add_operative(K, ground_env, "$binds?", Sbindsp, 0); /* 6.7.2 get-current-environment */ add_applicative(K, ground_env, "get-current-environment", - get_current_environment, 0); + get_current_environment, 0); /* 6.7.3 make-kernel-standard-environment */ add_applicative(K, ground_env, "make-kernel-standard-environment", - make_kernel_standard_environment, 0); + make_kernel_standard_environment, 0); /* 6.7.4 $let* */ add_operative(K, ground_env, "$let*", SletS, 1, symbol); /* 6.7.5 $letrec */ @@ -783,7 +783,7 @@ void kinit_environments_ground_env(klisp_State *K) add_operative(K, ground_env, "$remote-eval", Sremote_eval, 0); /* 6.7.10 $bindings->environment */ add_operative(K, ground_env, "$bindings->environment", - Sbindings_to_environment, 1, symbol); + Sbindings_to_environment, 1, symbol); /* ?.? eval-string */ add_applicative(K, ground_env, "eval-string", eval_string, 0); } diff --git a/src/kgeqp.c b/src/kgeqp.c @@ -41,14 +41,14 @@ void eqp(klisp_State *K) TValue tail = ptree; TValue res = KTRUE; while(comps-- > 0) { /* comps could be -1 if ptree is nil */ - TValue first = kcar(tail); - tail = kcdr(tail); /* tail only advances one place per iteration */ - TValue second = kcar(tail); + TValue first = kcar(tail); + tail = kcdr(tail); /* tail only advances one place per iteration */ + TValue second = kcar(tail); - if (!eq2p(K, first, second)) { - res = KFALSE; - break; - } + if (!eq2p(K, first, second)) { + res = KFALSE; + break; + } } kapply_cc(K, res); diff --git a/src/kgequalp.c b/src/kgequalp.c @@ -53,14 +53,14 @@ void equalp(klisp_State *K) TValue tail = ptree; TValue res = KTRUE; while(comps-- > 0) { /* comps could be -1 if ptree is nil */ - TValue first = kcar(tail); - tail = kcdr(tail); /* tail only advances one place per iteration */ - TValue second = kcar(tail); + TValue first = kcar(tail); + tail = kcdr(tail); /* tail only advances one place per iteration */ + TValue second = kcar(tail); - if (!equal2p(K, first, second)) { - res = KFALSE; - break; - } + if (!equal2p(K, first, second)) { + res = KFALSE; + break; + } } kapply_cc(K, res); diff --git a/src/kgerrors.c b/src/kgerrors.c @@ -94,7 +94,7 @@ void kinit_error_hierarchy(klisp_State *K) klisp_assert(ttisinert(K->system_error_cont)); K->system_error_cont = kmake_continuation(K, K->error_cont, - do_exception_cont, 0); + do_exception_cont, 0); } /* init ground */ @@ -104,14 +104,14 @@ void kinit_error_ground_env(klisp_State *K) TValue symbol, value; add_applicative(K, ground_env, "error-object?", typep, 2, symbol, - i2tv(K_TERROR)); + i2tv(K_TERROR)); add_applicative(K, ground_env, "error", kgerror, 0); add_applicative(K, ground_env, "raise", kgraise, 0); /* MAYBE add get- and remove object from these names */ add_applicative(K, ground_env, "error-object-message", - error_object_message, 0); + error_object_message, 0); add_applicative(K, ground_env, "error-object-irritants", - error_object_irritants, 0); + error_object_irritants, 0); /* TODO raise-continuable from r7rs doesn't make sense in the Kernel system of handling continuations. What we could have is a more sofisticated system diff --git a/src/kgffi.c b/src/kgffi.c @@ -72,7 +72,7 @@ typedef struct { size_t index; } ffi_callback_t; -#define CB_INDEX_N 0 +#define CB_INDEX_N 0 #define CB_INDEX_STACK 1 #define CB_INDEX_FIRST_CALLBACK 2 @@ -131,7 +131,7 @@ static void ffi_encode_pointer(ffi_codec_t *self, klisp_State *K, TValue v, void } else if (ttisnil(v)) { *(void **)buf = NULL; } else if (tbasetype_(v) == K_TAG_USER) { - /* TODO: do not use internal macro tbasetype_ */ + /* TODO: do not use internal macro tbasetype_ */ *(void **)buf = pvalue(v); } else { klispE_throw_simple_with_irritants(K, "neither bytevector, string, pointer or nil", 1, v); @@ -236,8 +236,8 @@ static TValue ffi_decode_uint32(ffi_codec_t *self, klisp_State *K, const void *b uint8_t d[4]; for (int i = 3; i >= 0; i--) { - d[i] = (x & 0xFF); - x >>= 8; + d[i] = (x & 0xFF); + x >>= 8; } mp_int_read_unsigned(K, tv2bigint(res), d, 4); @@ -292,14 +292,14 @@ static void ffi_encode_uint64(ffi_codec_t *self, klisp_State *K, TValue v, void if (ttisfixint(v) && 0 <= ivalue(v)) { *(uint64_t *) buf = ivalue(v); } else if (ttisbigint(v) - && mp_int_compare_zero(tv2bigint(v)) >= 0 - && mp_int_unsigned_len(tv2bigint(v)) <= 8) { + && mp_int_compare_zero(tv2bigint(v)) >= 0 + && mp_int_unsigned_len(tv2bigint(v)) <= 8) { uint8_t d[8]; mp_int_to_unsigned(K, tv2bigint(v), d, 8); uint64_t tmp = d[0]; for (int i = 1; i < 8; i++) - tmp = (tmp << 8) | d[i]; + tmp = (tmp << 8) | d[i]; *(uint64_t *) buf = tmp; } else { klispE_throw_simple_with_irritants(K, "unable to convert to C uint64_t", 1, v); @@ -391,8 +391,8 @@ void ffi_load_library(klisp_State *K) TValue filename = ptree; const char *filename_c = - get_opt_tpar(K, filename, "string", ttisstring) - ? kstring_buf(filename) : NULL; + get_opt_tpar(K, filename, "string", ttisstring) + ? kstring_buf(filename) : NULL; #if KGFFI_DLFCN void *handle = dlopen(filename_c, RTLD_LAZY | RTLD_GLOBAL); @@ -408,11 +408,11 @@ void ffi_load_library(klisp_State *K) /* TODO: unicode and wide character issues ??? */ HMODULE handle = LoadLibrary(filename_c); if (handle == NULL) { - krooted_tvs_push(K, filename); - TValue err = ffi_win32_error_message(K, GetLastError()); - klispE_throw_simple_with_irritants(K, "couldn't load dynamic library", - 2, filename, err); - return; + krooted_tvs_push(K, filename); + TValue err = ffi_win32_error_message(K, GetLastError()); + klispE_throw_simple_with_irritants(K, "couldn't load dynamic library", + 2, filename, err); + return; } #else # error @@ -451,12 +451,12 @@ static ffi_abi tv2ffi_abi(klisp_State *K, TValue v) static ffi_codec_t *tv2ffi_codec(klisp_State *K, TValue v) { - for (size_t i = 0; i < sizeof(ffi_codecs)/sizeof(ffi_codecs[0]); i++) { - if (!strcmp(ffi_codecs[i].name, kstring_buf(v))) - return &ffi_codecs[i]; - } - klispE_throw_simple_with_irritants(K, "unsupported FFI type", 1, v); - return NULL; + for (size_t i = 0; i < sizeof(ffi_codecs)/sizeof(ffi_codecs[0]); i++) { + if (!strcmp(ffi_codecs[i].name, kstring_buf(v))) + return &ffi_codecs[i]; + } + klispE_throw_simple_with_irritants(K, "unsupported FFI type", 1, v); + return NULL; } inline size_t align(size_t offset, size_t alignment) @@ -478,20 +478,20 @@ void ffi_make_call_interface(klisp_State *K) #define ttislist(v) (ttispair(v) || ttisnil(v)) bind_3tp(K, ptree, - "abi string", ttisstring, abi_tv, - "rtype string", ttisstring, rtype_tv, - "argtypes string list", ttislist, argtypes_tv); + "abi string", ttisstring, abi_tv, + "rtype string", ttisstring, rtype_tv, + "argtypes string list", ttislist, argtypes_tv); #undef ttislist size_t nargs; check_typed_list(K, kstringp, false, argtypes_tv, (int32_t *) &nargs, - NULL); + NULL); /* Allocate C structure ffi_call_interface_t inside - a mutable bytevector. The structure contains C pointers - into itself. It must never be reallocated or copied. - The bytevector will be encapsulated later to protect - it from lisp code. */ + a mutable bytevector. The structure contains C pointers + into itself. It must never be reallocated or copied. + The bytevector will be encapsulated later to protect + it from lisp code. */ size_t bytevector_size = sizeof(ffi_call_interface_t) + (sizeof(ffi_codec_t *) + sizeof(ffi_type)) * nargs; TValue bytevector = kbytevector_new_sf(K, bytevector_size, 0); @@ -524,17 +524,17 @@ void ffi_make_call_interface(klisp_State *K) ffi_status status = ffi_prep_cif(&p->cif, abi, nargs, p->rcodec->libffi_type, p->argtypes); switch (status) { - case FFI_OK: - break; - case FFI_BAD_ABI: - klispE_throw_simple(K, "FFI_BAD_ABI"); - return; - case FFI_BAD_TYPEDEF: - klispE_throw_simple(K, "FFI_BAD_TYPEDEF"); - return; - default: - klispE_throw_simple(K, "unknown error in ffi_prep_cif"); - return; + case FFI_OK: + break; + case FFI_BAD_ABI: + klispE_throw_simple(K, "FFI_BAD_ABI"); + return; + case FFI_BAD_TYPEDEF: + klispE_throw_simple(K, "FFI_BAD_TYPEDEF"); + return; + default: + klispE_throw_simple(K, "unknown error in ffi_prep_cif"); + return; } TValue key = xparams[0]; @@ -604,9 +604,9 @@ void ffi_make_applicative(klisp_State *K) */ bind_3tp(K, ptree, - "dynamic library", ttisencapsulation, lib_tv, - "function name string", ttisstring, name_tv, - "call interface", ttisencapsulation, cif_tv); + "dynamic library", ttisencapsulation, lib_tv, + "function name string", ttisstring, name_tv, + "call interface", ttisencapsulation, cif_tv); if (!kis_encapsulation_type(lib_tv, xparams[0])) { klispE_throw_simple(K, "first argument shall be dynamic library"); return; @@ -640,10 +640,10 @@ void ffi_make_applicative(klisp_State *K) HMODULE handle = pvalue(kcar(kget_enc_val(lib_tv))); void *funptr = GetProcAddress(handle, kstring_buf(name_tv)); if (NULL == funptr) { - TValue err = ffi_win32_error_message(K, GetLastError()); - klispE_throw_simple_with_irritants(K, "couldn't find symbol", - 3, lib_name, name_tv, err); - return; + TValue err = ffi_win32_error_message(K, GetLastError()); + klispE_throw_simple_with_irritants(K, "couldn't find symbol", + 3, lib_name, name_tv, err); + return; } #else # error @@ -914,8 +914,8 @@ void ffi_make_callback(klisp_State *K) */ bind_2tp(K, ptree, - "applicative", ttisapplicative, app_tv, - "call interface", ttisencapsulation, cif_tv); + "applicative", ttisapplicative, app_tv, + "call interface", ttisencapsulation, cif_tv); if (!kis_encapsulation_type(cif_tv, xparams[0])) { klispE_throw_simple(K, "second argument shall be call interface"); return; @@ -1032,12 +1032,12 @@ void ffi_memmove(klisp_State *K) UNUSED(denv); bind_3tp(K, ptree, - "any", anytype, dst_tv, - "any", anytype, src_tv, - "integer", ttisfixint, sz_tv); + "any", anytype, dst_tv, + "any", anytype, src_tv, + "integer", ttisfixint, sz_tv); if (ivalue(sz_tv) < 0) - klispE_throw_simple(K, "size should be nonnegative fixint"); + klispE_throw_simple(K, "size should be nonnegative fixint"); size_t sz = (size_t) ivalue(sz_tv); uint8_t * dst = ffi_memory_location(K, true, dst_tv, true, sz); @@ -1064,7 +1064,7 @@ static void ffi_type_ref(klisp_State *K) const uint8_t *ptr = ffi_memory_location(K, true, location_tv, false, codec->libffi_type->size); #if KGFFI_CHECK_ALIGNMENT if ((size_t) ptr % codec->libffi_type->alignment != 0) - klispE_throw_simple(K, "unaligned memory read through FFI"); + klispE_throw_simple(K, "unaligned memory read through FFI"); #endif TValue result = codec->decode(codec, K, ptr); @@ -1090,7 +1090,7 @@ static void ffi_type_set(klisp_State *K) uint8_t *ptr = ffi_memory_location(K, true, location_tv, false, codec->libffi_type->size); #if KGFFI_CHECK_ALIGNMENT if ((size_t) ptr % codec->libffi_type->alignment != 0) - klispE_throw_simple(K, "unaligned memory write through FFI"); + klispE_throw_simple(K, "unaligned memory write through FFI"); #endif codec->encode(codec, K, value_tv, ptr); @@ -1189,7 +1189,7 @@ void kinit_ffi_cont_names(klisp_State *K) Table *t = tv2table(K->cont_name_table); add_cont_name(K, t, do_ffi_callback_encode_result, - "ffi-callback-encode-result"); + "ffi-callback-encode-result"); add_cont_name(K, t, do_ffi_callback_return, - "ffi-callback-ret"); + "ffi-callback-ret"); } diff --git a/src/kghelpers.c b/src/kghelpers.c @@ -94,18 +94,18 @@ void enc_typep(klisp_State *K) TValue tail = ptree; while(ttispair(tail) && kis_unmarked(tail)) { - kmark(tail); - res &= kis_encapsulation_type(kcar(tail), key); - tail = kcdr(tail); + kmark(tail); + res &= kis_encapsulation_type(kcar(tail), key); + tail = kcdr(tail); } unmark_list(K, ptree); if (ttispair(tail) || ttisnil(tail)) { - kapply_cc(K, b2tv(res)); + kapply_cc(K, b2tv(res)); } else { - /* try to get name from encapsulation */ - klispE_throw_simple(K, "expected list"); - return; + /* try to get name from encapsulation */ + klispE_throw_simple(K, "expected list"); + return; } } /* /Type predicates */ @@ -117,18 +117,18 @@ bool kpositivep(TValue n) case K_TFIXINT: case K_TEINF: case K_TIINF: - return ivalue(n) > 0; + return ivalue(n) > 0; case K_TBIGINT: - return kbigint_positivep(n); + return kbigint_positivep(n); case K_TBIGRAT: - return kbigrat_positivep(n); + return kbigrat_positivep(n); case K_TDOUBLE: - return dvalue(n) > 0.0; - /* real with no prim value, complex and undefined should be captured by - type predicate */ + return dvalue(n) > 0.0; + /* real with no prim value, complex and undefined should be captured by + type predicate */ default: - klisp_assert(0); - return false; + klisp_assert(0); + return false; } } @@ -138,18 +138,18 @@ bool knegativep(TValue n) case K_TFIXINT: case K_TEINF: case K_TIINF: - return ivalue(n) < 0; + return ivalue(n) < 0; case K_TBIGINT: - return kbigint_negativep(n); + return kbigint_negativep(n); case K_TBIGRAT: - return kbigrat_negativep(n); + return kbigrat_negativep(n); case K_TDOUBLE: - return dvalue(n) < 0.0; - /* real with no prim value, complex and undefined should be captured by - type predicate */ + return dvalue(n) < 0.0; + /* real with no prim value, complex and undefined should be captured by + type predicate */ default: - klisp_assert(0); - return false; + klisp_assert(0); + return false; } } /* /some number functions */ @@ -174,17 +174,17 @@ void typep(klisp_State *K) TValue tail = ptree; while(ttispair(tail) && kis_unmarked(tail)) { - kmark(tail); - res &= ttype(kcar(tail)) == tag; - tail = kcdr(tail); + kmark(tail); + res &= ttype(kcar(tail)) == tag; + tail = kcdr(tail); } unmark_list(K, ptree); if (ttispair(tail) || ttisnil(tail)) { - kapply_cc(K, b2tv(res)); + kapply_cc(K, b2tv(res)); } else { - klispE_throw_simple(K, "expected list"); - return; + klispE_throw_simple(K, "expected list"); + return; } } @@ -208,17 +208,17 @@ void ftypep(klisp_State *K) TValue tail = ptree; while(ttispair(tail) && kis_unmarked(tail)) { - kmark(tail); - res &= (*fn)(kcar(tail)); - tail = kcdr(tail); + kmark(tail); + res &= (*fn)(kcar(tail)); + tail = kcdr(tail); } unmark_list(K, ptree); if (ttispair(tail) || ttisnil(tail)) { - kapply_cc(K, b2tv(res)); + kapply_cc(K, b2tv(res)); } else { - klispE_throw_simple(K, "expected list"); - return; + klispE_throw_simple(K, "expected list"); + return; } } @@ -252,15 +252,15 @@ void ftyped_predp(klisp_State *K) Keep going even if the result is false to catch errors in type */ while(pairs--) { - TValue first = kcar(tail); - - if (!(*typep)(first)) { - /* TODO show expected type */ - klispE_throw_simple(K, "bad argument type"); - return; - } - res &= (*predp)(first); - tail = kcdr(tail); + TValue first = kcar(tail); + + if (!(*typep)(first)) { + /* TODO show expected type */ + klispE_throw_simple(K, "bad argument type"); + return; + } + res &= (*predp)(first); + tail = kcdr(tail); } kapply_cc(K, b2tv(res)); } @@ -300,27 +300,27 @@ void ftyped_bpredp(klisp_State *K) type */ if (comps == 0) { - /* this case has to be here because otherwise there is no check - for the type of the lone operand */ - TValue first = kcar(tail); - if (!(*typep)(first)) { - /* TODO show expected type */ - klispE_throw_simple(K, "bad argument type"); - return; - } + /* this case has to be here because otherwise there is no check + for the type of the lone operand */ + TValue first = kcar(tail); + if (!(*typep)(first)) { + /* TODO show expected type */ + klispE_throw_simple(K, "bad argument type"); + return; + } } while(comps-- > 0) { /* comps could be -1 if ptree is () */ - TValue first = kcar(tail); - tail = kcdr(tail); /* tail only advances one place per iteration */ - TValue second = kcar(tail); - - if (!(*typep)(first) || !(*typep)(second)) { - /* TODO show expected type */ - klispE_throw_simple(K, "bad argument type"); - return; - } - res &= (*predp)(first, second); + TValue first = kcar(tail); + tail = kcdr(tail); /* tail only advances one place per iteration */ + TValue second = kcar(tail); + + if (!(*typep)(first) || !(*typep)(second)) { + /* TODO show expected type */ + klispE_throw_simple(K, "bad argument type"); + return; + } + res &= (*predp)(first, second); } kapply_cc(K, b2tv(res)); } @@ -341,7 +341,7 @@ void ftyped_kbpredp(klisp_State *K) */ bool (*typep)(TValue obj) = pvalue(xparams[1]); bool (*predp)(klisp_State *K, TValue obj1, TValue obj2) = - pvalue(xparams[2]); + pvalue(xparams[2]); /* check the ptree is a list first to allow the structure errors to take precedence over the type errors. */ @@ -360,153 +360,153 @@ void ftyped_kbpredp(klisp_State *K) type */ if (comps == 0) { - /* this case has to be here because otherwise there is no check - for the type of the lone operand */ - TValue first = kcar(tail); - if (!(*typep)(first)) { - /* TODO show expected type */ - klispE_throw_simple(K, "bad argument type"); - return; - } + /* this case has to be here because otherwise there is no check + for the type of the lone operand */ + TValue first = kcar(tail); + if (!(*typep)(first)) { + /* TODO show expected type */ + klispE_throw_simple(K, "bad argument type"); + return; + } } while(comps-- > 0) { /* comps could be -1 if ptree is () */ - TValue first = kcar(tail); - tail = kcdr(tail); /* tail only advances one place per iteration */ - TValue second = kcar(tail); - - if (!(*typep)(first) || !(*typep)(second)) { - /* TODO show expected type */ - klispE_throw_simple(K, "bad argument type"); - return; - } - res &= (*predp)(K, first, second); + TValue first = kcar(tail); + tail = kcdr(tail); /* tail only advances one place per iteration */ + TValue second = kcar(tail); + + if (!(*typep)(first) || !(*typep)(second)) { + /* TODO show expected type */ + klispE_throw_simple(K, "bad argument type"); + return; + } + res &= (*predp)(K, first, second); } kapply_cc(K, b2tv(res)); } /* typed finite list. Structure error should be throw before type errors */ void check_typed_list(klisp_State *K, bool (*typep)(TValue), bool allow_infp, - TValue obj, int32_t *pairs, int32_t *cpairs) + TValue obj, int32_t *pairs, int32_t *cpairs) { TValue tail = obj; int32_t p = 0; bool type_errorp = false; while(ttispair(tail) && !kis_marked(tail)) { - /* even if there is a type error continue checking the structure */ - type_errorp |= !(*typep)(kcar(tail)); - kset_mark(tail, i2tv(p)); - tail = kcdr(tail); - ++p; + /* even if there is a type error continue checking the structure */ + type_errorp |= !(*typep)(kcar(tail)); + kset_mark(tail, i2tv(p)); + tail = kcdr(tail); + ++p; } if (pairs != NULL) *pairs = p; if (cpairs != NULL) - *cpairs = ttispair(tail)? (p - ivalue(kget_mark(tail))) : 0; + *cpairs = ttispair(tail)? (p - ivalue(kget_mark(tail))) : 0; unmark_list(K, obj); if (!ttispair(tail) && !ttisnil(tail)) { - klispE_throw_simple(K, allow_infp? "expected list" : - "expected finite list"); - return; + klispE_throw_simple(K, allow_infp? "expected list" : + "expected finite list"); + return; } else if(ttispair(tail) && !allow_infp) { - klispE_throw_simple(K, "expected finite list"); - return; + klispE_throw_simple(K, "expected finite list"); + return; } else if (type_errorp) { - /* TODO put type name too, should be extracted from a - table of type names */ - klispE_throw_simple(K, "bad operand type"); - return; + /* TODO put type name too, should be extracted from a + table of type names */ + klispE_throw_simple(K, "bad operand type"); + return; } } void check_list(klisp_State *K, bool allow_infp, TValue obj, - int32_t *pairs, int32_t *cpairs) + int32_t *pairs, int32_t *cpairs) { TValue tail = obj; int32_t p = 0; while(ttispair(tail) && !kis_marked(tail)) { - kset_mark(tail, i2tv(p)); - tail = kcdr(tail); - ++p; + kset_mark(tail, i2tv(p)); + tail = kcdr(tail); + ++p; } if (pairs != NULL) *pairs = p; if (cpairs != NULL) - *cpairs = ttispair(tail)? (p - ivalue(kget_mark(tail))) : 0; + *cpairs = ttispair(tail)? (p - ivalue(kget_mark(tail))) : 0; unmark_list(K, obj); if (!ttispair(tail) && !ttisnil(tail)) { - klispE_throw_simple(K, allow_infp? "expected list" : - "expected finite list"); - return; + klispE_throw_simple(K, allow_infp? "expected list" : + "expected finite list"); + return; } else if(ttispair(tail) && !allow_infp) { - klispE_throw_simple(K, "expected finite list"); - return; + klispE_throw_simple(K, "expected finite list"); + return; } } TValue check_copy_list(klisp_State *K, TValue obj, bool force_copy, - int32_t *pairs, int32_t *cpairs) + int32_t *pairs, int32_t *cpairs) { int32_t p = 0; if (ttisnil(obj)) { - if (pairs != NULL) *pairs = 0; - if (cpairs != NULL) *cpairs = 0; - return obj; + if (pairs != NULL) *pairs = 0; + if (cpairs != NULL) *cpairs = 0; + return obj; } if (ttispair(obj) && kis_immutable(obj) && !force_copy) { - /* this will properly set pairs and cpairs */ - check_list(K, true, obj, pairs, cpairs); - return obj; + /* this will properly set pairs and cpairs */ + check_list(K, true, obj, pairs, cpairs); + return obj; } else { - TValue copy = kcons(K, KNIL, KNIL); - krooted_vars_push(K, &copy); - TValue last_pair = copy; - TValue tail = obj; + TValue copy = kcons(K, KNIL, KNIL); + krooted_vars_push(K, &copy); + TValue last_pair = copy; + TValue tail = obj; - while(ttispair(tail) && !kis_marked(tail)) { - TValue new_pair = kcons(K, kcar(tail), KNIL); - /* record the corresponding pair to simplify cycle handling */ - kset_mark(tail, new_pair); - /* record the pair number in the new pair, to set cpairs */ - kset_mark(new_pair, i2tv(p)); - /* copy the source code info */ - TValue si = ktry_get_si(K, tail); - if (!ttisnil(si)) - kset_source_info(K, new_pair, si); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - tail = kcdr(tail); - ++p; - } - - if (pairs != NULL) *pairs = p; - if (cpairs != NULL) - *cpairs = ttispair(tail)? - (p - ivalue(kget_mark(kget_mark(tail)))) : - 0; - - if (ttispair(tail)) { - /* complete the cycle */ - kset_cdr(last_pair, kget_mark(tail)); - } - - unmark_list(K, obj); - unmark_list(K, kcdr(copy)); - - if (!ttispair(tail) && !ttisnil(tail)) { - klispE_throw_simple(K, "expected list"); - return KINERT; - } - krooted_vars_pop(K); - return kcdr(copy); + while(ttispair(tail) && !kis_marked(tail)) { + TValue new_pair = kcons(K, kcar(tail), KNIL); + /* record the corresponding pair to simplify cycle handling */ + kset_mark(tail, new_pair); + /* record the pair number in the new pair, to set cpairs */ + kset_mark(new_pair, i2tv(p)); + /* copy the source code info */ + TValue si = ktry_get_si(K, tail); + if (!ttisnil(si)) + kset_source_info(K, new_pair, si); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + tail = kcdr(tail); + ++p; + } + + if (pairs != NULL) *pairs = p; + if (cpairs != NULL) + *cpairs = ttispair(tail)? + (p - ivalue(kget_mark(kget_mark(tail)))) : + 0; + + if (ttispair(tail)) { + /* complete the cycle */ + kset_cdr(last_pair, kget_mark(tail)); + } + + unmark_list(K, obj); + unmark_list(K, kcdr(copy)); + + if (!ttispair(tail) && !ttisnil(tail)) { + klispE_throw_simple(K, "expected list"); + return KINERT; + } + krooted_vars_pop(K); + return kcdr(copy); } } @@ -518,32 +518,32 @@ TValue check_copy_env_list(klisp_State *K, TValue obj) TValue tail = obj; while(ttispair(tail) && !kis_marked(tail)) { - TValue first = kcar(tail); - if (!ttisenvironment(first)) { - klispE_throw_simple(K, "not an environment in parent list"); - return KINERT; - } - TValue new_pair = kcons(K, first, KNIL); - kmark(tail); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - tail = kcdr(tail); + TValue first = kcar(tail); + if (!ttisenvironment(first)) { + klispE_throw_simple(K, "not an environment in parent list"); + return KINERT; + } + TValue new_pair = kcons(K, first, KNIL); + kmark(tail); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + tail = kcdr(tail); } /* even if there was a cycle, the copy ends with nil */ unmark_list(K, obj); if (!ttispair(tail) && !ttisnil(tail)) { - klispE_throw_simple(K, "expected list"); - return KINERT; + klispE_throw_simple(K, "expected list"); + return KINERT; } krooted_vars_pop(K); return kcdr(copy); } /* Helpers for string, list->string, and string-map, - bytevector, list->bytevector, bytevector-map, - vector, list->vector, and vector-map */ + bytevector, list->bytevector, bytevector-map, + vector, list->vector, and vector-map */ /* GC: Assume ls is rooted */ /* ls should a list of length 'length' of the correct type (chars for string, u8 for bytevector, any for vector) */ @@ -554,21 +554,21 @@ TValue list_to_string_h(klisp_State *K, TValue ls, int32_t length) TValue new_str; /* the if isn't strictly necessary but it's clearer this way */ if (length == 0) { - return K->empty_string; + return K->empty_string; } else { - new_str = kstring_new_s(K, length); - char *buf = kstring_buf(new_str); - while(length-- > 0) { - TValue head = kcar(ls); - if (!ttischar(head)) { - klispE_throw_simple_with_irritants(K, "Bad type (expected " - "char)", 1, head); - return KINERT; - } - *buf++ = chvalue(head); - ls = kcdr(ls); - } - return new_str; + new_str = kstring_new_s(K, length); + char *buf = kstring_buf(new_str); + while(length-- > 0) { + TValue head = kcar(ls); + if (!ttischar(head)) { + klispE_throw_simple_with_irritants(K, "Bad type (expected " + "char)", 1, head); + return KINERT; + } + *buf++ = chvalue(head); + ls = kcdr(ls); + } + return new_str; } } @@ -579,7 +579,7 @@ TValue list_to_vector_h(klisp_State *K, TValue ls, int32_t length) return K->empty_vector; } else { TValue new_vec = kvector_new_sf(K, length, KINERT); - TValue *buf = kvector_buf(new_vec); + TValue *buf = kvector_buf(new_vec); while(length-- > 0) { *buf++ = kcar(ls); ls = kcdr(ls); @@ -593,21 +593,21 @@ TValue list_to_bytevector_h(klisp_State *K, TValue ls, int32_t length) TValue new_bb; /* the if isn't strictly necessary but it's clearer this way */ if (length == 0) { - return K->empty_bytevector; + return K->empty_bytevector; } else { - new_bb = kbytevector_new_s(K, length); - uint8_t *buf = kbytevector_buf(new_bb); - while(length-- > 0) { - TValue head = kcar(ls); - if (!ttisu8(head)) { - klispE_throw_simple_with_irritants(K, "Bad type (expected " - "u8)", 1, head); - return KINERT; - } - *buf++ = ivalue(head); - ls = kcdr(ls); - } - return new_bb; + new_bb = kbytevector_new_s(K, length); + uint8_t *buf = kbytevector_buf(new_bb); + while(length-- > 0) { + TValue head = kcar(ls); + if (!ttisu8(head)) { + klispE_throw_simple_with_irritants(K, "Bad type (expected " + "u8)", 1, head); + return KINERT; + } + *buf++ = ivalue(head); + ls = kcdr(ls); + } + return new_bb; } } @@ -618,9 +618,9 @@ TValue list_to_bytevector_h(klisp_State *K, TValue ls, int32_t length) TValue string_to_list_h(klisp_State *K, TValue obj, int32_t *length) { if (!ttisstring(obj)) { - klispE_throw_simple_with_irritants(K, "Bad type (expected string)", - 1, obj); - return KINERT; + klispE_throw_simple_with_irritants(K, "Bad type (expected string)", + 1, obj); + return KINERT; } int32_t pairs = kstring_size(obj); @@ -630,8 +630,8 @@ TValue string_to_list_h(klisp_State *K, TValue obj, int32_t *length) TValue tail = KNIL; krooted_vars_push(K, &tail); while(pairs-- > 0) { - tail = kcons(K, ch2tv(*buf), tail); - --buf; + tail = kcons(K, ch2tv(*buf), tail); + --buf; } krooted_vars_pop(K); return tail; @@ -640,9 +640,9 @@ TValue string_to_list_h(klisp_State *K, TValue obj, int32_t *length) TValue vector_to_list_h(klisp_State *K, TValue obj, int32_t *length) { if (!ttisvector(obj)) { - klispE_throw_simple_with_irritants(K, "Bad type (expected vector)", - 1, obj); - return KINERT; + klispE_throw_simple_with_irritants(K, "Bad type (expected vector)", + 1, obj); + return KINERT; } int32_t pairs = kvector_size(obj); @@ -652,8 +652,8 @@ TValue vector_to_list_h(klisp_State *K, TValue obj, int32_t *length) TValue tail = KNIL; krooted_vars_push(K, &tail); while(pairs-- > 0) { - tail = kcons(K, *buf, tail); - --buf; + tail = kcons(K, *buf, tail); + --buf; } krooted_vars_pop(K); return tail; @@ -662,9 +662,9 @@ TValue vector_to_list_h(klisp_State *K, TValue obj, int32_t *length) TValue bytevector_to_list_h(klisp_State *K, TValue obj, int32_t *length) { if (!ttisbytevector(obj)) { - klispE_throw_simple_with_irritants(K, "Bad type (expected bytevector)", - 1, obj); - return KINERT; + klispE_throw_simple_with_irritants(K, "Bad type (expected bytevector)", + 1, obj); + return KINERT; } int32_t pairs = kbytevector_size(obj); @@ -674,8 +674,8 @@ TValue bytevector_to_list_h(klisp_State *K, TValue obj, int32_t *length) TValue tail = KNIL; krooted_vars_push(K, &tail); while(pairs-- > 0) { - tail = kcons(K, i2tv(*buf), tail); - --buf; + tail = kcons(K, i2tv(*buf), tail); + --buf; } krooted_vars_pop(K); return tail; @@ -694,29 +694,29 @@ int64_t kgcd32_64(int32_t a_, int32_t b_) int powerof2; /* the easy cases first, unlike the general kernel gcd the - gcd2 of a number and zero is zero */ + gcd2 of a number and zero is zero */ if (a == 0) - return (int64_t) b; + return (int64_t) b; else if (b == 0) - return (int64_t) a; + return (int64_t) a; for (powerof2 = 0; ((a & 1) == 0) && - ((b & 1) == 0); ++powerof2, a >>= 1, b >>= 1) - ; + ((b & 1) == 0); ++powerof2, a >>= 1, b >>= 1) + ; while(a != 0 && b!= 0) { - /* either a or b are odd, make them both odd */ - for (; (a & 1) == 0; a >>= 1) - ; - for (; (b & 1) == 0; b >>= 1) - ; - - /* now the difference is sure to be even */ - if (a < b) { - b = (b - a) >> 1; - } else { - a = (a - b) >> 1; - } + /* either a or b are odd, make them both odd */ + for (; (a & 1) == 0; a >>= 1) + ; + for (; (b & 1) == 0; b >>= 1) + ; + + /* now the difference is sure to be even */ + if (a < b) { + b = (b - a) >> 1; + } else { + a = (a - b) >> 1; + } } return ((int64_t) (a == 0? b : a)) << powerof2; @@ -763,32 +763,32 @@ void list(klisp_State *K) /* Helper for get-list-metrics, and list-tail, list-ref and list-set! when receiving bigint indexes */ void get_list_metrics_aux(klisp_State *K, TValue obj, int32_t *p, int32_t *n, - int32_t *a, int32_t *c) + int32_t *a, int32_t *c) { TValue tail = obj; int32_t pairs = 0; while(ttispair(tail) && !kis_marked(tail)) { - /* record the pair number to simplify cycle pair counting */ - kset_mark(tail, i2tv(pairs)); - ++pairs; - tail = kcdr(tail); + /* record the pair number to simplify cycle pair counting */ + kset_mark(tail, i2tv(pairs)); + ++pairs; + tail = kcdr(tail); } int32_t apairs, cpairs, nils; if (ttisnil(tail)) { - /* simple (possibly empty) list */ - apairs = pairs; - nils = 1; - cpairs = 0; + /* simple (possibly empty) list */ + apairs = pairs; + nils = 1; + cpairs = 0; } else if (ttispair(tail)) { - /* cyclic (maybe circular) list */ - apairs = ivalue(kget_mark(tail)); - cpairs = pairs - apairs; - nils = 0; + /* cyclic (maybe circular) list */ + apairs = ivalue(kget_mark(tail)); + cpairs = pairs - apairs; + nils = 0; } else { - apairs = pairs; - cpairs = 0; - nils = 0; + apairs = pairs; + cpairs = 0; + nils = 0; } unmark_list(K, obj); @@ -810,9 +810,9 @@ int32_t ksmallest_index(klisp_State *K, TValue obj, TValue tk) int32_t apairs, cpairs; get_list_metrics_aux(K, obj, NULL, NULL, &apairs, &cpairs); if (cpairs == 0) { - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return 0; + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return 0; } TValue tv_apairs = i2tv(apairs); TValue tv_cpairs = i2tv(cpairs); @@ -837,32 +837,32 @@ bool eq2p(klisp_State *K, TValue obj1, TValue obj2) { bool res = (tv_equal(obj1, obj2)); if (!res && (ttype(obj1) == ttype(obj2))) { - switch (ttype(obj1)) { - case K_TSYMBOL: + switch (ttype(obj1)) { + case K_TSYMBOL: /* symbols can't be compared with tv_equal! */ - res = tv_sym_equal(obj1, obj2); - break; - case K_TAPPLICATIVE: - while(ttisapplicative(obj1) && ttisapplicative(obj2)) { - obj1 = kunwrap(obj1); - obj2 = kunwrap(obj2); - } - res = (tv_equal(obj1, obj2)); - break; - case K_TBIGINT: - /* it's important to know that it can't be the case - that obj1 is bigint and obj is some other type and - (eq? obj1 obj2) */ - res = kbigint_eqp(obj1, obj2); - break; - case K_TBIGRAT: - /* it's important to know that it can't be the case - that obj1 is bigrat and obj is some other type and - (eq? obj1 obj2) */ - res = kbigrat_eqp(K, obj1, obj2); - break; - } /* immutable strings & bytevectors are interned so they are - covered already by tv_equalp */ + res = tv_sym_equal(obj1, obj2); + break; + case K_TAPPLICATIVE: + while(ttisapplicative(obj1) && ttisapplicative(obj2)) { + obj1 = kunwrap(obj1); + obj2 = kunwrap(obj2); + } + res = (tv_equal(obj1, obj2)); + break; + case K_TBIGINT: + /* it's important to know that it can't be the case + that obj1 is bigint and obj is some other type and + (eq? obj1 obj2) */ + res = kbigint_eqp(obj1, obj2); + break; + case K_TBIGRAT: + /* it's important to know that it can't be the case + that obj1 is bigrat and obj is some other type and + (eq? obj1 obj2) */ + res = kbigrat_eqp(K, obj1, obj2); + break; + } /* immutable strings & bytevectors are interned so they are + covered already by tv_equalp */ } return res; @@ -891,31 +891,31 @@ inline TValue equal_find(klisp_State *K, TValue obj) { /* GC: should root obj */ if (kis_unmarked(obj)) { - /* object wasn't compared before, create new set */ - TValue new_node = kcons(K, KTRUE, i2tv(1)); - kset_mark(obj, new_node); - return new_node; + /* object wasn't compared before, create new set */ + TValue new_node = kcons(K, KTRUE, i2tv(1)); + kset_mark(obj, new_node); + return new_node; } else { - TValue node = kget_mark(obj); - - /* First obtain the root and a list of all the other objects in this - branch, as said above the root is the one with #t in its car */ - /* NOTE: the stack is being used, so we must remember how many pairs we - push, we can't just pop 'till is empty */ - int np = 0; - while(kis_false(kcar(node))) { - ks_spush(K, node); - node = kcdr(node); - ++np; - } - TValue root = node; - - /* set all parents to root, to flatten the branch */ - while(np--) { - node = ks_spop(K); - kset_cdr(node, root); - } - return root; + TValue node = kget_mark(obj); + + /* First obtain the root and a list of all the other objects in this + branch, as said above the root is the one with #t in its car */ + /* NOTE: the stack is being used, so we must remember how many pairs we + push, we can't just pop 'till is empty */ + int np = 0; + while(kis_false(kcar(node))) { + ks_spush(K, node); + node = kcdr(node); + ++np; + } + TValue root = node; + + /* set all parents to root, to flatten the branch */ + while(np--) { + node = ks_spop(K); + kset_cdr(node, root); + } + return root; } } @@ -929,15 +929,15 @@ inline void equal_merge(klisp_State *K, TValue root1, TValue root2) TValue new_size = i2tv(size1 + size2); if (size1 < size2) { - /* add root1 set (the smaller one) to root2 */ - kset_cdr(root2, new_size); - kset_car(root1, KFALSE); - kset_cdr(root1, root2); + /* add root1 set (the smaller one) to root2 */ + kset_cdr(root2, new_size); + kset_car(root1, KFALSE); + kset_cdr(root1, root2); } else { - /* add root2 set (the smaller one) to root1 */ - kset_cdr(root1, new_size); - kset_car(root2, KFALSE); - kset_cdr(root2, root1); + /* add root2 set (the smaller one) to root1 */ + kset_cdr(root1, new_size); + kset_car(root2, KFALSE); + kset_cdr(root2, root1); } } @@ -949,11 +949,11 @@ inline bool equal_find2_mergep(klisp_State *K, TValue obj1, TValue obj2) TValue root1 = equal_find(K, obj1); TValue root2 = equal_find(K, obj2); if (tv_equal(root1, root2)) { - /* they are in the same set => they were already compared */ - return true; + /* they are in the same set => they were already compared */ + return true; } else { - equal_merge(K, root1, root2); - return false; + equal_merge(K, root1, root2); + return false; } } @@ -985,65 +985,65 @@ bool equal2p(klisp_State *K, TValue obj1, TValue obj2) TValue saved_obj2 = obj2; while(!ks_sisempty(K)) { - obj2 = ks_spop(K); - obj1 = ks_spop(K); - - if (!eq2p(K, obj1, obj2)) { - /* This type comparison works because we just care about - pairs, vectors, strings & bytevectors */ - if (ttype(obj1) == ttype(obj2)) { - switch(ttype(obj1)) { - case K_TPAIR: - /* if they were already compaired, consider equal for - now otherwise they are equal if both their cars - and cdrs are */ - if (!equal_find2_mergep(K, obj1, obj2)) { - ks_spush(K, kcdr(obj1)); - ks_spush(K, kcdr(obj2)); - ks_spush(K, kcar(obj1)); - ks_spush(K, kcar(obj2)); - } - break; - case K_TVECTOR: - if (kvector_size(obj1) == kvector_size(obj2)) { - /* if they were already compaired, consider equal for - now otherwise they are equal if all their elements - are equal pairwise */ - if (!equal_find2_mergep(K, obj1, obj2)) { - uint32_t i = kvector_size(obj1); - TValue *array1 = kvector_buf(obj1); - TValue *array2 = kvector_buf(obj2); - while(i-- > 0) { - ks_spush(K, array1[i]); - ks_spush(K, array2[i]); - } - } - } else { - result = false; - goto end; - } - break; - case K_TSTRING: - if (!kstring_equalp(obj1, obj2)) { - result = false; - goto end; - } - break; - case K_TBYTEVECTOR: - if (!kbytevector_equalp(obj1, obj2)) { - result = false; - goto end; - } - break; - default: - result = false; - goto end; - } - } else { - result = false; - goto end; - } - } + obj2 = ks_spop(K); + obj1 = ks_spop(K); + + if (!eq2p(K, obj1, obj2)) { + /* This type comparison works because we just care about + pairs, vectors, strings & bytevectors */ + if (ttype(obj1) == ttype(obj2)) { + switch(ttype(obj1)) { + case K_TPAIR: + /* if they were already compaired, consider equal for + now otherwise they are equal if both their cars + and cdrs are */ + if (!equal_find2_mergep(K, obj1, obj2)) { + ks_spush(K, kcdr(obj1)); + ks_spush(K, kcdr(obj2)); + ks_spush(K, kcar(obj1)); + ks_spush(K, kcar(obj2)); + } + break; + case K_TVECTOR: + if (kvector_size(obj1) == kvector_size(obj2)) { + /* if they were already compaired, consider equal for + now otherwise they are equal if all their elements + are equal pairwise */ + if (!equal_find2_mergep(K, obj1, obj2)) { + uint32_t i = kvector_size(obj1); + TValue *array1 = kvector_buf(obj1); + TValue *array2 = kvector_buf(obj2); + while(i-- > 0) { + ks_spush(K, array1[i]); + ks_spush(K, array2[i]); + } + } + } else { + result = false; + goto end; + } + break; + case K_TSTRING: + if (!kstring_equalp(obj1, obj2)) { + result = false; + goto end; + } + break; + case K_TBYTEVECTOR: + if (!kbytevector_equalp(obj1, obj2)) { + result = false; + goto end; + } + break; + default: + result = false; + goto end; + } + } else { + result = false; + goto end; + } + } } end: /* if result is false, the stack may not be empty */ @@ -1084,51 +1084,51 @@ TValue copy_es_immutable_h(klisp_State *K, TValue obj, bool mut_flag) ks_tbpush(K, ST_PUSH); while(!ks_sisempty(K)) { - char state = ks_tbpop(K); - TValue top = ks_spop(K); - - if (state == ST_PUSH) { - /* if the pair is immutable & we are constructing immutable - pairs there is no need to copy */ - if (ttispair(top) && (mut_flag || kis_mutable(top))) { - if (kis_marked(top)) { - /* this pair was already seen, use the same */ - copy = kget_mark(top); - } else { - TValue new_pair = kcons_g(K, mut_flag, KINERT, KINERT); - kset_mark(top, new_pair); - /* save the source code info on the new pair */ - /* MAYBE: only do it if mutable */ - TValue si = ktry_get_si(K, top); - if (!ttisnil(si)) - kset_source_info(K, new_pair, si); - /* leave the pair in the stack, continue with the car */ - ks_spush(K, top); - ks_tbpush(K, ST_CAR); + char state = ks_tbpop(K); + TValue top = ks_spop(K); + + if (state == ST_PUSH) { + /* if the pair is immutable & we are constructing immutable + pairs there is no need to copy */ + if (ttispair(top) && (mut_flag || kis_mutable(top))) { + if (kis_marked(top)) { + /* this pair was already seen, use the same */ + copy = kget_mark(top); + } else { + TValue new_pair = kcons_g(K, mut_flag, KINERT, KINERT); + kset_mark(top, new_pair); + /* save the source code info on the new pair */ + /* MAYBE: only do it if mutable */ + TValue si = ktry_get_si(K, top); + if (!ttisnil(si)) + kset_source_info(K, new_pair, si); + /* leave the pair in the stack, continue with the car */ + ks_spush(K, top); + ks_tbpush(K, ST_CAR); - ks_spush(K, kcar(top)); - ks_tbpush(K, ST_PUSH); - } - } else { - copy = top; - } - } else { /* last action was a pop */ - TValue new_pair = kget_mark(top); - if (state == ST_CAR) { - /* new_pair may be immutable */ - kset_car_unsafe(K, new_pair, copy); - /* leave the pair on the stack, continue with the cdr */ - ks_spush(K, top); - ks_tbpush(K, ST_CDR); - - ks_spush(K, kcdr(top)); - ks_tbpush(K, ST_PUSH); - } else { - /* new_pair may be immutable */ - kset_cdr_unsafe(K, new_pair, copy); - copy = new_pair; - } - } + ks_spush(K, kcar(top)); + ks_tbpush(K, ST_PUSH); + } + } else { + copy = top; + } + } else { /* last action was a pop */ + TValue new_pair = kget_mark(top); + if (state == ST_CAR) { + /* new_pair may be immutable */ + kset_car_unsafe(K, new_pair, copy); + /* leave the pair on the stack, continue with the cdr */ + ks_spush(K, top); + ks_tbpush(K, ST_CDR); + + ks_spush(K, kcdr(top)); + ks_tbpush(K, ST_PUSH); + } else { + /* new_pair may be immutable */ + kset_cdr_unsafe(K, new_pair, copy); + copy = new_pair; + } + } } unmark_tree(K, obj); krooted_vars_pop(K); @@ -1145,14 +1145,14 @@ TValue copy_es_immutable_h(klisp_State *K, TValue obj, bool mut_flag) inline void ptree_clear_all(klisp_State *K, TValue sym_ls) { while(!ttisnil(sym_ls)) { - TValue first = sym_ls; - sym_ls = kget_symbol_mark(first); - kunmark_symbol(first); + TValue first = sym_ls; + sym_ls = kget_symbol_mark(first); + kunmark_symbol(first); } while(!ks_sisempty(K)) { - kunmark(ks_sget(K)); - ks_sdpop(K); + kunmark(ks_sget(K)); + ks_sdpop(K); } ks_tbclear(K); @@ -1166,41 +1166,41 @@ void match(klisp_State *K, TValue env, TValue ptree, TValue obj) ks_spush(K, ptree); while(!ks_sisempty(K)) { - ptree = ks_spop(K); - obj = ks_spop(K); - - switch(ttype(ptree)) { - case K_TNIL: - if (!ttisnil(obj)) { - /* TODO show ptree and arguments */ - ks_sclear(K); - klispE_throw_simple(K, "ptree doesn't match arguments"); - return; - } - break; - case K_TIGNORE: - /* do nothing */ - break; - case K_TSYMBOL: - kadd_binding(K, env, ptree, obj); - break; - case K_TPAIR: - if (ttispair(obj)) { - ks_spush(K, kcdr(obj)); - ks_spush(K, kcdr(ptree)); - ks_spush(K, kcar(obj)); - ks_spush(K, kcar(ptree)); - } else { - /* TODO show ptree and arguments */ - ks_sclear(K); - klispE_throw_simple(K, "ptree doesn't match arguments"); - return; - } - break; - default: - /* can't really happen */ - break; - } + ptree = ks_spop(K); + obj = ks_spop(K); + + switch(ttype(ptree)) { + case K_TNIL: + if (!ttisnil(obj)) { + /* TODO show ptree and arguments */ + ks_sclear(K); + klispE_throw_simple(K, "ptree doesn't match arguments"); + return; + } + break; + case K_TIGNORE: + /* do nothing */ + break; + case K_TSYMBOL: + kadd_binding(K, env, ptree, obj); + break; + case K_TPAIR: + if (ttispair(obj)) { + ks_spush(K, kcdr(obj)); + ks_spush(K, kcdr(ptree)); + ks_spush(K, kcar(obj)); + ks_spush(K, kcar(ptree)); + } else { + /* TODO show ptree and arguments */ + ks_sclear(K); + klispE_throw_simple(K, "ptree doesn't match arguments"); + return; + } + break; + default: + /* can't really happen */ + break; + } } } @@ -1225,115 +1225,115 @@ TValue check_copy_ptree(klisp_State *K, TValue ptree, TValue penv) ks_spush(K, ptree); while(!ks_sisempty(K)) { - char state = ks_tbpop(K); - TValue top = ks_spop(K); - - if (state == ST_PUSH) { - switch(ttype(top)) { - case K_TIGNORE: - case K_TNIL: - copy = top; - break; - case K_TSYMBOL: { - if (kis_symbol_marked(top)) { - ptree_clear_all(K, sym_ls); - klispE_throw_simple_with_irritants(K, "repeated symbol " - "in ptree", 1, top); - return KNIL; - } else { - copy = top; - /* add it to the symbol list */ - kset_symbol_mark(top, sym_ls); - sym_ls = top; - } - break; - } - case K_TPAIR: { - if (kis_unmarked(top)) { - if (kis_immutable(top)) { - /* don't copy mutable pairs, just use them */ - /* NOTE: immutable pairs can't have mutable - car or cdr */ - /* we have to continue thou, because there could be a - cycle */ - kset_mark(top, top); - } else { - /* create a new pair as copy, save it in the mark */ - TValue new_pair = kimm_cons(K, KNIL, KNIL); - kset_mark(top, new_pair); - /* copy the source code info */ - TValue si = ktry_get_si(K, top); - if (!ttisnil(si)) - kset_source_info(K, new_pair, si); - } - /* keep the old pair and continue with the car */ - ks_tbpush(K, ST_CAR); - ks_spush(K, top); - - ks_tbpush(K, ST_PUSH); - ks_spush(K, kcar(top)); - } else { - /* marked pair means a cycle was found */ - /* NOTE: the pair should be in the stack already so - it isn't necessary to push it again to clear the mark */ - ptree_clear_all(K, sym_ls); - klispE_throw_simple(K, "cycle detected in ptree"); - /* avoid warning */ - return KNIL; - } - break; - } - default: - ptree_clear_all(K, sym_ls); - klispE_throw_simple(K, "bad object type in ptree"); - /* avoid warning */ - return KNIL; - } - } else { + char state = ks_tbpop(K); + TValue top = ks_spop(K); + + if (state == ST_PUSH) { + switch(ttype(top)) { + case K_TIGNORE: + case K_TNIL: + copy = top; + break; + case K_TSYMBOL: { + if (kis_symbol_marked(top)) { + ptree_clear_all(K, sym_ls); + klispE_throw_simple_with_irritants(K, "repeated symbol " + "in ptree", 1, top); + return KNIL; + } else { + copy = top; + /* add it to the symbol list */ + kset_symbol_mark(top, sym_ls); + sym_ls = top; + } + break; + } + case K_TPAIR: { + if (kis_unmarked(top)) { + if (kis_immutable(top)) { + /* don't copy mutable pairs, just use them */ + /* NOTE: immutable pairs can't have mutable + car or cdr */ + /* we have to continue thou, because there could be a + cycle */ + kset_mark(top, top); + } else { + /* create a new pair as copy, save it in the mark */ + TValue new_pair = kimm_cons(K, KNIL, KNIL); + kset_mark(top, new_pair); + /* copy the source code info */ + TValue si = ktry_get_si(K, top); + if (!ttisnil(si)) + kset_source_info(K, new_pair, si); + } + /* keep the old pair and continue with the car */ + ks_tbpush(K, ST_CAR); + ks_spush(K, top); + + ks_tbpush(K, ST_PUSH); + ks_spush(K, kcar(top)); + } else { + /* marked pair means a cycle was found */ + /* NOTE: the pair should be in the stack already so + it isn't necessary to push it again to clear the mark */ + ptree_clear_all(K, sym_ls); + klispE_throw_simple(K, "cycle detected in ptree"); + /* avoid warning */ + return KNIL; + } + break; + } + default: + ptree_clear_all(K, sym_ls); + klispE_throw_simple(K, "bad object type in ptree"); + /* avoid warning */ + return KNIL; + } + } else { /* last operation was a pop */ - /* top is a marked pair, the mark is the copied obj */ - /* NOTE: if top is immutable the mark is also top - we could still do the set-car/set-cdr because the - copy would be the same as the car/cdr, but why bother */ - if (state == ST_CAR) { - /* only car was checked (not yet copied) */ - if (kis_mutable(top)) { - TValue copied_pair = kget_mark(top); - /* copied_pair may be immutable */ - kset_car_unsafe(K, copied_pair, copy); - } - /* put the copied pair again, continue with the cdr */ - ks_tbpush(K, ST_CDR); - ks_spush(K, top); - - ks_tbpush(K, ST_PUSH); - ks_spush(K, kcdr(top)); - } else { + /* top is a marked pair, the mark is the copied obj */ + /* NOTE: if top is immutable the mark is also top + we could still do the set-car/set-cdr because the + copy would be the same as the car/cdr, but why bother */ + if (state == ST_CAR) { + /* only car was checked (not yet copied) */ + if (kis_mutable(top)) { + TValue copied_pair = kget_mark(top); + /* copied_pair may be immutable */ + kset_car_unsafe(K, copied_pair, copy); + } + /* put the copied pair again, continue with the cdr */ + ks_tbpush(K, ST_CDR); + ks_spush(K, top); + + ks_tbpush(K, ST_PUSH); + ks_spush(K, kcdr(top)); + } else { /* both car & cdr were checked (cdr not yet copied) */ - TValue copied_pair = kget_mark(top); - /* the unmark is needed to allow diamonds */ - kunmark(top); - - if (kis_mutable(top)) { - /* copied_pair may be immutable */ - kset_cdr_unsafe(K, copied_pair, copy); - } - copy = copied_pair; - } - } + TValue copied_pair = kget_mark(top); + /* the unmark is needed to allow diamonds */ + kunmark(top); + + if (kis_mutable(top)) { + /* copied_pair may be immutable */ + kset_cdr_unsafe(K, copied_pair, copy); + } + copy = copied_pair; + } + } } if (ttissymbol(penv)) { - if (kis_symbol_marked(penv)) { - ptree_clear_all(K, sym_ls); - klispE_throw_simple_with_irritants(K, "same symbol in both ptree " - "and environment parameter", - 1, sym_ls); - } + if (kis_symbol_marked(penv)) { + ptree_clear_all(K, sym_ls); + klispE_throw_simple_with_irritants(K, "same symbol in both ptree " + "and environment parameter", + 1, sym_ls); + } } else if (!ttisignore(penv)) { ptree_clear_all(K, sym_ls); klispE_throw_simple(K, "symbol or #ignore expected as " - "environment parmameter"); + "environment parmameter"); } ptree_clear_all(K, sym_ls); krooted_vars_pop(K); @@ -1342,8 +1342,8 @@ TValue check_copy_ptree(klisp_State *K, TValue ptree, TValue penv) /* Helpers for map (also used by for each) */ void map_for_each_get_metrics(klisp_State *K, TValue lss, - int32_t *app_apairs_out, int32_t *app_cpairs_out, - int32_t *res_apairs_out, int32_t *res_cpairs_out) + int32_t *app_apairs_out, int32_t *app_cpairs_out, + int32_t *res_apairs_out, int32_t *res_cpairs_out) { /* avoid warnings (shouldn't happen if _No_return was used in throw) */ *app_apairs_out = 0; @@ -1363,52 +1363,52 @@ void map_for_each_get_metrics(klisp_State *K, TValue lss, int32_t res_apairs = res_pairs - res_cpairs; if (res_cpairs == 0) { - /* finite list of length res_pairs (all lists should - have the same structure: acyclic with same length) */ - int32_t pairs = app_pairs - 1; - TValue tail = kcdr(lss); - while(pairs--) { - int32_t first_pairs, first_cpairs; - check_list(K, true, kcar(tail), &first_pairs, &first_cpairs); - tail = kcdr(tail); - - if (first_cpairs != 0) { - klispE_throw_simple(K, "mixed finite and infinite lists"); - return; - } else if (first_pairs != res_pairs) { - klispE_throw_simple(K, "lists of different length"); - return; - } - } + /* finite list of length res_pairs (all lists should + have the same structure: acyclic with same length) */ + int32_t pairs = app_pairs - 1; + TValue tail = kcdr(lss); + while(pairs--) { + int32_t first_pairs, first_cpairs; + check_list(K, true, kcar(tail), &first_pairs, &first_cpairs); + tail = kcdr(tail); + + if (first_cpairs != 0) { + klispE_throw_simple(K, "mixed finite and infinite lists"); + return; + } else if (first_pairs != res_pairs) { + klispE_throw_simple(K, "lists of different length"); + return; + } + } } else { - /* cyclic list: all lists should be cyclic. - result will have acyclic length equal to the - max of all the lists and cyclic length equal to the lcm - of all the lists. res_pairs may be broken but will be - restored by after the loop */ - int32_t pairs = app_pairs - 1; - TValue tail = kcdr(lss); - while(pairs--) { - int32_t first_pairs, first_cpairs; - check_list(K, true, kcar(tail), &first_pairs, &first_cpairs); - int32_t first_apairs = first_pairs - first_cpairs; - tail = kcdr(tail); - - if (first_cpairs == 0) { - klispE_throw_simple(K, "mixed finite and infinite lists"); - return; - } - res_apairs = kmax32(res_apairs, first_apairs); - /* this can throw an error if res_cpairs doesn't - fit in 32 bits, which is a reasonable implementation - restriction because the list wouldn't fit in memory - anyways */ - res_cpairs = kcheck32(K, "map/for-each: result list is too big", - klcm32_64(res_cpairs, first_cpairs)); - } - res_pairs = kcheck32(K, "map/for-each: result list is too big", - (int64_t) res_cpairs + (int64_t) res_apairs); - UNUSED(res_pairs); + /* cyclic list: all lists should be cyclic. + result will have acyclic length equal to the + max of all the lists and cyclic length equal to the lcm + of all the lists. res_pairs may be broken but will be + restored by after the loop */ + int32_t pairs = app_pairs - 1; + TValue tail = kcdr(lss); + while(pairs--) { + int32_t first_pairs, first_cpairs; + check_list(K, true, kcar(tail), &first_pairs, &first_cpairs); + int32_t first_apairs = first_pairs - first_cpairs; + tail = kcdr(tail); + + if (first_cpairs == 0) { + klispE_throw_simple(K, "mixed finite and infinite lists"); + return; + } + res_apairs = kmax32(res_apairs, first_apairs); + /* this can throw an error if res_cpairs doesn't + fit in 32 bits, which is a reasonable implementation + restriction because the list wouldn't fit in memory + anyways */ + res_cpairs = kcheck32(K, "map/for-each: result list is too big", + klcm32_64(res_cpairs, first_cpairs)); + } + res_pairs = kcheck32(K, "map/for-each: result list is too big", + (int64_t) res_cpairs + (int64_t) res_apairs); + UNUSED(res_pairs); } *app_apairs_out = app_apairs; @@ -1422,7 +1422,7 @@ void map_for_each_get_metrics(klisp_State *K, TValue lss, /* GC: assumes lss is rooted */ TValue map_for_each_get_cars_cdrs(klisp_State *K, TValue *lss, - int32_t apairs, int32_t cpairs) + int32_t apairs, int32_t cpairs) { TValue tail = *lss; @@ -1437,45 +1437,45 @@ TValue map_for_each_get_cars_cdrs(klisp_State *K, TValue *lss, TValue lap_cdrs = lp_cdrs; while(apairs != 0 || cpairs != 0) { - int32_t pairs; - if (apairs != 0) { - pairs = apairs; - } else { - /* remember last acyclic pair of both lists to to encycle! later */ - lap_cars = lp_cars; - lap_cdrs = lp_cdrs; - pairs = cpairs; - } - - while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); + int32_t pairs; + if (apairs != 0) { + pairs = apairs; + } else { + /* remember last acyclic pair of both lists to to encycle! later */ + lap_cars = lp_cars; + lap_cdrs = lp_cdrs; + pairs = cpairs; + } + + while(pairs--) { + TValue first = kcar(tail); + tail = kcdr(tail); - /* accumulate both cars and cdrs */ - TValue np; - np = kcons(K, kcar(first), KNIL); - kset_cdr(lp_cars, np); - lp_cars = np; - - np = kcons(K, kcdr(first), KNIL); - kset_cdr(lp_cdrs, np); - lp_cdrs = np; - } - - if (apairs != 0) { - apairs = 0; - } else { - cpairs = 0; - /* encycle! the list of cars and the list of cdrs */ - TValue fcp, lcp; - fcp = kcdr(lap_cars); - lcp = lp_cars; - kset_cdr(lcp, fcp); - - fcp = kcdr(lap_cdrs); - lcp = lp_cdrs; - kset_cdr(lcp, fcp); - } + /* accumulate both cars and cdrs */ + TValue np; + np = kcons(K, kcar(first), KNIL); + kset_cdr(lp_cars, np); + lp_cars = np; + + np = kcons(K, kcdr(first), KNIL); + kset_cdr(lp_cdrs, np); + lp_cdrs = np; + } + + if (apairs != 0) { + apairs = 0; + } else { + cpairs = 0; + /* encycle! the list of cars and the list of cdrs */ + TValue fcp, lcp; + fcp = kcdr(lap_cars); + lcp = lp_cars; + kset_cdr(lcp, fcp); + + fcp = kcdr(lap_cdrs); + lcp = lp_cdrs; + kset_cdr(lcp, fcp); + } } krooted_vars_pop(K); @@ -1490,8 +1490,8 @@ TValue map_for_each_get_cars_cdrs(klisp_State *K, TValue *lss, /* GC: assumes lss is rooted */ TValue map_for_each_transpose(klisp_State *K, TValue lss, - int32_t app_apairs, int32_t app_cpairs, - int32_t res_apairs, int32_t res_cpairs) + int32_t app_apairs, int32_t app_cpairs, + int32_t res_apairs, int32_t res_cpairs) { TValue tlist = kcons(K, KNIL, KNIL); krooted_vars_push(K, &tlist); @@ -1510,33 +1510,33 @@ TValue map_for_each_transpose(klisp_State *K, TValue lss, a list of cdrs, accumulate the list of cars and loop with the list of cdrs as the new list of lists (lss) */ while(res_apairs != 0 || res_cpairs != 0) { - int32_t pairs; + int32_t pairs; - if (res_apairs != 0) { - pairs = res_apairs; - } else { - pairs = res_cpairs; - /* remember last acyclic pair to encycle! later */ - lap = lp; - } - - while(pairs--) { - /* accumulate cars and replace tail with cdrs */ - cars = map_for_each_get_cars_cdrs(K, &tail, app_apairs, app_cpairs); - TValue np = kcons(K, cars, KNIL); - kset_cdr(lp, np); - lp = np; - } - - if (res_apairs != 0) { - res_apairs = 0; - } else { - res_cpairs = 0; - /* encycle! the list of list of cars */ - TValue fcp = kcdr(lap); - TValue lcp = lp; - kset_cdr(lcp, fcp); - } + if (res_apairs != 0) { + pairs = res_apairs; + } else { + pairs = res_cpairs; + /* remember last acyclic pair to encycle! later */ + lap = lp; + } + + while(pairs--) { + /* accumulate cars and replace tail with cdrs */ + cars = map_for_each_get_cars_cdrs(K, &tail, app_apairs, app_cpairs); + TValue np = kcons(K, cars, KNIL); + kset_cdr(lp, np); + lp = np; + } + + if (res_apairs != 0) { + res_apairs = 0; + } else { + res_cpairs = 0; + /* encycle! the list of list of cars */ + TValue fcp = kcdr(lap); + TValue lcp = lp; + kset_cdr(lcp, fcp); + } } krooted_vars_pop(K); @@ -1567,13 +1567,13 @@ void do_seq(klisp_State *K) TValue denv = xparams[1]; if (ttispair(tail)) { - TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, tail, - denv); - kset_cc(K, new_cont); + TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, tail, + denv); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, ls)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, ls)); #endif } ktail_eval(K, first, denv); @@ -1620,7 +1620,7 @@ void do_bind(klisp_State *K) ** xparams[0]: dynamic key */ bind_2tp(K, ptree, "any", anytype, obj, - "combiner", ttiscombiner, comb); + "combiner", ttiscombiner, comb); UNUSED(denv); /* the combiner is called in an empty environment */ TValue key = xparams[0]; /* GC: root intermediate objs */ @@ -1632,14 +1632,14 @@ void do_bind(klisp_State *K) kset_car(key, new_flag); kset_cdr(key, new_value); /* Old value must be protected from GC. It is no longer - reachable through key and not yet reachable through - continuation xparams. Boolean flag needn't be rooted, - because is not heap-allocated. */ + reachable through key and not yet reachable through + continuation xparams. Boolean flag needn't be rooted, + because is not heap-allocated. */ krooted_tvs_push(K, old_value); /* create a continuation to set the var to the correct value/flag on both - normal return and abnormal passes */ + normal return and abnormal passes */ TValue new_cont = make_bind_continuation(K, key, old_flag, old_value, - new_flag, new_value); + new_flag, new_value); krooted_tvs_pop(K); kset_cc(K, new_cont); /* implicit rooting */ TValue env = kmake_empty_environment(K); @@ -1647,7 +1647,7 @@ void do_bind(klisp_State *K) TValue expr = kcons(K, comb, KNIL); krooted_tvs_pop(K); ktail_eval(K, expr, env) -} + } /* accesor returned */ void do_access(klisp_State *K) @@ -1664,10 +1664,10 @@ void do_access(klisp_State *K) TValue key = xparams[0]; if (kis_true(kcar(key))) { - kapply_cc(K, kcdr(key)); + kapply_cc(K, kcdr(key)); } else { - klispE_throw_simple(K, "variable is unbound"); - return; + klispE_throw_simple(K, "variable is unbound"); + return; } } @@ -1728,17 +1728,17 @@ void do_set_pass(klisp_State *K) /* GC: this assumes that key, old_value and new_value are rooted */ TValue make_bind_continuation(klisp_State *K, TValue key, - TValue old_flag, TValue old_value, - TValue new_flag, TValue new_value) + TValue old_flag, TValue old_value, + TValue new_flag, TValue new_value) { TValue unbind_cont = kmake_continuation(K, kget_cc(K), - do_unbind, 3, key, old_flag, - old_value); + do_unbind, 3, key, old_flag, + old_value); krooted_tvs_push(K, unbind_cont); /* create the guards to guarantee that the values remain consistent on abnormal passes (in both directions) */ TValue exit_int = kmake_operative(K, do_set_pass, - 3, key, old_flag, old_value); + 3, key, old_flag, old_value); krooted_tvs_push(K, exit_int); TValue exit_guard = kcons(K, K->root_cont, exit_int); krooted_tvs_pop(K); /* already rooted in guard */ @@ -1748,7 +1748,7 @@ TValue make_bind_continuation(klisp_State *K, TValue key, krooted_tvs_push(K, exit_guards); TValue entry_int = kmake_operative(K, do_set_pass, - 3, key, new_flag, new_value); + 3, key, new_flag, new_value); krooted_tvs_push(K, entry_int); TValue entry_guard = kcons(K, K->root_cont, entry_int); krooted_tvs_pop(K); /* already rooted in guard */ @@ -1763,11 +1763,11 @@ TValue make_bind_continuation(klisp_State *K, TValue key, TValue env = kmake_empty_environment(K); krooted_tvs_push(K, env); TValue outer_cont = kmake_continuation(K, unbind_cont, - do_pass_value, 2, entry_guards, env); + do_pass_value, 2, entry_guards, env); kset_outer_cont(outer_cont); krooted_tvs_push(K, outer_cont); TValue inner_cont = kmake_continuation(K, outer_cont, - do_pass_value, 2, exit_guards, env); + do_pass_value, 2, exit_guards, env); kset_inner_cont(inner_cont); /* unbind_cont & 2 guard_lists */ @@ -1780,39 +1780,39 @@ TValue make_bind_continuation(klisp_State *K, TValue key, /* Helpers for guard-continuation (& guard-dynamic-extent) */ -#define singly_wrapped(obj_) (ttisapplicative(obj_) && \ - ttisoperative(kunwrap(obj_))) +#define singly_wrapped(obj_) (ttisapplicative(obj_) && \ + ttisoperative(kunwrap(obj_))) /* this unmarks root before throwing any error */ /* TODO: this isn't very clean, refactor */ /* GC: assumes obj & root are rooted */ inline TValue check_copy_single_entry(klisp_State *K, char *name, - TValue obj, TValue root) + TValue obj, TValue root) { if (!ttispair(obj) || !ttispair(kcdr(obj)) || !ttisnil(kcddr(obj))) { - unmark_list(K, root); - klispE_throw_simple(K, "Bad entry (expected list of length 2)"); - return KINERT; + unmark_list(K, root); + klispE_throw_simple(K, "Bad entry (expected list of length 2)"); + return KINERT; } TValue cont = kcar(obj); TValue app = kcadr(obj); if (!ttiscontinuation(cont)) { - unmark_list(K, root); - klispE_throw_simple(K, "Bad type on first element (expected " - "continuation)"); - return KINERT; + unmark_list(K, root); + klispE_throw_simple(K, "Bad type on first element (expected " + "continuation)"); + return KINERT; } else if (!singly_wrapped(app)) { - unmark_list(K, root); - klispE_throw_simple(K, "Bad type on second element (expected " - "singly wrapped applicative)"); - return KINERT; + unmark_list(K, root); + klispE_throw_simple(K, "Bad type on second element (expected " + "singly wrapped applicative)"); + return KINERT; } /* save the operative directly, don't waste space/time - with a list, use just a pair */ + with a list, use just a pair */ return kcons(K, cont, kunwrap(app)); } @@ -1822,34 +1822,34 @@ inline TValue check_copy_single_entry(klisp_State *K, char *name, TValue check_copy_guards(klisp_State *K, char *name, TValue obj) { if (ttisnil(obj)) { - return obj; + return obj; } else { - TValue copy = kcons(K, KNIL, KNIL); - krooted_vars_push(K, &copy); - TValue last_pair = copy; - TValue tail = obj; + TValue copy = kcons(K, KNIL, KNIL); + krooted_vars_push(K, &copy); + TValue last_pair = copy; + TValue tail = obj; - while(ttispair(tail) && !kis_marked(tail)) { - /* this will clear the marks and throw an error if the structure - is incorrect */ - TValue entry = check_copy_single_entry(K, name, kcar(tail), obj); - krooted_tvs_push(K, entry); - TValue new_pair = kcons(K, entry, KNIL); - krooted_tvs_pop(K); - kmark(tail); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - tail = kcdr(tail); - } - - /* dont close the cycle (if there is one) */ - unmark_list(K, obj); - if (!ttispair(tail) && !ttisnil(tail)) { - klispE_throw_simple(K, "expected list"); - return KINERT; - } - krooted_vars_pop(K); - return kcdr(copy); + while(ttispair(tail) && !kis_marked(tail)) { + /* this will clear the marks and throw an error if the structure + is incorrect */ + TValue entry = check_copy_single_entry(K, name, kcar(tail), obj); + krooted_tvs_push(K, entry); + TValue new_pair = kcons(K, entry, KNIL); + krooted_tvs_pop(K); + kmark(tail); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + tail = kcdr(tail); + } + + /* dont close the cycle (if there is one) */ + unmark_list(K, obj); + if (!ttispair(tail) && !ttisnil(tail)) { + klispE_throw_simple(K, "expected list"); + return KINERT; + } + krooted_vars_pop(K); + return kcdr(copy); } } @@ -1862,28 +1862,28 @@ void guard_dynamic_extent(klisp_State *K) UNUSED(xparams); bind_3tp(K, ptree, "any", anytype, entry_guards, - "combiner", ttiscombiner, comb, - "any", anytype, exit_guards); + "combiner", ttiscombiner, comb, + "any", anytype, exit_guards); entry_guards = check_copy_guards(K, "guard-dynamic-extent: entry guards", - entry_guards); + entry_guards); krooted_tvs_push(K, entry_guards); exit_guards = check_copy_guards(K, "guard-dynamic-extent: exit guards", - exit_guards); + exit_guards); krooted_tvs_push(K, exit_guards); /* GC: root continuations */ /* The current continuation is guarded */ TValue outer_cont = kmake_continuation(K, kget_cc(K), do_pass_value, - 2, entry_guards, denv); + 2, entry_guards, denv); kset_outer_cont(outer_cont); kset_cc(K, outer_cont); /* this implicitly roots outer_cont */ TValue inner_cont = kmake_continuation(K, outer_cont, do_pass_value, 2, - exit_guards, denv); + exit_guards, denv); kset_inner_cont(inner_cont); /* call combiner with no operands in the dynamic extent of inner, - with the dynamic env of this call */ + with the dynamic env of this call */ kset_cc(K, inner_cont); /* this implicitly roots inner_cont */ TValue expr = kcons(K, comb, KNIL); diff --git a/src/kghelpers.h b/src/kghelpers.h @@ -77,21 +77,21 @@ bool knegativep(TValue n); inline bool kfast_zerop(TValue n) { return (ttisfixint(n) && ivalue(n) == 0) || - (ttisdouble(n) && dvalue(n) == 0.0); + (ttisdouble(n) && dvalue(n) == 0.0); } inline bool kfast_onep(TValue n) { return (ttisfixint(n) && ivalue(n) == 1) || - (ttisdouble(n) && dvalue(n) == 1.0); + (ttisdouble(n) && dvalue(n) == 1.0); } inline TValue kneg_inf(TValue i) { if (ttiseinf(i)) - return tv_equal(i, KEPINF)? KEMINF : KEPINF; + return tv_equal(i, KEPINF)? KEMINF : KEPINF; else /* ttisiinf(i) */ - return tv_equal(i, KIPINF)? KIMINF : KIPINF; + return tv_equal(i, KIPINF)? KIMINF : KIPINF; } inline bool knum_same_signp(klisp_State *K, TValue n1, TValue n2) @@ -112,162 +112,162 @@ inline bool knum_same_signp(klisp_State *K, TValue n1, TValue n2) (the same with check_0tp and check_al1tp) add a number param and use an array of strings for msgs */ -#define check_0p(K_, ptree_) \ - if (!ttisnil(ptree_)) { \ - klispE_throw_simple((K_), \ - "Bad ptree (expected no arguments)"); \ - return; \ +#define check_0p(K_, ptree_) \ + if (!ttisnil(ptree_)) { \ + klispE_throw_simple((K_), \ + "Bad ptree (expected no arguments)"); \ + return; \ } -#define bind_1p(K_, ptree_, v_) \ +#define bind_1p(K_, ptree_, v_) \ bind_1tp((K_), (ptree_), "any", anytype, (v_)) -#define bind_1tp(K_, ptree_, tstr_, t_, v_) \ - TValue v_; \ - if (!ttispair(ptree_) || !ttisnil(kcdr(ptree_))) { \ - klispE_throw_simple((K_), \ - "Bad ptree (expected one argument)"); \ - return; \ - } \ - v_ = kcar(ptree_); \ - if (!t_(v_)) { \ - klispE_throw_simple(K_, "Bad type on first argument " \ - "(expected " tstr_ ")"); \ - return; \ +#define bind_1tp(K_, ptree_, tstr_, t_, v_) \ + TValue v_; \ + if (!ttispair(ptree_) || !ttisnil(kcdr(ptree_))) { \ + klispE_throw_simple((K_), \ + "Bad ptree (expected one argument)"); \ + return; \ + } \ + v_ = kcar(ptree_); \ + if (!t_(v_)) { \ + klispE_throw_simple(K_, "Bad type on first argument " \ + "(expected " tstr_ ")"); \ + return; \ } -#define bind_2p(K_, ptree_, v1_, v2_) \ +#define bind_2p(K_, ptree_, v1_, v2_) \ bind_2tp((K_), (ptree_), "any", anytype, (v1_), \ - "any", anytype, (v2_)) - -#define bind_2tp(K_, ptree_, tstr1_, t1_, v1_, \ - tstr2_, t2_, v2_) \ - TValue v1_, v2_; \ - if (!ttispair(ptree_) || !ttispair(kcdr(ptree_)) || \ - !ttisnil(kcddr(ptree_))) { \ - klispE_throw_simple(K_, "Bad ptree (expected two arguments)"); \ - return; \ - } \ - v1_ = kcar(ptree_); \ - v2_ = kcadr(ptree_); \ - if (!t1_(v1_)) { \ - klispE_throw_simple(K_, "Bad type on first argument (expected " \ - tstr1_ ")"); \ - return; \ - } else if (!t2_(v2_)) { \ - klispE_throw_simple(K_, "Bad type on second argument (expected " \ - tstr2_ ")"); \ - return; \ + "any", anytype, (v2_)) + +#define bind_2tp(K_, ptree_, tstr1_, t1_, v1_, \ + tstr2_, t2_, v2_) \ + TValue v1_, v2_; \ + if (!ttispair(ptree_) || !ttispair(kcdr(ptree_)) || \ + !ttisnil(kcddr(ptree_))) { \ + klispE_throw_simple(K_, "Bad ptree (expected two arguments)"); \ + return; \ + } \ + v1_ = kcar(ptree_); \ + v2_ = kcadr(ptree_); \ + if (!t1_(v1_)) { \ + klispE_throw_simple(K_, "Bad type on first argument (expected " \ + tstr1_ ")"); \ + return; \ + } else if (!t2_(v2_)) { \ + klispE_throw_simple(K_, "Bad type on second argument (expected " \ + tstr2_ ")"); \ + return; \ } -#define bind_3p(K_, ptree_, v1_, v2_, v3_) \ - bind_3tp(K_, ptree_, "any", anytype, v1_, \ - "any", anytype, v2_, "any", anytype, v3_) - -#define bind_3tp(K_, ptree_, tstr1_, t1_, v1_, \ - tstr2_, t2_, v2_, tstr3_, t3_, v3_) \ - TValue v1_, v2_, v3_; \ - if (!ttispair(ptree_) || !ttispair(kcdr(ptree_)) || \ - !ttispair(kcddr (ptree_)) || !ttisnil(kcdddr(ptree_))) { \ - klispE_throw_simple(K_, "Bad ptree (expected three arguments)"); \ - return; \ - } \ - v1_ = kcar(ptree_); \ - v2_ = kcadr(ptree_); \ - v3_ = kcaddr(ptree_); \ - if (!t1_(v1_)) { \ - klispE_throw_simple(K_, "Bad type on first argument (expected " \ - tstr1_ ")"); \ - return; \ - } else if (!t2_(v2_)) { \ - klispE_throw_simple(K_, "Bad type on second argument (expected " \ - tstr2_ ")"); \ - return; \ - } else if (!t3_(v3_)) { \ - klispE_throw_simple(K_, "Bad type on third argument (expected " \ - tstr3_ ")"); \ - return; \ +#define bind_3p(K_, ptree_, v1_, v2_, v3_) \ + bind_3tp(K_, ptree_, "any", anytype, v1_, \ + "any", anytype, v2_, "any", anytype, v3_) + +#define bind_3tp(K_, ptree_, tstr1_, t1_, v1_, \ + tstr2_, t2_, v2_, tstr3_, t3_, v3_) \ + TValue v1_, v2_, v3_; \ + if (!ttispair(ptree_) || !ttispair(kcdr(ptree_)) || \ + !ttispair(kcddr (ptree_)) || !ttisnil(kcdddr(ptree_))) { \ + klispE_throw_simple(K_, "Bad ptree (expected three arguments)"); \ + return; \ + } \ + v1_ = kcar(ptree_); \ + v2_ = kcadr(ptree_); \ + v3_ = kcaddr(ptree_); \ + if (!t1_(v1_)) { \ + klispE_throw_simple(K_, "Bad type on first argument (expected " \ + tstr1_ ")"); \ + return; \ + } else if (!t2_(v2_)) { \ + klispE_throw_simple(K_, "Bad type on second argument (expected " \ + tstr2_ ")"); \ + return; \ + } else if (!t3_(v3_)) { \ + klispE_throw_simple(K_, "Bad type on third argument (expected " \ + tstr3_ ")"); \ + return; \ } /* bind at least 1 parameter, like (v1_ . v2_) */ -#define bind_al1p(K_, ptree_, v1_, v2_) \ +#define bind_al1p(K_, ptree_, v1_, v2_) \ bind_al1tp((K_), (ptree_), "any", anytype, (v1_), (v2_)) /* bind at least 1 parameters (with type), like (v1_ . v2_) */ -#define bind_al1tp(K_, ptree_, tstr1_, t1_, v1_, v2_) \ - TValue v1_, v2_; \ - if (!ttispair(ptree_)) { \ - klispE_throw_simple(K_, "Bad ptree (expected at least " \ - "one argument)"); \ - return; \ - } \ - v1_ = kcar(ptree_); \ - v2_ = kcdr(ptree_); \ - if (!t1_(v1_)) { \ - klispE_throw_simple(K_, "Bad type on first argument (expected " \ - tstr1_ ")"); \ - return; \ +#define bind_al1tp(K_, ptree_, tstr1_, t1_, v1_, v2_) \ + TValue v1_, v2_; \ + if (!ttispair(ptree_)) { \ + klispE_throw_simple(K_, "Bad ptree (expected at least " \ + "one argument)"); \ + return; \ + } \ + v1_ = kcar(ptree_); \ + v2_ = kcdr(ptree_); \ + if (!t1_(v1_)) { \ + klispE_throw_simple(K_, "Bad type on first argument (expected " \ + tstr1_ ")"); \ + return; \ } /* bind at least 2 parameters, like (v1_ v2_ . v3_) */ -#define bind_al2p(K_, ptree_, v1_, v2_, v3_) \ +#define bind_al2p(K_, ptree_, v1_, v2_, v3_) \ bind_al2tp((K_), (ptree_), "any", anytype, (v1_), \ - "any", anytype, (v2_), (v3_)) + "any", anytype, (v2_), (v3_)) /* bind at least 2 parameters (with type), like (v1_ v2_ . v3_) */ -#define bind_al2tp(K_, ptree_, tstr1_, t1_, v1_, \ - tstr2_, t2_, v2_, v3_) \ - TValue v1_, v2_, v3_; \ - if (!ttispair(ptree_) || !ttispair(kcdr(ptree_))) { \ - klispE_throw_simple(K_, "Bad ptree (expected at least " \ - "two arguments)"); \ - return; \ - } \ - v1_ = kcar(ptree_); \ - v2_ = kcadr(ptree_); \ - v3_ = kcddr(ptree_); \ - if (!t1_(v1_)) { \ - klispE_throw_simple(K_, "Bad type on first argument (expected " \ - tstr1_ ")"); \ - return; \ - } else if (!t2_(v2_)) { \ - klispE_throw_simple(K_, "Bad type on second argument (expected " \ - tstr2_ ")"); \ - return; \ +#define bind_al2tp(K_, ptree_, tstr1_, t1_, v1_, \ + tstr2_, t2_, v2_, v3_) \ + TValue v1_, v2_, v3_; \ + if (!ttispair(ptree_) || !ttispair(kcdr(ptree_))) { \ + klispE_throw_simple(K_, "Bad ptree (expected at least " \ + "two arguments)"); \ + return; \ + } \ + v1_ = kcar(ptree_); \ + v2_ = kcadr(ptree_); \ + v3_ = kcddr(ptree_); \ + if (!t1_(v1_)) { \ + klispE_throw_simple(K_, "Bad type on first argument (expected " \ + tstr1_ ")"); \ + return; \ + } else if (!t2_(v2_)) { \ + klispE_throw_simple(K_, "Bad type on second argument (expected " \ + tstr2_ ")"); \ + return; \ } /* bind at least 3 parameters, like (v1_ v2_ v3_ . v4_) */ -#define bind_al3p(K_, ptree_, v1_, v2_, v3_, v4_) \ - bind_al3tp((K_), (ptree_), "any", anytype, (v1_), \ - "any", anytype, (v2_), "any", anytype, (v3_), (v4_)) \ +#define bind_al3p(K_, ptree_, v1_, v2_, v3_, v4_) \ + bind_al3tp((K_), (ptree_), "any", anytype, (v1_), \ + "any", anytype, (v2_), "any", anytype, (v3_), (v4_)) \ /* bind at least 3 parameters (with type), like (v1_ v2_ v3_ . v4_) */ -#define bind_al3tp(K_, ptree_, tstr1_, t1_, v1_, \ - tstr2_, t2_, v2_, tstr3_, t3_, v3_, v4_) \ - TValue v1_, v2_, v3_, v4_; \ - if (!ttispair(ptree_) || !ttispair(kcdr(ptree_)) || \ - !ttispair(kcddr(ptree_))) { \ - klispE_throw_simple(K_, "Bad ptree (expected at least " \ - "three arguments)"); \ - return; \ - } \ - v1_ = kcar(ptree_); \ - v2_ = kcadr(ptree_); \ - v3_ = kcaddr(ptree_); \ - v4_ = kcdddr(ptree_); \ - if (!t1_(v1_)) { \ - klispE_throw_simple(K_, "Bad type on first argument (expected " \ - tstr1_ ")"); \ - return; \ - } else if (!t2_(v2_)) { \ - klispE_throw_simple(K_, "Bad type on second argument (expected " \ - tstr2_ ")"); \ - return; \ - } else if (!t3_(v3_)) { \ - klispE_throw_simple(K_, "Bad type on third argument (expected " \ - tstr3_ ")"); \ - return; \ +#define bind_al3tp(K_, ptree_, tstr1_, t1_, v1_, \ + tstr2_, t2_, v2_, tstr3_, t3_, v3_, v4_) \ + TValue v1_, v2_, v3_, v4_; \ + if (!ttispair(ptree_) || !ttispair(kcdr(ptree_)) || \ + !ttispair(kcddr(ptree_))) { \ + klispE_throw_simple(K_, "Bad ptree (expected at least " \ + "three arguments)"); \ + return; \ + } \ + v1_ = kcar(ptree_); \ + v2_ = kcadr(ptree_); \ + v3_ = kcaddr(ptree_); \ + v4_ = kcdddr(ptree_); \ + if (!t1_(v1_)) { \ + klispE_throw_simple(K_, "Bad type on first argument (expected " \ + tstr1_ ")"); \ + return; \ + } else if (!t2_(v2_)) { \ + klispE_throw_simple(K_, "Bad type on second argument (expected " \ + tstr2_ ")"); \ + return; \ + } else if (!t3_(v3_)) { \ + klispE_throw_simple(K_, "Bad type on third argument (expected " \ + tstr3_ ")"); \ + return; \ } @@ -275,24 +275,24 @@ inline bool knum_same_signp(klisp_State *K, TValue n1, TValue n2) type type, and puts that element in par returns false if par is nil In any other case it throws an error */ -#define get_opt_tpar(K_, par_, tstr_, t_) ({ \ - bool res_; \ - if (ttisnil(par_)) { \ - res_ = false; \ - } else if (!ttispair(par_) || !ttisnil(kcdr(par_))) { \ - klispE_throw_simple((K_), \ - "Bad ptree structure " \ - "(in optional argument)"); \ - return; \ - } else if (!t_(kcar(par_))) { \ - klispE_throw_simple(K_, "Bad type on optional argument " \ - "(expected " tstr_ ")"); \ - return; \ - } else { \ - par_ = kcar(par_); \ - res_ = true; \ - } \ - res_; }) +#define get_opt_tpar(K_, par_, tstr_, t_) ({ \ + bool res_; \ + if (ttisnil(par_)) { \ + res_ = false; \ + } else if (!ttispair(par_) || !ttisnil(kcdr(par_))) { \ + klispE_throw_simple((K_), \ + "Bad ptree structure " \ + "(in optional argument)"); \ + return; \ + } else if (!t_(kcar(par_))) { \ + klispE_throw_simple(K_, "Bad type on optional argument " \ + "(expected " tstr_ ")"); \ + return; \ + } else { \ + par_ = kcar(par_); \ + res_ = true; \ + } \ + res_; }) /* ** This states are useful for traversing trees, saving the state in the @@ -311,8 +311,8 @@ inline void unmark_list(klisp_State *K, TValue obj) { UNUSED(K); /* not needed, it's here for consistency */ while(ttispair(obj) && kis_marked(obj)) { - kunmark(obj); - obj = kcdr(obj); + kunmark(obj); + obj = kcdr(obj); } } @@ -323,13 +323,13 @@ inline void unmark_tree(klisp_State *K, TValue obj) ks_spush(K, obj); while(!ks_sisempty(K)) { - obj = ks_spop(K); + obj = ks_spop(K); - if (ttispair(obj) && kis_marked(obj)) { - kunmark(obj); - ks_spush(K, kcdr(obj)); - ks_spush(K, kcar(obj)); - } else if (ttisvector(obj) && kis_marked(obj)) { + if (ttispair(obj) && kis_marked(obj)) { + kunmark(obj); + ks_spush(K, kcdr(obj)); + ks_spush(K, kcar(obj)); + } else if (ttisvector(obj) && kis_marked(obj)) { kunmark(obj); uint32_t i = kvector_size(obj); const TValue *array = kvector_buf(obj); @@ -347,20 +347,20 @@ inline void unmark_tree(klisp_State *K, TValue obj) KCHK_LS_FORCE_COPY, KCHK_ALLOW_CYCLE, KCHK_AVOID_ENCYCLE, etc) */ /* typed finite list. Structure error are thrown before type errors */ void check_typed_list(klisp_State *K, bool (*typep)(TValue), bool allow_infp, - TValue obj, int32_t *pairs, int32_t *cpairs); + TValue obj, int32_t *pairs, int32_t *cpairs); /* check that obj is a list, returns the number of pairs */ /* TODO change the return to void and add int32_t pairs obj */ void check_list(klisp_State *K, bool allow_infp, TValue obj, - int32_t *pairs, int32_t *cpairs); + int32_t *pairs, int32_t *cpairs); /* TODO: add unchecked_copy_list */ /* TODO: add check_copy_typed_list */ /* check that obj is a list and make a copy if it is not immutable or - force_copy is true */ + force_copy is true */ /* GC: assumes obj is rooted */ TValue check_copy_list(klisp_State *K, TValue obj, bool force_copy, - int32_t *pairs, int32_t *cpairs); + int32_t *pairs, int32_t *cpairs); /* check that obj is a list of environments and make a copy but don't keep the cycles */ @@ -427,8 +427,8 @@ void do_set_pass(klisp_State *K); /* dynamic var */ TValue make_bind_continuation(klisp_State *K, TValue key, - TValue old_flag, TValue old_value, - TValue new_flag, TValue new_value); + TValue old_flag, TValue old_value, + TValue new_flag, TValue new_value); TValue check_copy_guards(klisp_State *K, char *name, TValue obj); void guard_dynamic_extent(klisp_State *K); @@ -442,10 +442,10 @@ inline int32_t kmax32(int32_t a, int32_t b) { return a > b? a : b; } inline int32_t kcheck32(klisp_State *K, char *msg, int64_t i) { if (i > (int64_t) INT32_MAX || i < (int64_t) INT32_MIN) { - klispE_throw_simple(K, msg); - return 0; + klispE_throw_simple(K, msg); + return 0; } else { - return (int32_t) i; + return (int32_t) i; } } @@ -468,7 +468,7 @@ int32_t ksmallest_index(klisp_State *K, TValue obj, TValue tk); /* Helper for get-list-metrics, and list-tail, list-ref and list-set! when receiving bigint indexes */ void get_list_metrics_aux(klisp_State *K, TValue obj, int32_t *p, int32_t *n, - int32_t *a, int32_t *c); + int32_t *a, int32_t *c); /* Helper for eq? and equal? */ bool eq2p(klisp_State *K, TValue obj1, TValue obj2); @@ -497,7 +497,7 @@ void map_for_each_get_metrics( of cdrs (replacing the value of lss) */ /* GC: Assumes lss is rooted */ TValue map_for_each_get_cars_cdrs(klisp_State *K, TValue *lss, - int32_t apairs, int32_t cpairs); + int32_t apairs, int32_t cpairs); /* Transpose lss so that the result is a list of lists, each one having metrics (app_apairs, app_cpairs). The metrics of the returned list @@ -505,8 +505,8 @@ TValue map_for_each_get_cars_cdrs(klisp_State *K, TValue *lss, /* GC: Assumes lss is rooted */ TValue map_for_each_transpose(klisp_State *K, TValue lss, - int32_t app_apairs, int32_t app_cpairs, - int32_t res_apairs, int32_t res_cpairs); + int32_t app_apairs, int32_t app_cpairs, + int32_t res_apairs, int32_t res_cpairs); /* @@ -522,47 +522,47 @@ TValue map_for_each_transpose(klisp_State *K, TValue lss, /* TODO add si to the symbols */ #if KTRACK_SI -#define add_operative(K_, env_, n_, fn_, ...) \ - { symbol = ksymbol_new_b(K_, n_, KNIL); \ - value = kmake_operative(K_, fn_, __VA_ARGS__); \ - TValue str = kstring_new_b_imm(K_, __FILE__); \ - TValue si = kcons(K, str, kcons(K_, i2tv(__LINE__), \ - i2tv(0))); \ - kset_source_info(K_, value, si); \ - kadd_binding(K_, env_, symbol, value); } +#define add_operative(K_, env_, n_, fn_, ...) \ + { symbol = ksymbol_new_b(K_, n_, KNIL); \ + value = kmake_operative(K_, fn_, __VA_ARGS__); \ + TValue str = kstring_new_b_imm(K_, __FILE__); \ + TValue si = kcons(K, str, kcons(K_, i2tv(__LINE__), \ + i2tv(0))); \ + kset_source_info(K_, value, si); \ + kadd_binding(K_, env_, symbol, value); } #define add_applicative(K_, env_, n_, fn_, ...) \ - { symbol = ksymbol_new_b(K_, n_, KNIL); \ - value = kmake_applicative(K_, fn_, __VA_ARGS__); \ - TValue str = kstring_new_b_imm(K_, __FILE__); \ - TValue si = kcons(K, str, kcons(K_, i2tv(__LINE__), \ - i2tv(0))); \ - kset_source_info(K_, kunwrap(value), si); \ - kset_source_info(K_, value, si); \ - kadd_binding(K_, env_, symbol, value); } + { symbol = ksymbol_new_b(K_, n_, KNIL); \ + value = kmake_applicative(K_, fn_, __VA_ARGS__); \ + TValue str = kstring_new_b_imm(K_, __FILE__); \ + TValue si = kcons(K, str, kcons(K_, i2tv(__LINE__), \ + i2tv(0))); \ + kset_source_info(K_, kunwrap(value), si); \ + kset_source_info(K_, value, si); \ + kadd_binding(K_, env_, symbol, value); } #else /* KTRACK_SI */ -#define add_operative(K_, env_, n_, fn_, ...) \ - { symbol = ksymbol_new_b(K_, n_, KNIL); \ - value = kmake_operative(K_, fn_, __VA_ARGS__); \ - kadd_binding(K_, env_, symbol, value); } - -#define add_applicative(K_, env_, n_, fn_, ...) \ - { symbol = ksymbol_new_b(K_, n_, KNIL); \ - value = kmake_applicative(K_, fn_, __VA_ARGS__); \ - kadd_binding(K_, env_, symbol, value); } +#define add_operative(K_, env_, n_, fn_, ...) \ + { symbol = ksymbol_new_b(K_, n_, KNIL); \ + value = kmake_operative(K_, fn_, __VA_ARGS__); \ + kadd_binding(K_, env_, symbol, value); } + +#define add_applicative(K_, env_, n_, fn_, ...) \ + { symbol = ksymbol_new_b(K_, n_, KNIL); \ + value = kmake_applicative(K_, fn_, __VA_ARGS__); \ + kadd_binding(K_, env_, symbol, value); } #endif /* KTRACK_SI */ -#define add_value(K_, env_, n_, v_) \ - { value = v_; \ - symbol = ksymbol_new_b(K_, n_, KNIL); \ - kadd_binding(K_, env_, symbol, v_); } +#define add_value(K_, env_, n_, v_) \ + { value = v_; \ + symbol = ksymbol_new_b(K_, n_, KNIL); \ + kadd_binding(K_, env_, symbol, v_); } #endif /* for initiliazing continuation names */ #define add_cont_name(K_, t_, c_, n_) \ - { TValue str = kstring_new_b_imm(K_, n_); \ - TValue *node = klispH_set(K_, t_, p2tv(c_)); \ - *node = str; \ + { TValue str = kstring_new_b_imm(K_, n_); \ + TValue *node = klispH_set(K_, t_, p2tv(c_)); \ + *node = str; \ } diff --git a/src/kgkd_vars.c b/src/kgkd_vars.c @@ -64,5 +64,5 @@ void kinit_kgkd_vars_ground_env(klisp_State *K) /* 10.1.1 make-keyed-dynamic-variable */ add_applicative(K, ground_env, "make-keyed-dynamic-variable", - make_keyed_dynamic_variable, 0); + make_keyed_dynamic_variable, 0); } diff --git a/src/kgkeywords.c b/src/kgkeywords.c @@ -93,7 +93,7 @@ void kinit_keywords_ground_env(klisp_State *K) /* ?.? keyword? */ add_applicative(K, ground_env, "keyword?", typep, 2, symbol, - i2tv(K_TKEYWORD)); + i2tv(K_TKEYWORD)); /* ?.? keyword->string, string->keyword */ add_applicative(K, ground_env, "keyword->string", keyword_to_string, 0); add_applicative(K, ground_env, "string->keyword", string_to_keyword, 0); diff --git a/src/kgks_vars.c b/src/kgks_vars.c @@ -53,7 +53,7 @@ void do_sv_bind(klisp_State *K) ** xparams[0]: static key */ bind_2tp(K, ptree, "any", anytype, obj, - "environment", ttisenvironment, env); + "environment", ttisenvironment, env); UNUSED(denv); TValue key = xparams[0]; /* GC: all objs are rooted in ptree, or xparams */ @@ -95,5 +95,5 @@ void kinit_kgks_vars_ground_env(klisp_State *K) /* 11.1.1 make-keyed-static-variable */ add_applicative(K, ground_env, "make-keyed-static-variable", - make_keyed_static_variable, 0); + make_keyed_static_variable, 0); } diff --git a/src/kgnumbers.c b/src/kgnumbers.c @@ -66,14 +66,14 @@ inline int32_t min_ttype(TValue obj1, TValue obj2) } /* helper to make both arguments inexact if one of them is, - n1 & n2 should be variable names that may be overwritten */ + n1 & n2 should be variable names that may be overwritten */ /* GC: There is no problem because for now all inexact are stack allocated */ -#define kensure_same_exactness(K, n1, n2) \ +#define kensure_same_exactness(K, n1, n2) \ ({if (ttisinexact(n1) || ttisinexact(n2)) { \ - n1 = kexact_to_inexact(K, n1); \ - n2 = kexact_to_inexact(K, n2); \ - }}) + n1 = kexact_to_inexact(K, n1); \ + n2 = kexact_to_inexact(K, n2); \ + }}) /* ASK John: this isn't quite right I think. The problem is with implicit @@ -98,35 +98,35 @@ bool knum_eqp(klisp_State *K, TValue n1, TValue n2) switch(max_ttype(n1, n2)) { case K_TFIXINT: - return ivalue(n1) == ivalue(n2); + return ivalue(n1) == ivalue(n2); case K_TBIGINT: - if (min_ttype(n1, n2) != K_TBIGINT) { - /* NOTE: no fixint is =? to a bigint */ - return false; - } else { - /* both are bigints */ - return kbigint_eqp(n1, n2); - } + if (min_ttype(n1, n2) != K_TBIGINT) { + /* NOTE: no fixint is =? to a bigint */ + return false; + } else { + /* both are bigints */ + return kbigint_eqp(n1, n2); + } case K_TBIGRAT: - if (min_ttype(n1, n2) != K_TBIGRAT) { - /* NOTE: no fixint or bigint is =? to a bigrat */ - return false; - } else { - /* both are bigints */ - return kbigrat_eqp(K, n1, n2); - } + if (min_ttype(n1, n2) != K_TBIGRAT) { + /* NOTE: no fixint or bigint is =? to a bigrat */ + return false; + } else { + /* both are bigints */ + return kbigrat_eqp(K, n1, n2); + } case K_TEINF: - return (tv_equal(n1, n2)); + return (tv_equal(n1, n2)); case K_TDOUBLE: - return (tv_equal(n1, n2)); + return (tv_equal(n1, n2)); case K_TIINF: /* if the other was exact it was converted already */ - return (tv_equal(n1, n2)); + return (tv_equal(n1, n2)); case K_TRWNPV: case K_TUNDEFINED: /* no primary value, should throw an error */ - /* TEMP: this was already contemplated in type predicate */ + /* TEMP: this was already contemplated in type predicate */ default: - klispE_throw_simple(K, "unsupported type"); - return false; + klispE_throw_simple(K, "unsupported type"); + return false; } } @@ -137,32 +137,32 @@ bool knum_ltp(klisp_State *K, TValue n1, TValue n2) switch(max_ttype(n1, n2)) { case K_TFIXINT: - return ivalue(n1) < ivalue(n2); + return ivalue(n1) < ivalue(n2); case K_TBIGINT: { - kensure_bigint(n1); - kensure_bigint(n2); - return kbigint_ltp(n1, n2); + kensure_bigint(n1); + kensure_bigint(n2); + return kbigint_ltp(n1, n2); } case K_TBIGRAT: { - kensure_bigrat(n1); - kensure_bigrat(n2); - return kbigrat_ltp(K, n1, n2); + kensure_bigrat(n1); + kensure_bigrat(n2); + return kbigrat_ltp(K, n1, n2); } case K_TDOUBLE: /* both must be double, all inferior types - convert to either double or inexact infinity */ - return (dvalue(n1) < dvalue(n2)); + convert to either double or inexact infinity */ + return (dvalue(n1) < dvalue(n2)); case K_TEINF: - return !tv_equal(n1, n2) && (tv_equal(n1, KEMINF) || - tv_equal(n2, KEPINF)); + return !tv_equal(n1, n2) && (tv_equal(n1, KEMINF) || + tv_equal(n2, KEPINF)); case K_TIINF: /* if the other was exact it was converted already */ - return !tv_equal(n1, n2) && (tv_equal(n1, KIMINF) || - tv_equal(n2, KIPINF)); + return !tv_equal(n1, n2) && (tv_equal(n1, KIMINF) || + tv_equal(n2, KIPINF)); case K_TRWNPV: case K_TUNDEFINED: /* no primary value, should throw an error */ - /* TEMP: this was already contemplated in type predicate */ + /* TEMP: this was already contemplated in type predicate */ default: - klispE_throw_simple(K, "unsupported type"); - return false; + klispE_throw_simple(K, "unsupported type"); + return false; } } @@ -184,22 +184,22 @@ bool knum_gep(klisp_State *K, TValue n1, TValue n2) ** have a primary value */ /* may evaluate K & n more than once */ -#define arith_return(K, n) \ - ({ if (ttisnwnpv(n) && kcurr_strict_arithp(K)) { \ - klispE_throw_simple_with_irritants(K, "result has no " \ - "primary value", \ - 1, n); \ - return KINERT; \ - } else { return n;}}) +#define arith_return(K, n) \ + ({ if (ttisnwnpv(n) && kcurr_strict_arithp(K)) { \ + klispE_throw_simple_with_irritants(K, "result has no " \ + "primary value", \ + 1, n); \ + return KINERT; \ + } else { return n;}}) /* may evaluate K & n more than once */ -#define arith_kapply_cc(K, n) \ - ({ if (ttisnwnpv(n) && kcurr_strict_arithp(K)) { \ - klispE_throw_simple_with_irritants(K, "result has no " \ - "primary value", \ - 1, n); \ - return; \ - } else { kapply_cc(K, n); return;}}) +#define arith_kapply_cc(K, n) \ + ({ if (ttisnwnpv(n) && kcurr_strict_arithp(K)) { \ + klispE_throw_simple_with_irritants(K, "result has no " \ + "primary value", \ + 1, n); \ + return; \ + } else { kapply_cc(K, n); return;}}) @@ -214,68 +214,68 @@ TValue knum_plus(klisp_State *K, TValue n1, TValue n2) TValue res; /* used for results with no primary value */ switch(max_ttype(n1, n2)) { case K_TFIXINT: { - int64_t res = (int64_t) ivalue(n1) + (int64_t) ivalue(n2); - if (res >= (int64_t) INT32_MIN && - res <= (int64_t) INT32_MAX) { - return i2tv((int32_t) res); - } /* else fall through */ + int64_t res = (int64_t) ivalue(n1) + (int64_t) ivalue(n2); + if (res >= (int64_t) INT32_MIN && + res <= (int64_t) INT32_MAX) { + return i2tv((int32_t) res); + } /* else fall through */ } case K_TBIGINT: { - kensure_bigint(n1); - kensure_bigint(n2); - return kbigint_plus(K, n1, n2); + kensure_bigint(n1); + kensure_bigint(n2); + return kbigint_plus(K, n1, n2); } case K_TBIGRAT: { - kensure_bigrat(n1); - kensure_bigrat(n2); - return kbigrat_plus(K, n1, n2); + kensure_bigrat(n1); + kensure_bigrat(n2); + return kbigrat_plus(K, n1, n2); } case K_TDOUBLE: { - double res = dvalue(n1) + dvalue(n2); - /* check under & overflow */ - if (kcurr_strict_arithp(K)) { - if (res == 0 && dvalue(n1) != -dvalue(n2)) { - klispE_throw_simple(K, "underflow"); - return KINERT; - } else if (isinf(res)) { - klispE_throw_simple(K, "overflow"); - return KINERT; - } - } - /* correctly encapsulate infinities and -0.0 */ - return ktag_double(res); + double res = dvalue(n1) + dvalue(n2); + /* check under & overflow */ + if (kcurr_strict_arithp(K)) { + if (res == 0 && dvalue(n1) != -dvalue(n2)) { + klispE_throw_simple(K, "underflow"); + return KINERT; + } else if (isinf(res)) { + klispE_throw_simple(K, "overflow"); + return KINERT; + } + } + /* correctly encapsulate infinities and -0.0 */ + return ktag_double(res); } case K_TEINF: - if (!ttiseinf(n1)) - return n2; - else if (!ttiseinf(n2)) - return n1; - if (tv_equal(n1, n2)) - return n1; - else { /* no primary value; handle error at the end of function */ - res = KRWNPV; - break; - } + if (!ttiseinf(n1)) + return n2; + else if (!ttiseinf(n2)) + return n1; + if (tv_equal(n1, n2)) + return n1; + else { /* no primary value; handle error at the end of function */ + res = KRWNPV; + break; + } case K_TIINF: - if (!ttisiinf(n1)) - return n2; - else if (!ttisiinf(n2)) - return n1; - if (tv_equal(n1, n2)) - return n1; - else { /* no primary value; handle error at the end of function */ - res = KRWNPV; - break; - } + if (!ttisiinf(n1)) + return n2; + else if (!ttisiinf(n2)) + return n1; + if (tv_equal(n1, n2)) + return n1; + else { /* no primary value; handle error at the end of function */ + res = KRWNPV; + break; + } case K_TRWNPV: /* no primary value */ - res = KRWNPV; - break; + res = KRWNPV; + break; case K_TUNDEFINED: /* undefined */ - res = KUNDEF; - break; + res = KUNDEF; + break; default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } /* check for no primary value and value of strict arith */ @@ -290,68 +290,68 @@ TValue knum_times(klisp_State *K, TValue n1, TValue n2) TValue res; /* used for results with no primary value */ switch(max_ttype(n1, n2)) { case K_TFIXINT: { - int64_t res = (int64_t) ivalue(n1) * (int64_t) ivalue(n2); - if (res >= (int64_t) INT32_MIN && - res <= (int64_t) INT32_MAX) { - return i2tv((int32_t) res); - } /* else fall through */ + int64_t res = (int64_t) ivalue(n1) * (int64_t) ivalue(n2); + if (res >= (int64_t) INT32_MIN && + res <= (int64_t) INT32_MAX) { + return i2tv((int32_t) res); + } /* else fall through */ } case K_TBIGINT: { - kensure_bigint(n1); - kensure_bigint(n2); - return kbigint_times(K, n1, n2); + kensure_bigint(n1); + kensure_bigint(n2); + return kbigint_times(K, n1, n2); } case K_TBIGRAT: { - kensure_bigrat(n1); - kensure_bigrat(n2); - return kbigrat_times(K, n1, n2); + kensure_bigrat(n1); + kensure_bigrat(n2); + return kbigrat_times(K, n1, n2); } case K_TDOUBLE: { - double res = dvalue(n1) * dvalue(n2); - /* check under & overflow */ - if (kcurr_strict_arithp(K)) { - if (res == 0 && dvalue(n1) != 0.0 && dvalue(n2) != 0.00) { - klispE_throw_simple(K, "underflow"); - return KINERT; - } else if (isinf(res)) { - klispE_throw_simple(K, "overflow"); - return KINERT; - } - } - /* correctly encapsulate infinities and -0.0 */ - return ktag_double(res); + double res = dvalue(n1) * dvalue(n2); + /* check under & overflow */ + if (kcurr_strict_arithp(K)) { + if (res == 0 && dvalue(n1) != 0.0 && dvalue(n2) != 0.00) { + klispE_throw_simple(K, "underflow"); + return KINERT; + } else if (isinf(res)) { + klispE_throw_simple(K, "overflow"); + return KINERT; + } + } + /* correctly encapsulate infinities and -0.0 */ + return ktag_double(res); } case K_TEINF: - if (!ttiseinf(n1) || !ttiseinf(n2)) { - if (kfast_zerop(n1) || kfast_zerop(n2)) { - /* report: #e+infinity * 0 has no primary value */ - res = KRWNPV; - break; - } else if (ttisexact(n1) && ttisexact(n2)) - return knum_same_signp(K, n1, n2)? KEPINF : KEMINF; - else - return knum_same_signp(K, n1, n2)? KIPINF : KIMINF; - } else - return (tv_equal(n1, n2))? KEPINF : KEMINF; + if (!ttiseinf(n1) || !ttiseinf(n2)) { + if (kfast_zerop(n1) || kfast_zerop(n2)) { + /* report: #e+infinity * 0 has no primary value */ + res = KRWNPV; + break; + } else if (ttisexact(n1) && ttisexact(n2)) + return knum_same_signp(K, n1, n2)? KEPINF : KEMINF; + else + return knum_same_signp(K, n1, n2)? KIPINF : KIMINF; + } else + return (tv_equal(n1, n2))? KEPINF : KEMINF; case K_TIINF: - if (!ttisiinf(n1) || !ttisiinf(n2)) { - if (kfast_zerop(n1) || kfast_zerop(n2)) { - /* report: #i[+-]infinity * 0 has no primary value */ - res = KRWNPV; - break; - } else - return knum_same_signp(K, n1, n2)? KIPINF : KIMINF; - } else - return (tv_equal(n1, n2))? KIPINF : KIMINF; + if (!ttisiinf(n1) || !ttisiinf(n2)) { + if (kfast_zerop(n1) || kfast_zerop(n2)) { + /* report: #i[+-]infinity * 0 has no primary value */ + res = KRWNPV; + break; + } else + return knum_same_signp(K, n1, n2)? KIPINF : KIMINF; + } else + return (tv_equal(n1, n2))? KIPINF : KIMINF; case K_TRWNPV: - res = KRWNPV; - break; + res = KRWNPV; + break; case K_TUNDEFINED: - res = KUNDEF; - break; + res = KUNDEF; + break; default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } /* check for no primary value and value of strict arith */ @@ -367,69 +367,69 @@ TValue knum_minus(klisp_State *K, TValue n1, TValue n2) switch(max_ttype(n1, n2)) { case K_TFIXINT: { - int64_t res = (int64_t) ivalue(n1) - (int64_t) ivalue(n2); - if (res >= (int64_t) INT32_MIN && - res <= (int64_t) INT32_MAX) { - return i2tv((int32_t) res); - } /* else fall through */ + int64_t res = (int64_t) ivalue(n1) - (int64_t) ivalue(n2); + if (res >= (int64_t) INT32_MIN && + res <= (int64_t) INT32_MAX) { + return i2tv((int32_t) res); + } /* else fall through */ } case K_TBIGINT: { - kensure_bigint(n1); - kensure_bigint(n2); - return kbigint_minus(K, n1, n2); + kensure_bigint(n1); + kensure_bigint(n2); + return kbigint_minus(K, n1, n2); } case K_TBIGRAT: { - kensure_bigrat(n1); - kensure_bigrat(n2); - return kbigrat_minus(K, n1, n2); + kensure_bigrat(n1); + kensure_bigrat(n2); + return kbigrat_minus(K, n1, n2); } case K_TDOUBLE: { - /* both are double */ - double res = dvalue(n1) - dvalue(n2); - /* check under & overflow */ - if (kcurr_strict_arithp(K)) { - if (res == 0 && dvalue(n1) != dvalue(n2)) { - klispE_throw_simple(K, "underflow"); - return KINERT; - } else if (isinf(res)) { - klispE_throw_simple(K, "overflow"); - return KINERT; - } - } - /* correctly encapsulate infinities and -0.0 */ - return ktag_double(res); + /* both are double */ + double res = dvalue(n1) - dvalue(n2); + /* check under & overflow */ + if (kcurr_strict_arithp(K)) { + if (res == 0 && dvalue(n1) != dvalue(n2)) { + klispE_throw_simple(K, "underflow"); + return KINERT; + } else if (isinf(res)) { + klispE_throw_simple(K, "overflow"); + return KINERT; + } + } + /* correctly encapsulate infinities and -0.0 */ + return ktag_double(res); } case K_TEINF: - if (!ttiseinf(n1)) - return kneg_inf(n2); - else if (!ttiseinf(n2)) - return n1; - if (tv_equal(n1, n2)) { - /* no primary value; handle error at the end of function */ - res = KRWNPV; - break; - } else - return n1; + if (!ttiseinf(n1)) + return kneg_inf(n2); + else if (!ttiseinf(n2)) + return n1; + if (tv_equal(n1, n2)) { + /* no primary value; handle error at the end of function */ + res = KRWNPV; + break; + } else + return n1; case K_TIINF: - if (!ttisiinf(n1)) - return kneg_inf(n2); - else if (!ttisiinf(n2)) - return n1; - if (tv_equal(n1, n2)) { - /* no primary value; handle error at the end of function */ - res = KRWNPV; - break; - } else - return n1; + if (!ttisiinf(n1)) + return kneg_inf(n2); + else if (!ttisiinf(n2)) + return n1; + if (tv_equal(n1, n2)) { + /* no primary value; handle error at the end of function */ + res = KRWNPV; + break; + } else + return n1; case K_TRWNPV: /* no primary value */ - res = KRWNPV; - break; + res = KRWNPV; + break; case K_TUNDEFINED: /* undefined */ - res = KUNDEF; - break; + res = KUNDEF; + break; default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } /* check for no primary value and value of strict arith */ @@ -445,69 +445,69 @@ TValue knum_divided(klisp_State *K, TValue n1, TValue n2) /* first check the most common error, division by zero */ if (kfast_zerop(n2)) { - klispE_throw_simple(K, "division by zero"); - return KINERT; + klispE_throw_simple(K, "division by zero"); + return KINERT; } switch(max_ttype(n1, n2)) { case K_TFIXINT: { - int64_t res = (int64_t) ivalue(n1) / (int64_t) ivalue(n2); - int64_t rem = (int64_t) ivalue(n1) % (int64_t) ivalue(n2); - if (rem == 0 && res >= (int64_t) INT32_MIN && - res <= (int64_t) INT32_MAX) { - return i2tv((int32_t) res); - } /* else fall through */ + int64_t res = (int64_t) ivalue(n1) / (int64_t) ivalue(n2); + int64_t rem = (int64_t) ivalue(n1) % (int64_t) ivalue(n2); + if (rem == 0 && res >= (int64_t) INT32_MIN && + res <= (int64_t) INT32_MAX) { + return i2tv((int32_t) res); + } /* else fall through */ } case K_TBIGINT: /* just handle it as a rational */ case K_TBIGRAT: { - kensure_bigrat(n1); - kensure_bigrat(n2); - return kbigrat_divided(K, n1, n2); + kensure_bigrat(n1); + kensure_bigrat(n2); + return kbigrat_divided(K, n1, n2); } case K_TDOUBLE: { - double res = dvalue(n1) / dvalue(n2); - /* check under & overflow */ - if (kcurr_strict_arithp(K)) { - if (res == 0 && dvalue(n1) != 0.0) { - klispE_throw_simple(K, "underflow"); - return KINERT; - } else if (isinf(res)) { - klispE_throw_simple(K, "overflow"); - return KINERT; - } - } - /* correctly encapsulate infinities and -0.0 */ - return ktag_double(res); + double res = dvalue(n1) / dvalue(n2); + /* check under & overflow */ + if (kcurr_strict_arithp(K)) { + if (res == 0 && dvalue(n1) != 0.0) { + klispE_throw_simple(K, "underflow"); + return KINERT; + } else if (isinf(res)) { + klispE_throw_simple(K, "overflow"); + return KINERT; + } + } + /* correctly encapsulate infinities and -0.0 */ + return ktag_double(res); } case K_TEINF: { - if (ttiseinf(n1) && ttiseinf(n2)) { - klispE_throw_simple(K, "infinity divided by infinity"); - return KINERT; - } else if (ttiseinf(n1)) { - return knum_same_signp(K, n1, n2)? KEPINF : KEMINF; - } else { /* ttiseinf(n2) */ - return i2tv(0); - } + if (ttiseinf(n1) && ttiseinf(n2)) { + klispE_throw_simple(K, "infinity divided by infinity"); + return KINERT; + } else if (ttiseinf(n1)) { + return knum_same_signp(K, n1, n2)? KEPINF : KEMINF; + } else { /* ttiseinf(n2) */ + return i2tv(0); + } } case K_TIINF: - if (ttisiinf(n1) && ttisiinf(n2)) { - klispE_throw_simple(K, "infinity divided by infinity"); - return KINERT; - } else if (ttisiinf(n1)) { - return knum_same_signp(K, n1, n2)? KIPINF : KIMINF; - } else { /* ttiseinf(n2) */ - /* NOTE: I guess this doens't count as underflow */ - return d2tv(0.0); - } + if (ttisiinf(n1) && ttisiinf(n2)) { + klispE_throw_simple(K, "infinity divided by infinity"); + return KINERT; + } else if (ttisiinf(n1)) { + return knum_same_signp(K, n1, n2)? KIPINF : KIMINF; + } else { /* ttiseinf(n2) */ + /* NOTE: I guess this doens't count as underflow */ + return d2tv(0.0); + } case K_TRWNPV: - res = KRWNPV; - break; + res = KRWNPV; + break; case K_TUNDEFINED: - res = KUNDEF; - break; + res = KUNDEF; + break; default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } /* check for no primary value and value of strict arith */ @@ -519,37 +519,37 @@ TValue knum_abs(klisp_State *K, TValue n) { switch(ttype(n)) { case K_TFIXINT: { - int32_t i = ivalue(n); - if (i != INT32_MIN) - return (i < 0? i2tv(-i) : n); - /* if i == INT32_MIN, fall through */ - /* MAYBE: we could cache the bigint INT32_MAX+1 */ - /* else fall through */ + int32_t i = ivalue(n); + if (i != INT32_MIN) + return (i < 0? i2tv(-i) : n); + /* if i == INT32_MIN, fall through */ + /* MAYBE: we could cache the bigint INT32_MAX+1 */ + /* else fall through */ } case K_TBIGINT: { - /* this is needed for INT32_MIN, can't be in previous - case because it should be in the same block, remember - the bigint is allocated on the stack. */ - kensure_bigint(n); - return kbigint_abs(K, n); + /* this is needed for INT32_MIN, can't be in previous + case because it should be in the same block, remember + the bigint is allocated on the stack. */ + kensure_bigint(n); + return kbigint_abs(K, n); } case K_TBIGRAT: { - return kbigrat_abs(K, n); + return kbigrat_abs(K, n); } case K_TDOUBLE: { - return ktag_double(fabs(dvalue(n))); + return ktag_double(fabs(dvalue(n))); } case K_TEINF: - return KEPINF; + return KEPINF; case K_TIINF: - return KIPINF; + return KIPINF; case K_TRWNPV: - /* ASK John: is the error here okay */ - arith_return(K, KRWNPV); + /* ASK John: is the error here okay */ + arith_return(K, KRWNPV); default: - /* shouldn't happen */ - klispE_throw_simple(K, "unsupported type"); - return KINERT; + /* shouldn't happen */ + klispE_throw_simple(K, "unsupported type"); + return KINERT; } } @@ -564,48 +564,48 @@ TValue knum_gcd(klisp_State *K, TValue n1, TValue n2) switch(max_ttype(n1, n2)) { case K_TFIXINT: { - int64_t gcd = kgcd32_64(ivalue(n1), ivalue(n2)); + int64_t gcd = kgcd32_64(ivalue(n1), ivalue(n2)); /* May fail for gcd(INT32_MIN, INT32_MIN) because - it would return INT32_MAX+1 */ - if (kfit_int32_t(gcd)) - return i2tv((int32_t) gcd); - /* else fall through */ + it would return INT32_MAX+1 */ + if (kfit_int32_t(gcd)) + return i2tv((int32_t) gcd); + /* else fall through */ } case K_TBIGINT: { - kensure_bigint(n1); - kensure_bigint(n2); - return kbigint_gcd(K, n1, n2); + kensure_bigint(n1); + kensure_bigint(n2); + return kbigint_gcd(K, n1, n2); } case K_TDOUBLE: { - krooted_vars_push(K, &n1); - krooted_vars_push(K, &n2); - n1 = kinexact_to_exact(K, n1); - n2 = kinexact_to_exact(K, n2); - TValue res = knum_gcd(K, n1, n2); - krooted_tvs_push(K, res); - res = kexact_to_inexact(K, res); - krooted_tvs_pop(K); - krooted_vars_pop(K); - krooted_vars_pop(K); - return res; + krooted_vars_push(K, &n1); + krooted_vars_push(K, &n2); + n1 = kinexact_to_exact(K, n1); + n2 = kinexact_to_exact(K, n2); + TValue res = knum_gcd(K, n1, n2); + krooted_tvs_push(K, res); + res = kexact_to_inexact(K, res); + krooted_tvs_pop(K); + krooted_vars_pop(K); + krooted_vars_pop(K); + return res; } case K_TEINF: - if (kfast_zerop(n2) || !ttiseinf(n1)) - return knum_abs(K, n1); - else if (kfast_zerop(n1) || !ttiseinf(n2)) - return knum_abs(K, n2); - else - return KEPINF; + if (kfast_zerop(n2) || !ttiseinf(n1)) + return knum_abs(K, n1); + else if (kfast_zerop(n1) || !ttiseinf(n2)) + return knum_abs(K, n2); + else + return KEPINF; case K_TIINF: - if (kfast_zerop(n2) || !ttisiinf(n1)) - return knum_abs(K, n1); - else if (kfast_zerop(n1) || !ttisiinf(n2)) - return knum_abs(K, n2); - else - return KIPINF; + if (kfast_zerop(n2) || !ttisiinf(n1)) + return knum_abs(K, n1); + else if (kfast_zerop(n1) || !ttisiinf(n2)) + return knum_abs(K, n2); + else + return KIPINF; default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } } @@ -619,43 +619,43 @@ TValue knum_lcm(klisp_State *K, TValue n1, TValue n2) /* get this out of the way first */ if (kfast_zerop(n1) || kfast_zerop(n2)) { - arith_return(K, KRWNPV); + arith_return(K, KRWNPV); } switch(max_ttype(n1, n2)) { case K_TFIXINT: { - int64_t lcm = klcm32_64(ivalue(n1), ivalue(n2)); - /* May fail for lcm(INT32_MIN, 1) because - it would return INT32_MAX+1 */ - if (kfit_int32_t(lcm)) - return i2tv((int32_t) lcm); - /* else fall through */ + int64_t lcm = klcm32_64(ivalue(n1), ivalue(n2)); + /* May fail for lcm(INT32_MIN, 1) because + it would return INT32_MAX+1 */ + if (kfit_int32_t(lcm)) + return i2tv((int32_t) lcm); + /* else fall through */ } case K_TBIGINT: { - kensure_bigint(n1); - kensure_bigint(n2); - return kbigint_lcm(K, n1, n2); + kensure_bigint(n1); + kensure_bigint(n2); + return kbigint_lcm(K, n1, n2); } case K_TDOUBLE: { - krooted_vars_push(K, &n1); - krooted_vars_push(K, &n2); - n1 = kinexact_to_exact(K, n1); - n2 = kinexact_to_exact(K, n2); - TValue res = knum_lcm(K, n1, n2); - krooted_tvs_push(K, res); - res = kexact_to_inexact(K, res); - krooted_tvs_pop(K); - krooted_vars_pop(K); - krooted_vars_pop(K); - return res; + krooted_vars_push(K, &n1); + krooted_vars_push(K, &n2); + n1 = kinexact_to_exact(K, n1); + n2 = kinexact_to_exact(K, n2); + TValue res = knum_lcm(K, n1, n2); + krooted_tvs_push(K, res); + res = kexact_to_inexact(K, res); + krooted_tvs_pop(K); + krooted_vars_pop(K); + krooted_vars_pop(K); + return res; } case K_TEINF: - return KEPINF; + return KEPINF; case K_TIINF: - return KIPINF; + return KIPINF; default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } } @@ -665,21 +665,21 @@ TValue knum_numerator(klisp_State *K, TValue n) switch(ttype(n)) { case K_TFIXINT: case K_TBIGINT: - return n; + return n; case K_TBIGRAT: - return kbigrat_numerator(K, n); + return kbigrat_numerator(K, n); case K_TDOUBLE: { - TValue res = kinexact_to_exact(K, n); - krooted_vars_push(K, &res); - res = knum_numerator(K, res); - res = kexact_to_inexact(K, res); - krooted_vars_pop(K); - return res; + TValue res = kinexact_to_exact(K, n); + krooted_vars_push(K, &res); + res = knum_numerator(K, res); + res = kexact_to_inexact(K, res); + krooted_vars_pop(K); + return res; } /* case K_TEINF: infinities are not rational! */ default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } } @@ -689,21 +689,21 @@ TValue knum_denominator(klisp_State *K, TValue n) switch(ttype(n)) { case K_TFIXINT: case K_TBIGINT: - return i2tv(1); /* denominator of integer is always (+)1 */ + return i2tv(1); /* denominator of integer is always (+)1 */ case K_TBIGRAT: - return kbigrat_denominator(K, n); + return kbigrat_denominator(K, n); case K_TDOUBLE: { - TValue res = kinexact_to_exact(K, n); - krooted_vars_push(K, &res); - res = knum_denominator(K, res); - res = kexact_to_inexact(K, res); - krooted_vars_pop(K); - return res; + TValue res = kinexact_to_exact(K, n); + krooted_vars_push(K, &res); + res = knum_denominator(K, res); + res = kexact_to_inexact(K, res); + krooted_vars_pop(K); + return res; } /* case K_TEINF: infinities are not rational! */ default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } } @@ -713,24 +713,24 @@ TValue knum_real_to_integer(klisp_State *K, TValue n, kround_mode mode) switch(ttype(n)) { case K_TFIXINT: case K_TBIGINT: - return n; /* integers are easy */ + return n; /* integers are easy */ case K_TBIGRAT: - return kbigrat_to_integer(K, n, mode); + return kbigrat_to_integer(K, n, mode); case K_TDOUBLE: - return kdouble_to_integer(K, n, mode); + return kdouble_to_integer(K, n, mode); case K_TEINF: - klispE_throw_simple(K, "infinite value"); - return KINERT; + klispE_throw_simple(K, "infinite value"); + return KINERT; case K_TIINF: - klispE_throw_simple(K, "infinite value"); - return KINERT; + klispE_throw_simple(K, "infinite value"); + return KINERT; case K_TRWNPV: - arith_return(K, KRWNPV); + arith_return(K, KRWNPV); case K_TUNDEFINED: - /* undefined in not a real, shouldn't get here, fall through */ + /* undefined in not a real, shouldn't get here, fall through */ default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } } @@ -742,8 +742,8 @@ TValue knum_simplest_rational(klisp_State *K, TValue n1, TValue n2) /* first check that case that n1 > n2 */ if (knum_gtp(K, n1, n2)) { - klispE_throw_simple(K, "x0 doesn't exists (n1 > n2)"); - return KINERT; + klispE_throw_simple(K, "x0 doesn't exists (n1 > n2)"); + return KINERT; } /* we know that n1 <= n2 */ @@ -751,68 +751,68 @@ TValue knum_simplest_rational(klisp_State *K, TValue n1, TValue n2) case K_TFIXINT: case K_TBIGINT: /* for now do all with bigrat */ case K_TBIGRAT: { - /* we know that n1 <= n2 */ - kensure_bigrat(n1); - kensure_bigrat(n2); - return kbigrat_simplest_rational(K, n1, n2); + /* we know that n1 <= n2 */ + kensure_bigrat(n1); + kensure_bigrat(n2); + return kbigrat_simplest_rational(K, n1, n2); } case K_TDOUBLE: { - /* both are double, for now just convert to rational */ - krooted_vars_push(K, &n1); - krooted_vars_push(K, &n2); - n1 = kinexact_to_exact(K, n1); - n2 = kinexact_to_exact(K, n2); - TValue res = knum_simplest_rational(K, n1, n2); - krooted_tvs_push(K, res); - res = kexact_to_inexact(K, res); - krooted_tvs_pop(K); - krooted_vars_pop(K); - krooted_vars_pop(K); - return res; + /* both are double, for now just convert to rational */ + krooted_vars_push(K, &n1); + krooted_vars_push(K, &n2); + n1 = kinexact_to_exact(K, n1); + n2 = kinexact_to_exact(K, n2); + TValue res = knum_simplest_rational(K, n1, n2); + krooted_tvs_push(K, res); + res = kexact_to_inexact(K, res); + krooted_tvs_pop(K); + krooted_vars_pop(K); + krooted_vars_pop(K); + return res; } case K_TEINF: - /* we know that n1 <= n2 */ - if (tv_equal(n1, n2)) { - klispE_throw_simple(K, "x0 doesn't exists (n1 == n2 & " - "irrational)"); - return KINERT; - } else if (knegativep(n1) && kpositivep(n2)) { - return i2tv(0); - } else if (knegativep(n1)) { - /* n1 -inf, n2 finite negative */ - /* ASK John: is this behaviour for infinities ok? */ - /* Also in the report example both 1/3 & 1/2 are simpler than - 2/5... */ - return knum_real_to_integer(K, n2, K_FLOOR); - } else { - /* n1 finite positive, n2 +inf */ - /* ASK John: is this behaviour for infinities ok? */ - return knum_real_to_integer(K, n1, K_CEILING); - } + /* we know that n1 <= n2 */ + if (tv_equal(n1, n2)) { + klispE_throw_simple(K, "x0 doesn't exists (n1 == n2 & " + "irrational)"); + return KINERT; + } else if (knegativep(n1) && kpositivep(n2)) { + return i2tv(0); + } else if (knegativep(n1)) { + /* n1 -inf, n2 finite negative */ + /* ASK John: is this behaviour for infinities ok? */ + /* Also in the report example both 1/3 & 1/2 are simpler than + 2/5... */ + return knum_real_to_integer(K, n2, K_FLOOR); + } else { + /* n1 finite positive, n2 +inf */ + /* ASK John: is this behaviour for infinities ok? */ + return knum_real_to_integer(K, n1, K_CEILING); + } case K_TIINF: - /* we know that n1 <= n2 */ - if (tv_equal(n1, n2)) { - klispE_throw_simple(K, "result with no primary value"); - return KINERT; - } else if (knegativep(n1) && kpositivep(n2)) { - return d2tv(0.0); - } else if (knegativep(n1)) { - /* n1 -inf, n2 finite negative */ - /* ASK John: is this behaviour for infinities ok? */ - /* Also in the report example both 1/3 & 1/2 are simpler than - 2/5... */ - return knum_real_to_integer(K, n2, K_FLOOR); - } else { - /* n1 finite positive, n2 +inf */ - /* ASK John: is this behaviour for infinities ok? */ - return knum_real_to_integer(K, n1, K_CEILING); - } + /* we know that n1 <= n2 */ + if (tv_equal(n1, n2)) { + klispE_throw_simple(K, "result with no primary value"); + return KINERT; + } else if (knegativep(n1) && kpositivep(n2)) { + return d2tv(0.0); + } else if (knegativep(n1)) { + /* n1 -inf, n2 finite negative */ + /* ASK John: is this behaviour for infinities ok? */ + /* Also in the report example both 1/3 & 1/2 are simpler than + 2/5... */ + return knum_real_to_integer(K, n2, K_FLOOR); + } else { + /* n1 finite positive, n2 +inf */ + /* ASK John: is this behaviour for infinities ok? */ + return knum_real_to_integer(K, n1, K_CEILING); + } case K_TRWNPV: - arith_return(K, KRWNPV); - /* complex and undefined should be captured by type predicate */ + arith_return(K, KRWNPV); + /* complex and undefined should be captured by type predicate */ default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } } @@ -826,44 +826,44 @@ TValue knum_rationalize(klisp_State *K, TValue n1, TValue n2) case K_TFIXINT: case K_TBIGINT: /* for now do all with bigrat */ case K_TBIGRAT: { - /* we know that n1 <= n2 */ - kensure_bigrat(n1); - kensure_bigrat(n2); - return kbigrat_rationalize(K, n1, n2); + /* we know that n1 <= n2 */ + kensure_bigrat(n1); + kensure_bigrat(n2); + return kbigrat_rationalize(K, n1, n2); } case K_TDOUBLE: { - /* both are double, for now just convert to rational */ - krooted_vars_push(K, &n1); - krooted_vars_push(K, &n2); - n1 = kinexact_to_exact(K, n1); - n2 = kinexact_to_exact(K, n2); - TValue res = knum_rationalize(K, n1, n2); - krooted_tvs_push(K, res); - res = kexact_to_inexact(K, res); - krooted_tvs_pop(K); - krooted_vars_pop(K); - krooted_vars_pop(K); - return res; + /* both are double, for now just convert to rational */ + krooted_vars_push(K, &n1); + krooted_vars_push(K, &n2); + n1 = kinexact_to_exact(K, n1); + n2 = kinexact_to_exact(K, n2); + TValue res = knum_rationalize(K, n1, n2); + krooted_tvs_push(K, res); + res = kexact_to_inexact(K, res); + krooted_tvs_pop(K); + krooted_vars_pop(K); + krooted_vars_pop(K); + return res; } case K_TEINF: - if (kfinitep(n1) || !kfinitep(n2)) { - return i2tv(0); - } else { /* infinite n1, finite n2 */ - /* ASK John: is this behaviour for infinities ok? */ - klispE_throw_simple(K, "x0 doesn't exists"); - return KINERT; - } + if (kfinitep(n1) || !kfinitep(n2)) { + return i2tv(0); + } else { /* infinite n1, finite n2 */ + /* ASK John: is this behaviour for infinities ok? */ + klispE_throw_simple(K, "x0 doesn't exists"); + return KINERT; + } case K_TIINF: - if (kfinitep(n1) || !kfinitep(n2)) { - return d2tv(0.0); - } else { /* infinite n1, finite n2 */ - /* ASK John: is this behaviour for infinities ok? */ - klispE_throw_simple(K, "x0 doesn't exists"); - return KINERT; - } + if (kfinitep(n1) || !kfinitep(n2)) { + return d2tv(0.0); + } else { /* infinite n1, finite n2 */ + /* ASK John: is this behaviour for infinities ok? */ + klispE_throw_simple(K, "x0 doesn't exists"); + return KINERT; + } default: - klispE_throw_simple(K, "unsupported type"); - return KINERT; + klispE_throw_simple(K, "unsupported type"); + return KINERT; } } @@ -889,50 +889,50 @@ void kplus(klisp_State *K) TValue tail = ptree; while(apairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); + TValue first = kcar(tail); + tail = kcdr(tail); - /* may throw an exception */ - ares = knum_plus(K, ares, first); + /* may throw an exception */ + ares = knum_plus(K, ares, first); } /* next the cyclic part */ TValue cres = i2tv(0); /* push it only if needed */ if (cpairs == 0 && !ttisnwnpv(ares)) { /* #undefined or #real */ - /* speed things up if there is no cycle and - no possible error (on no primary value) */ - res = ares; - krooted_vars_pop(K); + /* speed things up if there is no cycle and + no possible error (on no primary value) */ + res = ares; + krooted_vars_pop(K); } else { - bool all_zero = true; - bool all_exact = true; - - krooted_vars_push(K, &cres); - while(cpairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - - all_zero = all_zero && kfast_zerop(first); - all_exact = all_exact && ttisexact(first); - - cres = knum_plus(K, cres, first); - } - - if (ttisnwnpv(cres)) /* #undefined or #real */ - ; /* do nothing, check is made later */ - else if (kfast_zerop(cres)) { - if (!all_zero) - cres = KRWNPV; /* check is made later */ - } else if (all_exact) - cres = knegativep(cres)? KEMINF : KEPINF; - else - cres = knegativep(cres)? KIMINF : KIPINF; - - /* here if any of the two has no primary an error is signaled */ - res = knum_plus(K, ares, cres); - krooted_vars_pop(K); - krooted_vars_pop(K); + bool all_zero = true; + bool all_exact = true; + + krooted_vars_push(K, &cres); + while(cpairs--) { + TValue first = kcar(tail); + tail = kcdr(tail); + + all_zero = all_zero && kfast_zerop(first); + all_exact = all_exact && ttisexact(first); + + cres = knum_plus(K, cres, first); + } + + if (ttisnwnpv(cres)) /* #undefined or #real */ + ; /* do nothing, check is made later */ + else if (kfast_zerop(cres)) { + if (!all_zero) + cres = KRWNPV; /* check is made later */ + } else if (all_exact) + cres = knegativep(cres)? KEMINF : KEPINF; + else + cres = knegativep(cres)? KIMINF : KIPINF; + + /* here if any of the two has no primary an error is signaled */ + res = knum_plus(K, ares, cres); + krooted_vars_pop(K); + krooted_vars_pop(K); } kapply_cc(K, res); } @@ -959,61 +959,61 @@ void ktimes(klisp_State *K) krooted_vars_push(K, &ares); while(apairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - ares = knum_times(K, ares, first); + TValue first = kcar(tail); + tail = kcdr(tail); + ares = knum_times(K, ares, first); } /* next the cyclic part */ TValue cres = i2tv(1); if (cpairs == 0 && !ttisnwnpv(ares)) { /* #undefined or #real */ - /* speed things up if there is no cycle */ - res = ares; - krooted_vars_pop(K); + /* speed things up if there is no cycle */ + res = ares; + krooted_vars_pop(K); } else { - bool all_one = true; - bool all_exact = true; - - krooted_vars_push(K, &cres); - while(cpairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - all_one = all_one && kfast_onep(first); - all_exact = all_exact && ttisexact(first); - cres = knum_times(K, cres, first); - } - - /* think of cres as the product of an infinite series */ - if (ttisnwnpv(ares)) - ; /* do nothing */ - if (kfast_zerop(cres)) - ; /* do nothing */ - else if (kpositivep(cres) && knum_ltp(K, cres, i2tv(1))) { - if (all_exact) - cres = i2tv(0); - else - cres = d2tv(0.0); - } - else if (kfast_onep(cres)) { - if (all_one) { - if (all_exact) - cres = i2tv(1); - else - cres = d2tv(1.0); - } else - cres = KRWNPV; - } else if (knum_gtp(K, cres, i2tv(1))) { - /* ASK JOHN: this is as per the report, but maybe we should check - that all elements are positive... */ - cres = all_exact? KEPINF : KIPINF; - } else - cres = KRWNPV; - - /* this will throw error if necessary on no primary value */ - res = knum_times(K, ares, cres); - krooted_vars_pop(K); - krooted_vars_pop(K); + bool all_one = true; + bool all_exact = true; + + krooted_vars_push(K, &cres); + while(cpairs--) { + TValue first = kcar(tail); + tail = kcdr(tail); + all_one = all_one && kfast_onep(first); + all_exact = all_exact && ttisexact(first); + cres = knum_times(K, cres, first); + } + + /* think of cres as the product of an infinite series */ + if (ttisnwnpv(ares)) + ; /* do nothing */ + if (kfast_zerop(cres)) + ; /* do nothing */ + else if (kpositivep(cres) && knum_ltp(K, cres, i2tv(1))) { + if (all_exact) + cres = i2tv(0); + else + cres = d2tv(0.0); + } + else if (kfast_onep(cres)) { + if (all_one) { + if (all_exact) + cres = i2tv(1); + else + cres = d2tv(1.0); + } else + cres = KRWNPV; + } else if (knum_gtp(K, cres, i2tv(1))) { + /* ASK JOHN: this is as per the report, but maybe we should check + that all elements are positive... */ + cres = all_exact? KEPINF : KIPINF; + } else + cres = KRWNPV; + + /* this will throw error if necessary on no primary value */ + res = knum_times(K, ares, cres); + krooted_vars_pop(K); + krooted_vars_pop(K); } kapply_cc(K, res); } @@ -1032,11 +1032,11 @@ void kminus(klisp_State *K) /* - in kernel (and unlike in scheme) requires at least 2 arguments */ if (!ttispair(ptree) || !ttispair(kcdr(ptree))) { - klispE_throw_simple(K, "at least two values are required"); - return; + klispE_throw_simple(K, "at least two values are required"); + return; } else if (!knumberp(kcar(ptree))) { - klispE_throw_simple(K, "bad type on first argument (expected number)"); - return; + klispE_throw_simple(K, "bad type on first argument (expected number)"); + return; } TValue first_val = kcar(ptree); check_typed_list(K, knumberp, true, kcdr(ptree), &pairs, &cpairs); @@ -1051,48 +1051,48 @@ void kminus(klisp_State *K) krooted_vars_push(K, &ares); while(apairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - ares = knum_plus(K, ares, first); + TValue first = kcar(tail); + tail = kcdr(tail); + ares = knum_plus(K, ares, first); } /* next the cyclic part */ TValue cres = i2tv(0); /* push it only if needed */ if (cpairs == 0 && !ttisnwnpv(ares)) { /* #undefined or #real */ - /* speed things up if there is no cycle and - no possible error (on no primary value) */ - res = ares; - krooted_vars_pop(K); + /* speed things up if there is no cycle and + no possible error (on no primary value) */ + res = ares; + krooted_vars_pop(K); } else { - bool all_zero = true; - bool all_exact = true; - - krooted_vars_push(K, &cres); - while(cpairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - - all_zero = all_zero && kfast_zerop(first); - all_exact = all_exact && ttisexact(first); - - cres = knum_plus(K, cres, first); - } - - if (ttisnwnpv(cres)) /* #undefined or #real */ - ; /* do nothing, check is made later */ - else if (kfast_zerop(cres)) { - if (!all_zero) - cres = KRWNPV; /* check is made later */ - } else if (all_exact) - cres = knegativep(cres)? KEMINF : KEPINF; - else - cres = knegativep(cres)? KIMINF : KIPINF; - - /* here if any of the two has no primary an error is signaled */ - res = knum_plus(K, ares, cres); - krooted_vars_pop(K); - krooted_vars_pop(K); + bool all_zero = true; + bool all_exact = true; + + krooted_vars_push(K, &cres); + while(cpairs--) { + TValue first = kcar(tail); + tail = kcdr(tail); + + all_zero = all_zero && kfast_zerop(first); + all_exact = all_exact && ttisexact(first); + + cres = knum_plus(K, cres, first); + } + + if (ttisnwnpv(cres)) /* #undefined or #real */ + ; /* do nothing, check is made later */ + else if (kfast_zerop(cres)) { + if (!all_zero) + cres = KRWNPV; /* check is made later */ + } else if (all_exact) + cres = knegativep(cres)? KEMINF : KEPINF; + else + cres = knegativep(cres)? KIMINF : KIPINF; + + /* here if any of the two has no primary an error is signaled */ + res = knum_plus(K, ares, cres); + krooted_vars_pop(K); + krooted_vars_pop(K); } /* now substract the sum of all the elements in the list to the first value */ @@ -1125,13 +1125,13 @@ int32_t kfixint_div_mod(int32_t n, int32_t d, int32_t *res_mod) /* div, mod or div-and-mod */ /* 0 <= mod0 < |d| */ if (mod < 0) { - if (d < 0) { - mod -= d; - ++div; - } else { - mod += d; - --div; - } + if (d < 0) { + mod -= d; + ++div; + } else { + mod += d; + --div; + } } *res_mod = mod; return div; @@ -1153,21 +1153,21 @@ int32_t kfixint_div0_mod0(int32_t n, int32_t d, int32_t *res_mod) int32_t dmax = ((d<0? -d : d) + 1) / 2; if (mod < dmin) { - if (d < 0) { - mod -= d; - ++div; - } else { - mod += d; - --div; - } + if (d < 0) { + mod -= d; + ++div; + } else { + mod += d; + --div; + } } else if (mod >= dmax) { - if (d < 0) { - mod += d; - --div; - } else { - mod -= d; - ++div; - } + if (d < 0) { + mod += d; + --div; + } else { + mod -= d; + ++div; + } } *res_mod = mod; return div; @@ -1194,147 +1194,147 @@ void kdiv_mod(klisp_State *K) UNUSED(denv); bind_2tp(K, ptree, "real", krealp, tv_n, - "real", krealp, tv_d); + "real", krealp, tv_d); TValue tv_div, tv_mod; kensure_same_exactness(K, tv_n, tv_d); if (kfast_zerop(tv_d)) { - klispE_throw_simple(K, "division by zero"); - return; + klispE_throw_simple(K, "division by zero"); + return; } switch(max_ttype(tv_n, tv_d)) { case K_TFIXINT: - /* NOTE: the only case were the result wouldn't fit in a fixint - is INT32_MIN divided by -1, resulting in INT32_MAX + 1. - The remainder is always < |tv_d| so no problem there, and - the quotient is always <= |tv_n|. All that said, the code to - correct the result returned by c operators / and % could cause - problems if d = INT32_MIN or d = INT32_MAX so just to be safe - we restrict d to be |d| < INT32_MAX and n to be - |n| < INT32_MAX */ - if (!(ivalue(tv_n) <= INT32_MIN+2 || ivalue(tv_n) >= INT32_MAX-1 || - ivalue(tv_d) <= INT32_MIN+2 || ivalue(tv_d) >= INT32_MAX-1)) { - int32_t div, mod; - if ((flags & FDIV_ZERO) == 0) - div = kfixint_div_mod(ivalue(tv_n), ivalue(tv_d), &mod); - else - div = kfixint_div0_mod0(ivalue(tv_n), ivalue(tv_d), &mod); - tv_div = i2tv(div); - tv_mod = i2tv(mod); - break; - } /* else fall through */ + /* NOTE: the only case were the result wouldn't fit in a fixint + is INT32_MIN divided by -1, resulting in INT32_MAX + 1. + The remainder is always < |tv_d| so no problem there, and + the quotient is always <= |tv_n|. All that said, the code to + correct the result returned by c operators / and % could cause + problems if d = INT32_MIN or d = INT32_MAX so just to be safe + we restrict d to be |d| < INT32_MAX and n to be + |n| < INT32_MAX */ + if (!(ivalue(tv_n) <= INT32_MIN+2 || ivalue(tv_n) >= INT32_MAX-1 || + ivalue(tv_d) <= INT32_MIN+2 || ivalue(tv_d) >= INT32_MAX-1)) { + int32_t div, mod; + if ((flags & FDIV_ZERO) == 0) + div = kfixint_div_mod(ivalue(tv_n), ivalue(tv_d), &mod); + else + div = kfixint_div0_mod0(ivalue(tv_n), ivalue(tv_d), &mod); + tv_div = i2tv(div); + tv_mod = i2tv(mod); + break; + } /* else fall through */ case K_TBIGINT: - kensure_bigint(tv_n); - kensure_bigint(tv_d); - if ((flags & FDIV_ZERO) == 0) - tv_div = kbigint_div_mod(K, tv_n, tv_d, &tv_mod); - else - tv_div = kbigint_div0_mod0(K, tv_n, tv_d, &tv_mod); - break; + kensure_bigint(tv_n); + kensure_bigint(tv_d); + if ((flags & FDIV_ZERO) == 0) + tv_div = kbigint_div_mod(K, tv_n, tv_d, &tv_mod); + else + tv_div = kbigint_div0_mod0(K, tv_n, tv_d, &tv_mod); + break; case K_TBIGRAT: - kensure_bigrat(tv_n); - kensure_bigrat(tv_d); - if ((flags & FDIV_ZERO) == 0) - tv_div = kbigrat_div_mod(K, tv_n, tv_d, &tv_mod); - else - tv_div = kbigrat_div0_mod0(K, tv_n, tv_d, &tv_mod); - break; + kensure_bigrat(tv_n); + kensure_bigrat(tv_d); + if ((flags & FDIV_ZERO) == 0) + tv_div = kbigrat_div_mod(K, tv_n, tv_d, &tv_mod); + else + tv_div = kbigrat_div0_mod0(K, tv_n, tv_d, &tv_mod); + break; case K_TDOUBLE: { - /* both are double */ - double div, mod; - if ((flags & FDIV_ZERO) == 0) - div = kdouble_div_mod(dvalue(tv_n), dvalue(tv_d), &mod); - else - div = kdouble_div0_mod0(dvalue(tv_n), dvalue(tv_d), &mod); - tv_div = ktag_double(div); - tv_mod = ktag_double(mod); - break; + /* both are double */ + double div, mod; + if ((flags & FDIV_ZERO) == 0) + div = kdouble_div_mod(dvalue(tv_n), dvalue(tv_d), &mod); + else + div = kdouble_div0_mod0(dvalue(tv_n), dvalue(tv_d), &mod); + tv_div = ktag_double(div); + tv_mod = ktag_double(mod); + break; } case K_TEINF: - if (ttiseinf(tv_n)) { - klispE_throw_simple(K, "non finite dividend"); - return; - } else { /* if (ttiseinf(tv_d)) */ - /* The semantics here are unclear, following the general - guideline of the report that says that if an infinity is - involved it should be understand as a limit. In that - case once the divisor is greater in magnitude than the - dividend the division stabilizes itself at q = 0; r = n - if both have the same sign, and q = 1; r = +infinity if - both have different sign (but in that case !(r < |d|) - !!) */ + if (ttiseinf(tv_n)) { + klispE_throw_simple(K, "non finite dividend"); + return; + } else { /* if (ttiseinf(tv_d)) */ + /* The semantics here are unclear, following the general + guideline of the report that says that if an infinity is + involved it should be understand as a limit. In that + case once the divisor is greater in magnitude than the + dividend the division stabilizes itself at q = 0; r = n + if both have the same sign, and q = 1; r = +infinity if + both have different sign (but in that case !(r < |d|) + !!) */ /* RATIONALE: if q were 0 we can't accomplish - q * d + r = n because q * d is undefined, if q isn't zero - then, either q*d + r is infinite or undefined so - there's no good q. on the other hand if we want - n - q*d = r & 0 <= r < d, r can't be infinite because it - would be equal to d, but q*d is infinite, so there's no - way out */ - /* throw an exception, until this is resolved */ - /* ASK John */ - klispE_throw_simple(K, "non finite divisor"); - return; - } + q * d + r = n because q * d is undefined, if q isn't zero + then, either q*d + r is infinite or undefined so + there's no good q. on the other hand if we want + n - q*d = r & 0 <= r < d, r can't be infinite because it + would be equal to d, but q*d is infinite, so there's no + way out */ + /* throw an exception, until this is resolved */ + /* ASK John */ + klispE_throw_simple(K, "non finite divisor"); + return; + } case K_TIINF: - if (ttisiinf(tv_n)) { - klispE_throw_simple(K, "non finite dividend"); - return; - } else { /* if (ttiseinf(tv_d)) */ - /* The semantics here are unclear, following the general - guideline of the report that says that if an infinity is - involved it should be understand as a limit. In that - case once the divisor is greater in magnitude than the - dividend the division stabilizes itself at q = 0; r = n - if both have the same sign, and q = 1; r = +infinity if - both have different sign (but in that case !(r < |d|) - !!) */ + if (ttisiinf(tv_n)) { + klispE_throw_simple(K, "non finite dividend"); + return; + } else { /* if (ttiseinf(tv_d)) */ + /* The semantics here are unclear, following the general + guideline of the report that says that if an infinity is + involved it should be understand as a limit. In that + case once the divisor is greater in magnitude than the + dividend the division stabilizes itself at q = 0; r = n + if both have the same sign, and q = 1; r = +infinity if + both have different sign (but in that case !(r < |d|) + !!) */ /* RATIONALE: if q were 0 we can't accomplish - q * d + r = n because q * d is undefined, if q isn't zero - then, either q*d + r is infinite or undefined so - there's no good q. on the other hand if we want - n - q*d = r & 0 <= r < d, r can't be infinite because it - would be equal to d, but q*d is infinite, so there's no - way out */ - /* throw an exception, until this is resolved */ - /* ASK John */ - klispE_throw_simple(K, "non finite divisor"); - return; - } + q * d + r = n because q * d is undefined, if q isn't zero + then, either q*d + r is infinite or undefined so + there's no good q. on the other hand if we want + n - q*d = r & 0 <= r < d, r can't be infinite because it + would be equal to d, but q*d is infinite, so there's no + way out */ + /* throw an exception, until this is resolved */ + /* ASK John */ + klispE_throw_simple(K, "non finite divisor"); + return; + } case K_TRWNPV: { /* no primary value */ - /* ASK John: what happens with undefined & real with no primary values */ - TValue n = ttisrwnpv(tv_n)? tv_n : tv_d; - if (kcurr_strict_arithp(K)) { - klispE_throw_simple_with_irritants(K, "operand has no primary " - "value", 1, n); - return; - } else { - tv_div = KRWNPV; - tv_mod = KRWNPV; - break; - } + /* ASK John: what happens with undefined & real with no primary values */ + TValue n = ttisrwnpv(tv_n)? tv_n : tv_d; + if (kcurr_strict_arithp(K)) { + klispE_throw_simple_with_irritants(K, "operand has no primary " + "value", 1, n); + return; + } else { + tv_div = KRWNPV; + tv_mod = KRWNPV; + break; + } } default: - klispE_throw_simple(K, "unsupported type"); - return; + klispE_throw_simple(K, "unsupported type"); + return; } TValue res; if (flags & FDIV_DIV) { - if (flags & FDIV_MOD) { /* return both div and mod */ - krooted_tvs_push(K, tv_div); - krooted_tvs_push(K, tv_mod); - res = klist(K, 2, tv_div, tv_mod); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - } else { - res = tv_div; - } + if (flags & FDIV_MOD) { /* return both div and mod */ + krooted_tvs_push(K, tv_div); + krooted_tvs_push(K, tv_mod); + res = klist(K, 2, tv_div, tv_mod); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + } else { + res = tv_div; + } } else { - res = tv_mod; + res = tv_mod; } kapply_cc(K, res); } @@ -1352,16 +1352,16 @@ bool koddp(TValue n) { switch (ttype(n)) { case K_TFIXINT: - return (ivalue(n) & 1) != 0; + return (ivalue(n) & 1) != 0; case K_TBIGINT: - return kbigint_oddp(n); + return kbigint_oddp(n); case K_TDOUBLE: - return fmod(dvalue(n), 2.0) != 0.0; - /* real with no prim value, complex and undefined should be captured by - type predicate */ + return fmod(dvalue(n), 2.0) != 0.0; + /* real with no prim value, complex and undefined should be captured by + type predicate */ default: - assert(0); - return false; + assert(0); + return false; } } @@ -1369,16 +1369,16 @@ bool kevenp(TValue n) { switch (ttype(n)) { case K_TFIXINT: - return (ivalue(n) & 1) == 0; + return (ivalue(n) & 1) == 0; case K_TBIGINT: - return kbigint_evenp(n); + return kbigint_evenp(n); case K_TDOUBLE: - return fmod(dvalue(n), 2.0) == 0.0; - /* real with no prim value, complex and undefined should be captured by - type predicate */ + return fmod(dvalue(n), 2.0) == 0.0; + /* real with no prim value, complex and undefined should be captured by + type predicate */ default: - assert(0); - return false; + assert(0); + return false; } } @@ -1430,11 +1430,11 @@ void kmin_max(klisp_State *K) bool (*cmp)(klisp_State *K, TValue, TValue) = minp? knum_ltp : knum_gtp; while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); + TValue first = kcar(tail); + tail = kcdr(tail); - if ((*cmp)(K, first, res)) - res = first; + if ((*cmp)(K, first, res)) + res = first; } kapply_cc(K, res); } @@ -1456,22 +1456,22 @@ void kgcd(klisp_State *K) krooted_vars_push(K, &res); if (pairs == 0) { - res = KEPINF; /* report: (gcd) = #e+infinity */ + res = KEPINF; /* report: (gcd) = #e+infinity */ } else { - TValue tail = ptree; - bool seen_finite_non_zero = false; - /* res = 0 */ + TValue tail = ptree; + bool seen_finite_non_zero = false; + /* res = 0 */ - while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - seen_finite_non_zero |= - (!ttisinf(first) && !kfast_zerop(first)); - res = knum_gcd(K, res, first); - } + while(pairs--) { + TValue first = kcar(tail); + tail = kcdr(tail); + seen_finite_non_zero |= + (!ttisinf(first) && !kfast_zerop(first)); + res = knum_gcd(K, res, first); + } - if (!seen_finite_non_zero) - res = KRWNPV; + if (!seen_finite_non_zero) + res = KRWNPV; } krooted_vars_pop(K); @@ -1496,10 +1496,10 @@ void klcm(klisp_State *K) TValue tail = ptree; while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - /* This will check that neither is zero */ - res = knum_lcm(K, res, first); + TValue first = kcar(tail); + tail = kcdr(tail); + /* This will check that neither is zero */ + res = knum_lcm(K, res, first); } krooted_vars_pop(K); @@ -1526,9 +1526,9 @@ void kget_real_internal_bounds(klisp_State *K) [-inf, +inf] bounds */ TValue res; if (ttisexact(tv_n)) { - res = klist(K, 2, tv_n, tv_n); + res = klist(K, 2, tv_n, tv_n); } else { - res = klist(K, 2, KIMINF, KIPINF); + res = klist(K, 2, KIMINF, KIPINF); } kapply_cc(K, res); } @@ -1549,9 +1549,9 @@ void kget_real_exact_bounds(klisp_State *K) +inf when converting to exact */ TValue res; if (ttisexact(tv_n)) { - res = klist(K, 2, tv_n, tv_n); + res = klist(K, 2, tv_n, tv_n); } else { - res = klist(K, 2, KEMINF, KEPINF); + res = klist(K, 2, KEMINF, KEPINF); } kapply_cc(K, res); } @@ -1569,10 +1569,10 @@ void kget_real_internal_primary(klisp_State *K) bind_1tp(K, ptree, "real", krealp, tv_n); /* TEMP: do it here directly */ if (ttisrwnpv(tv_n)) { - klispE_throw_simple_with_irritants(K, "no primary value", 1, tv_n); - return; + klispE_throw_simple_with_irritants(K, "no primary value", 1, tv_n); + return; } else { - kapply_cc(K, tv_n); + kapply_cc(K, tv_n); } } @@ -1603,17 +1603,17 @@ void kmake_inexact(klisp_State *K) UNUSED(xparams); bind_3tp(K, ptree, "real", krealp, real1, - "real", krealp, real2, "real", krealp, real3); + "real", krealp, real2, "real", krealp, real3); TValue res; UNUSED(real1); UNUSED(real3); if (ttisinexact(real2)) { - res = real2; + res = real2; } else { - /* TEMP: for now bounds are ignored */ - /* NOTE: this handles overflow and underflow */ - res = kexact_to_inexact(K, real2); + /* TEMP: for now bounds are ignored */ + /* NOTE: this handles overflow and underflow */ + res = kexact_to_inexact(K, real2); } kapply_cc(K, res); } @@ -1660,7 +1660,7 @@ void kwith_strict_arithmetic(klisp_State *K) UNUSED(xparams); bind_2tp(K, ptree, "bool", ttisboolean, strictp, - "combiner", ttiscombiner, comb); + "combiner", ttiscombiner, comb); TValue op = kmake_operative(K, do_bind, 1, K->kd_strict_arith_key); krooted_tvs_push(K, op); @@ -1707,11 +1707,11 @@ void kdivided(klisp_State *K) /* / in kernel (and unlike in scheme) requires at least 2 arguments */ if (!ttispair(ptree) || !ttispair(kcdr(ptree))) { - klispE_throw_simple(K, "at least two values are required"); - return; + klispE_throw_simple(K, "at least two values are required"); + return; } else if (!knumberp(kcar(ptree))) { - klispE_throw_simple(K, "bad type on first argument (expected number)"); - return; + klispE_throw_simple(K, "bad type on first argument (expected number)"); + return; } TValue first_val = kcar(ptree); check_typed_list(K, knumberp, true, kcdr(ptree), &pairs, &cpairs); @@ -1726,61 +1726,61 @@ void kdivided(klisp_State *K) krooted_vars_push(K, &ares); while(apairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - ares = knum_times(K, ares, first); + TValue first = kcar(tail); + tail = kcdr(tail); + ares = knum_times(K, ares, first); } /* next the cyclic part */ TValue cres = i2tv(1); if (cpairs == 0 && !ttisnwnpv(ares)) { /* #undefined or #real */ - /* speed things up if there is no cycle */ - res = ares; - krooted_vars_pop(K); + /* speed things up if there is no cycle */ + res = ares; + krooted_vars_pop(K); } else { - bool all_one = true; - bool all_exact = true; - - krooted_vars_push(K, &cres); - while(cpairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - all_one = all_one && kfast_onep(first); - all_exact = all_exact && ttisexact(first); - cres = knum_times(K, cres, first); - } - - /* think of cres as the product of an infinite series */ - if (ttisnwnpv(ares)) - ; /* do nothing */ - if (kfast_zerop(cres)) - ; /* do nothing */ - else if (kpositivep(cres) && knum_ltp(K, cres, i2tv(1))) { - if (all_exact) - cres = i2tv(0); - else - cres = d2tv(0.0); - } - else if (kfast_onep(cres)) { - if (all_one) { - if (all_exact) - cres = i2tv(1); - else - cres = d2tv(1.0); - } else - cres = KRWNPV; - } else if (knum_gtp(K, cres, i2tv(1))) { - /* ASK JOHN: this is as per the report, but maybe we should check - that all elements are positive... */ - cres = all_exact? KEPINF : KIPINF; - } else - cres = KRWNPV; - - /* this will throw error if necessary on no primary value */ - res = knum_times(K, ares, cres); - krooted_vars_pop(K); - krooted_vars_pop(K); + bool all_one = true; + bool all_exact = true; + + krooted_vars_push(K, &cres); + while(cpairs--) { + TValue first = kcar(tail); + tail = kcdr(tail); + all_one = all_one && kfast_onep(first); + all_exact = all_exact && ttisexact(first); + cres = knum_times(K, cres, first); + } + + /* think of cres as the product of an infinite series */ + if (ttisnwnpv(ares)) + ; /* do nothing */ + if (kfast_zerop(cres)) + ; /* do nothing */ + else if (kpositivep(cres) && knum_ltp(K, cres, i2tv(1))) { + if (all_exact) + cres = i2tv(0); + else + cres = d2tv(0.0); + } + else if (kfast_onep(cres)) { + if (all_one) { + if (all_exact) + cres = i2tv(1); + else + cres = d2tv(1.0); + } else + cres = KRWNPV; + } else if (knum_gtp(K, cres, i2tv(1))) { + /* ASK JOHN: this is as per the report, but maybe we should check + that all elements are positive... */ + cres = all_exact? KEPINF : KIPINF; + } else + cres = KRWNPV; + + /* this will throw error if necessary on no primary value */ + res = knum_times(K, ares, cres); + krooted_vars_pop(K); + krooted_vars_pop(K); } /* now divide first value by the product of all the elements in the list */ @@ -1853,7 +1853,7 @@ void krationalize(klisp_State *K) UNUSED(xparams); bind_2tp(K, ptree, "real", krealp, n1, - "real", krealp, n2); + "real", krealp, n2); TValue res = knum_rationalize(K, n1, n2); kapply_cc(K, res); @@ -1869,7 +1869,7 @@ void ksimplest_rational(klisp_State *K) UNUSED(xparams); bind_2tp(K, ptree, "real", krealp, n1, - "real", krealp, n2); + "real", krealp, n2); TValue res = knum_simplest_rational(K, n1, n2); kapply_cc(K, res); @@ -1892,26 +1892,26 @@ void kexp(klisp_State *K) case K_TFIXINT: case K_TBIGINT: case K_TBIGRAT: - /* for now, all go to double */ - n = kexact_to_inexact(K, n); /* no need to root it */ - /* fall through */ + /* for now, all go to double */ + n = kexact_to_inexact(K, n); /* no need to root it */ + /* fall through */ case K_TDOUBLE: { - double d = exp(dvalue(n)); - res = ktag_double(d); - break; + double d = exp(dvalue(n)); + res = ktag_double(d); + break; } case K_TEINF: /* in any case return inexact result (e is inexact) */ case K_TIINF: - res = kpositivep(n)? KIPINF : d2tv(0.0); - break; + res = kpositivep(n)? KIPINF : d2tv(0.0); + break; case K_TRWNPV: case K_TUNDEFINED: - klispE_throw_simple_with_irritants(K, "no primary value", 1, n); - return; - /* complex and undefined should be captured by type predicate */ + klispE_throw_simple_with_irritants(K, "no primary value", 1, n); + return; + /* complex and undefined should be captured by type predicate */ default: - klispE_throw_simple(K, "unsupported type"); - return; + klispE_throw_simple(K, "unsupported type"); + return; } kapply_cc(K, res); } @@ -1929,11 +1929,11 @@ void klog(klisp_State *K) /* ASK John: error or no primary value, or undefined */ if (kfast_zerop(n)) { - klispE_throw_simple_with_irritants(K, "zero argument", 1, n); - return; + klispE_throw_simple_with_irritants(K, "zero argument", 1, n); + return; } else if (knegativep(n)) { - klispE_throw_simple_with_irritants(K, "negative argument", 1, n); - return; + klispE_throw_simple_with_irritants(K, "negative argument", 1, n); + return; } /* TEMP: do it inline for now */ @@ -1942,27 +1942,27 @@ void klog(klisp_State *K) case K_TFIXINT: case K_TBIGINT: case K_TBIGRAT: - /* for now, all go to double */ - n = kexact_to_inexact(K, n); /* no need to root it */ - /* fall through */ + /* for now, all go to double */ + n = kexact_to_inexact(K, n); /* no need to root it */ + /* fall through */ case K_TDOUBLE: { - double d = log(dvalue(n)); - res = ktag_double(d); - break; + double d = log(dvalue(n)); + res = ktag_double(d); + break; } case K_TEINF: /* in any case return inexact result (e is inexact) */ case K_TIINF: - /* is this ok? */ - res = KIPINF; - break; + /* is this ok? */ + res = KIPINF; + break; case K_TRWNPV: case K_TUNDEFINED: - klispE_throw_simple_with_irritants(K, "no primary value", 1, n); - return; - /* complex and undefined should be captured by type predicate */ + klispE_throw_simple_with_irritants(K, "no primary value", 1, n); + return; + /* complex and undefined should be captured by type predicate */ default: - klispE_throw_simple(K, "unsupported type"); - return; + klispE_throw_simple(K, "unsupported type"); + return; } kapply_cc(K, res); } @@ -1987,26 +1987,26 @@ void ktrig(klisp_State *K) case K_TFIXINT: case K_TBIGINT: case K_TBIGRAT: - /* for now, all go to double */ - n = kexact_to_inexact(K, n); /* no need to root it */ - /* fall through */ + /* for now, all go to double */ + n = kexact_to_inexact(K, n); /* no need to root it */ + /* fall through */ case K_TDOUBLE: { - double d = (*fn)(dvalue(n)); - res = ktag_double(d); - break; + double d = (*fn)(dvalue(n)); + res = ktag_double(d); + break; } case K_TEINF: case K_TIINF: - /* is this ok? */ - res = KRWNPV; - break; + /* is this ok? */ + res = KRWNPV; + break; case K_TRWNPV: case K_TUNDEFINED: - klispE_throw_simple_with_irritants(K, "no primary value", 1, n); - return; + klispE_throw_simple_with_irritants(K, "no primary value", 1, n); + return; default: - klispE_throw_simple(K, "unsupported type"); - return; + klispE_throw_simple(K, "unsupported type"); + return; } arith_kapply_cc(K, res); } @@ -2031,31 +2031,31 @@ void katrig(klisp_State *K) case K_TFIXINT: case K_TBIGINT: case K_TBIGRAT: - /* for now, all go to double */ - n = kexact_to_inexact(K, n); /* no need to root it */ - /* fall through */ + /* for now, all go to double */ + n = kexact_to_inexact(K, n); /* no need to root it */ + /* fall through */ case K_TDOUBLE: { - double d = dvalue(n); - if (d >= -1.0 && d <= 1.0) { - d = (*fn)(dvalue(n)); - res = ktag_double(d); - } else { - res = KUNDEF; /* ASK John: is this ok, or should throw error? */ - } - break; + double d = dvalue(n); + if (d >= -1.0 && d <= 1.0) { + d = (*fn)(dvalue(n)); + res = ktag_double(d); + } else { + res = KUNDEF; /* ASK John: is this ok, or should throw error? */ + } + break; } case K_TEINF: case K_TIINF: - /* ASK John: is this ok? */ - res = KRWNPV; - break; + /* ASK John: is this ok? */ + res = KRWNPV; + break; case K_TRWNPV: case K_TUNDEFINED: - klispE_throw_simple_with_irritants(K, "no primary value", 1, n); - return; + klispE_throw_simple_with_irritants(K, "no primary value", 1, n); + return; default: - klispE_throw_simple(K, "unsupported type"); - return; + klispE_throw_simple(K, "unsupported type"); + return; } arith_kapply_cc(K, res); } @@ -2073,22 +2073,22 @@ void katan(klisp_State *K) bool two_params; TValue n2; if (ttisnil(rest)) { - two_params = false; - n2 = n1; + two_params = false; + n2 = n1; } else { - two_params = true; - if (!ttispair(rest) || !ttisnil(kcdr(rest))) { - klispE_throw_simple(K, "Bad ptree structure (in optional " - "argument)"); - return; - } else if (!ttisnumber(kcar(rest))) { - klispE_throw_simple(K, "Bad type on optional argument " - "(expected number)"); - return; - } else { - n2 = kcar(rest); - kensure_same_exactness(K, n1, n2); - } + two_params = true; + if (!ttispair(rest) || !ttisnil(kcdr(rest))) { + klispE_throw_simple(K, "Bad ptree structure (in optional " + "argument)"); + return; + } else if (!ttisnumber(kcar(rest))) { + klispE_throw_simple(K, "Bad type on optional argument " + "(expected number)"); + return; + } else { + n2 = kcar(rest); + kensure_same_exactness(K, n1, n2); + } } /* TEMP: do it inline for now */ @@ -2097,56 +2097,56 @@ void katan(klisp_State *K) case K_TFIXINT: case K_TBIGINT: case K_TBIGRAT: - /* for now, all go to double */ - n1 = kexact_to_inexact(K, n1); /* no need to root it */ - if (two_params) - n2 = kexact_to_inexact(K, n2); /* no need to root it */ - /* fall through */ + /* for now, all go to double */ + n1 = kexact_to_inexact(K, n1); /* no need to root it */ + if (two_params) + n2 = kexact_to_inexact(K, n2); /* no need to root it */ + /* fall through */ case K_TDOUBLE: { - double d1 = dvalue(n1); - if (two_params) { - double d2 = dvalue(n2); - d1 = atan2(d1, d2); - } else { - d1 = atan(d1); - } - res = ktag_double(d1); - break; + double d1 = dvalue(n1); + if (two_params) { + double d2 = dvalue(n2); + d1 = atan2(d1, d2); + } else { + d1 = atan(d1); + } + res = ktag_double(d1); + break; } case K_TEINF: case K_TIINF: - /* ASK John: is this ok? */ - if (two_params) { - if (kfinitep(n1)) { - res = ktag_double(0.0); - } else if (!kfinitep(n2)) { - klispE_throw_simple_with_irritants(K, "infinite divisor & " - "dividend", 2, n1, n2); - return; - } else { - /* XXX either pi/2 or -pi/2, but we don't have the constant */ - double d = knum_same_signp(K, n1, n2)? atan(INFINITY) : - atan(-INFINITY); - res = ktag_double(d); - } - } else { - /* XXX either pi/2 or -pi/2, but we don't have the constant */ - double d = kpositivep(n1)? atan(INFINITY) : atan(-INFINITY); - res = ktag_double(d); - } - break; + /* ASK John: is this ok? */ + if (two_params) { + if (kfinitep(n1)) { + res = ktag_double(0.0); + } else if (!kfinitep(n2)) { + klispE_throw_simple_with_irritants(K, "infinite divisor & " + "dividend", 2, n1, n2); + return; + } else { + /* XXX either pi/2 or -pi/2, but we don't have the constant */ + double d = knum_same_signp(K, n1, n2)? atan(INFINITY) : + atan(-INFINITY); + res = ktag_double(d); + } + } else { + /* XXX either pi/2 or -pi/2, but we don't have the constant */ + double d = kpositivep(n1)? atan(INFINITY) : atan(-INFINITY); + res = ktag_double(d); + } + break; case K_TRWNPV: case K_TUNDEFINED: - if (two_params) { - klispE_throw_simple_with_irritants(K, "no primary value", 2, - n1, n2); - } else { - klispE_throw_simple_with_irritants(K, "no primary value", 1, n1); - } - return; + if (two_params) { + klispE_throw_simple_with_irritants(K, "no primary value", 2, + n1, n2); + } else { + klispE_throw_simple_with_irritants(K, "no primary value", 1, n1); + } + return; default: - klispE_throw_simple(K, "unsupported type"); - return; + klispE_throw_simple(K, "unsupported type"); + return; } arith_kapply_cc(K, res); } @@ -2168,30 +2168,30 @@ void ksqrt(klisp_State *K) case K_TFIXINT: case K_TBIGINT: case K_TBIGRAT: - /* TEMP: for now, all go to double */ - n = kexact_to_inexact(K, n); /* no need to root it */ - /* fall through */ + /* TEMP: for now, all go to double */ + n = kexact_to_inexact(K, n); /* no need to root it */ + /* fall through */ case K_TDOUBLE: { - double d = dvalue(n); - if (d < 0.0) - res = KUNDEF; /* ASK John: is this ok, or should throw error? */ - else { - d = sqrt(d); - res = ktag_double(d); - } - break; + double d = dvalue(n); + if (d < 0.0) + res = KUNDEF; /* ASK John: is this ok, or should throw error? */ + else { + d = sqrt(d); + res = ktag_double(d); + } + break; } case K_TEINF: case K_TIINF: - res = knegativep(n)? KUNDEF : KIPINF; - break; + res = knegativep(n)? KUNDEF : KIPINF; + break; case K_TRWNPV: case K_TUNDEFINED: - klispE_throw_simple_with_irritants(K, "no primary value", 1, n); - return; + klispE_throw_simple_with_irritants(K, "no primary value", 1, n); + return; default: - klispE_throw_simple(K, "unsupported type"); - return; + klispE_throw_simple(K, "unsupported type"); + return; } arith_kapply_cc(K, res); } @@ -2206,7 +2206,7 @@ void kexpt(klisp_State *K) UNUSED(xparams); bind_2tp(K, ptree, "number", knumberp, n1, - "number", knumberp, n2); + "number", knumberp, n2); kensure_same_exactness(K, n1, n2); @@ -2216,56 +2216,56 @@ void kexpt(klisp_State *K) case K_TFIXINT: case K_TBIGINT: case K_TBIGRAT: - /* TEMP: for now, all go to double */ - n1 = kexact_to_inexact(K, n1); /* no need to root it */ - n2 = kexact_to_inexact(K, n2); /* no need to root it */ - /* fall through */ + /* TEMP: for now, all go to double */ + n1 = kexact_to_inexact(K, n1); /* no need to root it */ + n2 = kexact_to_inexact(K, n2); /* no need to root it */ + /* fall through */ case K_TDOUBLE: { - double d1 = dvalue(n1); - double d2 = dvalue(n2); - d1 = pow(d1, d2); - res = ktag_double(d1); - break; + double d1 = dvalue(n1); + double d2 = dvalue(n2); + d1 = pow(d1, d2); + res = ktag_double(d1); + break; } case K_TEINF: case K_TIINF: - if (ttisinf(n1) && ttisinf(n2)) { - if (knegativep(n1) && knegativep(n2)) - res = d2tv(0.0); - else if (knegativep(n1) || knegativep(n2)) - res = KUNDEF; /* ASK John: is this ok? */ - else - res = KIPINF; - } else if (ttisinf(n1)) { - if (knegativep(n1)) { - if (knegativep(n2)) - res = d2tv(0.0); - else { - TValue num = knum_numerator(K, n2); - krooted_tvs_push(K, num); - res = kevenp(num)? KIPINF : KIMINF; - krooted_tvs_pop(K); - } - } else { - res = KIPINF; - } - } else { /* ttisinf(n2) */ - if (knegativep(n2)) - res = d2tv(0.0); - else if (knegativep(n1)) - res = KUNDEF; /* ASK John: is this ok? */ - else - res = KIPINF; - } - break; + if (ttisinf(n1) && ttisinf(n2)) { + if (knegativep(n1) && knegativep(n2)) + res = d2tv(0.0); + else if (knegativep(n1) || knegativep(n2)) + res = KUNDEF; /* ASK John: is this ok? */ + else + res = KIPINF; + } else if (ttisinf(n1)) { + if (knegativep(n1)) { + if (knegativep(n2)) + res = d2tv(0.0); + else { + TValue num = knum_numerator(K, n2); + krooted_tvs_push(K, num); + res = kevenp(num)? KIPINF : KIMINF; + krooted_tvs_pop(K); + } + } else { + res = KIPINF; + } + } else { /* ttisinf(n2) */ + if (knegativep(n2)) + res = d2tv(0.0); + else if (knegativep(n1)) + res = KUNDEF; /* ASK John: is this ok? */ + else + res = KIPINF; + } + break; case K_TRWNPV: case K_TUNDEFINED: - klispE_throw_simple_with_irritants(K, "no primary value", 2, - n1, n2); - return; + klispE_throw_simple_with_irritants(K, "no primary value", 2, + n1, n2); + return; default: - klispE_throw_simple(K, "unsupported type"); - return; + klispE_throw_simple(K, "unsupported type"); + return; } arith_kapply_cc(K, res); } @@ -2285,7 +2285,7 @@ void number_to_string(klisp_State *K) bind_al1tp(K, ptree, "number", knumberp, obj, maybe_radix); int radix = 10; if (get_opt_tpar(K, maybe_radix, "radix (2, 8, 10, or 16)", ttisradix)) - radix = ivalue(maybe_radix); + radix = ivalue(maybe_radix); char small_buf[64]; /* for fixints */ TValue buf_str = K->empty_string; /* for bigrats, bigints and doubles */ @@ -2294,87 +2294,87 @@ void number_to_string(klisp_State *K) switch(ttype(obj)) { case K_TFIXINT: { - /* can't use snprintf here... there's no support for binary, - so just do by hand */ - uint32_t value; - /* convert to unsigned to write */ - value = (uint32_t) ((ivalue(obj) < 0)? - -((int64_t) ivalue(obj)) : - ivalue(obj)); - char *digits = "0123456789abcdef"; - /* write backwards so we don't have to reverse the buffer */ - buf = small_buf + sizeof(small_buf) - 1; - *buf-- = '\0'; - do { - *buf-- = digits[value % radix]; - value /= radix; - } while(value > 0); /* with the guard down it works for zero too */ - - /* only put the sign if negative, - then correct the pointer to the first char */ - if (ivalue(obj) < 0) - *buf = '-'; - else - ++buf; - break; + /* can't use snprintf here... there's no support for binary, + so just do by hand */ + uint32_t value; + /* convert to unsigned to write */ + value = (uint32_t) ((ivalue(obj) < 0)? + -((int64_t) ivalue(obj)) : + ivalue(obj)); + char *digits = "0123456789abcdef"; + /* write backwards so we don't have to reverse the buffer */ + buf = small_buf + sizeof(small_buf) - 1; + *buf-- = '\0'; + do { + *buf-- = digits[value % radix]; + value /= radix; + } while(value > 0); /* with the guard down it works for zero too */ + + /* only put the sign if negative, + then correct the pointer to the first char */ + if (ivalue(obj) < 0) + *buf = '-'; + else + ++buf; + break; } case K_TBIGINT: { - int32_t size = kbigint_print_size(obj, radix); - /* here we are using 1 byte extra, because size already includes - 1 for the terminator, but better be safe than sorry */ - buf_str = kstring_new_s(K, size); - buf = kstring_buf(buf_str); - kbigint_print_string(K, obj, radix, buf, size); - /* the string will be copied and trimmed later, - because print_size may overestimate */ - break; + int32_t size = kbigint_print_size(obj, radix); + /* here we are using 1 byte extra, because size already includes + 1 for the terminator, but better be safe than sorry */ + buf_str = kstring_new_s(K, size); + buf = kstring_buf(buf_str); + kbigint_print_string(K, obj, radix, buf, size); + /* the string will be copied and trimmed later, + because print_size may overestimate */ + break; } case K_TBIGRAT: { - int32_t size = kbigrat_print_size(obj, radix); - /* here we are using 1 byte extra, because size already includes - 1 for the terminator, but better be safe than sorry */ - buf_str = kstring_new_s(K, size); - buf = kstring_buf(buf_str); - kbigrat_print_string(K, obj, radix, buf, size); - /* the string will be copied and trimmed later, - because print_size may overestimate */ - break; + int32_t size = kbigrat_print_size(obj, radix); + /* here we are using 1 byte extra, because size already includes + 1 for the terminator, but better be safe than sorry */ + buf_str = kstring_new_s(K, size); + buf = kstring_buf(buf_str); + kbigrat_print_string(K, obj, radix, buf, size); + /* the string will be copied and trimmed later, + because print_size may overestimate */ + break; } case K_TEINF: - buf = tv_equal(obj, KEPINF)? "#e+infinity" : "#e-infinity"; - break; + buf = tv_equal(obj, KEPINF)? "#e+infinity" : "#e-infinity"; + break; case K_TIINF: - buf = tv_equal(obj, KIPINF)? "#i+infinity" : "#i-infinity"; - break; + buf = tv_equal(obj, KIPINF)? "#i+infinity" : "#i-infinity"; + break; case K_TDOUBLE: { - if (radix != 10) { - /* only radix 10 is supported for inexact numbers - see rationale in the report (technically they could be - printed without a decimal point, like fractions, but...*/ - klispE_throw_simple_with_irritants(K, "radix != 10 with inexact " - "number", 2, obj,maybe_radix); - return; - } + if (radix != 10) { + /* only radix 10 is supported for inexact numbers + see rationale in the report (technically they could be + printed without a decimal point, like fractions, but...*/ + klispE_throw_simple_with_irritants(K, "radix != 10 with inexact " + "number", 2, obj,maybe_radix); + return; + } /* radix is always 10 */ - int32_t size = kdouble_print_size(obj); - /* here we are using 1 byte extra, because size already includes - 1 for the terminator, but better be safe than sorry */ - buf_str = kstring_new_s(K, size); - buf = kstring_buf(buf_str); - kdouble_print_string(K, obj, buf, size); - /* the string will be copied and trimmed later, - because print_size may overestimate */ - break; + int32_t size = kdouble_print_size(obj); + /* here we are using 1 byte extra, because size already includes + 1 for the terminator, but better be safe than sorry */ + buf_str = kstring_new_s(K, size); + buf = kstring_buf(buf_str); + kdouble_print_string(K, obj, buf, size); + /* the string will be copied and trimmed later, + because print_size may overestimate */ + break; } case K_TRWNPV: - buf = "#real"; - break; + buf = "#real"; + break; case K_TUNDEFINED: - buf = "#undefined"; - break; + buf = "#undefined"; + break; default: - /* shouldn't happen */ - klisp_assert(0); + /* shouldn't happen */ + klisp_assert(0); } TValue str = kstring_new_b(K, buf); @@ -2386,16 +2386,16 @@ struct kspecial_number { const char *ext_rep; /* downcase external representation */ TValue obj; } kspecial_numbers[] = { { "#e+infinity", KEPINF_ }, - { "#e-infinity", KEMINF_ }, - { "#i+infinity", KIPINF_ }, - { "#i-infinity", KIMINF_ }, - { "#real", KRWNPV_ }, - { "#undefined", KUNDEF_ } + { "#e-infinity", KEMINF_ }, + { "#i+infinity", KIPINF_ }, + { "#i-infinity", KIMINF_ }, + { "#real", KRWNPV_ }, + { "#undefined", KUNDEF_ } }; /* N.B. If case insignificance is removed, check here too! - This will happily accept exactness and radix arguments in both cases - (but not the names of special numbers) */ + This will happily accept exactness and radix arguments in both cases + (but not the names of special numbers) */ void string_to_number(klisp_State *K) { /* MAYBE try to unify with ktoken */ @@ -2410,7 +2410,7 @@ void string_to_number(klisp_State *K) bind_al1tp(K, ptree, "string", ttisstring, str, maybe_radix); int radix = 10; if (get_opt_tpar(K, maybe_radix, "radix (2, 8, 10, or 16)", ttisradix)) - radix = ivalue(maybe_radix); + radix = ivalue(maybe_radix); /* track length to throw better error msgs */ char *buf = kstring_buf(str); @@ -2425,74 +2425,74 @@ void string_to_number(klisp_State *K) TValue res = KINERT; size_t snum_size = sizeof(kspecial_numbers) / - sizeof(struct kspecial_number); + sizeof(struct kspecial_number); for (int i = 0; i < snum_size; i++) { - struct kspecial_number number = kspecial_numbers[i]; - /* NOTE: must check type because buf may contain embedded '\0's */ - if (len == strlen(number.ext_rep) && - strcmp(number.ext_rep, buf) == 0) { - res = number.obj; - break; - } + struct kspecial_number number = kspecial_numbers[i]; + /* NOTE: must check type because buf may contain embedded '\0's */ + if (len == strlen(number.ext_rep) && + strcmp(number.ext_rep, buf) == 0) { + res = number.obj; + break; + } } if (ttisinert(res)) { - /* number wasn't a special number */ - while (*buf == '#') { - switch(*++buf) { - case 'e': case 'E': case 'i': case 'I': - if (has_exactp) { - klispE_throw_simple_with_irritants( - K, "two exactness prefixes", 1, str); - return; - } - has_exactp = true; - exactp = (*buf == 'e'); - ++buf; - break; - case 'b': case 'B': radix = 2; goto RADIX; - case 'o': case 'O': radix = 8; goto RADIX; - case 'd': case 'D': radix = 10; goto RADIX; - case 'x': case 'X': radix = 16; goto RADIX; - RADIX: - if (has_radixp) { - klispE_throw_simple_with_irritants( - K, "two radix prefixes", 1, str); - return; - } - has_radixp = true; - ++buf; - break; - default: - klispE_throw_simple_with_irritants(K, "unexpected char " - "after #", 1, str); - return; - } - } - - if (radix == 10) { - /* only allow decimals with radix 10 */ - bool decimalp = false; - if (!krational_read_decimal(K, buf, radix, &res, NULL, &decimalp)) { - klispE_throw_simple_with_irritants(K, "Bad format", 1, str); - return; - } - if (decimalp && !has_exactp) { - /* handle decimal format as an explicit #i */ - has_exactp = true; - exactp = false; - } - } else { - if (!krational_read(K, buf, radix, &res, NULL)) { - klispE_throw_simple_with_irritants(K, "Bad format", 1, str); - return; - } - } + /* number wasn't a special number */ + while (*buf == '#') { + switch(*++buf) { + case 'e': case 'E': case 'i': case 'I': + if (has_exactp) { + klispE_throw_simple_with_irritants( + K, "two exactness prefixes", 1, str); + return; + } + has_exactp = true; + exactp = (*buf == 'e'); + ++buf; + break; + case 'b': case 'B': radix = 2; goto RADIX; + case 'o': case 'O': radix = 8; goto RADIX; + case 'd': case 'D': radix = 10; goto RADIX; + case 'x': case 'X': radix = 16; goto RADIX; + RADIX: + if (has_radixp) { + klispE_throw_simple_with_irritants( + K, "two radix prefixes", 1, str); + return; + } + has_radixp = true; + ++buf; + break; + default: + klispE_throw_simple_with_irritants(K, "unexpected char " + "after #", 1, str); + return; + } + } + + if (radix == 10) { + /* only allow decimals with radix 10 */ + bool decimalp = false; + if (!krational_read_decimal(K, buf, radix, &res, NULL, &decimalp)) { + klispE_throw_simple_with_irritants(K, "Bad format", 1, str); + return; + } + if (decimalp && !has_exactp) { + /* handle decimal format as an explicit #i */ + has_exactp = true; + exactp = false; + } + } else { + if (!krational_read(K, buf, radix, &res, NULL)) { + klispE_throw_simple_with_irritants(K, "Bad format", 1, str); + return; + } + } - if (has_exactp && !exactp) { - krooted_tvs_push(K, res); - res = kexact_to_inexact(K, res); - krooted_tvs_pop(K); - } + if (has_exactp && !exactp) { + krooted_tvs_push(K, res); + res = kexact_to_inexact(K, res); + krooted_tvs_pop(K); + } } kapply_cc(K, res); } @@ -2506,26 +2506,26 @@ void kinit_numbers_ground_env(klisp_State *K) /* No complex or bounded reals for now */ /* 12.5.1 number?, finite?, integer? */ add_applicative(K, ground_env, "number?", ftypep, 2, symbol, - p2tv(knumberp)); + p2tv(knumberp)); add_applicative(K, ground_env, "finite?", ftyped_predp, 3, symbol, - p2tv(knumber_wpvp), p2tv(kfinitep)); + p2tv(knumber_wpvp), p2tv(kfinitep)); add_applicative(K, ground_env, "integer?", ftypep, 2, symbol, - p2tv(kintegerp)); + p2tv(kintegerp)); /* 12.5.? exact-integer? */ add_applicative(K, ground_env, "exact-integer?", ftypep, 2, symbol, - p2tv(keintegerp)); + p2tv(keintegerp)); /* 12.5.2 =? */ add_applicative(K, ground_env, "=?", ftyped_kbpredp, 3, - symbol, p2tv(knumber_wpvp), p2tv(knum_eqp)); + symbol, p2tv(knumber_wpvp), p2tv(knum_eqp)); /* 12.5.3 <?, <=?, >?, >=? */ add_applicative(K, ground_env, "<?", ftyped_kbpredp, 3, - symbol, p2tv(kreal_wpvp), p2tv(knum_ltp)); + symbol, p2tv(kreal_wpvp), p2tv(knum_ltp)); add_applicative(K, ground_env, "<=?", ftyped_kbpredp, 3, - symbol, p2tv(kreal_wpvp), p2tv(knum_lep)); + symbol, p2tv(kreal_wpvp), p2tv(knum_lep)); add_applicative(K, ground_env, ">?", ftyped_kbpredp, 3, - symbol, p2tv(kreal_wpvp), p2tv(knum_gtp)); + symbol, p2tv(kreal_wpvp), p2tv(knum_gtp)); add_applicative(K, ground_env, ">=?", ftyped_kbpredp, 3, - symbol, p2tv(kreal_wpvp), p2tv(knum_gep)); + symbol, p2tv(kreal_wpvp), p2tv(knum_gep)); /* 12.5.4 + */ add_applicative(K, ground_env, "+", kplus, 0); /* 12.5.5 * */ @@ -2534,31 +2534,31 @@ void kinit_numbers_ground_env(klisp_State *K) add_applicative(K, ground_env, "-", kminus, 0); /* 12.5.7 zero? */ add_applicative(K, ground_env, "zero?", ftyped_predp, 3, symbol, - p2tv(knumber_wpvp), p2tv(kzerop)); + p2tv(knumber_wpvp), p2tv(kzerop)); /* 12.5.8 div, mod, div-and-mod */ add_applicative(K, ground_env, "div", kdiv_mod, 2, symbol, - i2tv(FDIV_DIV)); + i2tv(FDIV_DIV)); add_applicative(K, ground_env, "mod", kdiv_mod, 2, symbol, - i2tv(FDIV_MOD)); + i2tv(FDIV_MOD)); add_applicative(K, ground_env, "div-and-mod", kdiv_mod, 2, symbol, - i2tv(FDIV_DIV | FDIV_MOD)); + i2tv(FDIV_DIV | FDIV_MOD)); /* 12.5.9 div0, mod0, div0-and-mod0 */ add_applicative(K, ground_env, "div0", kdiv_mod, 2, symbol, - i2tv(FDIV_ZERO | FDIV_DIV)); + i2tv(FDIV_ZERO | FDIV_DIV)); add_applicative(K, ground_env, "mod0", kdiv_mod, 2, symbol, - i2tv(FDIV_ZERO | FDIV_MOD)); + i2tv(FDIV_ZERO | FDIV_MOD)); add_applicative(K, ground_env, "div0-and-mod0", kdiv_mod, 2, symbol, - i2tv(FDIV_ZERO | FDIV_DIV | FDIV_MOD)); + i2tv(FDIV_ZERO | FDIV_DIV | FDIV_MOD)); /* 12.5.10 positive?, negative? */ add_applicative(K, ground_env, "positive?", ftyped_predp, 3, symbol, - p2tv(kreal_wpvp), p2tv(kpositivep)); + p2tv(kreal_wpvp), p2tv(kpositivep)); add_applicative(K, ground_env, "negative?", ftyped_predp, 3, symbol, - p2tv(kreal_wpvp), p2tv(knegativep)); + p2tv(kreal_wpvp), p2tv(knegativep)); /* 12.5.11 odd?, even? */ add_applicative(K, ground_env, "odd?", ftyped_predp, 3, symbol, - p2tv(kintegerp), p2tv(koddp)); + p2tv(kintegerp), p2tv(koddp)); add_applicative(K, ground_env, "even?", ftyped_predp, 3, symbol, - p2tv(kintegerp), p2tv(kevenp)); + p2tv(kintegerp), p2tv(kevenp)); /* 12.5.12 abs */ add_applicative(K, ground_env, "abs", kabs, 0); /* 12.5.13 min, max */ @@ -2569,23 +2569,23 @@ void kinit_numbers_ground_env(klisp_State *K) add_applicative(K, ground_env, "lcm", klcm, 0); /* 12.6.1 exact?, inexact?, robust?, undefined? */ add_applicative(K, ground_env, "exact?", ftyped_predp, 3, symbol, - p2tv(knumberp), p2tv(kexactp)); + p2tv(knumberp), p2tv(kexactp)); add_applicative(K, ground_env, "inexact?", ftyped_predp, 3, symbol, - p2tv(knumberp), p2tv(kinexactp)); + p2tv(knumberp), p2tv(kinexactp)); add_applicative(K, ground_env, "robust?", ftyped_predp, 3, symbol, - p2tv(knumberp), p2tv(krobustp)); + p2tv(knumberp), p2tv(krobustp)); add_applicative(K, ground_env, "undefined?", ftyped_predp, 3, symbol, - p2tv(knumberp), p2tv(kundefinedp)); + p2tv(knumberp), p2tv(kundefinedp)); /* 12.6.2 get-real-internal-bounds, get-real-exact-bounds */ add_applicative(K, ground_env, "get-real-internal-bounds", - kget_real_internal_bounds, 0); + kget_real_internal_bounds, 0); add_applicative(K, ground_env, "get-real-exact-bounds", - kget_real_exact_bounds, 0); + kget_real_exact_bounds, 0); /* 12.6.3 get-real-internal-primary, get-real-exact-primary */ add_applicative(K, ground_env, "get-real-internal-primary", - kget_real_internal_primary, 0); + kget_real_internal_primary, 0); add_applicative(K, ground_env, "get-real-exact-primary", - kget_real_exact_primary, 0); + kget_real_exact_primary, 0); /* 12.6.4 make-inexact */ add_applicative(K, ground_env, "make-inexact", kmake_inexact, 0); /* 12.6.5 real->inexact, real->exact */ @@ -2593,12 +2593,12 @@ void kinit_numbers_ground_env(klisp_State *K) add_applicative(K, ground_env, "real->exact", kreal_to_exact, 0); /* 12.6.6 with-strict-arithmetic, get-strict-arithmetic? */ add_applicative(K, ground_env, "with-strict-arithmetic", - kwith_strict_arithmetic, 0); + kwith_strict_arithmetic, 0); add_applicative(K, ground_env, "get-strict-arithmetic?", - kget_strict_arithmeticp, 0); + kget_strict_arithmeticp, 0); /* 12.8.1 rational? */ add_applicative(K, ground_env, "rational?", ftypep, 2, symbol, - p2tv(krationalp)); + p2tv(krationalp)); /* 12.8.2 / */ add_applicative(K, ground_env, "/", kdivided, 0); /* 12.8.3 numerator, denominator */ @@ -2606,19 +2606,19 @@ void kinit_numbers_ground_env(klisp_State *K) add_applicative(K, ground_env, "denominator", kdenominator, 0); /* 12.8.4 floor, ceiling, truncate, round */ add_applicative(K, ground_env, "floor", kreal_to_integer, 2, - symbol, i2tv((int32_t) K_FLOOR)); + symbol, i2tv((int32_t) K_FLOOR)); add_applicative(K, ground_env, "ceiling", kreal_to_integer, 2, - symbol, i2tv((int32_t) K_CEILING)); + symbol, i2tv((int32_t) K_CEILING)); add_applicative(K, ground_env, "truncate", kreal_to_integer, 2, - symbol, i2tv((int32_t) K_TRUNCATE)); + symbol, i2tv((int32_t) K_TRUNCATE)); add_applicative(K, ground_env, "round", kreal_to_integer, 2, - symbol, i2tv((int32_t) K_ROUND_EVEN)); + symbol, i2tv((int32_t) K_ROUND_EVEN)); /* 12.8.5 rationalize, simplest-rational */ add_applicative(K, ground_env, "rationalize", krationalize, 0); add_applicative(K, ground_env, "simplest-rational", ksimplest_rational, 0); /* 12.9.1 real? */ add_applicative(K, ground_env, "real?", ftypep, 2, symbol, - p2tv(krealp)); + p2tv(krealp)); /* 12.9.2 exp, log */ add_applicative(K, ground_env, "exp", kexp, 0); add_applicative(K, ground_env, "log", klog, 0); diff --git a/src/kgpair_mut.c b/src/kgpair_mut.c @@ -30,7 +30,7 @@ void set_carB(klisp_State *K) (void) denv; (void) xparams; bind_2tp(K, ptree, "pair", ttispair, pair, - "any", anytype, new_car); + "any", anytype, new_car); if(!kis_mutable(pair)) { klispE_throw_simple(K, "immutable pair"); @@ -49,7 +49,7 @@ void set_cdrB(klisp_State *K) (void) denv; (void) xparams; bind_2tp(K, ptree, "pair", ttispair, pair, - "any", anytype, new_cdr); + "any", anytype, new_cdr); if(!kis_mutable(pair)) { klispE_throw_simple(K, "immutable pair"); @@ -100,19 +100,19 @@ void encycleB(klisp_State *K) UNUSED(xparams); bind_3tp(K, ptree, "any", anytype, obj, - "exact integer", keintegerp, tk1, - "exact integer", keintegerp, tk2); + "exact integer", keintegerp, tk1, + "exact integer", keintegerp, tk2); if (knegativep(tk1) || knegativep(tk2)) { - klispE_throw_simple(K, "negative index"); - return; + klispE_throw_simple(K, "negative index"); + return; } if (!ttisfixint(tk1) || !ttisfixint(tk2)) { - /* no list can have that many pairs */ - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return; + /* no list can have that many pairs */ + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return; } int32_t k1 = ivalue(tk1); @@ -121,19 +121,19 @@ void encycleB(klisp_State *K) TValue tail = obj; while(k1 != 0) { - if (!ttispair(tail)) { - unmark_list(K, obj); - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return; - } else if (kis_marked(tail)) { - unmark_list(K, obj); - klispE_throw_simple(K, "too few pairs in cyclic list"); - return; - } - kmark(tail); - tail = kcdr(tail); - --k1; + if (!ttispair(tail)) { + unmark_list(K, obj); + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return; + } else if (kis_marked(tail)) { + unmark_list(K, obj); + klispE_throw_simple(K, "too few pairs in cyclic list"); + return; + } + kmark(tail); + tail = kcdr(tail); + --k1; } TValue fcp = tail; @@ -141,40 +141,40 @@ void encycleB(klisp_State *K) /* if k2 == 0 do nothing (but this still checks that the obj has at least k1 pairs */ if (k2 != 0) { - --k2; /* to have cycle length k2 we should discard k2-1 pairs */ - /* REFACTOR: should probably refactor this to avoid the - duplicated checks */ - while(k2 != 0) { - if (!ttispair(tail)) { - unmark_list(K, obj); - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return; - } else if (kis_marked(tail)) { - unmark_list(K, obj); - klispE_throw_simple(K, "too few pairs in cyclic list"); - return; - } - kmark(tail); - tail = kcdr(tail); - --k2; - } - if (!ttispair(tail)) { - unmark_list(K, obj); - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return; - } else if (kis_marked(tail)) { - unmark_list(K, obj); - klispE_throw_simple(K, "too few pairs in cyclic list"); - return; - } else if (!kis_mutable(tail)) { - unmark_list(K, obj); - klispE_throw_simple(K, "immutable pair"); - return; - } else { - kset_cdr(tail, fcp); - } + --k2; /* to have cycle length k2 we should discard k2-1 pairs */ + /* REFACTOR: should probably refactor this to avoid the + duplicated checks */ + while(k2 != 0) { + if (!ttispair(tail)) { + unmark_list(K, obj); + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return; + } else if (kis_marked(tail)) { + unmark_list(K, obj); + klispE_throw_simple(K, "too few pairs in cyclic list"); + return; + } + kmark(tail); + tail = kcdr(tail); + --k2; + } + if (!ttispair(tail)) { + unmark_list(K, obj); + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return; + } else if (kis_marked(tail)) { + unmark_list(K, obj); + klispE_throw_simple(K, "too few pairs in cyclic list"); + return; + } else if (!kis_mutable(tail)) { + unmark_list(K, obj); + klispE_throw_simple(K, "immutable pair"); + return; + } else { + kset_cdr(tail, fcp); + } } unmark_list(K, obj); kapply_cc(K, KINERT); @@ -193,36 +193,36 @@ void list_setB(klisp_State *K) UNUSED(xparams); bind_3tp(K, ptree, "any", anytype, obj, - "exact integer", keintegerp, tk, - "any", anytype, val); + "exact integer", keintegerp, tk, + "any", anytype, val); if (knegativep(tk)) { - klispE_throw_simple(K, "negative index"); - return; + klispE_throw_simple(K, "negative index"); + return; } int32_t k = (ttisfixint(tk))? ivalue(tk) - : ksmallest_index(K, obj, tk); + : ksmallest_index(K, obj, tk); while(k) { - if (!ttispair(obj)) { - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return; - } - obj = kcdr(obj); - --k; + if (!ttispair(obj)) { + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return; + } + obj = kcdr(obj); + --k; } if (!ttispair(obj)) { - klispE_throw_simple(K, "non pair found while traversing " - "object"); + klispE_throw_simple(K, "non pair found while traversing " + "object"); } else if (kis_immutable(obj)) { - /* this could be checked before, but the error here seems better */ - klispE_throw_simple(K, "immutable pair"); + /* this could be checked before, but the error here seems better */ + klispE_throw_simple(K, "immutable pair"); } else { - kset_car(obj, val); - kapply_cc(K, KINERT); + kset_car(obj, val); + kapply_cc(K, KINERT); } } @@ -231,9 +231,9 @@ inline void appendB_clear_last_pairs(klisp_State *K, TValue ls) { UNUSED(K); while(ttispair(ls) && kis_marked(ls)) { - TValue first = ls; - ls = kget_mark(ls); - kunmark(first); + TValue first = ls; + ls = kget_mark(ls); + kunmark(first); } } @@ -244,7 +244,7 @@ inline void appendB_clear_last_pairs(klisp_State *K, TValue ls) /* GC: Assumes lss is rooted */ TValue appendB_get_lss_endpoints(klisp_State *K, TValue lss, int32_t apairs, - int32_t cpairs) + int32_t cpairs) { TValue elist = kcons(K, KNIL, KNIL); krooted_vars_push(K, &elist); @@ -255,126 +255,126 @@ TValue appendB_get_lss_endpoints(klisp_State *K, TValue lss, int32_t apairs, TValue last_apair = KNIL; while(apairs != 0 || cpairs != 0) { - int32_t pairs; + int32_t pairs; - if (apairs == 0) { - /* this is the first run of the loop (if there is no acyclic part) - or the second run of the loop (the cyclic part), - must remember the last acyclic pair to encycle! the result */ - last_apair = last_pair; - pairs = cpairs; - } else { - /* this is the first (maybe only) run of the loop - (the acyclic part) */ - pairs = apairs; - } - - while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - - /* skip over non final nils, but final nil - should be added as last pair to let the result - be even */ - if (ttisnil(first)) { - if (ttisnil(tail)) { - kset_cdr(last_pair, kcons(K, first, KNIL)); - } - continue; - } - - TValue ftail = first; - TValue flastp = first; - - /* find the last pair to check the object */ - while(ttispair(ftail) && !kis_marked(ftail)) { - kmark(ftail); - flastp = ftail; /* remember last pair */ - ftail = kcdr(ftail); - } + if (apairs == 0) { + /* this is the first run of the loop (if there is no acyclic part) + or the second run of the loop (the cyclic part), + must remember the last acyclic pair to encycle! the result */ + last_apair = last_pair; + pairs = cpairs; + } else { + /* this is the first (maybe only) run of the loop + (the acyclic part) */ + pairs = apairs; + } + + while(pairs--) { + TValue first = kcar(tail); + tail = kcdr(tail); + + /* skip over non final nils, but final nil + should be added as last pair to let the result + be even */ + if (ttisnil(first)) { + if (ttisnil(tail)) { + kset_cdr(last_pair, kcons(K, first, KNIL)); + } + continue; + } + + TValue ftail = first; + TValue flastp = first; + + /* find the last pair to check the object */ + while(ttispair(ftail) && !kis_marked(ftail)) { + kmark(ftail); + flastp = ftail; /* remember last pair */ + ftail = kcdr(ftail); + } - /* can't unmark the list till the errors are checked, - otherwise the unmarking may be incorrect */ - if (ttisnil(tail)) { - /* last argument has special treatment */ - if (ttispair(ftail) && ttisnil(kcdr(ftail))) { - /* repeated last pair, this is the only check - that is done on the last argument */ - appendB_clear_last_pairs(K, last_pairs); - unmark_list(K, first); - klispE_throw_simple(K, "repeated last pairs"); - return KINERT; - } else { - unmark_list(K, first); - /* add last object to the endpoints list, don't add - its last pair */ - kset_cdr(last_pair, kcons(K, first, KNIL)); - } - } else { /* non final argument, must be an acyclic list - with unique, mutable last pair */ - if (ttisnil(ftail)) { - /* acyclic list with non repeated last pair, - check mutability */ - unmark_list(K, first); - if (kis_immutable(flastp)) { - appendB_clear_last_pairs(K, last_pairs); - klispE_throw_simple(K, "immutable pair found"); - return KINERT; - } - /* add the last pair to the list of last pairs */ - kset_mark(flastp, last_pairs); - last_pairs = flastp; + /* can't unmark the list till the errors are checked, + otherwise the unmarking may be incorrect */ + if (ttisnil(tail)) { + /* last argument has special treatment */ + if (ttispair(ftail) && ttisnil(kcdr(ftail))) { + /* repeated last pair, this is the only check + that is done on the last argument */ + appendB_clear_last_pairs(K, last_pairs); + unmark_list(K, first); + klispE_throw_simple(K, "repeated last pairs"); + return KINERT; + } else { + unmark_list(K, first); + /* add last object to the endpoints list, don't add + its last pair */ + kset_cdr(last_pair, kcons(K, first, KNIL)); + } + } else { /* non final argument, must be an acyclic list + with unique, mutable last pair */ + if (ttisnil(ftail)) { + /* acyclic list with non repeated last pair, + check mutability */ + unmark_list(K, first); + if (kis_immutable(flastp)) { + appendB_clear_last_pairs(K, last_pairs); + klispE_throw_simple(K, "immutable pair found"); + return KINERT; + } + /* add the last pair to the list of last pairs */ + kset_mark(flastp, last_pairs); + last_pairs = flastp; - /* add both the first and last pair to the endpoints - list */ - TValue new_pair = kcons(K, first, KNIL); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - new_pair = kcons(K, flastp, KNIL); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - } else { - /* impoper list or repeated last pair or cyclic list */ - appendB_clear_last_pairs(K, last_pairs); - unmark_list(K, first); - - if (ttispair(ftail)) { - if (ttisnil(kcdr(ftail))) { - klispE_throw_simple(K, "repeated last pairs"); - } else { - klispE_throw_simple(K, "cyclic list as non last " - "argument"); - } - } else { - klispE_throw_simple(K, "improper list as non last " - "argument"); - } - return KINERT; - } - } - } - if (apairs != 0) { - /* acyclic part done */ - apairs = 0; - } else { - /* cyclic part done, program encycle if necessary */ - cpairs = 0; - if (!tv_equal(last_apair, last_pair)) { - TValue first_cpair = kcadr(last_apair); - kset_cdr(last_pair, kcons(K, first_cpair, KNIL)); - } else { - /* all elements of the cycle are (), add extra - nil to simplify the code setting the cdrs */ - kset_cdr(last_pair, kcons(K, KNIL, KNIL)); - } - } + /* add both the first and last pair to the endpoints + list */ + TValue new_pair = kcons(K, first, KNIL); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + new_pair = kcons(K, flastp, KNIL); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + } else { + /* impoper list or repeated last pair or cyclic list */ + appendB_clear_last_pairs(K, last_pairs); + unmark_list(K, first); + + if (ttispair(ftail)) { + if (ttisnil(kcdr(ftail))) { + klispE_throw_simple(K, "repeated last pairs"); + } else { + klispE_throw_simple(K, "cyclic list as non last " + "argument"); + } + } else { + klispE_throw_simple(K, "improper list as non last " + "argument"); + } + return KINERT; + } + } + } + if (apairs != 0) { + /* acyclic part done */ + apairs = 0; + } else { + /* cyclic part done, program encycle if necessary */ + cpairs = 0; + if (!tv_equal(last_apair, last_pair)) { + TValue first_cpair = kcadr(last_apair); + kset_cdr(last_pair, kcons(K, first_cpair, KNIL)); + } else { + /* all elements of the cycle are (), add extra + nil to simplify the code setting the cdrs */ + kset_cdr(last_pair, kcons(K, KNIL, KNIL)); + } + } } appendB_clear_last_pairs(K, last_pairs); /* discard the first element (there is always one) because it - isn't necessary, the list is used to set the last pairs of - the objects to the correspoding next first pair */ + isn't necessary, the list is used to set the last pairs of + the objects to the correspoding next first pair */ krooted_vars_pop(K); return kcdr(kcdr(elist)); } @@ -389,34 +389,34 @@ void appendB(klisp_State *K) UNUSED(xparams); UNUSED(denv); if (ttisnil(ptree)) { - klispE_throw_simple(K, "no lists"); - return; + klispE_throw_simple(K, "no lists"); + return; } else if (!ttispair(ptree)) { - klispE_throw_simple(K, "bad ptree"); - return; + klispE_throw_simple(K, "bad ptree"); + return; } else if (ttisnil(kcar(ptree))) { - klispE_throw_simple(K, "empty first list"); - return; + klispE_throw_simple(K, "empty first list"); + return; } TValue lss = ptree; TValue first_ls = kcar(lss); int32_t pairs, cpairs; /* ASK John: if encycle! has only one argument, can't it be cyclic? - the report says no, but the wording is poor */ + the report says no, but the wording is poor */ check_list(K, false, first_ls, NULL, NULL); check_list(K, true, lss, &pairs, &cpairs); int32_t apairs = pairs - cpairs; TValue endpoints = - appendB_get_lss_endpoints(K, lss, apairs, cpairs); + appendB_get_lss_endpoints(K, lss, apairs, cpairs); /* connect all the last pairs to the corresponding next first pair, - endpoints is even */ + endpoints is even */ while(!ttisnil(endpoints)) { - TValue first = kcar(endpoints); - endpoints = kcdr(endpoints); - TValue second = kcar(endpoints); - endpoints = kcdr(endpoints); - kset_cdr(first, second); + TValue first = kcar(endpoints); + endpoints = kcdr(endpoints); + TValue second = kcar(endpoints); + endpoints = kcdr(endpoints); + kset_cdr(first, second); } kapply_cc(K, KINERT); } @@ -442,12 +442,12 @@ void assq(klisp_State *K) TValue tail = ls; TValue res = KNIL; while(pairs--) { - TValue first = kcar(tail); - if (eq2p(K, kcar(first), obj)) { - res = first; - break; - } - tail = kcdr(tail); + TValue first = kcar(tail); + if (eq2p(K, kcar(first), obj)) { + res = first; + break; + } + tail = kcdr(tail); } kapply_cc(K, res); @@ -471,12 +471,12 @@ void memqp(klisp_State *K) TValue tail = ls; TValue res = KFALSE; while(pairs--) { - TValue first = kcar(tail); - if (eq2p(K, first, obj)) { - res = KTRUE; - break; - } - tail = kcdr(tail); + TValue first = kcar(tail); + if (eq2p(K, first, obj)) { + res = KTRUE; + break; + } + tail = kcdr(tail); } kapply_cc(K, res); @@ -496,7 +496,7 @@ void kinit_pair_mut_ground_env(klisp_State *K) add_applicative(K, ground_env, "set-cdr!", set_cdrB, 0); /* 4.7.2 copy-es-immutable */ add_applicative(K, ground_env, "copy-es-immutable", copy_es, 2, symbol, - b2tv(false)); + b2tv(false)); /* 5.8.1 encycle! */ add_applicative(K, ground_env, "encycle!", encycleB, 0); /* 6.?? list-set! */ @@ -511,7 +511,7 @@ void kinit_pair_mut_ground_env(klisp_State *K) add_applicative(K, ground_env, "memq?", memqp, 0); /* ?.? immutable-pair?, mutable-pair? */ add_applicative(K, ground_env, "immutable-pair?", ftypep, 2, symbol, - p2tv(kimmutable_pairp)); + p2tv(kimmutable_pairp)); add_applicative(K, ground_env, "mutable-pair?", ftypep, 2, symbol, - p2tv(kmutable_pairp)); + p2tv(kmutable_pairp)); } diff --git a/src/kgpairs_lists.c b/src/kgpairs_lists.c @@ -79,8 +79,8 @@ void listS(klisp_State *K) UNUSED(denv); if (ttisnil(ptree)) { - klispE_throw_simple(K, "empty argument list"); - return; + klispE_throw_simple(K, "empty argument list"); + return; } TValue res_obj = kcons(K, KNIL, KNIL); krooted_vars_push(K, &res_obj); @@ -89,41 +89,41 @@ void listS(klisp_State *K) /* First copy the list, but remembering the next to last pair */ while(ttispair(tail) && !kis_marked(tail)) { - kmark(tail); - /* we save the next_to last pair in the cdr to - allow the change into an improper list later */ - TValue new_pair = kcons(K, kcar(tail), last_pair); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - tail = kcdr(tail); + kmark(tail); + /* we save the next_to last pair in the cdr to + allow the change into an improper list later */ + TValue new_pair = kcons(K, kcar(tail), last_pair); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + tail = kcdr(tail); } unmark_list(K, ptree); if (ttisnil(tail)) { - /* Now eliminate the last pair to get the correct improper list. - This avoids an if in the above loop. It's inside the if because - we need at least one pair for this to work. */ - TValue next_to_last_pair = kcdr(last_pair); - kset_cdr(next_to_last_pair, kcar(last_pair)); - krooted_vars_pop(K); - kapply_cc(K, kcdr(res_obj)); + /* Now eliminate the last pair to get the correct improper list. + This avoids an if in the above loop. It's inside the if because + we need at least one pair for this to work. */ + TValue next_to_last_pair = kcdr(last_pair); + kset_cdr(next_to_last_pair, kcar(last_pair)); + krooted_vars_pop(K); + kapply_cc(K, kcdr(res_obj)); } else if (ttispair(tail)) { /* cyclic argument list */ - klispE_throw_simple(K, "cyclic argument list"); - return; + klispE_throw_simple(K, "cyclic argument list"); + return; } else { - klispE_throw_simple(K, "argument list is improper"); - return; + klispE_throw_simple(K, "argument list is improper"); + return; } } /* Helper macros to construct xparams[1] for c[ad]{1,4}r */ -#define C_AD_R_PARAM(len_, br_) \ +#define C_AD_R_PARAM(len_, br_) \ (i2tv((C_AD_R_LEN(len_) | (C_AD_R_BRANCH(br_))))) #define C_AD_R_LEN(len_) ((len_) << 4) -#define C_AD_R_BRANCH(br_) \ - ((br_ & 0x0001? 0x1 : 0) | \ - (br_ & 0x0010? 0x2 : 0) | \ - (br_ & 0x0100? 0x4 : 0) | \ +#define C_AD_R_BRANCH(br_) \ + ((br_ & 0x0001? 0x1 : 0) | \ + (br_ & 0x0010? 0x2 : 0) | \ + (br_ & 0x0100? 0x4 : 0) | \ (br_ & 0x1000? 0x8 : 0)) /* 5.4.1 car, cdr */ @@ -140,14 +140,14 @@ void c_ad_r(klisp_State *K) /* ** xparams[0]: name as symbol ** xparams[1]: an int with the less significant 2 nibbles - ** standing for the count and the branch selection. - ** The high nibble is the count: that is the number of - ** 'a's and 'd's in the name, for example: - ** 0x1? for car and cdr. - ** 0x2? for caar, cadr, cdar and cddr. - ** The low nibble is the branch selection, a 0 bit means - ** car, a 1 bit means cdr, the first bit to be applied - ** is bit 0 so: caar=0x20, cadr=0x21, cdar:0x22, cddr 0x23 + ** standing for the count and the branch selection. + ** The high nibble is the count: that is the number of + ** 'a's and 'd's in the name, for example: + ** 0x1? for car and cdr. + ** 0x2? for caar, cadr, cdar and cddr. + ** The low nibble is the branch selection, a 0 bit means + ** car, a 1 bit means cdr, the first bit to be applied + ** is bit 0 so: caar=0x20, cadr=0x21, cdar:0x22, cddr 0x23 */ int p = ivalue(xparams[1]); @@ -157,13 +157,13 @@ void c_ad_r(klisp_State *K) bind_1p(K, ptree, obj); while(count) { - if (!ttispair(obj)) { - klispE_throw_simple(K, "non pair found while traversing"); - return; - } - obj = ((branches & 1) == 0)? kcar(obj) : kcdr(obj); - branches >>= 1; - --count; + if (!ttispair(obj)) { + klispE_throw_simple(K, "non pair found while traversing"); + return; + } + obj = ((branches & 1) == 0)? kcar(obj) : kcdr(obj); + branches >>= 1; + --count; } kapply_cc(K, obj); } @@ -195,7 +195,7 @@ void make_list(klisp_State *K) int i = ivalue(tv_s); krooted_vars_push(K, &tail); while(i-- > 0) { - tail = kcons(K, fill, tail); + tail = kcons(K, fill, tail); } krooted_vars_pop(K); @@ -234,19 +234,19 @@ void reverse(klisp_State *K) TValue res = KNIL; krooted_vars_push(K, &res); while(ttispair(tail) && !kis_marked(tail)) { - kmark(tail); - res = kcons(K, kcar(tail), res); - tail = kcdr(tail); + kmark(tail); + res = kcons(K, kcar(tail), res); + tail = kcdr(tail); } unmark_list(K, ls); krooted_vars_pop(K); if (ttispair(tail)) { - klispE_throw_simple(K, "expected acyclic list"); + klispE_throw_simple(K, "expected acyclic list"); } else if (!ttisnil(tail)) { - klispE_throw_simple(K, "expected list"); + klispE_throw_simple(K, "expected list"); } else { - kapply_cc(K, res); + kapply_cc(K, res); } } @@ -266,7 +266,7 @@ void get_list_metrics(klisp_State *K) get_list_metrics_aux(K, obj, &pairs, &nils, &apairs, &cpairs); TValue res = klist(K, 4, i2tv(pairs), i2tv(nils), - i2tv(apairs), i2tv(cpairs)); + i2tv(apairs), i2tv(cpairs)); kapply_cc(K, res); } @@ -283,24 +283,24 @@ void list_tail(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "any", anytype, obj, - "exact integer", keintegerp, tk); + "exact integer", keintegerp, tk); if (knegativep(tk)) { - klispE_throw_simple(K, "negative index"); - return; + klispE_throw_simple(K, "negative index"); + return; } int32_t k = (ttisfixint(tk))? ivalue(tk) - : ksmallest_index(K, obj, tk); + : ksmallest_index(K, obj, tk); while(k) { - if (!ttispair(obj)) { - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return; - } - obj = kcdr(obj); - --k; + if (!ttispair(obj)) { + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return; + } + obj = kcdr(obj); + --k; } kapply_cc(K, obj); } @@ -320,9 +320,9 @@ void length(klisp_State *K) TValue tail = obj; int pairs = 0; while(ttispair(tail) && !kis_marked(tail)) { - kmark(tail); - tail = kcdr(tail); - ++pairs; + kmark(tail); + tail = kcdr(tail); + ++pairs; } unmark_list(K, obj); @@ -344,29 +344,29 @@ void list_ref(klisp_State *K) UNUSED(xparams); bind_2tp(K, ptree, "any", anytype, obj, - "exact integer", keintegerp, tk); + "exact integer", keintegerp, tk); if (knegativep(tk)) { - klispE_throw_simple(K, "negative index"); - return; + klispE_throw_simple(K, "negative index"); + return; } int32_t k = (ttisfixint(tk))? ivalue(tk) - : ksmallest_index(K, obj, tk); + : ksmallest_index(K, obj, tk); while(k) { - if (!ttispair(obj)) { - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return; - } - obj = kcdr(obj); - --k; + if (!ttispair(obj)) { + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return; + } + obj = kcdr(obj); + --k; } if (!ttispair(obj)) { - klispE_throw_simple(K, "non pair found while traversing " - "object"); - return; + klispE_throw_simple(K, "non pair found while traversing " + "object"); + return; } TValue res = kcar(obj); kapply_cc(K, res); @@ -380,11 +380,11 @@ void list_ref(klisp_State *K) /* GC: Assumes obj is rooted */ TValue append_check_copy_list(klisp_State *K, char *name, TValue obj, - TValue *last_pair_ptr) + TValue *last_pair_ptr) { /* return early if nil to avoid setting *last_pair_ptr */ if (ttisnil(obj)) - return obj; + return obj; TValue copy = kcons(K, KNIL, KNIL); krooted_vars_push(K, &copy); @@ -392,20 +392,20 @@ TValue append_check_copy_list(klisp_State *K, char *name, TValue obj, TValue tail = obj; while(ttispair(tail) && !kis_marked(tail)) { - kmark(tail); - TValue new_pair = kcons(K, kcar(tail), KNIL); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - tail = kcdr(tail); + kmark(tail); + TValue new_pair = kcons(K, kcar(tail), KNIL); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + tail = kcdr(tail); } unmark_list(K, obj); if (ttispair(tail)) { - klispE_throw_simple(K, "expected acyclic list"); - return KINERT; + klispE_throw_simple(K, "expected acyclic list"); + return KINERT; } else if (!ttisnil(tail)) { - klispE_throw_simple(K, "expected list"); - return KINERT; + klispE_throw_simple(K, "expected list"); + return KINERT; } *last_pair_ptr = last_pair; krooted_vars_pop(K); @@ -433,48 +433,48 @@ void append(klisp_State *K) TValue last_apair; while (apairs != 0 || cpairs != 0) { - if (apairs == 0) { - /* this is the first run of the loop (if there is no acyclic part) - or the second run of the loop (the cyclic part), - must remember the last acyclic pair to encycle! the result */ - last_apair = last_pair; - pairs = cpairs; - } else { - /* this is the first (maybe only) run of the loop - (the acyclic part) */ - pairs = apairs; - } - - while (pairs--) { - TValue first = kcar(lss); - lss = kcdr(lss); - TValue next_list; - TValue new_last_pair = last_pair; /* this helps if first is nil */ - /* don't check or copy last list */ - if (ttisnil(lss)) { - /* here, new_last_pair is bogus, but it isn't necessary - anymore so don't set it */ - next_list = first; - } else { - next_list = append_check_copy_list(K, "append", first, - &new_last_pair); - } - kset_cdr(last_pair, next_list); - last_pair = new_last_pair; - } - - if (apairs != 0) { - /* acyclic part done */ - apairs = 0; - } else { - /* cyclic part done */ - cpairs = 0; - TValue first_cpair = kcdr(last_apair); - TValue last_cpair = last_pair; - /* this works even if there is no cycle to be formed - (kcdr(last_apair) == ()) */ - kset_cdr(last_cpair, first_cpair); /* encycle! */ - } + if (apairs == 0) { + /* this is the first run of the loop (if there is no acyclic part) + or the second run of the loop (the cyclic part), + must remember the last acyclic pair to encycle! the result */ + last_apair = last_pair; + pairs = cpairs; + } else { + /* this is the first (maybe only) run of the loop + (the acyclic part) */ + pairs = apairs; + } + + while (pairs--) { + TValue first = kcar(lss); + lss = kcdr(lss); + TValue next_list; + TValue new_last_pair = last_pair; /* this helps if first is nil */ + /* don't check or copy last list */ + if (ttisnil(lss)) { + /* here, new_last_pair is bogus, but it isn't necessary + anymore so don't set it */ + next_list = first; + } else { + next_list = append_check_copy_list(K, "append", first, + &new_last_pair); + } + kset_cdr(last_pair, next_list); + last_pair = new_last_pair; + } + + if (apairs != 0) { + /* acyclic part done */ + apairs = 0; + } else { + /* cyclic part done */ + cpairs = 0; + TValue first_cpair = kcdr(last_apair); + TValue last_cpair = last_pair; + /* this works even if there is no cycle to be formed + (kcdr(last_apair) == ()) */ + kset_cdr(last_cpair, first_cpair); /* encycle! */ + } } krooted_vars_pop(K); kapply_cc(K, kcdr(res_list)); @@ -504,30 +504,30 @@ void list_neighbors(klisp_State *K) bool doing_cycle = false; while(count > 0 || !doing_cycle) { - while(count-- > 0) { /* can be -1 if ls is nil */ - TValue first = kcar(tail); - tail = kcdr(tail); /* tail advances one place per iter */ - TValue new_car = klist(K, 2, first, kcar(tail)); - krooted_tvs_push(K, new_car); - TValue new_pair = kcons(K, new_car, KNIL); - krooted_tvs_pop(K); - kset_cdr(last_pair, new_pair); - last_pair = new_pair; - } - - if (doing_cycle) { - TValue first_cpair = kcdr(last_apair); - kset_cdr(last_pair, first_cpair); - } else { /* this is done even if cpairs is 0 to terminate the loop */ - doing_cycle = true; - /* must remember first cycle pair to reconstruct the cycle, - we can save the last outside of the cycle and then check - its cdr */ - last_apair = last_pair; - count = cpairs; /* this contains the sublist that has the last - and first element of the cycle */ - /* this will loop once more */ - } + while(count-- > 0) { /* can be -1 if ls is nil */ + TValue first = kcar(tail); + tail = kcdr(tail); /* tail advances one place per iter */ + TValue new_car = klist(K, 2, first, kcar(tail)); + krooted_tvs_push(K, new_car); + TValue new_pair = kcons(K, new_car, KNIL); + krooted_tvs_pop(K); + kset_cdr(last_pair, new_pair); + last_pair = new_pair; + } + + if (doing_cycle) { + TValue first_cpair = kcdr(last_apair); + kset_cdr(last_pair, first_cpair); + } else { /* this is done even if cpairs is 0 to terminate the loop */ + doing_cycle = true; + /* must remember first cycle pair to reconstruct the cycle, + we can save the last outside of the cycle and then check + its cdr */ + last_apair = last_pair; + count = cpairs; /* this contains the sublist that has the last + and first element of the cycle */ + /* this will loop once more */ + } } krooted_vars_pop(K); kapply_cc(K, kcdr(neighbors)); @@ -571,14 +571,14 @@ void do_filter_encycle(klisp_State *K) TValue last_non_cycle_pair = xparams[1]; if (tv_equal(last_pair, last_non_cycle_pair)) { - /* no cycle in result, this isn't strictly necessary - but just in case */ - kset_cdr(last_non_cycle_pair, KNIL); + /* no cycle in result, this isn't strictly necessary + but just in case */ + kset_cdr(last_non_cycle_pair, KNIL); } else { - /* There are pairs in the cycle, so close it */ - TValue first_cycle_pair = kcdr(last_non_cycle_pair); - TValue last_cycle_pair = last_pair; - kset_cdr(last_cycle_pair, first_cycle_pair); + /* There are pairs in the cycle, so close it */ + TValue first_cycle_pair = kcdr(last_non_cycle_pair); + TValue last_cycle_pair = last_pair; + kset_cdr(last_cycle_pair, first_cycle_pair); } /* copy the list to avoid problems with continuations @@ -610,34 +610,34 @@ void do_filter(klisp_State *K) int32_t n = ivalue(xparams[3]); if (!ttisboolean(obj)) { - klispE_throw_simple(K, "expected boolean result"); - return; + klispE_throw_simple(K, "expected boolean result"); + return; } if (kis_true(obj)) { - TValue np = kcons(K, last_obj, KNIL); - kset_cdr(last_pair, np); - last_pair = np; + TValue np = kcons(K, last_obj, KNIL); + kset_cdr(last_pair, np); + last_pair = np; } if (n == 0) { /* pass the rest of the list and last pair for cycle handling */ - kapply_cc(K, kcons(K, ls, last_pair)); + kapply_cc(K, kcons(K, ls, last_pair)); } else { - TValue new_n = i2tv(n-1); - TValue first = kcar(ls); - TValue new_env = kmake_empty_environment(K); - krooted_tvs_push(K, new_env); - /* have to unwrap the applicative to avoid extra evaluation of first */ - TValue new_expr = klist(K, 2, kunwrap(app), first, KNIL); - krooted_tvs_push(K, new_expr); - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_filter, 4, app, - ls, last_pair, new_n); - kset_cc(K, new_cont); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - ktail_eval(K, new_expr, new_env); + TValue new_n = i2tv(n-1); + TValue first = kcar(ls); + TValue new_env = kmake_empty_environment(K); + krooted_tvs_push(K, new_env); + /* have to unwrap the applicative to avoid extra evaluation of first */ + TValue new_expr = klist(K, 2, kunwrap(app), first, KNIL); + krooted_tvs_push(K, new_expr); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_filter, 4, app, + ls, last_pair, new_n); + kset_cc(K, new_cont); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + ktail_eval(K, new_expr, new_env); } } @@ -662,15 +662,15 @@ void do_filter_cycle(klisp_State *K) /* this continuation will close the cycle and return the list */ TValue encycle_cont = - kmake_continuation(K, kget_cc(K), do_filter_encycle, 2, - dummy, last_apair); + kmake_continuation(K, kget_cc(K), do_filter_encycle, 2, + dummy, last_apair); krooted_tvs_push(K, encycle_cont); /* schedule the filtering of the elements of the cycle */ /* add inert before first element to be discarded when KFALSE is received */ TValue new_cont = - kmake_continuation(K, encycle_cont, do_filter, 4, app, - kcons(K, KINERT, ls), last_apair, cpairs); + kmake_continuation(K, encycle_cont, do_filter, 4, app, + kcons(K, KINERT, ls), last_apair, cpairs); kset_cc(K, new_cont); krooted_tvs_pop(K); /* this will be like a nop and will continue with do_filter */ @@ -687,7 +687,7 @@ void filter(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "applicative", ttisapplicative, app, - "any", anytype, ls); + "any", anytype, ls); /* copy the list to ignore changes made by the applicative */ /* REFACTOR: do this in a single pass */ /* ASK John: the semantics when this is mixed with continuations, @@ -705,17 +705,17 @@ void filter(klisp_State *K) krooted_tvs_push(K, dummy); TValue ret_cont = (cpairs == 0)? - kmake_continuation(K, kget_cc(K), do_ret_cdr, 1, dummy) - : kmake_continuation(K, kget_cc(K), do_filter_cycle, 3, - app, dummy, i2tv(cpairs)); + kmake_continuation(K, kget_cc(K), do_ret_cdr, 1, dummy) + : kmake_continuation(K, kget_cc(K), do_filter_cycle, 3, + app, dummy, i2tv(cpairs)); krooted_tvs_pop(K); /* already in cont */ krooted_tvs_push(K, ret_cont); /* add inert before first element to be discarded when KFALSE is received */ TValue new_cont = - kmake_continuation(K, ret_cont, do_filter, 4, app, - kcons(K, KINERT, ls), dummy, i2tv(pairs-cpairs)); + kmake_continuation(K, ret_cont, do_filter, 4, app, + kcons(K, KINERT, ls), dummy, i2tv(pairs-cpairs)); kset_cc(K, new_cont); krooted_tvs_pop(K); /* this will be a nop, and will continue with do_filter */ @@ -742,26 +742,26 @@ void do_assoc(klisp_State *K) int32_t pairs = ivalue(xparams[3]); if (!ttisboolean(obj)) { - klispE_throw_simple_with_irritants(K, "expected boolean", 1, obj); - return; + klispE_throw_simple_with_irritants(K, "expected boolean", 1, obj); + return; } else if (kis_true(obj) || pairs == 0) { - TValue res = kis_true(obj)? kcar(ls) : KNIL; - kapply_cc(K, res); + TValue res = kis_true(obj)? kcar(ls) : KNIL; + kapply_cc(K, res); } else { - /* object not YET found */ - TValue cont = kmake_continuation(K, kget_cc(K), do_assoc, 4, pred, - cmp_obj, kcdr(ls), i2tv(pairs-1)); - /* not necessary but may save a continuation in some cases */ - kset_bool_check_cont(cont); - kset_cc(K, cont); - TValue exp = kcons(K, kcar(kcar(kcdr(ls))), KNIL); - krooted_vars_push(K, &exp); - exp = kcons(K, cmp_obj, exp); - exp = kcons(K, pred, exp); - /* TEMP for now use an empty environment for dynamic env */ - TValue env = kmake_empty_environment(K); - krooted_vars_pop(K); - ktail_eval(K, exp, env); + /* object not YET found */ + TValue cont = kmake_continuation(K, kget_cc(K), do_assoc, 4, pred, + cmp_obj, kcdr(ls), i2tv(pairs-1)); + /* not necessary but may save a continuation in some cases */ + kset_bool_check_cont(cont); + kset_cc(K, cont); + TValue exp = kcons(K, kcar(kcar(kcdr(ls))), KNIL); + krooted_vars_push(K, &exp); + exp = kcons(K, cmp_obj, exp); + exp = kcons(K, pred, exp); + /* TEMP for now use an empty environment for dynamic env */ + TValue env = kmake_empty_environment(K); + krooted_vars_pop(K); + ktail_eval(K, exp, env); } } @@ -782,30 +782,30 @@ void assoc(klisp_State *K) TValue res; if (predp) { - /* we'll need use continuations, copy list first to - avoid troubles with mutation */ - ls = check_copy_list(K, ls, false, NULL, NULL); - krooted_vars_push(K, &ls); - ls = kcons(K, KINERT, ls); /* add dummy obj to stand as last - compared obj */ - TValue cont = kmake_continuation(K, kget_cc(K), do_assoc, 4, - maybe_pred, obj, ls, i2tv(pairs)); - krooted_vars_pop(K); - kset_cc(K, cont); - /* pass false to have it keep looking (in the whole list) */ - res = KFALSE; + /* we'll need use continuations, copy list first to + avoid troubles with mutation */ + ls = check_copy_list(K, ls, false, NULL, NULL); + krooted_vars_push(K, &ls); + ls = kcons(K, KINERT, ls); /* add dummy obj to stand as last + compared obj */ + TValue cont = kmake_continuation(K, kget_cc(K), do_assoc, 4, + maybe_pred, obj, ls, i2tv(pairs)); + krooted_vars_pop(K); + kset_cc(K, cont); + /* pass false to have it keep looking (in the whole list) */ + res = KFALSE; } else { - /* use equal?, no continuation needed */ - TValue tail = ls; - res = KNIL; - while(pairs--) { - TValue first = kcar(tail); - if (equal2p(K, kcar(first), obj)) { - res = first; - break; - } - tail = kcdr(tail); - } + /* use equal?, no continuation needed */ + TValue tail = ls; + res = KNIL; + while(pairs--) { + TValue first = kcar(tail); + if (equal2p(K, kcar(first), obj)) { + res = first; + break; + } + tail = kcdr(tail); + } } kapply_cc(K, res); } @@ -830,26 +830,26 @@ void do_memberp(klisp_State *K) int32_t pairs = ivalue(xparams[3]); if (!ttisboolean(obj)) { - klispE_throw_simple_with_irritants(K, "expected boolean", 1, obj); - return; + klispE_throw_simple_with_irritants(K, "expected boolean", 1, obj); + return; } else if (kis_true(obj) || pairs == 0) { - /* object found if obj is true and not found if obj is false */ - kapply_cc(K, obj); + /* object found if obj is true and not found if obj is false */ + kapply_cc(K, obj); } else { - /* object not YET found */ - TValue cont = kmake_continuation(K, kget_cc(K), do_memberp, 4, pred, - cmp_obj, kcdr(ls), i2tv(pairs-1)); - /* not necessary but may save a continuation in some cases */ - kset_bool_check_cont(cont); - kset_cc(K, cont); - TValue exp = kcons(K, kcar(ls), KNIL); - krooted_vars_push(K, &exp); - exp = kcons(K, cmp_obj, exp); - exp = kcons(K, pred, exp); - /* TEMP for now use an empty environment for dynamic env */ - TValue env = kmake_empty_environment(K); - krooted_vars_pop(K); - ktail_eval(K, exp, env); + /* object not YET found */ + TValue cont = kmake_continuation(K, kget_cc(K), do_memberp, 4, pred, + cmp_obj, kcdr(ls), i2tv(pairs-1)); + /* not necessary but may save a continuation in some cases */ + kset_bool_check_cont(cont); + kset_cc(K, cont); + TValue exp = kcons(K, kcar(ls), KNIL); + krooted_vars_push(K, &exp); + exp = kcons(K, cmp_obj, exp); + exp = kcons(K, pred, exp); + /* TEMP for now use an empty environment for dynamic env */ + TValue env = kmake_empty_environment(K); + krooted_vars_pop(K); + ktail_eval(K, exp, env); } } @@ -868,34 +868,34 @@ void memberp(klisp_State *K) /* first pass, check structure */ int32_t pairs; if (predp) { /* copy if a custom predicate is used */ - ls = check_copy_list(K, ls, false, &pairs, NULL); + ls = check_copy_list(K, ls, false, &pairs, NULL); } else { - check_list(K, true, ls, &pairs, NULL); + check_list(K, true, ls, &pairs, NULL); } TValue res; if (predp) { - /* we'll need use continuations */ - krooted_tvs_push(K, ls); - TValue cont = kmake_continuation(K, kget_cc(K), do_memberp, 4, - maybe_pred, obj, ls, i2tv(pairs)); - krooted_tvs_pop(K); - kset_cc(K, cont); - /* pass false to have it keep looking (in the whole list) */ - res = KFALSE; + /* we'll need use continuations */ + krooted_tvs_push(K, ls); + TValue cont = kmake_continuation(K, kget_cc(K), do_memberp, 4, + maybe_pred, obj, ls, i2tv(pairs)); + krooted_tvs_pop(K); + kset_cc(K, cont); + /* pass false to have it keep looking (in the whole list) */ + res = KFALSE; } else { - /* if using equal? we need no continuation, we can - do it all here */ - TValue tail = ls; - res = KFALSE; - while(pairs--) { - TValue first = kcar(tail); - if (equal2p(K, first, obj)) { - res = KTRUE; - break; - } - tail = kcdr(tail); - } + /* if using equal? we need no continuation, we can + do it all here */ + TValue tail = ls; + res = KFALSE; + while(pairs--) { + TValue first = kcar(tail); + if (equal2p(K, first, obj)) { + res = KTRUE; + break; + } + tail = kcdr(tail); + } } kapply_cc(K, res); } @@ -916,19 +916,19 @@ void finite_listp(klisp_State *K) TValue res = KTRUE; TValue tail = ptree; while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - TValue itail = first; - while(ttispair(itail) && !kis_marked(itail)) { - kmark(itail); - itail = kcdr(itail); - } - unmark_list(K, first); + TValue first = kcar(tail); + tail = kcdr(tail); + TValue itail = first; + while(ttispair(itail) && !kis_marked(itail)) { + kmark(itail); + itail = kcdr(itail); + } + unmark_list(K, first); - if (!ttisnil(itail)) { - res = KFALSE; - break; - } + if (!ttisnil(itail)) { + res = KFALSE; + break; + } } kapply_cc(K, res); } @@ -949,19 +949,19 @@ void countable_listp(klisp_State *K) TValue res = KTRUE; TValue tail = ptree; while(pairs--) { - TValue first = kcar(tail); - tail = kcdr(tail); - TValue itail = first; - while(ttispair(itail) && !kis_marked(itail)) { - kmark(itail); - itail = kcdr(itail); - } - unmark_list(K, first); + TValue first = kcar(tail); + tail = kcdr(tail); + TValue itail = first; + while(ttispair(itail) && !kis_marked(itail)) { + kmark(itail); + itail = kcdr(itail); + } + unmark_list(K, first); - if (!ttisnil(itail) && !ttispair(itail)) { - res = KFALSE; - break; - } + if (!ttisnil(itail) && !ttispair(itail)) { + res = KFALSE; + break; + } } kapply_cc(K, res); } @@ -992,17 +992,17 @@ void do_reduce_prec(klisp_State *K) kset_car(last_pair, obj); if (cpairs == 0) { - /* pass the first element to the do_reduce_inc continuation */ - kapply_cc(K, kcar(first_pair)); + /* pass the first element to the do_reduce_inc continuation */ + kapply_cc(K, kcar(first_pair)); } else { - TValue expr = klist(K, 2, kunwrap(prec), kcar(ls)); - krooted_tvs_push(K, expr); - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_reduce_prec, - 5, first_pair, ls, i2tv(cpairs-1), prec, denv); - kset_cc(K, new_cont); - krooted_tvs_pop(K); - ktail_eval(K, expr, denv); + TValue expr = klist(K, 2, kunwrap(prec), kcar(ls)); + krooted_tvs_push(K, expr); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_reduce_prec, + 5, first_pair, ls, i2tv(cpairs-1), prec, denv); + kset_cc(K, new_cont); + krooted_tvs_pop(K); + ktail_eval(K, expr, denv); } } @@ -1043,7 +1043,7 @@ void do_reduce_combine(klisp_State *K) /* obj: cyclic_res */ TValue cyclic_res = obj; TValue expr = klist(K, 3, kunwrap(bin), acyclic_res, - cyclic_res); + cyclic_res); ktail_eval(K, expr, denv); } @@ -1077,23 +1077,23 @@ void do_reduce_cycle(klisp_State *K) */ if (has_acyclic_partp) { - TValue acyclic_obj = obj; - TValue combine_cont = - kmake_continuation(K, kget_cc(K), do_reduce_combine, - 3, acyclic_obj, bin, denv); - kset_cc(K, combine_cont); /* implitly rooted */ + TValue acyclic_obj = obj; + TValue combine_cont = + kmake_continuation(K, kget_cc(K), do_reduce_combine, + 3, acyclic_obj, bin, denv); + kset_cc(K, combine_cont); /* implitly rooted */ } /* if there is no acyclic part, just let the result pass through */ TValue post_cont = - kmake_continuation(K, kget_cc(K), do_reduce_postc, - 2, postc, denv); + kmake_continuation(K, kget_cc(K), do_reduce_postc, + 2, postc, denv); kset_cc(K, post_cont); /* implitly rooted */ /* pass one less so that pre_cont can pass the first argument to the continuation */ TValue in_cont = - kmake_continuation(K, kget_cc(K), do_reduce, - 4, kcdr(ls), i2tv(cpairs - 1), inc, denv); + kmake_continuation(K, kget_cc(K), do_reduce, + 4, kcdr(ls), i2tv(cpairs - 1), inc, denv); kset_cc(K, in_cont); /* add dummy to allow passing inert to pre_cont */ @@ -1102,8 +1102,8 @@ void do_reduce_cycle(klisp_State *K) /* pass ls as the first pair to be passed to the do_reduce continuation */ TValue pre_cont = - kmake_continuation(K, kget_cc(K), do_reduce_prec, - 5, ls, dummy, i2tv(cpairs), prec, denv); + kmake_continuation(K, kget_cc(K), do_reduce_prec, + 5, ls, dummy, i2tv(cpairs), prec, denv); kset_cc(K, pre_cont); krooted_tvs_pop(K); /* this will overwrite dummy, but that's ok */ @@ -1129,22 +1129,22 @@ void do_reduce(klisp_State *K) TValue denv = xparams[3]; if (pairs == 0) { - /* NOTE: this continuation could have been avoided (made a - tail context) but since it isn't a requirement having - this will help with error signaling and backtraces */ - kapply_cc(K, obj); + /* NOTE: this continuation could have been avoided (made a + tail context) but since it isn't a requirement having + this will help with error signaling and backtraces */ + kapply_cc(K, obj); } else { - TValue next = kcar(ls); - TValue expr = klist(K, 3, kunwrap(bin), obj, next); - krooted_tvs_push(K, expr); + TValue next = kcar(ls); + TValue expr = klist(K, 3, kunwrap(bin), obj, next); + krooted_tvs_push(K, expr); - TValue new_cont = - kmake_continuation(K, kget_cc(K), do_reduce, 4, - kcdr(ls), i2tv(pairs-1), bin, denv); - kset_cc(K, new_cont); - krooted_tvs_pop(K); - /* use the dynamic environment of the call to reduce */ - ktail_eval(K, expr, denv); + TValue new_cont = + kmake_continuation(K, kget_cc(K), do_reduce, 4, + kcdr(ls), i2tv(pairs-1), bin, denv); + kset_cc(K, new_cont); + krooted_tvs_pop(K); + /* use the dynamic environment of the call to reduce */ + ktail_eval(K, expr, denv); } } @@ -1164,29 +1164,29 @@ void reduce(klisp_State *K) UNUSED(xparams); bind_al3tp(K, ptree, "any", anytype, ls, "applicative", - ttisapplicative, bin, "any", anytype, id, rest); + ttisapplicative, bin, "any", anytype, id, rest); TValue prec, inc, postc; bool extended_form = !ttisnil(rest); if (extended_form) { - /* the variables are an artifact of the way bind_3tp macro works, - XXX: this will also send wrong error msgs (bad number of arg) */ - bind_3tp(K, rest, - "applicative", ttisapplicative, prec_h, - "applicative", ttisapplicative, inc_h, - "applicative", ttisapplicative, postc_h); - prec = prec_h; - inc = inc_h; - postc = postc_h; + /* the variables are an artifact of the way bind_3tp macro works, + XXX: this will also send wrong error msgs (bad number of arg) */ + bind_3tp(K, rest, + "applicative", ttisapplicative, prec_h, + "applicative", ttisapplicative, inc_h, + "applicative", ttisapplicative, postc_h); + prec = prec_h; + inc = inc_h; + postc = postc_h; } else { - /* dummy init */ - prec = inc = postc = KINERT; + /* dummy init */ + prec = inc = postc = KINERT; } /* the easy case first */ if (ttisnil(ls)) { - kapply_cc(K, id); + kapply_cc(K, id); } /* TODO all of these in one procedure */ @@ -1200,37 +1200,37 @@ void reduce(klisp_State *K) /* REFACTOR: add an extra return value to check_copy_list to output the last pair of the list */ while(dapairs--) - first_cycle_pair = kcdr(first_cycle_pair); + first_cycle_pair = kcdr(first_cycle_pair); TValue res; if (cpairs != 0) { - if (!extended_form) { - klispE_throw_simple(K, "no cyclic handling applicatives"); - return; - } - /* make cycle reducing cont */ - TValue cyc_cont = - kmake_continuation(K, kget_cc(K), do_reduce_cycle, 8, - first_cycle_pair, i2tv(cpairs), bin, prec, - inc, postc, denv, b2tv(apairs != 0)); - kset_cc(K, cyc_cont); + if (!extended_form) { + klispE_throw_simple(K, "no cyclic handling applicatives"); + return; + } + /* make cycle reducing cont */ + TValue cyc_cont = + kmake_continuation(K, kget_cc(K), do_reduce_cycle, 8, + first_cycle_pair, i2tv(cpairs), bin, prec, + inc, postc, denv, b2tv(apairs != 0)); + kset_cc(K, cyc_cont); } if (apairs == 0) { - /* this will be ignore by cyc_cont */ - res = KINERT; + /* this will be ignore by cyc_cont */ + res = KINERT; } else { - /* this will pass the parent continuation either - a list of (rem-ls result) if there is a cycle or - result if there is no cycle, this should be a list - and not a regular pair to allow the above case of - a one element list to signal no acyclic part */ - TValue acyc_cont = - kmake_continuation(K, kget_cc(K), do_reduce, 4, - kcdr(ls), i2tv(apairs-1), bin, denv); - kset_cc(K, acyc_cont); - res = kcar(ls); + /* this will pass the parent continuation either + a list of (rem-ls result) if there is a cycle or + result if there is no cycle, this should be a list + and not a regular pair to allow the above case of + a one element list to signal no acyclic part */ + TValue acyc_cont = + kmake_continuation(K, kget_cc(K), do_reduce, 4, + kcdr(ls), i2tv(apairs-1), bin, denv); + kset_cc(K, acyc_cont); + res = kcar(ls); } kapply_cc(K, res); } @@ -1243,10 +1243,10 @@ void kinit_pairs_lists_ground_env(klisp_State *K) /* 4.6.1 pair? */ add_applicative(K, ground_env, "pair?", typep, 2, symbol, - i2tv(K_TPAIR)); + i2tv(K_TPAIR)); /* 4.6.2 null? */ add_applicative(K, ground_env, "null?", typep, 2, symbol, - i2tv(K_TNIL)); + i2tv(K_TNIL)); /* 4.6.3 cons */ add_applicative(K, ground_env, "cons", cons, 0); /* 5.2.1 list */ @@ -1255,66 +1255,66 @@ void kinit_pairs_lists_ground_env(klisp_State *K) add_applicative(K, ground_env, "list*", listS, 0); /* 5.4.1 car, cdr */ add_applicative(K, ground_env, "car", c_ad_r, 2, symbol, - C_AD_R_PARAM(1, 0x0000)); + C_AD_R_PARAM(1, 0x0000)); add_applicative(K, ground_env, "cdr", c_ad_r, 2, symbol, - C_AD_R_PARAM(1, 0x0001)); + C_AD_R_PARAM(1, 0x0001)); /* 5.4.2 caar, cadr, ... cddddr */ add_applicative(K, ground_env, "caar", c_ad_r, 2, symbol, - C_AD_R_PARAM(2, 0x0000)); + C_AD_R_PARAM(2, 0x0000)); add_applicative(K, ground_env, "cadr", c_ad_r, 2, symbol, - C_AD_R_PARAM(2, 0x0001)); + C_AD_R_PARAM(2, 0x0001)); add_applicative(K, ground_env, "cdar", c_ad_r, 2, symbol, - C_AD_R_PARAM(2, 0x0010)); + C_AD_R_PARAM(2, 0x0010)); add_applicative(K, ground_env, "cddr", c_ad_r, 2, symbol, - C_AD_R_PARAM(2, 0x0011)); + C_AD_R_PARAM(2, 0x0011)); add_applicative(K, ground_env, "caaar", c_ad_r, 2, symbol, - C_AD_R_PARAM(3, 0x0000)); + C_AD_R_PARAM(3, 0x0000)); add_applicative(K, ground_env, "caadr", c_ad_r, 2, symbol, - C_AD_R_PARAM(3, 0x0001)); + C_AD_R_PARAM(3, 0x0001)); add_applicative(K, ground_env, "cadar", c_ad_r, 2, symbol, - C_AD_R_PARAM(3, 0x0010)); + C_AD_R_PARAM(3, 0x0010)); add_applicative(K, ground_env, "caddr", c_ad_r, 2, symbol, - C_AD_R_PARAM(3, 0x0011)); + C_AD_R_PARAM(3, 0x0011)); add_applicative(K, ground_env, "cdaar", c_ad_r, 2, symbol, - C_AD_R_PARAM(3, 0x0100)); + C_AD_R_PARAM(3, 0x0100)); add_applicative(K, ground_env, "cdadr", c_ad_r, 2, symbol, - C_AD_R_PARAM(3, 0x0101)); + C_AD_R_PARAM(3, 0x0101)); add_applicative(K, ground_env, "cddar", c_ad_r, 2, symbol, - C_AD_R_PARAM(3, 0x0110)); + C_AD_R_PARAM(3, 0x0110)); add_applicative(K, ground_env, "cdddr", c_ad_r, 2, symbol, - C_AD_R_PARAM(3, 0x0111)); + C_AD_R_PARAM(3, 0x0111)); add_applicative(K, ground_env, "caaaar", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x0000)); + C_AD_R_PARAM(4, 0x0000)); add_applicative(K, ground_env, "caaadr", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x0001)); + C_AD_R_PARAM(4, 0x0001)); add_applicative(K, ground_env, "caadar", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x0010)); + C_AD_R_PARAM(4, 0x0010)); add_applicative(K, ground_env, "caaddr", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x0011)); + C_AD_R_PARAM(4, 0x0011)); add_applicative(K, ground_env, "cadaar", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x0100)); + C_AD_R_PARAM(4, 0x0100)); add_applicative(K, ground_env, "cadadr", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x0101)); + C_AD_R_PARAM(4, 0x0101)); add_applicative(K, ground_env, "caddar", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x0110)); + C_AD_R_PARAM(4, 0x0110)); add_applicative(K, ground_env, "cadddr", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x0111)); + C_AD_R_PARAM(4, 0x0111)); add_applicative(K, ground_env, "cdaaar", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x1000)); + C_AD_R_PARAM(4, 0x1000)); add_applicative(K, ground_env, "cdaadr", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x1001)); + C_AD_R_PARAM(4, 0x1001)); add_applicative(K, ground_env, "cdadar", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x1010)); + C_AD_R_PARAM(4, 0x1010)); add_applicative(K, ground_env, "cdaddr", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x1011)); + C_AD_R_PARAM(4, 0x1011)); add_applicative(K, ground_env, "cddaar", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x1100)); + C_AD_R_PARAM(4, 0x1100)); add_applicative(K, ground_env, "cddadr", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x1101)); + C_AD_R_PARAM(4, 0x1101)); add_applicative(K, ground_env, "cdddar", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x1110)); + C_AD_R_PARAM(4, 0x1110)); add_applicative(K, ground_env, "cddddr", c_ad_r, 2, symbol, - C_AD_R_PARAM(4, 0x1111)); + C_AD_R_PARAM(4, 0x1111)); /* 5.?.? make-list */ add_applicative(K, ground_env, "make-list", make_list, 0); /* 5.?.? list-copy */ diff --git a/src/kgports.c b/src/kgports.c @@ -82,13 +82,13 @@ void with_file(klisp_State *K) TValue key = xparams[2]; bind_2tp(K, ptree, "string", ttisstring, filename, - "combiner", ttiscombiner, comb); + "combiner", ttiscombiner, comb); TValue new_port = kmake_fport(K, filename, writep, false); krooted_tvs_push(K, new_port); /* make the continuation to close the file before returning */ TValue new_cont = kmake_continuation(K, kget_cc(K), - do_close_file_ret, 1, new_port); + do_close_file_ret, 1, new_port); kset_cc(K, new_cont); /* cont implicitly rooted */ krooted_tvs_pop(K); /* new_port is in cont */ @@ -169,14 +169,14 @@ void open_mport(klisp_State *K) /* This is kinda ugly but... */ if (writep) { - check_0p(K, ptree); - buffer = KINERT; + check_0p(K, ptree); + buffer = KINERT; } else if (binaryp) { - bind_1tp(K, ptree, "bytevector", ttisbytevector, bb); - buffer = bb; + bind_1tp(K, ptree, "bytevector", ttisbytevector, bb); + buffer = bb; } else { - bind_1tp(K, ptree, "string", ttisstring, str); - buffer = str; + bind_1tp(K, ptree, "string", ttisstring, str); + buffer = str; } TValue new_port = kmake_mport(K, buffer, writep, binaryp); @@ -203,11 +203,11 @@ void close_file(klisp_State *K) bool dir_ok = writep? kport_is_output(port) : kport_is_input(port); if (dir_ok) { - kclose_port(K, port); - kapply_cc(K, KINERT); + kclose_port(K, port); + kapply_cc(K, KINERT); } else { - klispE_throw_simple(K, "wrong input/output direction"); - return; + klispE_throw_simple(K, "wrong input/output direction"); + return; } } @@ -229,14 +229,14 @@ void close_port(klisp_State *K) bind_1tp(K, ptree, "port", ttisport, port); bool dir_ok = !((writep && !kport_is_output(port)) || - (readp && !kport_is_input(port))); + (readp && !kport_is_input(port))); if (dir_ok) { - kclose_port(K, port); - kapply_cc(K, KINERT); + kclose_port(K, port); + kapply_cc(K, KINERT); } else { - klispE_throw_simple(K, "wrong input/output direction"); - return; + klispE_throw_simple(K, "wrong input/output direction"); + return; } } @@ -255,21 +255,21 @@ void get_output_buffer(klisp_State *K) bind_1tp(K, ptree, "port", ttismport, port); if (binaryp && !kport_is_binary(port)) { - klispE_throw_simple(K, "the port should be a bytevector port"); - return; + klispE_throw_simple(K, "the port should be a bytevector port"); + return; } else if (!binaryp && !kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a string port"); - return; + klispE_throw_simple(K, "the port should be a string port"); + return; } else if (!kport_is_output(port)) { - klispE_throw_simple(K, "the port should be an output port"); - return; + klispE_throw_simple(K, "the port should be an output port"); + return; } TValue ret = binaryp? - kbytevector_new_bs(K, - kbytevector_buf(kmport_buf(port)), - kmport_off(port)) : - kstring_new_bs(K, kstring_buf(kmport_buf(port)), kmport_off(port)); + kbytevector_new_bs(K, + kbytevector_buf(kmport_buf(port)), + kmport_off(port)) : + kstring_new_bs(K, kstring_buf(kmport_buf(port)), kmport_off(port)); kapply_cc(K, ret); } @@ -285,18 +285,18 @@ void gread(klisp_State *K) TValue port = ptree; if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_in_port_key); /* access directly */ + port = kcdr(K->kd_in_port_key); /* access directly */ } if (!kport_is_input(port)) { - klispE_throw_simple(K, "the port should be an input port"); - return; + klispE_throw_simple(K, "the port should be an input port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } /* this may throw an error, that's ok */ @@ -315,21 +315,21 @@ void gwrite(klisp_State *K) UNUSED(denv); bind_al1tp(K, ptree, "any", anytype, obj, - port); + port); if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_out_port_key); /* access directly */ + port = kcdr(K->kd_out_port_key); /* access directly */ } if (!kport_is_output(port)) { - klispE_throw_simple(K, "the port should be an output port"); - return; + klispE_throw_simple(K, "the port should be an output port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } /* false: quote strings, escape chars */ @@ -348,21 +348,21 @@ void gwrite_simple(klisp_State *K) UNUSED(denv); bind_al1tp(K, ptree, "any", anytype, obj, - port); + port); if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_out_port_key); /* access directly */ + port = kcdr(K->kd_out_port_key); /* access directly */ } if (!kport_is_output(port)) { - klispE_throw_simple(K, "the port should be an output port"); - return; + klispE_throw_simple(K, "the port should be an output port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } kwrite_simple_to_port(K, port, obj); @@ -384,18 +384,18 @@ void newline(klisp_State *K) TValue port = ptree; if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_out_port_key); /* access directly */ + port = kcdr(K->kd_out_port_key); /* access directly */ } if (!kport_is_output(port)) { - klispE_throw_simple(K, "the port should be an output port"); - return; + klispE_throw_simple(K, "the port should be an output port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } kwrite_newline_to_port(K, port); @@ -413,21 +413,21 @@ void write_char(klisp_State *K) UNUSED(denv); bind_al1tp(K, ptree, "char", ttischar, ch, - port); + port); if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_out_port_key); /* access directly */ + port = kcdr(K->kd_out_port_key); /* access directly */ } if (!kport_is_output(port)) { - klispE_throw_simple(K, "the port should be an output port"); - return; + klispE_throw_simple(K, "the port should be an output port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } kwrite_char_to_port(K, port, ch); @@ -450,18 +450,18 @@ void read_peek_char(klisp_State *K) TValue port = ptree; if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_in_port_key); /* access directly */ + port = kcdr(K->kd_in_port_key); /* access directly */ } if (!kport_is_input(port)) { - klispE_throw_simple(K, "the port should be an input port"); - return; + klispE_throw_simple(K, "the port should be an input port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } TValue obj = kread_peek_char_from_port(K, port, ret_charp); @@ -491,18 +491,18 @@ void char_readyp(klisp_State *K) TValue port = ptree; if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_in_port_key); /* access directly */ + port = kcdr(K->kd_in_port_key); /* access directly */ } if (!kport_is_input(port)) { - klispE_throw_simple(K, "the port should be an input port"); - return; + klispE_throw_simple(K, "the port should be an input port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } /* TODO: check if there are pending chars */ @@ -522,18 +522,18 @@ void write_u8(klisp_State *K) bind_al1tp(K, ptree, "u8", ttisu8, u8, port); if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_out_port_key); /* access directly */ + port = kcdr(K->kd_out_port_key); /* access directly */ } if (!kport_is_output(port)) { - klispE_throw_simple(K, "the port should be an output port"); - return; + klispE_throw_simple(K, "the port should be an output port"); + return; } else if (!kport_is_binary(port)) { - klispE_throw_simple(K, "the port should be a binary port"); - return; + klispE_throw_simple(K, "the port should be a binary port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } kwrite_u8_to_port(K, port, u8); @@ -556,18 +556,18 @@ void read_peek_u8(klisp_State *K) TValue port = ptree; if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_in_port_key); /* access directly */ + port = kcdr(K->kd_in_port_key); /* access directly */ } if (!kport_is_input(port)) { - klispE_throw_simple(K, "the port should be an input port"); - return; + klispE_throw_simple(K, "the port should be an input port"); + return; } else if (!kport_is_binary(port)) { - klispE_throw_simple(K, "the port should be a binary port"); - return; + klispE_throw_simple(K, "the port should be a binary port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } TValue obj = kread_peek_u8_from_port(K, port, ret_u8p); @@ -597,18 +597,18 @@ void u8_readyp(klisp_State *K) TValue port = ptree; if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_in_port_key); /* access directly */ + port = kcdr(K->kd_in_port_key); /* access directly */ } if (!kport_is_input(port)) { - klispE_throw_simple(K, "the port should be an input port"); - return; + klispE_throw_simple(K, "the port should be an input port"); + return; } else if (!kport_is_binary(port)) { - klispE_throw_simple(K, "the port should be a binary port"); - return; + klispE_throw_simple(K, "the port should be a binary port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } /* TODO: check if there are pending chars */ @@ -630,13 +630,13 @@ void call_with_file(klisp_State *K) UNUSED(denv); bind_2tp(K, ptree, "string", ttisstring, filename, - "combiner", ttiscombiner, comb); + "combiner", ttiscombiner, comb); TValue new_port = kmake_fport(K, filename, writep, false); krooted_tvs_push(K, new_port); /* make the continuation to close the file before returning */ TValue new_cont = kmake_continuation(K, kget_cc(K), - do_close_file_ret, 1, new_port); + do_close_file_ret, 1, new_port); kset_cc(K, new_cont); /* implicit rooting */ krooted_tvs_pop(K); /* new_port is in new_cont */ TValue empty_env = kmake_empty_environment(K); @@ -679,7 +679,7 @@ TValue make_guarded_read_cont(klisp_State *K, TValue parent, TValue port) { /* create the guard to close file after read errors */ TValue exit_int = kmake_operative(K, do_int_close_file, - 1, port); + 1, port); krooted_tvs_push(K, exit_int); TValue exit_guard = kcons(K, K->error_cont, exit_int); krooted_tvs_pop(K); /* alread in guard */ @@ -694,11 +694,11 @@ TValue make_guarded_read_cont(klisp_State *K, TValue parent, TValue port) TValue env = kmake_empty_environment(K); krooted_tvs_push(K, env); TValue outer_cont = kmake_continuation(K, parent, - do_pass_value, 2, entry_guards, env); + do_pass_value, 2, entry_guards, env); kset_outer_cont(outer_cont); krooted_tvs_push(K, outer_cont); TValue inner_cont = kmake_continuation(K, outer_cont, - do_pass_value, 2, exit_guards, env); + do_pass_value, 2, exit_guards, env); kset_inner_cont(inner_cont); krooted_tvs_pop(K); krooted_tvs_pop(K); krooted_tvs_pop(K); return inner_cont; @@ -725,20 +725,20 @@ void load(klisp_State *K) bind_1tp(K, ptree, "string", ttisstring, filename); /* the reads must be guarded to close the file if there is some error - this continuation also will return inert after the evaluation of the - last expression is done */ + this continuation also will return inert after the evaluation of the + last expression is done */ TValue port = kmake_fport(K, filename, false, false); krooted_tvs_push(K, port); TValue inert_cont = kmake_continuation(K, kget_cc(K), do_return_value, 1, - KINERT); + KINERT); krooted_tvs_push(K, inert_cont); TValue guarded_cont = make_guarded_read_cont(K, kget_cc(K), port); /* this will be used later, but contruct it now to use the current continuation as parent - GC: root this obj */ + GC: root this obj */ kset_cc(K, guarded_cont); /* implicit rooting */ /* any error will close the port */ TValue ls = kread_list_from_port(K, port, false); /* immutable pairs */ @@ -750,24 +750,24 @@ void load(klisp_State *K) if (ttisnil(ls)) { - krooted_tvs_pop(K); /* port */ - kapply_cc(K, KINERT); + krooted_tvs_pop(K); /* port */ + kapply_cc(K, KINERT); } else { - TValue tail = kcdr(ls); - if (ttispair(tail)) { - krooted_tvs_push(K, ls); - TValue new_cont = kmake_continuation(K, kget_cc(K), - do_seq, 2, tail, denv); - kset_cc(K, new_cont); + TValue tail = kcdr(ls); + if (ttispair(tail)) { + krooted_tvs_push(K, ls); + TValue new_cont = kmake_continuation(K, kget_cc(K), + do_seq, 2, tail, denv); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, ls)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, ls)); #endif - krooted_tvs_pop(K); /* ls */ - } - krooted_tvs_pop(K); /* port */ - ktail_eval(K, kcar(ls), denv); + krooted_tvs_pop(K); /* ls */ + } + krooted_tvs_pop(K); /* port */ + ktail_eval(K, kcar(ls), denv); } } @@ -781,7 +781,7 @@ static bool readable(const char *filename) { /* Path can't/shouldn't contain embedded zeros */ static const char *get_next_template(klisp_State *K, const char *path, - TValue *next) { + TValue *next) { const char *l; while (*path == *KLISP_PATHSEP) path++; /* skip separators */ if (*path == '\0') return NULL; /* no more templates */ @@ -807,8 +807,8 @@ static TValue str_sub(klisp_State *K, TValue s, TValue p, TValue r) /* first calculate needed size */ while ((wild = strstr(sp, pp)) != NULL) { - size += diff_size; - sp = wild + psize; + size += diff_size; + sp = wild + psize; } /* now construct result buffer and fill it */ @@ -816,21 +816,21 @@ static TValue str_sub(klisp_State *K, TValue s, TValue p, TValue r) char *resp = kstring_buf(res); sp = kstring_buf(s); while ((wild = strstr(sp, pp)) != NULL) { - ptrdiff_t l = wild - sp; - memcpy(resp, sp, l); - resp += l; - memcpy(resp, rp, rsize); - resp += rsize; - sp = wild + psize; + ptrdiff_t l = wild - sp; + memcpy(resp, sp, l); + resp += l; + memcpy(resp, rp, rsize); + resp += rsize; + sp = wild + psize; } strcpy(resp, sp); /* the size was calculated beforehand */ return res; } static TValue find_file (klisp_State *K, TValue name, TValue pname) { - /* not used in klisp */ - /* name = luaL_gsub(L, name, ".", LUA_DIRSEP); */ - /* lua_getfield(L, LUA_ENVIRONINDEX, pname); */ + /* not used in klisp */ + /* name = luaL_gsub(L, name, ".", LUA_DIRSEP); */ + /* lua_getfield(L, LUA_ENVIRONINDEX, pname); */ klisp_assert(ttisstring(name) && !kstring_emptyp(name)); const char *path = kstring_buf(pname); TValue next = K->empty_string; @@ -839,12 +839,12 @@ static TValue find_file (klisp_State *K, TValue name, TValue pname) { krooted_tvs_push(K, wild); while ((path = get_next_template(K, path, &next)) != NULL) { - next = str_sub(K, next, wild, name); - if (readable(kstring_buf(next))) { /* does file exist and is readable? */ - krooted_tvs_pop(K); - krooted_vars_pop(K); - return next; /* return that file name */ - } + next = str_sub(K, next, wild, name); + if (readable(kstring_buf(next))) { /* does file exist and is readable? */ + krooted_tvs_pop(K); + krooted_vars_pop(K); + return next; /* return that file name */ + } } krooted_tvs_pop(K); @@ -873,21 +873,21 @@ void require(klisp_State *K) bind_1tp(K, ptree, "string", ttisstring, name); if (kstring_emptyp(name)) { - klispE_throw_simple(K, "Empty name"); - return; + klispE_throw_simple(K, "Empty name"); + return; } /* search for the named file in the table of already required files. N.B. this will be fooled if the same file is accessed through different names */ TValue saved_name = kstring_immutablep(name)? name : - kstring_new_bs_imm(K, kstring_buf(name), kstring_size(name)); + kstring_new_bs_imm(K, kstring_buf(name), kstring_size(name)); const TValue *node = klispH_getstr(tv2table(K->require_table), - tv2str(saved_name)); + tv2str(saved_name)); if (!ttisfree(*node)) { - /* was required already, nothing to be done */ - kapply_cc(K, KINERT); + /* was required already, nothing to be done */ + kapply_cc(K, KINERT); } krooted_tvs_push(K, saved_name); @@ -896,8 +896,8 @@ void require(klisp_State *K) filename = find_file(K, name, K->require_path); if (kstring_emptyp(filename)) { - klispE_throw_simple_with_irritants(K, "Not found", 1, name); - return; + klispE_throw_simple_with_irritants(K, "Not found", 1, name); + return; } /* the file was found, save it in the table */ @@ -907,25 +907,25 @@ void require(klisp_State *K) sate of the require in the table, so we could have: error, required, requiring, etc */ *(klispH_setstr(K, tv2table(K->require_table), tv2str(saved_name))) = - KTRUE; + KTRUE; krooted_tvs_pop(K); /* saved_name no longer necessary */ /* the reads must be guarded to close the file if there is some error - this continuation also will return inert after the evaluation of the - last expression is done */ + this continuation also will return inert after the evaluation of the + last expression is done */ TValue port = kmake_fport(K, filename, false, false); krooted_tvs_push(K, port); krooted_vars_pop(K); /* filename already rooted */ TValue inert_cont = kmake_continuation(K, kget_cc(K), do_return_value, 1, - KINERT); + KINERT); krooted_tvs_push(K, inert_cont); TValue guarded_cont = make_guarded_read_cont(K, kget_cc(K), port); /* this will be used later, but contruct it now to use the current continuation as parent - GC: root this obj */ + GC: root this obj */ kset_cc(K, guarded_cont); /* implicit rooting */ /* any error will close the port */ TValue ls = kread_list_from_port(K, port, false); /* immutable pairs */ @@ -936,28 +936,28 @@ void require(klisp_State *K) krooted_tvs_pop(K); /* already rooted */ if (ttisnil(ls)) { - krooted_tvs_pop(K); /* port */ - kapply_cc(K, KINERT); + krooted_tvs_pop(K); /* port */ + kapply_cc(K, KINERT); } else { - TValue tail = kcdr(ls); - /* std environments have hashtable for bindings */ - TValue env = kmake_table_environment(K, K->ground_env); - if (ttispair(tail)) { - krooted_tvs_push(K, ls); - krooted_tvs_push(K, env); - TValue new_cont = kmake_continuation(K, kget_cc(K), - do_seq, 2, tail, env); - kset_cc(K, new_cont); + TValue tail = kcdr(ls); + /* std environments have hashtable for bindings */ + TValue env = kmake_table_environment(K, K->ground_env); + if (ttispair(tail)) { + krooted_tvs_push(K, ls); + krooted_tvs_push(K, env); + TValue new_cont = kmake_continuation(K, kget_cc(K), + do_seq, 2, tail, env); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, ls)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, ls)); #endif - krooted_tvs_pop(K); /* env */ - krooted_tvs_pop(K); /* ls */ - } - krooted_tvs_pop(K); /* port */ - ktail_eval(K, kcar(ls), env); + krooted_tvs_pop(K); /* env */ + krooted_tvs_pop(K); /* ls */ + } + krooted_tvs_pop(K); /* port */ + ktail_eval(K, kcar(ls), env); } } @@ -966,18 +966,18 @@ void registered_requirementP(klisp_State *K) { bind_1tp(K, K->next_value, "string", ttisstring, name); if (kstring_emptyp(name)) { - klispE_throw_simple(K, "Empty name"); - return; + klispE_throw_simple(K, "Empty name"); + return; } /* search for the named file in the table of already required files. N.B. this will be fooled if the same file is accessed through different names */ TValue saved_name = kstring_immutablep(name)? name : - kstring_new_bs_imm(K, kstring_buf(name), kstring_size(name)); + kstring_new_bs_imm(K, kstring_buf(name), kstring_size(name)); const TValue *node = klispH_getstr(tv2table(K->require_table), - tv2str(saved_name)); + tv2str(saved_name)); kapply_cc(K, ttisfree(*node)? KFALSE : KTRUE); } @@ -985,11 +985,11 @@ void register_requirementB(klisp_State *K) { bind_1tp(K, K->next_value, "string", ttisstring, name); if (kstring_emptyp(name)) { - klispE_throw_simple(K, "Empty name"); - return; + klispE_throw_simple(K, "Empty name"); + return; } TValue saved_name = kstring_immutablep(name)? name : - kstring_new_bs_imm(K, kstring_buf(name), kstring_size(name)); + kstring_new_bs_imm(K, kstring_buf(name), kstring_size(name)); /* don't throw error if already registered */ *(klispH_setstr(K, tv2table(K->require_table), @@ -1001,11 +1001,11 @@ void unregister_requirementB(klisp_State *K) { bind_1tp(K, K->next_value, "string", ttisstring, name); if (kstring_emptyp(name)) { - klispE_throw_simple(K, "Empty name"); - return; + klispE_throw_simple(K, "Empty name"); + return; } TValue saved_name = kstring_immutablep(name)? name : - kstring_new_bs_imm(K, kstring_buf(name), kstring_size(name)); + kstring_new_bs_imm(K, kstring_buf(name), kstring_size(name)); /* don't throw error if not registered */ *(klispH_setstr(K, tv2table(K->require_table), @@ -1018,14 +1018,14 @@ void find_required_filename(klisp_State *K) { bind_1tp(K, K->next_value, "string", ttisstring, name); if (kstring_emptyp(name)) { - klispE_throw_simple(K, "Empty name"); - return; + klispE_throw_simple(K, "Empty name"); + return; } TValue filename = find_file(K, name, K->require_path); if (kstring_emptyp(filename)) { - klispE_throw_simple_with_irritants(K, "Not found", 1, name); - return; + klispE_throw_simple_with_irritants(K, "Not found", 1, name); + return; } kapply_cc(K, filename); } @@ -1040,7 +1040,7 @@ void get_module(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_al1tp(K, ptree, "string", ttisstring, filename, - maybe_env); + maybe_env); TValue port = kmake_fport(K, filename, false, false); krooted_tvs_push(K, port); @@ -1051,17 +1051,17 @@ void get_module(klisp_State *K) krooted_tvs_push(K, env); if (get_opt_tpar(K, maybe_env, "environment", ttisenvironment)) { - kadd_binding(K, env, K->module_params_sym, maybe_env); + kadd_binding(K, env, K->module_params_sym, maybe_env); } TValue ret_env_cont = kmake_continuation(K, kget_cc(K), do_return_value, - 1, env); + 1, env); krooted_tvs_pop(K); /* env alread in cont */ krooted_tvs_push(K, ret_env_cont); /* the reads must be guarded to close the file if there is some error - this continuation also will return inert after the evaluation of the - last expression is done */ + this continuation also will return inert after the evaluation of the + last expression is done */ TValue guarded_cont = make_guarded_read_cont(K, kget_cc(K), port); kset_cc(K, guarded_cont); /* implicit roooting */ @@ -1075,24 +1075,24 @@ void get_module(klisp_State *K) krooted_tvs_pop(K); /* implicitly rooted */ if (ttisnil(ls)) { - krooted_tvs_pop(K); /* port */ - kapply_cc(K, KINERT); + krooted_tvs_pop(K); /* port */ + kapply_cc(K, KINERT); } else { - TValue tail = kcdr(ls); - if (ttispair(tail)) { - krooted_tvs_push(K, ls); - TValue new_cont = kmake_continuation(K, kget_cc(K), - do_seq, 2, tail, env); - kset_cc(K, new_cont); + TValue tail = kcdr(ls); + if (ttispair(tail)) { + krooted_tvs_push(K, ls); + TValue new_cont = kmake_continuation(K, kget_cc(K), + do_seq, 2, tail, env); + kset_cc(K, new_cont); #if KTRACK_SI - /* put the source info of the list including the element - that we are about to evaluate */ - kset_source_info(K, new_cont, ktry_get_si(K, ls)); + /* put the source info of the list including the element + that we are about to evaluate */ + kset_source_info(K, new_cont, ktry_get_si(K, ls)); #endif - krooted_tvs_pop(K); - } - krooted_tvs_pop(K); /* port */ - ktail_eval(K, kcar(ls), env); + krooted_tvs_pop(K); + } + krooted_tvs_pop(K); /* port */ + ktail_eval(K, kcar(ls), env); } } @@ -1107,21 +1107,21 @@ void display(klisp_State *K) UNUSED(denv); bind_al1tp(K, ptree, "any", anytype, obj, - port); + port); if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_out_port_key); /* access directly */ + port = kcdr(K->kd_out_port_key); /* access directly */ } if (!kport_is_output(port)) { - klispE_throw_simple(K, "the port should be an output port"); - return; + klispE_throw_simple(K, "the port should be an output port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } /* true: don't quote strings, don't escape chars */ @@ -1141,18 +1141,18 @@ void read_line(klisp_State *K) TValue port = ptree; if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_in_port_key); /* access directly */ + port = kcdr(K->kd_in_port_key); /* access directly */ } if (!kport_is_input(port)) { - klispE_throw_simple(K, "the port should be an input port"); - return; + klispE_throw_simple(K, "the port should be an input port"); + return; } else if (!kport_is_textual(port)) { - klispE_throw_simple(K, "the port should be a textual port"); - return; + klispE_throw_simple(K, "the port should be a textual port"); + return; } else if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } TValue obj = kread_line_from_port(K, port); @@ -1172,17 +1172,17 @@ void flush(klisp_State *K) TValue port = ptree; if (!get_opt_tpar(K, port, "port", ttisport)) { - port = kcdr(K->kd_out_port_key); /* access directly */ + port = kcdr(K->kd_out_port_key); /* access directly */ } if (!kport_is_output(port)) { - klispE_throw_simple(K, "the port should be an output port"); - return; + klispE_throw_simple(K, "the port should be an output port"); + return; } if (kport_is_closed(port)) { - klispE_throw_simple(K, "the port is already closed"); - return; + klispE_throw_simple(K, "the port is already closed"); + return; } kwrite_flush_port(K, port); @@ -1201,85 +1201,85 @@ void kinit_ports_ground_env(klisp_State *K) /* 15.1.1 port? */ add_applicative(K, ground_env, "port?", ftypep, 2, symbol, - p2tv(kportp)); + p2tv(kportp)); /* 15.1.2 input-port?, output-port? */ add_applicative(K, ground_env, "input-port?", ftypep, 2, symbol, - p2tv(kinput_portp)); + p2tv(kinput_portp)); add_applicative(K, ground_env, "output-port?", ftypep, 2, symbol, - p2tv(koutput_portp)); + p2tv(koutput_portp)); /* 15.1.? binary-port?, textual-port? */ add_applicative(K, ground_env, "binary-port?", ftypep, 2, symbol, - p2tv(kbinary_portp)); + p2tv(kbinary_portp)); add_applicative(K, ground_env, "textual-port?", ftypep, 2, symbol, - p2tv(ktextual_portp)); + p2tv(ktextual_portp)); /* 15.1.2 file-port?, string-port?, bytevector-port? */ add_applicative(K, ground_env, "file-port?", ftypep, 2, symbol, - p2tv(kfile_portp)); + p2tv(kfile_portp)); add_applicative(K, ground_env, "string-port?", ftypep, 2, symbol, - p2tv(kstring_portp)); + p2tv(kstring_portp)); add_applicative(K, ground_env, "bytevector-port?", ftypep, 2, symbol, - p2tv(kbytevector_portp)); + p2tv(kbytevector_portp)); /* 15.1.? port-open? */ add_applicative(K, ground_env, "port-open?", ftyped_predp, 3, symbol, - p2tv(kportp), p2tv(kport_openp)); + p2tv(kportp), p2tv(kport_openp)); /* 15.1.3 with-input-from-file, with-ouput-to-file */ /* 15.1.? with-error-to-file */ add_applicative(K, ground_env, "with-input-from-file", with_file, - 3, symbol, b2tv(false), K->kd_in_port_key); + 3, symbol, b2tv(false), K->kd_in_port_key); add_applicative(K, ground_env, "with-output-to-file", with_file, - 3, symbol, b2tv(true), K->kd_out_port_key); + 3, symbol, b2tv(true), K->kd_out_port_key); add_applicative(K, ground_env, "with-error-to-file", with_file, - 3, symbol, b2tv(true), K->kd_error_port_key); + 3, symbol, b2tv(true), K->kd_error_port_key); /* 15.1.4 get-current-input-port, get-current-output-port */ /* 15.1.? get-current-error-port */ add_applicative(K, ground_env, "get-current-input-port", get_current_port, - 2, symbol, K->kd_in_port_key); + 2, symbol, K->kd_in_port_key); add_applicative(K, ground_env, "get-current-output-port", get_current_port, - 2, symbol, K->kd_out_port_key); + 2, symbol, K->kd_out_port_key); add_applicative(K, ground_env, "get-current-error-port", get_current_port, - 2, symbol, K->kd_error_port_key); + 2, symbol, K->kd_error_port_key); /* 15.1.5 open-input-file, open-output-file */ add_applicative(K, ground_env, "open-input-file", open_file, 2, - b2tv(false), b2tv(false)); + b2tv(false), b2tv(false)); add_applicative(K, ground_env, "open-output-file", open_file, 2, - b2tv(true), b2tv(false)); + b2tv(true), b2tv(false)); /* 15.1.? open-binary-input-file, open-binary-output-file */ add_applicative(K, ground_env, "open-binary-input-file", open_file, 2, - b2tv(false), b2tv(true)); + b2tv(false), b2tv(true)); add_applicative(K, ground_env, "open-binary-output-file", open_file, 2, - b2tv(true), b2tv(true)); + b2tv(true), b2tv(true)); /* 15.1.? open-input-string, open-output-string */ /* 15.1.? open-input-bytevector, open-output-bytevector */ add_applicative(K, ground_env, "open-input-string", open_mport, 2, - b2tv(false), b2tv(false)); + b2tv(false), b2tv(false)); add_applicative(K, ground_env, "open-output-string", open_mport, 2, - b2tv(true), b2tv(false)); + b2tv(true), b2tv(false)); add_applicative(K, ground_env, "open-input-bytevector", open_mport, 2, - b2tv(false), b2tv(true)); + b2tv(false), b2tv(true)); add_applicative(K, ground_env, "open-output-bytevector", open_mport, 2, - b2tv(true), b2tv(true)); + b2tv(true), b2tv(true)); /* 15.1.6 close-input-file, close-output-file */ /* ASK John: should this be called close-input-port & close-ouput-port like in r5rs? that doesn't seem consistent with open thou */ add_applicative(K, ground_env, "close-input-file", close_file, 1, - b2tv(false)); + b2tv(false)); add_applicative(K, ground_env, "close-output-file", close_file, 1, - b2tv(true)); + b2tv(true)); /* 15.1.? Use the r7rs names, in preparation for other kind of ports */ add_applicative(K, ground_env, "close-input-port", close_port, 2, - b2tv(true), b2tv(false)); + b2tv(true), b2tv(false)); add_applicative(K, ground_env, "close-output-port", close_port, 2, - b2tv(false), b2tv(true)); + b2tv(false), b2tv(true)); add_applicative(K, ground_env, "close-port", close_port, 2, - b2tv(false), b2tv(false)); + b2tv(false), b2tv(false)); /* 15.1.? get-output-string, get-output-bytevector */ add_applicative(K, ground_env, "get-output-string", get_output_buffer, 1, - b2tv(false)); + b2tv(false)); add_applicative(K, ground_env, "get-output-bytevector", get_output_buffer, - 1, b2tv(true)); + 1, b2tv(true)); /* 15.1.7 read */ add_applicative(K, ground_env, "read", gread, 0); @@ -1290,17 +1290,17 @@ void kinit_ports_ground_env(klisp_State *K) /* 15.1.? eof-object? */ add_applicative(K, ground_env, "eof-object?", typep, 2, symbol, - i2tv(K_TEOF)); + i2tv(K_TEOF)); /* 15.1.? newline */ add_applicative(K, ground_env, "newline", newline, 0); /* 15.1.? write-char */ add_applicative(K, ground_env, "write-char", write_char, 0); /* 15.1.? read-char */ add_applicative(K, ground_env, "read-char", read_peek_char, 1, - b2tv(false)); + b2tv(false)); /* 15.1.? peek-char */ add_applicative(K, ground_env, "peek-char", read_peek_char, 1, - b2tv(true)); + b2tv(true)); /* 15.1.? char-ready? */ /* XXX: this always return #t, proper behaviour requires platform specific code (probably select for posix, a thread for windows @@ -1311,10 +1311,10 @@ void kinit_ports_ground_env(klisp_State *K) add_applicative(K, ground_env, "write-u8", write_u8, 0); /* 15.1.? read-u8 */ add_applicative(K, ground_env, "read-u8", read_peek_u8, 1, - b2tv(false)); + b2tv(false)); /* 15.1.? peek-u8 */ add_applicative(K, ground_env, "peek-u8", read_peek_u8, 1, - b2tv(true)); + b2tv(true)); /* 15.1.? u8-ready? */ /* XXX: this always return #t, proper behaviour requires platform specific code (probably select for posix, a thread for windows @@ -1323,9 +1323,9 @@ void kinit_ports_ground_env(klisp_State *K) add_applicative(K, ground_env, "u8-ready?", u8_readyp, 0); /* 15.2.1 call-with-input-file, call-with-output-file */ add_applicative(K, ground_env, "call-with-input-file", call_with_file, - 2, symbol, b2tv(false)); + 2, symbol, b2tv(false)); add_applicative(K, ground_env, "call-with-output-file", call_with_file, - 2, symbol, b2tv(true)); + 2, symbol, b2tv(true)); /* 15.2.2 load */ add_applicative(K, ground_env, "load", load, 0); /* 15.2.? require */ diff --git a/src/kgpromises.c b/src/kgpromises.c @@ -42,29 +42,29 @@ void do_handle_result(klisp_State *K) /* check to see if promise was determined before the eval completed */ if (ttisnil(kpromise_maybe_env(prom))) { - /* discard obj, return previous result */ - kapply_cc(K, kpromise_exp(prom)); + /* discard obj, return previous result */ + kapply_cc(K, kpromise_exp(prom)); } else if (ttispromise(obj)) { - /* force iteratively, by sharing pairs so that when obj - determines a value, prom also does */ - TValue node = kpromise_node(obj); - kpromise_node(prom) = node; - TValue expr = kpromise_exp(prom); - TValue maybe_env = kpromise_maybe_env(prom); - if (ttisnil(maybe_env)) { - /* promise was already determined */ - kapply_cc(K, expr); - } else { - TValue new_cont = kmake_continuation(K, kget_cc(K), - do_handle_result, 1, prom); - kset_cc(K, new_cont); - ktail_eval(K, expr, maybe_env); - } + /* force iteratively, by sharing pairs so that when obj + determines a value, prom also does */ + TValue node = kpromise_node(obj); + kpromise_node(prom) = node; + TValue expr = kpromise_exp(prom); + TValue maybe_env = kpromise_maybe_env(prom); + if (ttisnil(maybe_env)) { + /* promise was already determined */ + kapply_cc(K, expr); + } else { + TValue new_cont = kmake_continuation(K, kget_cc(K), + do_handle_result, 1, prom); + kset_cc(K, new_cont); + ktail_eval(K, expr, maybe_env); + } } else { - /* memoize result */ - TValue node = kpromise_node(prom); - kset_car(node, obj); - kset_cdr(node, KNIL); + /* memoize result */ + TValue node = kpromise_node(prom); + kset_car(node, obj); + kset_cdr(node, KNIL); } } @@ -79,18 +79,18 @@ void force(klisp_State *K) UNUSED(denv); bind_1p(K, ptree, obj); if (!ttispromise(obj)) { - /* non promises force to themselves */ - kapply_cc(K, obj); + /* non promises force to themselves */ + kapply_cc(K, obj); } else if (ttisnil(kpromise_maybe_env(obj))) { - /* promise was already determined */ - kapply_cc(K, kpromise_exp(obj)); + /* promise was already determined */ + kapply_cc(K, kpromise_exp(obj)); } else { - TValue expr = kpromise_exp(obj); - TValue env = kpromise_maybe_env(obj); - TValue new_cont = kmake_continuation(K, kget_cc(K), do_handle_result, - 1, obj); - kset_cc(K, new_cont); - ktail_eval(K, expr, env); + TValue expr = kpromise_exp(obj); + TValue env = kpromise_maybe_env(obj); + TValue new_cont = kmake_continuation(K, kget_cc(K), do_handle_result, + 1, obj); + kset_cc(K, new_cont); + ktail_eval(K, expr, env); } } @@ -138,7 +138,7 @@ void kinit_promises_ground_env(klisp_State *K) /* 9.1.1 promise? */ add_applicative(K, ground_env, "promise?", typep, 2, symbol, - i2tv(K_TPROMISE)); + i2tv(K_TPROMISE)); /* 9.1.2 force */ add_applicative(K, ground_env, "force", force, 0); /* 9.1.3 $lazy */ diff --git a/src/kgstrings.c b/src/kgstrings.c @@ -43,18 +43,18 @@ void make_string(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_al1tp(K, ptree, "exact integer", keintegerp, tv_s, - maybe_char); + maybe_char); char fill = ' '; if (get_opt_tpar(K, maybe_char, "char", ttischar)) - fill = chvalue(maybe_char); + fill = chvalue(maybe_char); if (knegativep(tv_s)) { - klispE_throw_simple(K, "negative size"); - return; + klispE_throw_simple(K, "negative size"); + return; } else if (!ttisfixint(tv_s)) { - klispE_throw_simple(K, "size is too big"); - return; + klispE_throw_simple(K, "size is too big"); + return; } TValue new_str = kstring_new_sf(K, ivalue(tv_s), fill); @@ -86,19 +86,19 @@ void string_ref(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "string", ttisstring, str, - "exact integer", keintegerp, tv_i); + "exact integer", keintegerp, tv_i); if (!ttisfixint(tv_i)) { - /* TODO show index */ - klispE_throw_simple(K, "index out of bounds"); - return; + /* TODO show index */ + klispE_throw_simple(K, "index out of bounds"); + return; } int32_t i = ivalue(tv_i); if (i < 0 || i >= kstring_size(str)) { - /* TODO show index */ - klispE_throw_simple(K, "index out of bounds"); - return; + /* TODO show index */ + klispE_throw_simple(K, "index out of bounds"); + return; } TValue res = ch2tv(kstring_buf(str)[i]); @@ -115,23 +115,23 @@ void string_setB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_3tp(K, ptree, "string", ttisstring, str, - "exact integer", keintegerp, tv_i, "char", ttischar, tv_ch); + "exact integer", keintegerp, tv_i, "char", ttischar, tv_ch); if (!ttisfixint(tv_i)) { - /* TODO show index */ - klispE_throw_simple(K, "index out of bounds"); - return; + /* TODO show index */ + klispE_throw_simple(K, "index out of bounds"); + return; } else if (kstring_immutablep(str)) { - klispE_throw_simple(K, "immutable string"); - return; + klispE_throw_simple(K, "immutable string"); + return; } int32_t i = ivalue(tv_i); if (i < 0 || i >= kstring_size(str)) { - /* TODO show index */ - klispE_throw_simple(K, "index out of bounds"); - return; + /* TODO show index */ + klispE_throw_simple(K, "index out of bounds"); + return; } kstring_buf(str)[i] = chvalue(tv_ch); @@ -173,7 +173,7 @@ void kstring_change_case(klisp_State *K) TValue res = kstring_new_bs(K, kstring_buf(str), size); char *buf = kstring_buf(res); for(int32_t i = 0; i < size; ++i, buf++) { - *buf = fn(*buf); + *buf = fn(*buf); } kapply_cc(K, res); } @@ -192,17 +192,17 @@ void kstring_title_case(klisp_State *K) char *buf = kstring_buf(res); bool first = true; while(size-- > 0) { - char ch = *buf; - if (ch == ' ') - first = true; - else if (!first) - *buf = tolower(ch); - else if (isalpha(ch)) { + char ch = *buf; + if (ch == ' ') + first = true; + else if (!first) + *buf = tolower(ch); + else if (isalpha(ch)) { /* only count as first letter something that can be capitalized */ - *buf = toupper(ch); - first = false; - } - ++buf; + *buf = toupper(ch); + first = false; + } + ++buf; } kapply_cc(K, res); } @@ -227,17 +227,17 @@ bool kstring_ci_eqp(TValue str1, TValue str2) { int32_t size = kstring_size(str1); if (kstring_size(str2) != size) - return false; + return false; else { - char *buf1 = kstring_buf(str1); - char *buf2 = kstring_buf(str2); - - while(size--) { - if (tolower(*buf1) != tolower(*buf2)) - return false; - buf1++, buf2++; - } - return true; + char *buf1 = kstring_buf(str1); + char *buf2 = kstring_buf(str2); + + while(size--) { + if (tolower(*buf1) != tolower(*buf2)) + return false; + buf1++, buf2++; + } + return true; } } @@ -266,12 +266,12 @@ bool kstring_ci_ltp(TValue str1, TValue str2) char *buf2 = kstring_buf(str2); while(min_size--) { - int diff = (int) tolower(*buf1) - (int) tolower(*buf2); - if (diff > 0) - return false; - else if (diff < 0) - return true; - buf1++, buf2++; + int diff = (int) tolower(*buf1) - (int) tolower(*buf2); + if (diff > 0) + return false; + else if (diff < 0) + return true; + buf1++, buf2++; } return size1 < size2; } @@ -304,40 +304,40 @@ void substring(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_3tp(K, ptree, "string", ttisstring, str, - "exact integer", keintegerp, tv_start, - "exact integer", keintegerp, tv_end); + "exact integer", keintegerp, tv_start, + "exact integer", keintegerp, tv_end); if (!ttisfixint(tv_start) || ivalue(tv_start) < 0 || - ivalue(tv_start) > kstring_size(str)) { - /* TODO show index */ - klispE_throw_simple(K, "start index out of bounds"); - return; + ivalue(tv_start) > kstring_size(str)) { + /* TODO show index */ + klispE_throw_simple(K, "start index out of bounds"); + return; } int32_t start = ivalue(tv_start); if (!ttisfixint(tv_end) || ivalue(tv_end) < 0 || - ivalue(tv_end) > kstring_size(str)) { - klispE_throw_simple(K, "end index out of bounds"); - return; + ivalue(tv_end) > kstring_size(str)) { + klispE_throw_simple(K, "end index out of bounds"); + return; } int32_t end = ivalue(tv_end); if (start > end) { - /* TODO show indexes */ - klispE_throw_simple(K, "end index is smaller than start index"); - return; + /* TODO show indexes */ + klispE_throw_simple(K, "end index is smaller than start index"); + return; } int32_t size = end - start; TValue new_str; /* the if isn't strictly necessary but it's clearer this way */ if (size == 0) { - new_str = K->empty_string; + new_str = K->empty_string; } else { - /* always returns mutable strings */ - new_str = kstring_new_bs(K, kstring_buf(str)+start, size); + /* always returns mutable strings */ + new_str = kstring_new_bs(K, kstring_buf(str)+start, size); } kapply_cc(K, new_str); } @@ -363,32 +363,32 @@ void string_append(klisp_State *K) int32_t saved_pairs = pairs; /* save pairs for next loop */ TValue tail = ptree; while(pairs--) { - total_size += kstring_size(kcar(tail)); - if (total_size > INT32_MAX) { - klispE_throw_simple(K, "resulting string is too big"); - return; - } - tail = kcdr(tail); + total_size += kstring_size(kcar(tail)); + if (total_size > INT32_MAX) { + klispE_throw_simple(K, "resulting string is too big"); + return; + } + tail = kcdr(tail); } /* this is safe */ int32_t size = (int32_t) total_size; if (size == 0) { - new_str = K->empty_string; + new_str = K->empty_string; } else { - new_str = kstring_new_s(K, size); - char *buf = kstring_buf(new_str); - /* loop again to copy the chars of each string */ - tail = ptree; - pairs = saved_pairs; - - while(pairs--) { - TValue first = kcar(tail); - int32_t first_size = kstring_size(first); - memcpy(buf, kstring_buf(first), first_size); - buf += first_size; - tail = kcdr(tail); - } + new_str = kstring_new_s(K, size); + char *buf = kstring_buf(new_str); + /* loop again to copy the chars of each string */ + tail = ptree; + pairs = saved_pairs; + + while(pairs--) { + TValue first = kcar(tail); + int32_t first_size = kstring_size(first); + memcpy(buf, kstring_buf(first), first_size); + buf += first_size; + tail = kcdr(tail); + } } kapply_cc(K, new_str); @@ -442,19 +442,19 @@ void string_to_vector(klisp_State *K) TValue res; if (kstring_emptyp(str)) { - res = K->empty_vector; + res = K->empty_vector; } else { - uint32_t size = kstring_size(str); - - /* MAYBE add vector constructor without fill */ - /* no need to root this */ - res = kvector_new_sf(K, size, KINERT); - char *src = kstring_buf(str); - TValue *dst = kvector_buf(res); - while(size--) { - char ch = *src++; /* not needed but just in case */ - *dst++ = ch2tv(ch); - } + uint32_t size = kstring_size(str); + + /* MAYBE add vector constructor without fill */ + /* no need to root this */ + res = kvector_new_sf(K, size, KINERT); + char *src = kstring_buf(str); + TValue *dst = kvector_buf(res); + while(size--) { + char ch = *src++; /* not needed but just in case */ + *dst++ = ch2tv(ch); + } } kapply_cc(K, res); } @@ -473,22 +473,22 @@ void vector_to_string(klisp_State *K) TValue res; if (kvector_emptyp(vec)) { - res = K->empty_string; + res = K->empty_string; } else { - uint32_t size = kvector_size(vec); - - res = kstring_new_s(K, size); /* no need to root this */ - TValue *src = kvector_buf(vec); - char *dst = kstring_buf(res); - while(size--) { - TValue tv = *src++; - if (!ttischar(tv)) { - klispE_throw_simple_with_irritants(K, "Non char object found", - 1, tv); - return; - } - *dst++ = chvalue(tv); - } + uint32_t size = kvector_size(vec); + + res = kstring_new_s(K, size); /* no need to root this */ + TValue *src = kvector_buf(vec); + char *dst = kstring_buf(res); + while(size--) { + TValue tv = *src++; + if (!ttischar(tv)) { + klispE_throw_simple_with_irritants(K, "Non char object found", + 1, tv); + return; + } + *dst++ = chvalue(tv); + } } kapply_cc(K, res); } @@ -507,19 +507,19 @@ void string_to_bytevector(klisp_State *K) TValue res; if (kstring_emptyp(str)) { - res = K->empty_bytevector; + res = K->empty_bytevector; } else { - uint32_t size = kstring_size(str); + uint32_t size = kstring_size(str); - /* MAYBE add bytevector constructor without fill */ - /* no need to root this */ - res = kbytevector_new_s(K, size); - char *src = kstring_buf(str); - uint8_t *dst = kbytevector_buf(res); + /* MAYBE add bytevector constructor without fill */ + /* no need to root this */ + res = kbytevector_new_s(K, size); + char *src = kstring_buf(str); + uint8_t *dst = kbytevector_buf(res); - while(size--) { - *dst++ = (uint8_t)*src++; - } + while(size--) { + *dst++ = (uint8_t)*src++; + } } kapply_cc(K, res); } @@ -538,21 +538,21 @@ void bytevector_to_string(klisp_State *K) TValue res; if (kbytevector_emptyp(bb)) { - res = K->empty_string; + res = K->empty_string; } else { - uint32_t size = kbytevector_size(bb); - res = kstring_new_s(K, size); /* no need to root this */ - uint8_t *src = kbytevector_buf(bb); - char *dst = kstring_buf(res); - while(size--) { - uint8_t u8 = *src++; - if (u8 >= 128) { - klispE_throw_simple_with_irritants(K, "Char out of range", - 1, i2tv(u8)); - return; - } - *dst++ = (char) u8; - } + uint32_t size = kbytevector_size(bb); + res = kstring_new_s(K, size); /* no need to root this */ + uint8_t *src = kbytevector_buf(bb); + char *dst = kstring_buf(res); + while(size--) { + uint8_t u8 = *src++; + if (u8 >= 128) { + klispE_throw_simple_with_irritants(K, "Char out of range", + 1, i2tv(u8)); + return; + } + *dst++ = (char) u8; + } } kapply_cc(K, res); } @@ -572,9 +572,9 @@ void string_copy(klisp_State *K) TValue new_str; /* the if isn't strictly necessary but it's clearer this way */ if (tv_equal(str, K->empty_string)) { - new_str = str; + new_str = str; } else { - new_str = kstring_new_bs(K, kstring_buf(str), kstring_size(str)); + new_str = kstring_new_bs(K, kstring_buf(str), kstring_size(str)); } kapply_cc(K, new_str); } @@ -592,9 +592,9 @@ void string_to_immutable_string(klisp_State *K) TValue res_str; if (kstring_immutablep(str)) {/* this includes the empty list */ - res_str = str; + res_str = str; } else { - res_str = kstring_new_bs_imm(K, kstring_buf(str), kstring_size(str)); + res_str = kstring_new_bs_imm(K, kstring_buf(str), kstring_size(str)); } kapply_cc(K, res_str); } @@ -609,11 +609,11 @@ void string_fillB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "string", ttisstring, str, - "char", ttischar, tv_ch); + "char", ttischar, tv_ch); if (kstring_immutablep(str)) { - klispE_throw_simple(K, "immutable string"); - return; + klispE_throw_simple(K, "immutable string"); + return; } memset(kstring_buf(str), chvalue(tv_ch), kstring_size(str)); @@ -626,7 +626,7 @@ void kinit_strings_ground_env(klisp_State *K) TValue ground_env = K->ground_env; TValue symbol, value; - /* + /* ** This section is still missing from the report. The bindings here are ** taken from r5rs scheme and should not be considered standard. They are ** provided in the meantime to allow programs to use string features @@ -635,12 +635,12 @@ void kinit_strings_ground_env(klisp_State *K) /* 13.1.1? string? */ add_applicative(K, ground_env, "string?", typep, 2, symbol, - i2tv(K_TSTRING)); + i2tv(K_TSTRING)); /* 13.? immutable-string?, mutable-string? */ add_applicative(K, ground_env, "immutable-string?", ftypep, 2, symbol, - p2tv(kimmutable_stringp)); + p2tv(kimmutable_stringp)); add_applicative(K, ground_env, "mutable-string?", ftypep, 2, symbol, - p2tv(kmutable_stringp)); + p2tv(kmutable_stringp)); /* 13.1.2? make-string */ add_applicative(K, ground_env, "make-string", make_string, 0); /* 13.1.3? string-length */ @@ -654,35 +654,35 @@ void kinit_strings_ground_env(klisp_State *K) /* 13.?? string-upcase, string-downcase, string-titlecase, string-foldcase */ add_applicative(K, ground_env, "string-upcase", kstring_change_case, 1, - p2tv(toupper)); + p2tv(toupper)); add_applicative(K, ground_env, "string-downcase", kstring_change_case, 1, - p2tv(tolower)); + p2tv(tolower)); add_applicative(K, ground_env, "string-titlecase", kstring_title_case, 0); add_applicative(K, ground_env, "string-foldcase", kstring_change_case, 1, - p2tv(tolower)); + p2tv(tolower)); /* 13.2.2? string=?, string-ci=? */ add_applicative(K, ground_env, "string=?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_eqp)); + symbol, p2tv(kstringp), p2tv(kstring_eqp)); add_applicative(K, ground_env, "string-ci=?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_ci_eqp)); + symbol, p2tv(kstringp), p2tv(kstring_ci_eqp)); /* 13.2.3? string<?, string<=?, string>?, string>=? */ add_applicative(K, ground_env, "string<?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_ltp)); + symbol, p2tv(kstringp), p2tv(kstring_ltp)); add_applicative(K, ground_env, "string<=?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_lep)); + symbol, p2tv(kstringp), p2tv(kstring_lep)); add_applicative(K, ground_env, "string>?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_gtp)); + symbol, p2tv(kstringp), p2tv(kstring_gtp)); add_applicative(K, ground_env, "string>=?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_gep)); + symbol, p2tv(kstringp), p2tv(kstring_gep)); /* 13.2.4? string-ci<?, string-ci<=?, string-ci>?, string-ci>=? */ add_applicative(K, ground_env, "string-ci<?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_ci_ltp)); + symbol, p2tv(kstringp), p2tv(kstring_ci_ltp)); add_applicative(K, ground_env, "string-ci<=?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_ci_lep)); + symbol, p2tv(kstringp), p2tv(kstring_ci_lep)); add_applicative(K, ground_env, "string-ci>?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_ci_gtp)); + symbol, p2tv(kstringp), p2tv(kstring_ci_gtp)); add_applicative(K, ground_env, "string-ci>=?", ftyped_bpredp, 3, - symbol, p2tv(kstringp), p2tv(kstring_ci_gep)); + symbol, p2tv(kstringp), p2tv(kstring_ci_gep)); /* 13.2.5? substring */ add_applicative(K, ground_env, "substring", substring, 0); /* 13.2.6? string-append */ @@ -695,14 +695,14 @@ void kinit_strings_ground_env(klisp_State *K) add_applicative(K, ground_env, "vector->string", vector_to_string, 0); /* 13.?? string->bytevector, bytevector->string */ add_applicative(K, ground_env, "string->bytevector", - string_to_bytevector, 0); + string_to_bytevector, 0); add_applicative(K, ground_env, "bytevector->string", - bytevector_to_string, 0); + bytevector_to_string, 0); /* 13.2.8? string-copy */ add_applicative(K, ground_env, "string-copy", string_copy, 0); /* 13.2.9? string->immutable-string */ add_applicative(K, ground_env, "string->immutable-string", - string_to_immutable_string, 0); + string_to_immutable_string, 0); /* 13.2.10? string-fill! */ add_applicative(K, ground_env, "string-fill!", string_fillB, 0); diff --git a/src/kgsymbols.c b/src/kgsymbols.c @@ -64,7 +64,7 @@ void kinit_symbols_ground_env(klisp_State *K) /* 4.4.1 symbol? */ add_applicative(K, ground_env, "symbol?", typep, 2, symbol, - i2tv(K_TSYMBOL)); + i2tv(K_TSYMBOL)); /* ** This section is still missing from the report. The bindings here are ** taken from r5rs scheme and should not be considered standard. diff --git a/src/kgsystem.c b/src/kgsystem.c @@ -41,11 +41,11 @@ void current_second(klisp_State *K) check_0p(K, ptree); time_t now = time(NULL); if (now == -1) { - klispE_throw_simple(K, "couldn't get time"); - return; + klispE_throw_simple(K, "couldn't get time"); + return; } else { - TValue res = kinteger_new_uint64(K, (uint64_t) now); - kapply_cc(K, res); + TValue res = kinteger_new_uint64(K, (uint64_t) now); + kapply_cc(K, res); } } @@ -82,8 +82,8 @@ void file_existsp(klisp_State *K) TValue res = KFALSE; FILE *file = fopen(kstring_buf(filename), "r"); if (file) { - res = KTRUE; - UNUSED(fclose(file)); + res = KTRUE; + UNUSED(fclose(file)); } kapply_cc(K, res); } @@ -108,8 +108,8 @@ void delete_file(klisp_State *K) klispE_throw_errno_with_irritants(K, "remove", 1, filename); return; } else { - kapply_cc(K, KINERT); - return; + kapply_cc(K, KINERT); + return; } } @@ -124,7 +124,7 @@ void rename_file(klisp_State *K) UNUSED(denv); bind_2tp(K, ptree, "string", ttisstring, old_filename, - "string", ttisstring, new_filename); + "string", ttisstring, new_filename); /* TEMP: this should probably be done in a operating system specific manner, but this will do for now */ @@ -134,8 +134,8 @@ void rename_file(klisp_State *K) klispE_throw_errno_with_irritants(K, "rename", 2, old_filename, new_filename); return; } else { - kapply_cc(K, KINERT); - return; + kapply_cc(K, KINERT); + return; } } @@ -144,7 +144,7 @@ void get_arguments(klisp_State *K) { /* ** xparams[0]: immutable argument list - */ + */ TValue ptree = K->next_value; TValue *xparams = K->next_xparams; TValue denv = K->next_env; @@ -171,9 +171,9 @@ void get_environment_variable(klisp_State *K) /* I follow r7rs here, but should probably throw error */ TValue res; if (str == NULL) { - res = KFALSE; + res = KFALSE; } else { - res = kstring_new_b_imm(K, str); + res = kstring_new_b_imm(K, str); } kapply_cc(K, res); } @@ -182,7 +182,7 @@ void get_environment_variables(klisp_State *K) { /* ** xparams[0]: immutable variable list - */ + */ TValue ptree = K->next_value; TValue *xparams = K->next_xparams; TValue denv = K->next_env; @@ -197,14 +197,14 @@ void get_environment_variables(klisp_State *K) /* TODO test, if that doesn't work, try to find a way avoiding taking extra params in main */ /* I think it's defined in unistd, but it needs to have __USE_GNU - defined. The correct way to do that would be to define _GNU_SOURCE - before including any system files... That's not so good for an - embeddable interpreter, but it could be done in the makefile I guess */ + defined. The correct way to do that would be to define _GNU_SOURCE + before including any system files... That's not so good for an + embeddable interpreter, but it could be done in the makefile I guess */ extern #ifdef _WIN32 - __declspec(dllimport) +__declspec(dllimport) #endif - char **environ; +char **environ; /* Helper for get-environment-variables */ TValue create_env_var_list(klisp_State *K) @@ -217,15 +217,15 @@ TValue create_env_var_list(klisp_State *K) /* TODO test, if that doesn't work, try to find a way avoiding taking extra params in main */ for(char **env = environ; *env != NULL; ++env) { - /* *env is of the form: "<name>=<value>", presumably, name can't have - an equal sign! */ - char *eq = strchr(*env, '='); - int name_len = eq - *env; - klisp_assert(eq != NULL); /* shouldn't happen */ - var_name = kstring_new_bs_imm(K, *env, name_len); - var_value = kstring_new_b_imm(K, *env + name_len + 1); - TValue new_entry = kimm_cons(K, var_name, var_value); - tail = kimm_cons(K, new_entry, tail); + /* *env is of the form: "<name>=<value>", presumably, name can't have + an equal sign! */ + char *eq = strchr(*env, '='); + int name_len = eq - *env; + klisp_assert(eq != NULL); /* shouldn't happen */ + var_name = kstring_new_bs_imm(K, *env, name_len); + var_value = kstring_new_b_imm(K, *env + name_len + 1); + TValue new_entry = kimm_cons(K, var_name, var_value); + tail = kimm_cons(K, new_entry, tail); } return tail; } @@ -242,7 +242,7 @@ void kinit_system_ground_env(klisp_State *K) add_applicative(K, ground_env, "current-jiffy", current_jiffy, 0); /* ??.?.? jiffies-per-second */ add_applicative(K, ground_env, "jiffies-per-second", jiffies_per_second, - 0); + 0); /* ?.? file-exists? */ add_applicative(K, ground_env, "file-exists?", file_existsp, 0); /* ?.? delete-file */ @@ -253,12 +253,12 @@ void kinit_system_ground_env(klisp_State *K) /* The value for these two will get set later by the interpreter */ /* ?.? get-script-arguments, get-interpreter-arguments */ add_applicative(K, ground_env, "get-script-arguments", get_arguments, - 1, KNIL); + 1, KNIL); add_applicative(K, ground_env, "get-interpreter-arguments", get_arguments, - 1, KNIL); + 1, KNIL); /* ?.? get-environment-variable, get-environment-variables */ add_applicative(K, ground_env, "get-environment-variable", - get_environment_variable, 0); + get_environment_variable, 0); add_applicative(K, ground_env, "get-environment-variables", - get_environment_variables, 1, create_env_var_list(K)); + get_environment_variables, 1, create_env_var_list(K)); } diff --git a/src/kgvectors.c b/src/kgvectors.c @@ -48,7 +48,7 @@ void make_vector(klisp_State *K) return; } TValue new_vector = (ivalue(tv_s) == 0)? - K->empty_vector + K->empty_vector : kvector_new_sf(K, ivalue(tv_s), fill); kapply_cc(K, new_vector); } @@ -127,7 +127,7 @@ void vector_copy(klisp_State *K) bind_1tp(K, ptree, "vector", ttisvector, v); TValue new_vector = kvector_emptyp(v)? - v + v : kvector_new_bs_g(K, true, kvector_buf(v), kvector_size(v)); kapply_cc(K, new_vector); } @@ -185,19 +185,19 @@ void bytevector_to_vector(klisp_State *K) TValue res; if (kbytevector_emptyp(str)) { - res = K->empty_vector; + res = K->empty_vector; } else { - uint32_t size = kbytevector_size(str); - - /* MAYBE add vector constructor without fill */ - /* no need to root this */ - res = kvector_new_sf(K, size, KINERT); - uint8_t *src = kbytevector_buf(str); - TValue *dst = kvector_buf(res); - while(size--) { - uint8_t u8 = *src++; /* not needed but just in case */ - *dst++ = i2tv(u8); - } + uint32_t size = kbytevector_size(str); + + /* MAYBE add vector constructor without fill */ + /* no need to root this */ + res = kvector_new_sf(K, size, KINERT); + uint8_t *src = kbytevector_buf(str); + TValue *dst = kvector_buf(res); + while(size--) { + uint8_t u8 = *src++; /* not needed but just in case */ + *dst++ = i2tv(u8); + } } kapply_cc(K, res); } @@ -216,22 +216,22 @@ void vector_to_bytevector(klisp_State *K) TValue res; if (kvector_emptyp(vec)) { - res = K->empty_bytevector; + res = K->empty_bytevector; } else { - uint32_t size = kvector_size(vec); - - res = kbytevector_new_s(K, size); /* no need to root this */ - TValue *src = kvector_buf(vec); - uint8_t *dst = kbytevector_buf(res); - while(size--) { - TValue tv = *src++; - if (!ttisu8(tv)) { - klispE_throw_simple_with_irritants(K, "Non u8 object found", - 1, tv); - return; - } - *dst++ = (uint8_t) ivalue(tv); - } + uint32_t size = kvector_size(vec); + + res = kbytevector_new_s(K, size); /* no need to root this */ + TValue *src = kvector_buf(vec); + uint8_t *dst = kbytevector_buf(res); + while(size--) { + TValue tv = *src++; + if (!ttisu8(tv)) { + klispE_throw_simple_with_irritants(K, "Non u8 object found", + 1, tv); + return; + } + *dst++ = (uint8_t) ivalue(tv); + } } kapply_cc(K, res); } @@ -246,21 +246,21 @@ void vector_copyB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "vector", ttisvector, vector1, - "vector", ttisvector, vector2); + "vector", ttisvector, vector2); if (kvector_immutablep(vector2)) { - klispE_throw_simple(K, "immutable destination vector"); - return; + klispE_throw_simple(K, "immutable destination vector"); + return; } else if (kvector_size(vector1) > kvector_size(vector2)) { - klispE_throw_simple(K, "destination vector is too small"); - return; + klispE_throw_simple(K, "destination vector is too small"); + return; } if (!tv_equal(vector1, vector2) && - !tv_equal(vector1, K->empty_vector)) { - memcpy(kvector_buf(vector2), - kvector_buf(vector1), - kvector_size(vector1) * sizeof(TValue)); + !tv_equal(vector1, K->empty_vector)) { + memcpy(kvector_buf(vector2), + kvector_buf(vector1), + kvector_size(vector1) * sizeof(TValue)); } kapply_cc(K, KINERT); } @@ -276,40 +276,40 @@ void vector_copy_partial(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_3tp(K, ptree, "vector", ttisvector, vector, - "exact integer", keintegerp, tv_start, - "exact integer", keintegerp, tv_end); + "exact integer", keintegerp, tv_start, + "exact integer", keintegerp, tv_end); if (!ttisfixint(tv_start) || ivalue(tv_start) < 0 || - ivalue(tv_start) > kvector_size(vector)) { - /* TODO show index */ - klispE_throw_simple(K, "start index out of bounds"); - return; + ivalue(tv_start) > kvector_size(vector)) { + /* TODO show index */ + klispE_throw_simple(K, "start index out of bounds"); + return; } int32_t start = ivalue(tv_start); if (!ttisfixint(tv_end) || ivalue(tv_end) < 0 || - ivalue(tv_end) > kvector_size(vector)) { - klispE_throw_simple(K, "end index out of bounds"); - return; + ivalue(tv_end) > kvector_size(vector)) { + klispE_throw_simple(K, "end index out of bounds"); + return; } int32_t end = ivalue(tv_end); if (start > end) { - /* TODO show indexes */ - klispE_throw_simple(K, "end index is smaller than start index"); - return; + /* TODO show indexes */ + klispE_throw_simple(K, "end index is smaller than start index"); + return; } int32_t size = end - start; TValue new_vector; /* the if isn't strictly necessary but it's clearer this way */ if (size == 0) { - new_vector = K->empty_vector; + new_vector = K->empty_vector; } else { - new_vector = kvector_new_bs_g(K, true, kvector_buf(vector) - + start, size); + new_vector = kvector_new_bs_g(K, true, kvector_buf(vector) + + start, size); } kapply_cc(K, new_vector); } @@ -324,64 +324,64 @@ void vector_copy_partialB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_al3tp(K, ptree, "vector", ttisvector, vector1, - "exact integer", keintegerp, tv_start, - "exact integer", keintegerp, tv_end, - rest); + "exact integer", keintegerp, tv_start, + "exact integer", keintegerp, tv_end, + rest); /* XXX: this will send wrong error msgs (bad number of arg) */ bind_2tp(K, rest, - "vector", ttisvector, vector2, - "exact integer", keintegerp, tv_start2); + "vector", ttisvector, vector2, + "exact integer", keintegerp, tv_start2); if (!ttisfixint(tv_start) || ivalue(tv_start) < 0 || - ivalue(tv_start) > kvector_size(vector1)) { - /* TODO show index */ - klispE_throw_simple(K, "start index out of bounds"); - return; + ivalue(tv_start) > kvector_size(vector1)) { + /* TODO show index */ + klispE_throw_simple(K, "start index out of bounds"); + return; } int32_t start = ivalue(tv_start); if (!ttisfixint(tv_end) || ivalue(tv_end) < 0 || - ivalue(tv_end) > kvector_size(vector1)) { - klispE_throw_simple(K, "end index out of bounds"); - return; + ivalue(tv_end) > kvector_size(vector1)) { + klispE_throw_simple(K, "end index out of bounds"); + return; } int32_t end = ivalue(tv_end); if (start > end) { - /* TODO show indexes */ - klispE_throw_simple(K, "end index is smaller than start index"); - return; + /* TODO show indexes */ + klispE_throw_simple(K, "end index is smaller than start index"); + return; } int32_t size = end - start; if (kvector_immutablep(vector2)) { - klispE_throw_simple(K, "immutable destination vector"); - return; + klispE_throw_simple(K, "immutable destination vector"); + return; } if (!ttisfixint(tv_start2) || ivalue(tv_start2) < 0 || - ivalue(tv_start2) > kvector_size(vector2)) { - klispE_throw_simple(K, "to index out of bounds"); - return; + ivalue(tv_start2) > kvector_size(vector2)) { + klispE_throw_simple(K, "to index out of bounds"); + return; } int32_t start2 = ivalue(tv_start2); int64_t end2 = (int64_t) start2 + size; if ((end2 > INT32_MAX) || - (((int32_t) end2) > kvector_size(vector2))) { - klispE_throw_simple(K, "not enough space in destination"); - return; + (((int32_t) end2) > kvector_size(vector2))) { + klispE_throw_simple(K, "not enough space in destination"); + return; } if (size > 0) { - memcpy(kvector_buf(vector2) + start2, - kvector_buf(vector1) + start, - size * sizeof(TValue)); + memcpy(kvector_buf(vector2) + start2, + kvector_buf(vector1) + start, + size * sizeof(TValue)); } kapply_cc(K, KINERT); } @@ -396,17 +396,17 @@ void vector_fillB(klisp_State *K) UNUSED(xparams); UNUSED(denv); bind_2tp(K, ptree, "vector", ttisvector, vector, - "any", anytype, fill); + "any", anytype, fill); if (kvector_immutablep(vector)) { - klispE_throw_simple(K, "immutable vector"); - return; + klispE_throw_simple(K, "immutable vector"); + return; } uint32_t size = kvector_size(vector); TValue *buf = kvector_buf(vector); while(size-- > 0) { - *buf++ = fill; + *buf++ = fill; } kapply_cc(K, KINERT); } @@ -420,8 +420,8 @@ void vector_to_immutable_vector(klisp_State *K) bind_1tp(K, ptree, "vector", ttisvector, v); TValue res = kvector_immutablep(v)? - v - : kvector_new_bs_g(K, false, kvector_buf(v), kvector_size(v)); + v + : kvector_new_bs_g(K, false, kvector_buf(v), kvector_size(v)); kapply_cc(K, res); } @@ -431,7 +431,7 @@ void kinit_vectors_ground_env(klisp_State *K) TValue ground_env = K->ground_env; TValue symbol, value; - /* + /* ** This section is not in the report. The bindings here are ** taken from the r7rs scheme draft and should not be considered standard. ** They are provided in the meantime to allow programs to use vectors. @@ -464,9 +464,9 @@ void kinit_vectors_ground_env(klisp_State *K) /* ?.? vector->bytevector, bytevector->vector */ add_applicative(K, ground_env, "vector->bytevector", - vector_to_bytevector, 0); + vector_to_bytevector, 0); add_applicative(K, ground_env, "bytevector->vector", - bytevector_to_vector, 0); + bytevector_to_vector, 0); /* ?.? vector->string, string->vector */ /* in kgstrings.c */ @@ -476,15 +476,15 @@ void kinit_vectors_ground_env(klisp_State *K) /* ?.? vector-copy-partial */ add_applicative(K, ground_env, "vector-copy-partial", - vector_copy_partial, 0); + vector_copy_partial, 0); /* ?.? vector-copy-partial! */ add_applicative(K, ground_env, "vector-copy-partial!", - vector_copy_partialB, 0); + vector_copy_partialB, 0); /* ?.? vector-fill! */ add_applicative(K, ground_env, "vector-fill!", vector_fillB, 0); /* ?.? vector->immutable-vector */ add_applicative(K, ground_env, "vector->immutable-vector", - vector_to_immutable_vector, 0); + vector_to_immutable_vector, 0); } diff --git a/src/kinteger.c b/src/kinteger.c @@ -53,12 +53,12 @@ TValue kbigint_copy(klisp_State *K, TValue src) /* this works for bigints & fixints, returns true if ok */ bool kinteger_read(klisp_State *K, char *buf, int32_t base, TValue *out, - char **end) + char **end) { TValue res = kbigint_make_simple(K); krooted_tvs_push(K, res); bool ret_val = (mp_int_read_cstring(K, tv2bigint(res), base, - buf, end) == MP_OK); + buf, end) == MP_OK); krooted_tvs_pop(K); *out = kbigint_try_fixint(K, res); return ret_val; @@ -74,11 +74,11 @@ int32_t kbigint_print_size(TValue tv_bigint, int32_t base) /* this is used by write */ void kbigint_print_string(klisp_State *K, TValue tv_bigint, int32_t base, - char *buf, int32_t limit) + char *buf, int32_t limit) { klisp_assert(ttisbigint(tv_bigint)); mp_result res = mp_int_to_string(K, tv2bigint(tv_bigint), base, buf, - limit); + limit); /* only possible error is truncation */ klisp_assert(res == MP_OK); } @@ -87,31 +87,31 @@ void kbigint_print_string(klisp_State *K, TValue tv_bigint, int32_t base, bool kbigint_eqp(TValue tv_bigint1, TValue tv_bigint2) { return (mp_int_compare(tv2bigint(tv_bigint1), - tv2bigint(tv_bigint2)) == 0); + tv2bigint(tv_bigint2)) == 0); } bool kbigint_ltp(TValue tv_bigint1, TValue tv_bigint2) { return (mp_int_compare(tv2bigint(tv_bigint1), - tv2bigint(tv_bigint2)) < 0); + tv2bigint(tv_bigint2)) < 0); } bool kbigint_lep(TValue tv_bigint1, TValue tv_bigint2) { return (mp_int_compare(tv2bigint(tv_bigint1), - tv2bigint(tv_bigint2)) <= 0); + tv2bigint(tv_bigint2)) <= 0); } bool kbigint_gtp(TValue tv_bigint1, TValue tv_bigint2) { return (mp_int_compare(tv2bigint(tv_bigint1), - tv2bigint(tv_bigint2)) > 0); + tv2bigint(tv_bigint2)) > 0); } bool kbigint_gep(TValue tv_bigint1, TValue tv_bigint2) { return (mp_int_compare(tv2bigint(tv_bigint1), - tv2bigint(tv_bigint2)) >= 0); + tv2bigint(tv_bigint2)) >= 0); } /* @@ -162,13 +162,13 @@ TValue kbigint_div_mod(klisp_State *K, TValue n1, TValue n2, TValue *res_r) /* Adjust q & r so that 0 <= r < |d| */ if (mp_int_compare_zero(r) < 0) { - if (mp_int_compare_zero(d) < 0) { - mp_int_sub(K, r, d, r); - mp_int_add_value(K, q, 1, q); - } else { - mp_int_add(K, r, d, r); - mp_int_sub_value(K, q, 1, q); - } + if (mp_int_compare_zero(d) < 0) { + mp_int_sub(K, r, d, r); + mp_int_add_value(K, q, 1, q); + } else { + mp_int_add(K, r, d, r); + mp_int_sub_value(K, q, 1, q); + } } krooted_tvs_pop(K); @@ -210,25 +210,25 @@ TValue kbigint_div0_mod0(klisp_State *K, TValue n1, TValue n2, TValue *res_r) /* this checks 2r >= |d| (which is the same r >= |d/2|) */ if (mp_int_compare(two_r, abs_d) >= 0) { - if (mp_int_compare_zero(d) < 0) { - mp_int_add(K, r, d, r); - mp_int_sub_value(K, q, 1, q); - } else { - mp_int_sub(K, r, d, r); - mp_int_add_value(K, q, 1, q); - } + if (mp_int_compare_zero(d) < 0) { + mp_int_add(K, r, d, r); + mp_int_sub_value(K, q, 1, q); + } else { + mp_int_sub(K, r, d, r); + mp_int_add_value(K, q, 1, q); + } } else { - UNUSED(mp_int_neg(K, abs_d, abs_d)); - /* this checks 2r < -|d| (which is the same r < |d/2|) */ - if (mp_int_compare(two_r, abs_d) < 0) { - if (mp_int_compare_zero(d) < 0) { - mp_int_sub(K, r, d, r); - mp_int_add_value(K, q, 1, q); - } else { - mp_int_add(K, r, d, r); - mp_int_sub_value(K, q, 1, q); - } - } + UNUSED(mp_int_neg(K, abs_d, abs_d)); + /* this checks 2r < -|d| (which is the same r < |d/2|) */ + if (mp_int_compare(two_r, abs_d) < 0) { + if (mp_int_compare_zero(d) < 0) { + mp_int_sub(K, r, d, r); + mp_int_add_value(K, q, 1, q); + } else { + mp_int_add(K, r, d, r); + mp_int_sub_value(K, q, 1, q); + } + } } krooted_tvs_pop(K); @@ -263,14 +263,14 @@ bool kbigint_evenp(TValue tv_bigint) TValue kbigint_abs(klisp_State *K, TValue tv_bigint) { if (kbigint_negativep(tv_bigint)) { - TValue copy = kbigint_make_simple(K); - krooted_tvs_push(K, copy); - UNUSED(mp_int_abs(K, tv2bigint(tv_bigint), tv2bigint(copy))); - krooted_tvs_pop(K); - /* NOTE: this can never be a fixint if the parameter was a bigint */ - return copy; + TValue copy = kbigint_make_simple(K); + krooted_tvs_push(K, copy); + UNUSED(mp_int_abs(K, tv2bigint(tv_bigint), tv2bigint(copy))); + krooted_tvs_pop(K); + /* NOTE: this can never be a fixint if the parameter was a bigint */ + return copy; } else { - return tv_bigint; + return tv_bigint; } } @@ -306,8 +306,8 @@ TValue kinteger_new_uint64(klisp_State *K, uint64_t x) uint8_t d[8]; for (int i = 7; i >= 0; i--) { - d[i] = (x & 0xFF); - x >>= 8; + d[i] = (x & 0xFF); + x >>= 8; } mp_int_read_unsigned(K, tv2bigint(res), d, 8); diff --git a/src/kinteger.h b/src/kinteger.h @@ -27,15 +27,15 @@ inline TValue kbigint_try_fixint(klisp_State *K, TValue n) UNUSED(K); Bigint *b = tv2bigint(n); if (MP_USED(b) != 1) - return n; + return n; int64_t digit = (int64_t) *(MP_DIGITS(b)); if (MP_SIGN(b) == MP_NEG) digit = -digit; if (kfit_int32_t(digit)) { - /* n shouln't be reachable but the let the gc do its job */ - return i2tv((int32_t) digit); + /* n shouln't be reachable but the let the gc do its job */ + return i2tv((int32_t) digit); } else { - return n; + return n; } } @@ -50,39 +50,39 @@ TValue kbigint_copy(klisp_State *K, TValue src); /* Create a stack allocated bigints from a fixint, useful for mixed operations, relatively light weight compared to creating it in the heap and burdening the gc */ -#define kbind_bigint(name, fixint) \ - int32_t (KUNIQUE_NAME(i)) = ivalue(fixint); \ - Bigint KUNIQUE_NAME(bigint); \ - (KUNIQUE_NAME(bigint)).single = ({ \ - int64_t temp = (KUNIQUE_NAME(i)); \ - (uint32_t) ((temp < 0)? -temp : temp); \ - }); \ +#define kbind_bigint(name, fixint) \ + int32_t (KUNIQUE_NAME(i)) = ivalue(fixint); \ + Bigint KUNIQUE_NAME(bigint); \ + (KUNIQUE_NAME(bigint)).single = ({ \ + int64_t temp = (KUNIQUE_NAME(i)); \ + (uint32_t) ((temp < 0)? -temp : temp); \ + }); \ (KUNIQUE_NAME(bigint)).digits = &((KUNIQUE_NAME(bigint)).single); \ - (KUNIQUE_NAME(bigint)).alloc = 1; \ - (KUNIQUE_NAME(bigint)).used = 1; \ - (KUNIQUE_NAME(bigint)).sign = (KUNIQUE_NAME(i)) < 0? \ - MP_NEG : MP_ZPOS; \ + (KUNIQUE_NAME(bigint)).alloc = 1; \ + (KUNIQUE_NAME(bigint)).used = 1; \ + (KUNIQUE_NAME(bigint)).sign = (KUNIQUE_NAME(i)) < 0? \ + MP_NEG : MP_ZPOS; \ Bigint *name = &(KUNIQUE_NAME(bigint)) /* This can be used prior to calling a bigint functions to automatically convert fixints to bigints. NOTE: calls to this macro should go in different lines! */ -#define kensure_bigint(n) \ - /* must use goto, no block should be entered before calling \ - kbind_bigint */ \ - if (!ttisfixint(n)) \ - goto KUNIQUE_NAME(exit_lbl); \ - kbind_bigint(KUNIQUE_NAME(bint), (n)); \ - (n) = gc2bigint(KUNIQUE_NAME(bint)); \ - KUNIQUE_NAME(exit_lbl): +#define kensure_bigint(n) \ + /* must use goto, no block should be entered before calling \ + kbind_bigint */ \ + if (!ttisfixint(n)) \ + goto KUNIQUE_NAME(exit_lbl); \ + kbind_bigint(KUNIQUE_NAME(bint), (n)); \ + (n) = gc2bigint(KUNIQUE_NAME(bint)); \ +KUNIQUE_NAME(exit_lbl): /* This is used by the reader to destructively add digits to a number - tv_bigint must be positive */ + tv_bigint must be positive */ void kbigint_add_digit(klisp_State *K, TValue tv_bigint, int32_t base, - int32_t digit); + int32_t digit); /* This is used by the writer to get the digits of a number - tv_bigint must be positive */ + tv_bigint must be positive */ int32_t kbigint_remove_digit(klisp_State *K, TValue tv_bigint, int32_t base); /* This is used by write to test if there is any digit left to print */ @@ -96,7 +96,7 @@ void kbigint_invert_sign(klisp_State *K, TValue tv_bigint); /* this works for bigints & fixints, returns true if ok */ /* only positive numbers? */ bool kinteger_read(klisp_State *K, char *buf, int32_t base, TValue *out, - char **end); + char **end); /* this is used by write to estimate the number of chars necessary to print the number */ @@ -104,7 +104,7 @@ int32_t kbigint_print_size(TValue tv_bigint, int32_t base); /* this is used by write */ void kbigint_print_string(klisp_State *K, TValue tv_bigint, int32_t base, - char *buf, int32_t limit); + char *buf, int32_t limit); /* Interface for kgnumbers */ bool kbigint_eqp(TValue bigint1, TValue bigint2); diff --git a/src/kkeyword.c b/src/kkeyword.c @@ -20,34 +20,34 @@ TValue kkeyword_new_bs(klisp_State *K, const char *buf, int32_t size) /* First calculate the hash */ uint32_t h = size; /* seed */ size_t step = (size>>5)+1; /* if string is too long, don't hash all - its chars */ + its chars */ size_t size1; for (size1 = size; size1 >= step; size1 -= step) /* compute hash */ - h = h ^ ((h<<5)+(h>>2)+ ((unsigned char) buf[size1-1])); + h = h ^ ((h<<5)+(h>>2)+ ((unsigned char) buf[size1-1])); h ^= (uint32_t) 0x55555555; - /* keyword hash should be different from string & symbol hash - otherwise keywords and their respective immutable string - would always fall in the same bucket */ + /* keyword hash should be different from string & symbol hash + otherwise keywords and their respective immutable string + would always fall in the same bucket */ /* look for it in the table */ for (GCObject *o = K->strt.hash[lmod(h, K->strt.size)]; o != NULL; - o = o->gch.next) { - klisp_assert(o->gch.tt == K_TKEYWORD || o->gch.tt == K_TSYMBOL || - o->gch.tt == K_TSTRING || o->gch.tt == K_TBYTEVECTOR); - - if (o->gch.tt != K_TKEYWORD) continue; + o = o->gch.next) { + klisp_assert(o->gch.tt == K_TKEYWORD || o->gch.tt == K_TSYMBOL || + o->gch.tt == K_TSTRING || o->gch.tt == K_TBYTEVECTOR); + + if (o->gch.tt != K_TKEYWORD) continue; - String *ts = tv2str(((Keyword *) o)->str); - if (ts->size == size && (memcmp(buf, ts->b, size) == 0)) { - /* keyword and/or string may be dead */ - if (isdead(K, o)) changewhite(o); - if (isdead(K, (GCObject *) ts)) changewhite((GCObject *) ts); - return gc2keyw(o); - } + String *ts = tv2str(((Keyword *) o)->str); + if (ts->size == size && (memcmp(buf, ts->b, size) == 0)) { + /* keyword and/or string may be dead */ + if (isdead(K, o)) changewhite(o); + if (isdead(K, (GCObject *) ts)) changewhite((GCObject *) ts); + return gc2keyw(o); + } } /* REFACTOR: move this to a new function */ /* Didn't find it, alloc new immutable string and save in keyword table, - note that the hash value remained in h */ + note that the hash value remained in h */ TValue new_str = kstring_new_bs_imm(K, buf, size); krooted_tvs_push(K, new_str); Keyword *new_keyw = klispM_new(K, Keyword); @@ -74,9 +74,9 @@ TValue kkeyword_new_bs(klisp_State *K, const char *buf, int32_t size) tb->hash[h] = (GCObject *)(new_keyw); tb->nuse++; if (tb->nuse > ((uint32_t) tb->size) && tb->size <= INT32_MAX / 2) { - krooted_tvs_push(K, ret_tv); /* save in case of gc */ - klispS_resize(K, tb->size*2); /* too crowded */ - krooted_tvs_pop(K); + krooted_tvs_push(K, ret_tv); /* save in case of gc */ + klispS_resize(K, tb->size*2); /* too crowded */ + krooted_tvs_pop(K); } return ret_tv; } diff --git a/src/klisp.c b/src/klisp.c @@ -57,31 +57,31 @@ static const char *progname = KLISP_PROGNAME; static void print_usage (void) { fprintf(stderr, - "usage: %s [options] [script [args]].\n" - "Available options are:\n" - " -e exp eval string " KLISP_QL("exp") "\n" - " -l name load file " KLISP_QL("name") "\n" - " -r name require file " KLISP_QL("name") "\n" - " -i enter interactive mode after executing " - KLISP_QL("script") "\n" - " -v show version information\n" - " -- stop handling options\n" - " - execute stdin and stop handling options\n" - , - progname); + "usage: %s [options] [script [args]].\n" + "Available options are:\n" + " -e exp eval string " KLISP_QL("exp") "\n" + " -l name load file " KLISP_QL("name") "\n" + " -r name require file " KLISP_QL("name") "\n" + " -i enter interactive mode after executing " + KLISP_QL("script") "\n" + " -v show version information\n" + " -- stop handling options\n" + " - execute stdin and stop handling options\n" + , + progname); fflush(stderr); } static void k_message (const char *pname, const char *msg) { if (pname) - fprintf(stderr, "%s: ", pname); + fprintf(stderr, "%s: ", pname); fprintf(stderr, "%s\n", msg); fflush(stderr); } /* TODO move this to a common place to use it from elsewhere -(like the repl) */ + (like the repl) */ static void show_error(klisp_State *K, TValue obj) { /* FOR NOW used only for irritant list */ TValue port = kcdr(K->kd_error_port_key); @@ -89,66 +89,66 @@ static void show_error(klisp_State *K, TValue obj) { /* TEMP: obj should be an error obj */ if (ttiserror(obj)) { - Error *err_obj = tv2error(obj); - TValue who = err_obj->who; - char *who_str; - /* TEMP? */ - if (ttiscontinuation(who)) - who = tv2cont(who)->comb; - - if (ttisstring(who)) { - who_str = kstring_buf(who); + Error *err_obj = tv2error(obj); + TValue who = err_obj->who; + char *who_str; + /* TEMP? */ + if (ttiscontinuation(who)) + who = tv2cont(who)->comb; + + if (ttisstring(who)) { + who_str = kstring_buf(who); #if KTRACK_NAMES - } else if (khas_name(who)) { - TValue name = kget_name(K, who); - who_str = ksymbol_buf(name); + } else if (khas_name(who)) { + TValue name = kget_name(K, who); + who_str = ksymbol_buf(name); #endif - } else { - who_str = "?"; - } - char *msg = kstring_buf(err_obj->msg); - fprintf(stderr, "\n*ERROR*: \n"); - fprintf(stderr, "%s: %s", who_str, msg); - - krooted_tvs_push(K, obj); - - /* Msg + irritants */ - /* TODO move to a new function */ - if (!ttisnil(err_obj->irritants)) { - fprintf(stderr, ": "); - kwrite_display_to_port(K, port, err_obj->irritants, false); - } - kwrite_newline_to_port(K, port); + } else { + who_str = "?"; + } + char *msg = kstring_buf(err_obj->msg); + fprintf(stderr, "\n*ERROR*: \n"); + fprintf(stderr, "%s: %s", who_str, msg); + + krooted_tvs_push(K, obj); + + /* Msg + irritants */ + /* TODO move to a new function */ + if (!ttisnil(err_obj->irritants)) { + fprintf(stderr, ": "); + kwrite_display_to_port(K, port, err_obj->irritants, false); + } + kwrite_newline_to_port(K, port); #if KTRACK_NAMES #if KTRACK_SI - /* Location */ - /* TODO move to a new function */ - /* MAYBE: remove */ - if (khas_name(who) || khas_si(who)) { - fprintf(stderr, "Location: "); - kwrite_display_to_port(K, port, who, false); - kwrite_newline_to_port(K, port); - } - - /* Backtrace */ - /* TODO move to a new function */ - TValue tv_cont = err_obj->cont; - fprintf(stderr, "Backtrace: \n"); - while(ttiscontinuation(tv_cont)) { - kwrite_display_to_port(K, port, tv_cont, false); - kwrite_newline_to_port(K, port); - Continuation *cont = tv2cont(tv_cont); - tv_cont = cont->parent; - } - /* add extra newline at the end */ - kwrite_newline_to_port(K, port); + /* Location */ + /* TODO move to a new function */ + /* MAYBE: remove */ + if (khas_name(who) || khas_si(who)) { + fprintf(stderr, "Location: "); + kwrite_display_to_port(K, port, who, false); + kwrite_newline_to_port(K, port); + } + + /* Backtrace */ + /* TODO move to a new function */ + TValue tv_cont = err_obj->cont; + fprintf(stderr, "Backtrace: \n"); + while(ttiscontinuation(tv_cont)) { + kwrite_display_to_port(K, port, tv_cont, false); + kwrite_newline_to_port(K, port); + Continuation *cont = tv2cont(tv_cont); + tv_cont = cont->parent; + } + /* add extra newline at the end */ + kwrite_newline_to_port(K, port); #endif #endif - krooted_tvs_pop(K); + krooted_tvs_pop(K); } else { - fprintf(stderr, "\n*ERROR*: not an error object passed to " - "error continuation"); + fprintf(stderr, "\n*ERROR*: not an error object passed to " + "error continuation"); } fflush(stderr); } @@ -156,9 +156,9 @@ static void show_error(klisp_State *K, TValue obj) { static int report (klisp_State *K, int status) { if (status == STATUS_ERROR) { - const char *msg = "Error!"; - k_message(progname, msg); - show_error(K, K->next_value); + const char *msg = "Error!"; + k_message(progname, msg); + show_error(K, K->next_value); } return status; } @@ -210,7 +210,7 @@ static int dostring (klisp_State *K, const char *s, const char *name) /* create the guard set error flag after errors */ TValue exit_int = kmake_operative(K, do_int_mark_error, - 1, p2tv(&errorp)); + 1, p2tv(&errorp)); krooted_tvs_push(K, exit_int); TValue exit_guard = kcons(K, K->error_cont, exit_int); krooted_tvs_pop(K); /* already in guard */ @@ -225,11 +225,11 @@ static int dostring (klisp_State *K, const char *s, const char *name) TValue env = kmake_empty_environment(K); krooted_tvs_push(K, env); TValue outer_cont = kmake_continuation(K, K->root_cont, - do_pass_value, 2, entry_guards, env); + do_pass_value, 2, entry_guards, env); kset_outer_cont(outer_cont); krooted_tvs_push(K, outer_cont); TValue inner_cont = kmake_continuation(K, outer_cont, - do_pass_value, 2, exit_guards, env); + do_pass_value, 2, exit_guards, env); kset_inner_cont(inner_cont); krooted_tvs_pop(K); krooted_tvs_pop(K); krooted_tvs_pop(K); @@ -240,7 +240,7 @@ static int dostring (klisp_State *K, const char *s, const char *name) that the evaluation didn't explicitly invoke the root continuation */ TValue discard_cont = kmake_continuation(K, inner_cont, do_int_mark_root, - 1, p2tv(&rootp)); + 1, p2tv(&rootp)); krooted_tvs_pop(K); /* pop inner cont */ krooted_tvs_push(K, discard_cont); @@ -269,7 +269,7 @@ static int dostring (klisp_State *K, const char *s, const char *name) klispS_run(K); int status = errorp? STATUS_ERROR : - (rootp? STATUS_ROOT : STATUS_CONTINUE); + (rootp? STATUS_ROOT : STATUS_CONTINUE); /* get the standard environment again in K->next_env */ K->next_env = env; return report(K, status); @@ -286,8 +286,8 @@ void do_file_eval(klisp_State *K) TValue denv = xparams[0]; TValue ls = obj; if (!ttisnil(ls)) { - TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, ls, denv); - kset_cc(K, new_cont); + TValue new_cont = kmake_continuation(K, kget_cc(K), do_seq, 2, ls, denv); + kset_cc(K, new_cont); } kapply_cc(K, KINERT); } @@ -317,33 +317,33 @@ static int dofile(klisp_State *K, const char *name) /* XXX better do this in a continuation */ if (name == NULL) { - port = kcdr(K->kd_in_port_key); + port = kcdr(K->kd_in_port_key); } else { - FILE *file = fopen(name, "r"); - if (file == NULL) { - TValue mode_str = kstring_new_b(K, "r"); - krooted_tvs_push(K, mode_str); - TValue name_str = kstring_new_b(K, name); - krooted_tvs_push(K, mode_str); - TValue error_obj = klispE_new_simple_with_errno_irritants - (K, "fopen", 2, name_str, mode_str); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - K->next_value = error_obj; - return report(K, STATUS_ERROR); - } + FILE *file = fopen(name, "r"); + if (file == NULL) { + TValue mode_str = kstring_new_b(K, "r"); + krooted_tvs_push(K, mode_str); + TValue name_str = kstring_new_b(K, name); + krooted_tvs_push(K, mode_str); + TValue error_obj = klispE_new_simple_with_errno_irritants + (K, "fopen", 2, name_str, mode_str); + krooted_tvs_pop(K); + krooted_tvs_pop(K); + K->next_value = error_obj; + return report(K, STATUS_ERROR); + } - TValue name_str = kstring_new_b(K, name); - krooted_tvs_push(K, name_str); - port = kmake_std_fport(K, name_str, false, false, file); - krooted_tvs_pop(K); + TValue name_str = kstring_new_b(K, name); + krooted_tvs_push(K, name_str); + port = kmake_std_fport(K, name_str, false, false, file); + krooted_tvs_pop(K); } krooted_tvs_push(K, port); /* TODO this is exactly the same as in string, factor the code out */ /* create the guard set error flag after errors */ TValue exit_int = kmake_operative(K, do_int_mark_error, - 1, p2tv(&errorp)); + 1, p2tv(&errorp)); krooted_tvs_push(K, exit_int); TValue exit_guard = kcons(K, K->error_cont, exit_int); krooted_tvs_pop(K); /* already in guard */ @@ -358,11 +358,11 @@ static int dofile(klisp_State *K, const char *name) TValue env = kmake_empty_environment(K); krooted_tvs_push(K, env); TValue outer_cont = kmake_continuation(K, K->root_cont, - do_pass_value, 2, entry_guards, env); + do_pass_value, 2, entry_guards, env); kset_outer_cont(outer_cont); krooted_tvs_push(K, outer_cont); TValue inner_cont = kmake_continuation(K, outer_cont, - do_pass_value, 2, exit_guards, env); + do_pass_value, 2, exit_guards, env); kset_inner_cont(inner_cont); krooted_tvs_pop(K); krooted_tvs_pop(K); krooted_tvs_pop(K); @@ -375,20 +375,20 @@ static int dofile(klisp_State *K, const char *name) that the evaluation didn't explicitly invoke the root continuation */ TValue discard_cont = kmake_continuation(K, inner_cont, do_int_mark_root, - 1, p2tv(&rootp)); + 1, p2tv(&rootp)); krooted_tvs_pop(K); /* pop inner cont */ krooted_tvs_push(K, discard_cont); /* XXX This should probably be an extra param to the function */ env = K->next_env; /* this is the standard env that should be used for - evaluation */ + evaluation */ TValue eval_cont = kmake_continuation(K, discard_cont, do_file_eval, - 1, env); + 1, env); krooted_tvs_pop(K); /* pop discard cont */ krooted_tvs_push(K, eval_cont); TValue read_cont = kmake_continuation(K, eval_cont, do_file_read, - 1, port); + 1, port); krooted_tvs_pop(K); /* pop eval cont */ krooted_tvs_pop(K); /* pop port */ kset_cc(K, read_cont); /* this will protect all conts from gc */ @@ -397,7 +397,7 @@ static int dofile(klisp_State *K, const char *name) klispS_run(K); int status = errorp? STATUS_ERROR : - (rootp? STATUS_ROOT : STATUS_CONTINUE); + (rootp? STATUS_ROOT : STATUS_CONTINUE); /* get the standard environment again in K->next_env */ K->next_env = env; @@ -426,7 +426,7 @@ static int dorfile(klisp_State *K, const char *name) /* TODO this is exactly the same as in string, factor the code out */ /* create the guard set error flag after errors */ TValue exit_int = kmake_operative(K, do_int_mark_error, - 1, p2tv(&errorp)); + 1, p2tv(&errorp)); krooted_tvs_push(K, exit_int); TValue exit_guard = kcons(K, K->error_cont, exit_int); krooted_tvs_pop(K); /* already in guard */ @@ -441,11 +441,11 @@ static int dorfile(klisp_State *K, const char *name) TValue env = kmake_empty_environment(K); krooted_tvs_push(K, env); TValue outer_cont = kmake_continuation(K, K->root_cont, - do_pass_value, 2, entry_guards, env); + do_pass_value, 2, entry_guards, env); kset_outer_cont(outer_cont); krooted_tvs_push(K, outer_cont); TValue inner_cont = kmake_continuation(K, outer_cont,