From e48017a6c2c9b7c3d989d660a1561f1ebb415878 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Sun, 10 Dec 2023 15:15:47 -0800 Subject: Add more nxs functions --- doc/main.nxs | 18 ++++++++++--- src/main.c | 2 +- src/parser.c | 3 +++ stdlib/math.nxs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 90 insertions(+), 14 deletions(-) diff --git a/doc/main.nxs b/doc/main.nxs index 7967dbc..cddb7f3 100644 --- a/doc/main.nxs +++ b/doc/main.nxs @@ -1,8 +1,18 @@ (include "fib.nxs") (include "../stdlib/math.nxs") -(print (fib 7)) -(print (+ (factorial 5) 10)) +(print (fib 8)) +(print (factorial 5)) +(print (+(factorial 4) 8)) -(exp 3) -(print (quote (+ (3 2) 4))) +(bind x (lambda (y) (if (<= y 0) y (x (print (- y 1)))))) + +(x 5) + +(print (exp (/ 1.0 2.0))) +(print (sin 1.0)) +(print (cos 1.0)) +(print (tan 1.0)) +(print (sinh 1.0)) +(print (sin pi)) +(print (ln (exp 1.0))) diff --git a/src/main.c b/src/main.c index 858334b..ee2c381 100644 --- a/src/main.c +++ b/src/main.c @@ -134,6 +134,7 @@ int main(int argc, char **argv) { } else if (strcmp(argv[1], "-r") == 0 || strcmp(argv[1], "--repl") == 0) { printf("Welcome to the NoExcess REPL.\n"); while (true) { + /* TODO: Finish REPL */ } } @@ -165,7 +166,6 @@ int main(int argc, char **argv) { parser_t *parser = init_parser(lexer); visitor_t *visitor = init_visitor(parser); ast_t *root = eval(visitor); - /* print_root(root); */ ast_free(root); ast_free(visitor->root); visitor_free(visitor); diff --git a/src/parser.c b/src/parser.c index 5f97ef9..12b1256 100644 --- a/src/parser.c +++ b/src/parser.c @@ -140,6 +140,9 @@ void parse_bind(parser_t *parser) { parser_error(parser); token_t *t = parser->tokens[parser->i]; char *name = t->value; + if (hash_table_get(parser->symbol_table, name) != NULL) { + parser_error(parser); + } parser_move(parser); ast_t *expr = parse_expr(parser); /* unevaluated expr will be evaluated when hash table transfers to visitor JIT */ diff --git a/stdlib/math.nxs b/stdlib/math.nxs index 77ffc7f..b93d23b 100644 --- a/stdlib/math.nxs +++ b/stdlib/math.nxs @@ -1,7 +1,11 @@ +;; constants +(bind pi 3.1415926535) + ;; some bootstrapping needed in order to obtain more complicated functions; ;; the power series requires a factorial and integer power function +;; TODO: make it work for negative numbers (bind powi (lambda (x y) - (if (= y 1) + (if (<= y 1) x (* x (powi x (- y 1))) ) @@ -10,7 +14,7 @@ (bind inv (lambda (x) (/ 1.0 x))) (bind factorial (lambda (n) - (if (= n 0) 1 (* n (factorial (- n 1)))))) + (if (<= n 0) 1 (* n (factorial (- n 1)))))) ;; complicated functions (bind expr (lambda (x y) @@ -20,15 +24,74 @@ ) )) -(bind exp (lambda (x) - (expr x 13) -)) +(bind sinr (lambda (x y) + (if (<= y 0) + x + (+ + (* (powi (- 0 1) y) + (/ (powi x (+ (* y 2) 1)) + (factorial (+ (* y 2) 1))) + ) +(sinr x (- y 1)) + )))) + +(bind cosr (lambda (x y) + (if (<= y 0) + x + (+ + (* (powi (- 0 1) y) + (/ (powi x (* y 2)) + (factorial (* y 2))) + ) +(cosr x (- y 1)) + )))) + +;; TODO: actually complete this +(bind lnr (lambda (x y) + (if (<= y 0) + x + (+ + (* (powi (- 0 1) (+ y 1)) + (/ (powi (- x 1) y) + y) + ) +(lnr x (- y 1)) + )))) + +(bind sqrtr (lambda (x y) 5) -(bind sin (lambda (x) x)) +;; TODO: inverse of trig functions -(bind cos (lambda (x) x)) +;; composites of complicated functions + +(bind exp (lambda (x) (expr x 13))) + +(bind ln (lambda (x) (lnr x 13))) + +(bind sin (lambda (x) (sinr x 6))) + +(bind cos (lambda (x) (cosr x 6))) (bind tan (lambda (x) (/ (sin x) (cos x)))) -(print (exp 1.0)) -;(print (ln (exp 1))) +(bind csc (lambda (x) (inv (sin x)))) + +(bind sec (lamdba (x) (inv (cos x)))) + +(bind cot (lanbda (x) (inv (tan x)))) + +(bind sinh (lambda (x) (/ (- (exp x) (exp (- 0 x))) 2.0))) + +(bind cosh (lambda (x) (/ (+ (exp x) (exp (- 0 x))) 2.0))) + +(bind tanh (lambda (x) (/ (sinh x) (cosh x)))) + +(bind sech (lambda (x) (inv (cosh x)))) + +(bind csch (lambda (x) (inv (sinh x)))) + +(bind coth (lambda (x) (inv (tanh x)))) + +(bind sqrt (lambda (x) (sqrtr x 10))) + +(bind pow (lambda (x y) (exp (* y (ln x))))) -- cgit