From 2d4a5ed1eaa164724e9ecdca73adbb35ef41c160 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 15 Oct 2013 21:14:33 +0900 Subject: [PATCH] add standard functions: write/newline --- Makefile | 2 +- src/init.c | 14 ++++++++++++++ src/{write.c => port.c} | 27 ++++++++++++++++++++++++++- src/state.c | 7 ++++++- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 src/init.c rename src/{write.c => port.c} (62%) diff --git a/Makefile b/Makefile index 68b1e94b..b6651a43 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ build: cd src; \ yacc -d parse.y; \ lex scan.l - gcc -o bin/picrin -I./include src/main.c src/state.c src/gc.c src/pair.c src/write.c src/symbol.c src/value.c src/y.tab.c src/lex.yy.c src/eval.c src/bool.c src/vm.c + gcc -o bin/picrin -I./include src/main.c src/state.c src/gc.c src/pair.c src/port.c src/symbol.c src/value.c src/y.tab.c src/lex.yy.c src/eval.c src/bool.c src/vm.c src/init.c clean: rm -f src/y.tab.c src/y.tab.h src/lex.yy.c diff --git a/src/init.c b/src/init.c new file mode 100644 index 00000000..9775b408 --- /dev/null +++ b/src/init.c @@ -0,0 +1,14 @@ +#include "picrin.h" + +void pic_init_port(pic_state *); + +#define DONE pic_gc_arena_restore(pic, ai); + +void +pic_init_core(pic_state *pic) +{ + int ai; + + ai = pic_gc_arena_preserve(pic); + pic_init_port(pic); DONE; +} diff --git a/src/write.c b/src/port.c similarity index 62% rename from src/write.c rename to src/port.c index 2aaa84b8..ee31b2f6 100644 --- a/src/write.c +++ b/src/port.c @@ -22,7 +22,7 @@ pic_debug(pic_state *pic, pic_value obj) printf("%s", pic_symbol_ptr(obj)->name); break; case PIC_TT_FLOAT: - printf("%f", pic_float(obj)); + printf("%g", pic_float(obj)); break; case PIC_TT_UNDEF: printf("#"); @@ -35,3 +35,28 @@ pic_debug(pic_state *pic, pic_value obj) break; } } + +static pic_value +pic_port_write(pic_state *pic) +{ + pic_value v; + + pic_get_args(pic, "o", &v); + pic_debug(pic, v); + return pic_undef_value(); +} + +static pic_value +pic_port_newline(pic_state *pic) +{ + puts(""); + return pic_undef_value(); +} + +void +pic_init_port(pic_state *pic) +{ + pic_defun(pic, "write", pic_port_write); + pic_defun(pic, "newline", pic_port_newline); +} + diff --git a/src/state.c b/src/state.c index e7196abe..f197d57b 100644 --- a/src/state.c +++ b/src/state.c @@ -15,6 +15,8 @@ pic_new_empty_env() return env; } +void pic_init_core(pic_state *); + pic_state * pic_open() { @@ -34,11 +36,14 @@ pic_open() /* GC arena */ pic->arena_idx = 0; - pic->global_env = pic_new_empty_env(); pic->sDEFINE = pic_intern_cstr(pic, "define"); pic->sCONS = pic_intern_cstr(pic, "cons"); pic->sADD = pic_intern_cstr(pic, "add"); + /* global environment */ + pic->global_env = pic_new_empty_env(); + pic_init_core(pic); + return pic; }