aboutsummaryrefslogtreecommitdiff
path: root/forth/env.fs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-02-21 15:58:41 -0600
committerJoel Martin <github@martintribe.org>2015-02-21 15:58:41 -0600
commit2a42d8274072c44dd2d83762cc27cd810f5b8452 (patch)
treec778c4319f93c89b85879c0dd60914813c4cf3db /forth/env.fs
parent5a5edd508d20775fddcb5931f263042d8e0d8fef (diff)
parent9603289087755c880fbb16b7e36eedef940237be (diff)
downloadmal-2a42d8274072c44dd2d83762cc27cd810f5b8452.tar.gz
mal-2a42d8274072c44dd2d83762cc27cd810f5b8452.zip
Merge pull request #7 from Chouser/forth-pr
Add Forth
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..1b5a362
--- /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