klisp

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

kgsymbols.c (1997B)


      1 /*
      2 ** kgsymbols.c
      3 ** Symbol 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 "klisp.h"
     16 #include "kcontinuation.h"
     17 #include "kpair.h"
     18 #include "kstring.h"
     19 #include "ksymbol.h"
     20 #include "kerror.h"
     21 
     22 #include "kghelpers.h"
     23 #include "kgsymbols.h"
     24 
     25 /* 4.4.1 symbol? */
     26 /* uses typep */
     27 
     28 /* 13.3.1? symbol->string */
     29 /* The strings in symbols are immutable so we can just return that */
     30 void symbol_to_string(klisp_State *K)
     31 {
     32     TValue *xparams = K->next_xparams;
     33     TValue ptree = K->next_value;
     34     TValue denv = K->next_env;
     35     klisp_assert(ttisenvironment(K->next_env));
     36     UNUSED(xparams);
     37     UNUSED(denv);
     38     bind_1tp(K, ptree, "symbol", ttissymbol, sym);
     39     TValue str = ksymbol_str(sym);
     40     kapply_cc(K, str);
     41 }
     42 
     43 /* 13.3.2? string->symbol */
     44 void string_to_symbol(klisp_State *K)
     45 {
     46     TValue *xparams = K->next_xparams;
     47     TValue ptree = K->next_value;
     48     TValue denv = K->next_env;
     49     klisp_assert(ttisenvironment(K->next_env));
     50     UNUSED(xparams);
     51     UNUSED(denv);
     52     bind_1tp(K, ptree, "string", ttisstring, str);
     53     /* TODO si */
     54     /* If the string is mutable it is copied */
     55     TValue new_sym = ksymbol_new_str(K, str, KNIL);
     56     kapply_cc(K, new_sym);
     57 }
     58 
     59 /* init ground */
     60 void kinit_symbols_ground_env(klisp_State *K)
     61 {
     62     TValue ground_env = G(K)->ground_env;
     63     TValue symbol, value;
     64 
     65     /* 4.4.1 symbol? */
     66     add_applicative(K, ground_env, "symbol?", typep, 2, symbol, 
     67                     i2tv(K_TSYMBOL));
     68     /*
     69     ** This section is still missing from the report. The bindings here are
     70     ** taken from r5rs scheme and should not be considered standard. 
     71     */
     72     /* ?.?.1? symbol->string */
     73     add_applicative(K, ground_env, "symbol->string", symbol_to_string, 0);
     74     /* ?.?.2? string->symbol */
     75     add_applicative(K, ground_env, "string->symbol", string_to_symbol, 0);
     76 }