aboutsummaryrefslogtreecommitdiff
path: root/module/c/eval/environment.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2022-06-30 07:07:59 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2022-07-07 21:17:22 +0200
commit85bb810edc083b0bcc08f76e22d51d1555dc828d (patch)
tree7f686666507e0c97546c98ae9c1da408abddf16f /module/c/eval/environment.scm
parentC parser minor cleanup. (diff)
downloadcalp-85bb810edc083b0bcc08f76e22d51d1555dc828d.tar.gz
calp-85bb810edc083b0bcc08f76e22d51d1555dc828d.tar.xz
Add basic c evaluator.
Diffstat (limited to '')
-rw-r--r--module/c/eval/environment.scm34
1 files changed, 34 insertions, 0 deletions
diff --git a/module/c/eval/environment.scm b/module/c/eval/environment.scm
new file mode 100644
index 00000000..12eefaf7
--- /dev/null
+++ b/module/c/eval/environment.scm
@@ -0,0 +1,34 @@
+(define-module (c eval environment)
+ :use-module (srfi srfi-1)
+ :export (make-environment
+ env-set! env-ref push-frame! pop-frame!))
+
+(define (make-frame)
+ (make-hash-table))
+
+(define (make-environment)
+ (list (make-frame)))
+
+;; Returns an updated environment, linear update
+(define (env-set! env key value)
+ ;; get handle to differentiate #f
+ ;; (even though #f should never be stored since it's not a C value)
+ (cond ((find (lambda (frame) (hashq-get-handle frame key)) env)
+ => (lambda (frame) (hashq-set! frame key value)))
+ (else (hashq-set! (car env) key value)))
+ env)
+
+(define (env-ref env key)
+ (cond ((null? env)
+ (scm-error 'misc-error "env-ref"
+ "~s unbound"
+ (list key)
+ #f))
+ ((hashq-get-handle (car env) key) => cdr)
+ (else (env-ref (cdr env) key))))
+
+(define (push-frame! environment)
+ (cons (make-frame) environment))
+
+(define (pop-frame! environment)
+ (cdr environment))