-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv.lisp
More file actions
27 lines (22 loc) · 747 Bytes
/
env.lisp
File metadata and controls
27 lines (22 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
(in-package :cl-user)
(defpackage zpcalc/env
(:use :cl)
(:import-from #:zpcalc/util #:acond)
(:export
#:make-env
#:get-env))
(in-package :zpcalc/env)
;; A simple lexical environment backed by a hash table
;; environment :: (hash-table * Maybe environment)
;; initialize an environment, optionally with a parent environment
(defun make-env (&optional parent)
(cons (make-hash-table) parent))
;; Get the value of a variable in an environment
;; If not found, returns nil
(defun get-env (key env)
(acond ((gethash key (car env)) it)
((cdr env) (get-env it key))
(t nil)))
;; Allow the use of setf to set variable values
(defsetf get-env (name env) (new-value)
`(setf (gethash ,name (car ,env)) ,new-value))