klisp

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

commit 9c39568328b10196ccf2c35c29d008d297ba9c4b
parent d12b123da3e711359c477c556432228df0842053
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Thu, 17 Mar 2011 15:34:52 -0300

Added port?, input-port? and output-port? to the ground environment.

Diffstat:
Msrc/kground.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/kobject.c | 11+++++++++++
Msrc/kobject.h | 13++++---------
Msrc/kport.c | 3++-
4 files changed, 80 insertions(+), 10 deletions(-)

diff --git a/src/kground.c b/src/kground.c @@ -467,6 +467,69 @@ void kinit_ground_env(klisp_State *K) add_applicative(K, ground_env, "make-keyed-static-variable", make_keyed_static_variable, 0); + + /* + ** + ** 15 Ports + ** + */ + + /* + ** 15.1 Primitive features + */ + + /* 15.1.1 port? */ + add_applicative(K, ground_env, "port?", typep, 2, symbol, + i2tv(K_TPORT)); + + /* 15.1.2 input-port?, output-port? */ + add_applicative(K, ground_env, "input-port?", ftypep, 2, symbol, + kis_input_port); + + add_applicative(K, ground_env, "output-port?", ftypep, 2, symbol, + kis_output_port); + + /* 15.1.3 with-input-from-file, with-ouput-to-file */ + /* TODO */ + + /* 15.1.4 get-current-input-port, get-current-output-port */ + /* TODO */ + + /* 15.1.5 open-input-file, open-output-file */ + /* TODO */ + + /* ASK John: should this be called close-input-port & close-ouput-port + like in r5rs? that doesn't seem consistent with open thou */ + /* 15.1.5 close-input-file, close-output-file */ + /* TODO */ + + /* 15.1.7 read */ + /* TODO */ + + /* 15.1.8 write */ + /* TODO */ + + /* + ** 15.2 Library features + */ + + /* 15.2.1 call-with-input-file, call-with-output-file */ + /* TODO */ + + /* 15.2.2 load */ + /* TODO */ + + /* 15.2.3 get-module */ + /* TODO */ + + /* TODO: That's all there is in the report, but we will probably need: + (from r5rs) char-ready?, read-char, peek-char, eof-object?, newline, + and write-char; (from c) file-exists?, rename-file and remove-file. + It would also be good to be able to select between append, truncate and + error if a file exists, but that would need to be an option in all three + methods of opening */ + + return; } diff --git a/src/kobject.c b/src/kobject.c @@ -44,3 +44,14 @@ char *ktv_names[] = { [K_TSTRING] = "string", [K_TSYMBOL] = "symbol" }; + +bool kis_input_port(TValue o) +{ + return ttisport(o) && kport_is_input(o); +} + +bool kis_output_port(TValue o) +{ + return ttisport(o) && kport_is_output(o); +} + diff --git a/src/kobject.h b/src/kobject.h @@ -501,15 +501,10 @@ extern char *ktv_names[]; #define kport_is_output(o_) ((tv_get_flags(o_) & K_FLAG_OUTPUT_PORT) != 0) #define kport_is_closed(o_) ((tv_get_flags(o_) & K_FLAG_CLOSED_PORT) != 0) -inline bool kis_input_port(TValue o) -{ - return ttisport(o) && kport_is_input(o); -} - -inline bool kis_ouput_port(TValue o) -{ - return ttisport(o) && kport_is_output(o); -} +/* can't be inline because we also use pointers to them, + (at least gcc doesn't bother to create them and the linker fails) */ +bool kis_input_port(TValue o); +bool kis_output_port(TValue o); /* Macro to test the most basic equality on TValues */ #define tv_equal(tv1_, tv2_) ((tv1_).raw == (tv2_).raw) diff --git a/src/kport.c b/src/kport.c @@ -1,5 +1,5 @@ /* -** kport.h +** kport.c ** Kernel Ports ** See Copyright Notice in klisp.h */ @@ -17,6 +17,7 @@ TValue kmake_port(klisp_State *K, TValue filename, bool writep, TValue name, TValue si) { + /* for now always use text mode */ FILE *f = fopen(kstring_buf(filename), writep? "w": "r"); if (f == NULL) { klispE_throw(K, "Create port: could't open file");