add some mathematic functions

This commit is contained in:
Yuichi Nishiwaki 2013-10-15 23:26:18 +09:00
parent 7f04cad34d
commit f280b0a5ce
3 changed files with 89 additions and 1 deletions

View File

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

View File

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

86
src/number.c Normal file
View File

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