aboutsummaryrefslogtreecommitdiff
path: root/forth/env.fs
diff options
context:
space:
mode:
authorChouser <chouser@n01se.net>2015-02-14 13:40:07 -0500
committerChouser <chouser@n01se.net>2015-02-21 13:22:44 -0500
commit69972a8399efe4abb8567526e90262e131f90d26 (patch)
tree5e12e86da119a9c3f4372dab9e04777a746f90d0 /forth/env.fs
parent9da223a35a176d94fbb75cbcc1000871ff5aff0b (diff)
downloadmal-69972a8399efe4abb8567526e90262e131f90d26.tar.gz
mal-69972a8399efe4abb8567526e90262e131f90d26.zip
forth: Add step 3
Diffstat (limited to 'forth/env.fs')
-rw-r--r--forth/env.fs45
1 files changed, 45 insertions, 0 deletions
diff --git a/forth/env.fs b/forth/env.fs
new file mode 100644
index 0000000..c1dc278
--- /dev/null
+++ b/forth/env.fs
@@ -0,0 +1,45 @@
+require types.fs
+
+MalType%
+ cell% field MalEnv/outer
+ cell% field MalEnv/data
+deftype MalEnv
+
+: MalEnv. { outer -- env }
+ MalEnv new { env }
+ outer env MalEnv/outer !
+ MalMap/Empty env MalEnv/data !
+ env ;
+
+: env/set { key val env -- }
+ key val env MalEnv/data @ assoc
+ env MalEnv/data ! ;
+
+: env/find { key env -- env-or-0 }
+ env
+ begin ( env )
+ dup 0 key rot MalEnv/data @ get ( env val-or-0 )
+ 0= if ( env )
+ MalEnv/outer @ dup 0= ( env-or-0 done-looping? )
+ else
+ -1 \ found it! ( env -1 )
+ endif
+ until ;
+
+MalEnv
+ extend get { not-found key env -- }
+ key env env/find ( env-or-0 )
+ ?dup 0= if
+ not-found
+ else ( env )
+ not-found key rot MalEnv/data @ get
+ endif ;;
+ extend pr-buf { env }
+ env MalEnv/data @ pr-buf
+ a-space s" outer: " str-append
+ env MalEnv/outer @ ?dup 0= if
+ s" <none>" str-append
+ else
+ pr-buf
+ endif ;;
+drop \ No newline at end of file