From f280b0a5ce2ad7645b7e27dbc52e1b53cf1a6f32 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Tue, 15 Oct 2013 23:26:18 +0900 Subject: [PATCH] add some mathematic functions --- Makefile | 2 +- src/init.c | 2 ++ src/number.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/number.c diff --git a/Makefile b/Makefile index 0dbb9eba..8e0ce1ea 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/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 + 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/bool.c src/vm.c src/init.c src/number.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 9775b408..ce4defff 100644 --- a/src/init.c +++ b/src/init.c @@ -1,6 +1,7 @@ #include "picrin.h" void pic_init_port(pic_state *); +void pic_init_number(pic_state *); #define DONE pic_gc_arena_restore(pic, ai); @@ -11,4 +12,5 @@ pic_init_core(pic_state *pic) ai = pic_gc_arena_preserve(pic); pic_init_port(pic); DONE; + pic_init_number(pic); DONE; } diff --git a/src/number.c b/src/number.c new file mode 100644 index 00000000..8c591a70 --- /dev/null +++ b/src/number.c @@ -0,0 +1,86 @@ +#include + +#include "picrin.h" +#include "picrin/value.h" + +static pic_value +pic_number_sqrt(pic_state *pic) +{ + double f; + + pic_get_args(pic, "f", &f); + f = sqrt(f); + return pic_float_value(f); +} + +static pic_value +pic_number_sin(pic_state *pic) +{ + double f; + + pic_get_args(pic, "f", &f); + f = sin(f); + return pic_float_value(f); +} + +static pic_value +pic_number_cos(pic_state *pic) +{ + double f; + + pic_get_args(pic, "f", &f); + f = cos(f); + return pic_float_value(f); +} + +static pic_value +pic_number_tan(pic_state *pic) +{ + double f; + + pic_get_args(pic, "f", &f); + f = tan(f); + return pic_float_value(f); +} + +static pic_value +pic_number_acos(pic_state *pic) +{ + double f; + + pic_get_args(pic, "f", &f); + f = acos(f); + return pic_float_value(f); +} + +static pic_value +pic_number_asin(pic_state *pic) +{ + double f; + + pic_get_args(pic, "f", &f); + f = asin(f); + return pic_float_value(f); +} + +static pic_value +pic_number_atan(pic_state *pic) +{ + double f; + + pic_get_args(pic, "f", &f); + f = atan(f); + return pic_float_value(f); +} + +void +pic_init_number(pic_state *pic) +{ + pic_defun(pic, "sqrt", pic_number_sqrt); + pic_defun(pic, "sin", pic_number_sin); + pic_defun(pic, "cos", pic_number_cos); + pic_defun(pic, "tan", pic_number_tan); + pic_defun(pic, "acos", pic_number_acos); + pic_defun(pic, "asin", pic_number_asin); + pic_defun(pic, "atan", pic_number_atan); +}