klisp

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

kgkd_vars.c (1675B)


      1 /*
      2 ** kgkd_vars.c
      3 ** Keyed Dynamic Variables features for the ground environment
      4 ** See Copyright Notice in klisp.h
      5 */
      6 
      7 #include <assert.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <stdbool.h>
     11 #include <stdint.h>
     12 
     13 #include "kstate.h"
     14 #include "kobject.h"
     15 #include "kpair.h"
     16 #include "kcontinuation.h"
     17 #include "koperative.h"
     18 #include "kapplicative.h"
     19 #include "kenvironment.h"
     20 #include "kerror.h"
     21 
     22 #include "kghelpers.h"
     23 #include "kgkd_vars.h"
     24 
     25 /*
     26 ** A dynamic key is a pair with a boolean in the car indicating if the
     27 ** variable is bound and an arbitrary object in the cdr representing the
     28 ** currently bound value.
     29 */
     30 
     31 /* Helpers for make-keyed-dynamic-variable */
     32 /* in kghelpers */
     33 
     34 
     35 /* 10.1.1 make-keyed-dynamic-variable */
     36 void make_keyed_dynamic_variable(klisp_State *K)
     37 {
     38     TValue *xparams = K->next_xparams;
     39     TValue ptree = K->next_value;
     40     TValue denv = K->next_env;
     41     klisp_assert(ttisenvironment(K->next_env));
     42     UNUSED(denv); 
     43     UNUSED(xparams);
     44 
     45     check_0p(K, ptree);
     46     TValue key = kcons(K, KFALSE, KINERT);
     47     krooted_tvs_push(K, key);
     48     TValue a = kmake_applicative(K, do_access, 1, key);
     49     krooted_tvs_push(K, a);
     50     TValue b = kmake_applicative(K, do_bind, 1, key);
     51     krooted_tvs_push(K, b);
     52     TValue ls = klist(K, 2, b, a);
     53 
     54     krooted_tvs_pop(K); krooted_tvs_pop(K); krooted_tvs_pop(K);
     55 
     56     kapply_cc(K, ls);
     57 }
     58 
     59 /* init ground */
     60 void kinit_kgkd_vars_ground_env(klisp_State *K)
     61 {
     62     TValue ground_env = G(K)->ground_env;
     63     TValue symbol, value;
     64 
     65     /* 10.1.1 make-keyed-dynamic-variable */
     66     add_applicative(K, ground_env, "make-keyed-dynamic-variable", 
     67                     make_keyed_dynamic_variable, 0); 
     68 }