From 37c8d2a62a107017ec1b055b098ccca29581c2e4 Mon Sep 17 00:00:00 2001 From: haxala1r Date: Thu, 30 Oct 2025 23:42:10 +0300 Subject: [PATCH] Added some standard functions --- lib/eval.ml | 27 +++++++++++++++++++++++---- lib/interpreterStdlib.ml | 5 +++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/eval.ml b/lib/eval.ml index 219faa0..e87735c 100644 --- a/lib/eval.ml +++ b/lib/eval.ml @@ -133,18 +133,37 @@ let () = add_special "fn" lambda let () = add_special "fn-macro" lambda_macro let () = add_special "let-one" bind_local let () = add_special "quote" (fun _ -> function - | LCons (x, LNil) -> x - | _ -> invalid_arg "hmm") + | LCons (x, LNil) -> x + | _ -> invalid_arg "hmm") let () = add_special "if" lisp_if +let () = add_builtin "nil?" lisp_not +let () = add_builtin "not" lisp_not (* Yes, these are the same thing *) (*let () = add_builtin "print" lisp_prin *) -(* I know this looks insane. please trust me. *) -let _ = eval_all default_env (Read.parse_str " +(* I know this looks insane. please trust me. + Idea: maybe put this in a file instead of putting + literally the entire standard library in a constant string + *) +let _ = eval_all default_env (Read.parse_str +" (def defn (fn-macro (name lm . body) (list 'def name (cons 'fn (cons lm body))))) (def defmacro (fn-macro (name lm . body) (list 'def name (cons 'fn-macro (cons lm body))))) + +(defmacro setq (sym val) + (list 'set (list 'quote sym) val)) +(defmacro letfn (sym fun . body) + (cons 'let-one (cons sym (cons '() (cons (list 'setq sym fun) body))))) + + +(defn filter (f l) + (letfn helper + (fn (l acc) + (if (nil? l) acc (helper (cdr l) (if (f (car l)) (cons (car l) acc) acc)))) + (helper l '()))) + ") diff --git a/lib/interpreterStdlib.ml b/lib/interpreterStdlib.ml index a490ec5..59941ed 100644 --- a/lib/interpreterStdlib.ml +++ b/lib/interpreterStdlib.ml @@ -62,3 +62,8 @@ let lambda env = function let lambda_macro env = function | LCons (l, body) -> LUnnamedMacro (env, l, body) | _ -> invalid_arg "invalid args to lambda-macro" + + +let lisp_not _ = function + | LCons (LNil, LNil) -> LSymbol "t" + | _ -> LNil