add time functions

This commit is contained in:
Yuichi Nishiwaki 2013-10-20 12:04:15 +09:00
parent bcde104dd1
commit 6faaa5261c
5 changed files with 48 additions and 3 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -1,7 +1,6 @@
#include <math.h>
#include "picrin.h"
#include "picrin/value.h"
static pic_value
pic_number_lt(pic_state *pic)

View File

@ -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("#<undef>");

44
src/time.c Normal file
View File

@ -0,0 +1,44 @@
#include <time.h>
#include <stdio.h>
#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);
}