reorganized a bit, separated bind-symbol into two operators that have different uses, def and set
This commit is contained in:
		@@ -29,6 +29,14 @@ let env_set_local env s v =
 | 
				
			|||||||
  | [] -> ()
 | 
					  | [] -> ()
 | 
				
			||||||
  | e1 :: _ -> Hashtbl.replace e1 s v
 | 
					  | e1 :: _ -> Hashtbl.replace e1 s v
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					let rec env_update env s v =
 | 
				
			||||||
 | 
					  match env with
 | 
				
			||||||
 | 
					  | [] -> ()
 | 
				
			||||||
 | 
					  | e1 :: erest ->
 | 
				
			||||||
 | 
					     match Hashtbl.find_opt e1 s with
 | 
				
			||||||
 | 
					     | None -> env_update erest s v
 | 
				
			||||||
 | 
					     | Some _ -> Hashtbl.replace e1 s v
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let env_new_lexical env = 
 | 
					let env_new_lexical env = 
 | 
				
			||||||
  let h = Hashtbl.create 16 in
 | 
					  let h = Hashtbl.create 16 in
 | 
				
			||||||
  h :: env
 | 
					  h :: env
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										28
									
								
								lib/eval.ml
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								lib/eval.ml
									
									
									
									
									
								
							@@ -91,13 +91,21 @@ and eval_call env func args =
 | 
				
			|||||||
                  (Printf.sprintf "eval_apply: cannot call non-function object %s" (dbg_print_one v)))
 | 
					                  (Printf.sprintf "eval_apply: cannot call non-function object %s" (dbg_print_one v)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
and (* This only creates a *local* binding, contained to the body given. *)
 | 
					and (* This only creates a *local* binding, contained to the body given. *)
 | 
				
			||||||
  bind_local env =
 | 
					  bind_local env = function
 | 
				
			||||||
  function
 | 
					 | 
				
			||||||
  | LCons (LSymbol s, LCons (v, body)) ->
 | 
					  | LCons (LSymbol s, LCons (v, body)) ->
 | 
				
			||||||
    let e = env_new_lexical env in
 | 
					    let e = env_new_lexical env in
 | 
				
			||||||
    env_set_local e s v;
 | 
					    env_set_local e s v;
 | 
				
			||||||
    eval_body e body
 | 
					    eval_body e body
 | 
				
			||||||
  | _ -> invalid_arg "invalid argument to bind-local"
 | 
					  | _ -> invalid_arg "invalid argument to bind-local"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(* special form that creates a global binding *)
 | 
				
			||||||
 | 
					and lisp_define env = function
 | 
				
			||||||
 | 
					  | LCons (LSymbol s, LCons (v, LNil)) ->
 | 
				
			||||||
 | 
					     let evaluated = eval_one env v in
 | 
				
			||||||
 | 
					     env_set_global env s evaluated;
 | 
				
			||||||
 | 
					     evaluated
 | 
				
			||||||
 | 
					  | _ -> invalid_arg "invalid args to def"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
and lisp_if env = function
 | 
					and lisp_if env = function
 | 
				
			||||||
  | LCons (cond, LCons (if_true, LNil)) ->
 | 
					  | LCons (cond, LCons (if_true, LNil)) ->
 | 
				
			||||||
    (match eval_one env cond with
 | 
					    (match eval_one env cond with
 | 
				
			||||||
@@ -118,7 +126,8 @@ let () = add_builtin "-" sub
 | 
				
			|||||||
let () = add_builtin "car" car
 | 
					let () = add_builtin "car" car
 | 
				
			||||||
let () = add_builtin "cdr" cdr
 | 
					let () = add_builtin "cdr" cdr
 | 
				
			||||||
let () = add_builtin "cons" cons
 | 
					let () = add_builtin "cons" cons
 | 
				
			||||||
let () = add_builtin "bind-symbol" bind_symbol 
 | 
					let () = add_special "def" lisp_define
 | 
				
			||||||
 | 
					let () = add_builtin "set" lisp_set
 | 
				
			||||||
let () = add_builtin "list" lisp_list
 | 
					let () = add_builtin "list" lisp_list
 | 
				
			||||||
let () = add_special "fn" lambda
 | 
					let () = add_special "fn" lambda
 | 
				
			||||||
let () = add_special "fn-macro" lambda_macro
 | 
					let () = add_special "fn-macro" lambda_macro
 | 
				
			||||||
@@ -131,14 +140,11 @@ let () = add_special "if" lisp_if
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
(* I know this looks insane. please trust me. *)
 | 
					(* I know this looks insane. please trust me. *)
 | 
				
			||||||
let _ = eval_all default_env (Read.parse_str "
 | 
					let _ = eval_all default_env (Read.parse_str "
 | 
				
			||||||
(bind-symbol 'defn
 | 
					(def defn
 | 
				
			||||||
  (fn-macro (name lm . body)
 | 
					  (fn-macro (name lm . body)
 | 
				
			||||||
    (list 'bind-symbol (list 'quote name) (cons 'fn (cons lm body)))))
 | 
					    (list 'def name (cons 'fn (cons lm body)))))
 | 
				
			||||||
(bind-symbol 'defmacro
 | 
					(def defmacro
 | 
				
			||||||
  (fn-macro (name lm . body)
 | 
					  (fn-macro (name lm . body)
 | 
				
			||||||
    (list 'bind-symbol (list 'quote name) (cons 'fn-macro (cons lm body)))))
 | 
					    (list 'def name (cons 'fn-macro (cons lm body)))))
 | 
				
			||||||
(defmacro def
 | 
					")
 | 
				
			||||||
  (var val)
 | 
					 | 
				
			||||||
  (list 'bind-symbol (list 'quote var) val))
 | 
					 | 
				
			||||||
()")
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -47,23 +47,12 @@ let cons _ vs =
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
let lisp_list _ vs = vs
 | 
					let lisp_list _ vs = vs
 | 
				
			||||||
 | 
					
 | 
				
			||||||
(* Binds any value to a symbol, in the *global environment*. *)
 | 
					(* builtin function that updates an existing binding *)
 | 
				
			||||||
let bind_symbol env =
 | 
					let lisp_set env = function
 | 
				
			||||||
  function
 | 
					 | 
				
			||||||
  (* Special case for setting a function to a symbol, if the function
 | 
					 | 
				
			||||||
     is a lambda then we turn it into a real "function" by giving it this
 | 
					 | 
				
			||||||
     new name *)
 | 
					 | 
				
			||||||
  | LCons (LQuoted (LSymbol s), LCons (LLambda (e, l, b), LNil))
 | 
					 | 
				
			||||||
  | LCons (LSymbol s, LCons (LLambda (e, l, b), LNil)) ->
 | 
					 | 
				
			||||||
    let f = LFunction (s, e, l, b) in
 | 
					 | 
				
			||||||
    env_set_global env s f;
 | 
					 | 
				
			||||||
    f
 | 
					 | 
				
			||||||
  | LCons (LQuoted (LSymbol s), LCons (v, LNil))
 | 
					 | 
				
			||||||
  | LCons (LSymbol s, LCons (v, LNil)) ->
 | 
					  | LCons (LSymbol s, LCons (v, LNil)) ->
 | 
				
			||||||
    env_set_global env s v;
 | 
					     env_update env s v;
 | 
				
			||||||
     v
 | 
					     v
 | 
				
			||||||
  | _ -> raise (Invalid_argument "invalid args to set!")
 | 
					  | _ -> invalid_arg "invalid args to set"
 | 
				
			||||||
           
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
let lambda env = function
 | 
					let lambda env = function
 | 
				
			||||||
  | LCons (l, body) ->
 | 
					  | LCons (l, body) ->
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user