aboutsummaryrefslogtreecommitdiff
path: root/cs/env.cs
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-06 23:58:14 -0500
committerJoel Martin <github@martintribe.org>2014-04-06 23:58:14 -0500
commitb18969c0b8d47d67d4b73b5b20742a0bc3179e72 (patch)
tree5c3eb2c1545857801ff5e64504bdd2a2ee49aac0 /cs/env.cs
parentb56c49a12dad20c55fbf07775b536c9d656af313 (diff)
downloadmal-b18969c0b8d47d67d4b73b5b20742a0bc3179e72.tar.gz
mal-b18969c0b8d47d67d4b73b5b20742a0bc3179e72.zip
CS: add step3_env
Also, make Makefile more closely match the C Makefile.
Diffstat (limited to 'cs/env.cs')
-rw-r--r--cs/env.cs55
1 files changed, 55 insertions, 0 deletions
diff --git a/cs/env.cs b/cs/env.cs
new file mode 100644
index 0000000..cb5318f
--- /dev/null
+++ b/cs/env.cs
@@ -0,0 +1,55 @@
+using System.Collections.Generic;
+using Mal;
+using MalVal = Mal.types.MalVal;
+using MalSymbol = Mal.types.MalSymbol;
+using MalList = Mal.types.MalList;
+
+namespace Mal {
+ public class env {
+ public class Env {
+ Env outer = null;
+ Dictionary<string, MalVal> data = new Dictionary<string, MalVal>();
+
+ public Env(Env outer) {
+ this.outer = outer;
+ }
+ public Env(Env outer, MalList binds, MalList exprs) {
+ this.outer = outer;
+ for (int i=0; i<binds.size(); i++) {
+ string sym = ((MalSymbol)binds.nth(i)).getName();
+ if (sym == "&") {
+ data[((MalSymbol)binds.nth(i+1)).getName()] = exprs.slice(i);
+ break;
+ } else {
+ data[sym] = exprs.nth(i);
+ }
+ }
+ }
+
+ public Env find(string key) {
+ if (data.ContainsKey(key)) {
+ return this;
+ } else if (outer != null) {
+ return outer.find(key);
+ } else {
+ return null;
+ }
+ }
+
+ public MalVal get(string key) {
+ Env e = find(key);
+ if (e == null) {
+ throw new Mal.types.MalException(
+ "'" + key + "' not found");
+ } else {
+ return e.data[key];
+ }
+ }
+
+ public Env set(string key, MalVal value) {
+ data[key] = value;
+ return this;
+ }
+ }
+ }
+}