diff --git a/Makefile b/Makefile index 0aab4000..f2abb399 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ build: cd src; \ yacc -d parse.y; \ lex scan.l - $(CC) -Wall -o bin/picrin -I./include -lreadline 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/bool.c src/vm.c src/init.c src/number.c + $(CC) -Wall -o bin/picrin -I./include -lreadline 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/bool.c src/vm.c src/init.c src/number.c src/time.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 index ce4defff..2c330247 100644 --- a/src/init.c +++ b/src/init.c @@ -2,6 +2,7 @@ void pic_init_port(pic_state *); void pic_init_number(pic_state *); +void pic_init_time(pic_state *); #define DONE pic_gc_arena_restore(pic, ai); @@ -13,4 +14,5 @@ pic_init_core(pic_state *pic) ai = pic_gc_arena_preserve(pic); pic_init_port(pic); DONE; pic_init_number(pic); DONE; + pic_init_time(pic); DONE; } diff --git a/src/number.c b/src/number.c index f865ba7a..084637d4 100644 --- a/src/number.c +++ b/src/number.c @@ -1,7 +1,6 @@ #include #include "picrin.h" -#include "picrin/value.h" static pic_value pic_number_lt(pic_state *pic) diff --git a/src/port.c b/src/port.c index db278eb5..bf502534 100644 --- a/src/port.c +++ b/src/port.c @@ -29,7 +29,7 @@ write(pic_state *pic, pic_value obj) printf("%s", pic_symbol_ptr(obj)->name); break; case PIC_TT_FLOAT: - printf("%g", pic_float(obj)); + printf("%.10g", pic_float(obj)); break; case PIC_TT_UNDEF: printf("#"); diff --git a/src/time.c b/src/time.c new file mode 100644 index 00000000..3de5c44b --- /dev/null +++ b/src/time.c @@ -0,0 +1,44 @@ +#include +#include + +#include "picrin.h" + +#define UTC_TAI_DIFF 35 + +static pic_value +pic_current_second(pic_state *pic) +{ + time_t t; + + pic_get_args(pic, ""); + + time(&t); + return pic_float_value((double)t + UTC_TAI_DIFF); +} + +static pic_value +pic_current_jiffy(pic_state *pic) +{ + clock_t c; + + pic_get_args(pic, ""); + + c = clock(); + return pic_float_value((double)c); +} + +static pic_value +pic_jiffies_per_second(pic_state *pic) +{ + pic_get_args(pic, ""); + + return pic_float_value((double)CLOCKS_PER_SEC); +} + +void +pic_init_time(pic_state *pic) +{ + pic_defun(pic, "current-second", pic_current_second); + pic_defun(pic, "current-jiffy", pic_current_jiffy); + pic_defun(pic, "jiffies-per-second", pic_jiffies_per_second); +}