Added some standard functions

This commit is contained in:
2025-10-30 23:42:10 +03:00
parent 45828a8dd4
commit 37c8d2a62a
2 changed files with 28 additions and 4 deletions

View File

@@ -136,15 +136,34 @@ let () = add_special "quote" (fun _ -> function
| LCons (x, LNil) -> x | LCons (x, LNil) -> x
| _ -> invalid_arg "hmm") | _ -> invalid_arg "hmm")
let () = add_special "if" lisp_if 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 *) (*let () = add_builtin "print" lisp_prin *)
(* I know this looks insane. please trust me. *) (* I know this looks insane. please trust me.
let _ = eval_all default_env (Read.parse_str " 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 (def defn
(fn-macro (name lm . body) (fn-macro (name lm . body)
(list 'def name (cons 'fn (cons lm body))))) (list 'def name (cons 'fn (cons lm body)))))
(def defmacro (def defmacro
(fn-macro (name lm . body) (fn-macro (name lm . body)
(list 'def name (cons 'fn-macro (cons 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 '())))
") ")

View File

@@ -62,3 +62,8 @@ let lambda env = function
let lambda_macro env = function let lambda_macro env = function
| LCons (l, body) -> LUnnamedMacro (env, l, body) | LCons (l, body) -> LUnnamedMacro (env, l, body)
| _ -> invalid_arg "invalid args to lambda-macro" | _ -> invalid_arg "invalid args to lambda-macro"
let lisp_not _ = function
| LCons (LNil, LNil) -> LSymbol "t"
| _ -> LNil