Added some standard functions
This commit is contained in:
		
							
								
								
									
										23
									
								
								lib/eval.ml
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								lib/eval.ml
									
									
									
									
									
								
							@@ -136,15 +136,34 @@ let () = add_special "quote" (fun _ -> function
 | 
			
		||||
             | 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 '())))
 | 
			
		||||
 
 | 
			
		||||
")
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user