diff options
author | Preston Pan <preston@nullring.xyz> | 2023-01-09 23:05:10 -0800 |
---|---|---|
committer | Preston Pan <preston@nullring.xyz> | 2023-01-09 23:05:10 -0800 |
commit | 545f3a7a3e148324981c7a3bffff8045ec60ea05 (patch) | |
tree | bbd8f1ce1a656047139efcf1a073220546d0dfd7 | |
parent | 43f11a93385c4848bfad49510bdea2849f241816 (diff) |
add exp function
-rw-r--r-- | doc/main.nxs | 2 | ||||
-rw-r--r-- | src/ast.c | 2 | ||||
-rw-r--r-- | src/include/ast.h | 4 | ||||
-rw-r--r-- | src/parser.c | 2 | ||||
-rw-r--r-- | src/print.c | 2 | ||||
-rw-r--r-- | src/visitor.c | 2 | ||||
-rw-r--r-- | stdlib/math.nxs | 24 |
7 files changed, 27 insertions, 11 deletions
diff --git a/doc/main.nxs b/doc/main.nxs index cb6985d..9eaeffc 100644 --- a/doc/main.nxs +++ b/doc/main.nxs @@ -1,4 +1,5 @@ (include "fib.nxs") +(include "../stdlib/math.nxs") (bind factorial (lambda (n) (if (= n 0) 1 (* n (factorial (- n 1)))))) @@ -6,4 +7,5 @@ (print (fib 7)) (print (+ (factorial 5) 10)) +(exp 3) (print (quote (+ (3 2) 4))) @@ -26,7 +26,7 @@ ast_t *init_ast_int(int value) { return a; } -ast_t *init_ast_float(double value) { +ast_t *init_ast_float(long double value) { ast_t *a = init_ast(AST_FLOAT); a->float_value = value; return a; diff --git a/src/include/ast.h b/src/include/ast.h index d330ee5..77c3bc6 100644 --- a/src/include/ast.h +++ b/src/include/ast.h @@ -29,7 +29,7 @@ typedef struct AST_STRUCT { char *string_value; /* Also is symbol value */ int int_value; - double float_value; + long double float_value; bool bool_value; } ast_t; @@ -39,7 +39,7 @@ ast_t *init_ast_string(char *value); ast_t *init_ast_int(int value); -ast_t *init_ast_float(double value); +ast_t *init_ast_float(long double value); ast_t *init_ast_pair(ast_t *car, ast_t *cdr); diff --git a/src/parser.c b/src/parser.c index a6a5b82..5f97ef9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -86,7 +86,7 @@ ast_t *parse_int(parser_t *parser) { } ast_t *parse_float(parser_t *parser) { - double ret = atof(parser->tokens[parser->i]->value); + long double ret = atof(parser->tokens[parser->i]->value); parser_move(parser); return init_ast_float(ret); } diff --git a/src/print.c b/src/print.c index 65cfb66..d9fdbf8 100644 --- a/src/print.c +++ b/src/print.c @@ -13,7 +13,7 @@ void print_bool(ast_t *b) { printf("F"); } -void print_float(ast_t *f) { printf("%f", f->float_value); } +void print_float(ast_t *f) { printf("%Lf", f->float_value); } void print_symbol(ast_t *s) { printf(":%s", s->string_value); } diff --git a/src/visitor.c b/src/visitor.c index ffd6048..0ca29d9 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -94,7 +94,7 @@ ast_t *eval_symbol(visitor_t *v, ast_t *e) { hash_table_add(v->eval_table, e->string_value, eval); return eval; } else { - /* printf("symbol error\n"); */ + printf("symbol error\n"); eval_error(v, e); return NULL; } diff --git a/stdlib/math.nxs b/stdlib/math.nxs index f913d82..77ffc7f 100644 --- a/stdlib/math.nxs +++ b/stdlib/math.nxs @@ -3,18 +3,32 @@ (bind powi (lambda (x y) (if (= y 1) x - (* x (pow x (- y 1))) -))) + (* x (powi x (- y 1))) + ) +)) + +(bind inv (lambda (x) (/ 1.0 x))) + (bind factorial (lambda (n) - (if (= n 0) 1 (* n (factorial (print(- n 1))))))) + (if (= n 0) 1 (* n (factorial (- n 1)))))) ;; complicated functions -(bind exp (lambda (x) x)) +(bind expr (lambda (x y) + (if (<= y 0) + 1 + (+ (/ (powi x y) (factorial y)) (expr x (- y 1))) + ) +)) -(bind ln (lambda (x) x)) +(bind exp (lambda (x) + (expr x 13) +)) (bind sin (lambda (x) x)) (bind cos (lambda (x) x)) (bind tan (lambda (x) (/ (sin x) (cos x)))) + +(print (exp 1.0)) +;(print (ln (exp 1))) |