From 084d39cd9ec851d6dd8708dfc3b45a5351b00189 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 9 Jun 2015 16:19:57 +0900 Subject: [PATCH] let undefined be public API --- extlib/benz/read.c | 17 ++++++++++++++++- extlib/benz/state.c | 2 ++ extlib/benz/undef.c | 21 +++++++++++++++++++++ extlib/benz/write.c | 2 +- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 extlib/benz/undef.c diff --git a/extlib/benz/read.c b/extlib/benz/read.c index dc7a28a5..a43b28de 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -560,6 +560,21 @@ read_blob(pic_state *pic, struct pic_port *port, int c) return pic_obj_value(blob); } +static pic_value +read_undef_or_blob(pic_state *pic, struct pic_port *port, int c) +{ + if ((c = peek(port)) == 'n') { + if (! expect(port, "ndefined")) { + read_error(pic, "unexpected character while reading #undefined"); + } + return pic_undef_value(); + } + if (! isdigit(c)) { + read_error(pic, "expect #undefined or #u8(...), but illegal character given"); + } + return read_blob(pic, port, 'u'); +} + static pic_value read_pair(pic_state *pic, struct pic_port *port, int c) { @@ -786,7 +801,7 @@ reader_table_init(struct pic_reader *reader) reader->dispatch['f'] = read_false; reader->dispatch['\\'] = read_char; reader->dispatch['('] = read_vector; - reader->dispatch['u'] = read_blob; + reader->dispatch['u'] = read_undef_or_blob; reader->dispatch['.'] = read_eval; /* read labels */ diff --git a/extlib/benz/state.c b/extlib/benz/state.c index aa60da35..a555dc34 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -10,6 +10,7 @@ pic_add_feature(pic_state *pic, const char *feature) pic_push(pic, pic_obj_value(pic_intern_cstr(pic, feature)), pic->features); } +void pic_init_undef(pic_state *); void pic_init_bool(pic_state *); void pic_init_pair(pic_state *); void pic_init_port(pic_state *); @@ -107,6 +108,7 @@ pic_init_core(pic_state *pic) pic_define_syntactic_keyword(pic, pic->lib->env, pic->sBEGIN, pic->rBEGIN); pic_define_syntactic_keyword(pic, pic->lib->env, pic->sDEFINE_SYNTAX, pic->rDEFINE_SYNTAX); + pic_init_undef(pic); DONE; pic_init_bool(pic); DONE; pic_init_pair(pic); DONE; pic_init_port(pic); DONE; diff --git a/extlib/benz/undef.c b/extlib/benz/undef.c new file mode 100644 index 00000000..9e709c0e --- /dev/null +++ b/extlib/benz/undef.c @@ -0,0 +1,21 @@ +/** + * See Copyright Notice in picrin.h + */ + +#include "picrin.h" + +static pic_value +pic_undef_undefined_p(pic_state *pic) +{ + pic_value v; + + pic_get_args(pic, "o", &v); + + return pic_undef_p(v) ? pic_true_value() : pic_false_value(); +} + +void +pic_init_undef(pic_state *pic) +{ + pic_defun(pic, "undefined?", pic_undef_undefined_p); +} diff --git a/extlib/benz/write.c b/extlib/benz/write.c index d2c2a807..32aa3e8c 100644 --- a/extlib/benz/write.c +++ b/extlib/benz/write.c @@ -220,7 +220,7 @@ write_core(struct writer_control *p, pic_value obj) switch (pic_type(obj)) { case PIC_TT_UNDEF: - xfprintf(file, "#"); + xfprintf(file, "#undefined"); break; case PIC_TT_NIL: xfprintf(file, "()");