aboutsummaryrefslogtreecommitdiff
path: root/cs/env.cs
blob: 39ab100e0f235cfbe4752c5fc448b5c51bfe264c (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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(MalSymbol key) {
                if (data.ContainsKey(key.getName())) {
                    return this;
                } else if (outer != null) {
                    return outer.find(key);
                } else {
                    return null;
                }
            }

            public MalVal get(MalSymbol key) {
                Env e = find(key);
                if (e == null) {
                    throw new Mal.types.MalException(
                            "'" + key.getName() + "' not found");
                } else {
                    return e.data[key.getName()];
                }
            }

            public Env set(MalSymbol key, MalVal value) {
                data[key.getName()] = value;
                return this;
            }
        }
    }
}