klisp

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

commit 4d1f83db67b92f2e444b26a9b7412160764e90b2
parent ef72093ba723f1d9592e0b21399d774b00ebd939
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Tue, 22 Mar 2011 23:56:57 -0300

Added string-ref to the ground environment.

Diffstat:
Msrc/kground.c | 2+-
Msrc/kgstrings.c | 19++++++++++++++++++-
Msrc/kgstrings.h | 2+-
3 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/kground.c b/src/kground.c @@ -589,7 +589,7 @@ void kinit_ground_env(klisp_State *K) add_applicative(K, ground_env, "string-length", kgstring_length, 0); /* 13.1.4? string-ref */ - /* TODO */ + add_applicative(K, ground_env, "string-ref", kstring_ref, 0); /* 13.1.5? string-set! */ /* TODO */ diff --git a/src/kgstrings.c b/src/kgstrings.c @@ -60,7 +60,24 @@ void kgstring_length(klisp_State *K, TValue *xparams, TValue ptree, } /* 13.1.4? string-ref */ -/* TODO */ +void kstring_ref(klisp_State *K, TValue *xparams, TValue ptree, TValue denv) +{ + UNUSED(xparams); + UNUSED(denv); + bind_2tp(K, "string-ref", ptree, "string", ttisstring, str, + "finite integer", ttisfixint, tv_i); + + int32_t i = ivalue(tv_i); + + if (i < 0 || i >= kstring_size(str)) { + /* TODO show index */ + klispE_throw(K, "string-ref: index out of bounds"); + return; + } + + TValue res = ch2tv(kstring_buf(str)[i]); + kapply_cc(K, res); +} /* 13.1.5? string-set! */ /* TODO */ diff --git a/src/kgstrings.h b/src/kgstrings.h @@ -29,7 +29,7 @@ void kgstring_length(klisp_State *K, TValue *xparams, TValue ptree, TValue denv); /* 13.1.4? string-ref */ -/* TODO */ +void kstring_ref (klisp_State *K, TValue *xparams, TValue ptree, TValue denv); /* 13.1.5? string-set! */ /* TODO */