Merge pull request #406 from chr15m/lib-alias-hacks
[jackhill/mal.git] / matlab / Env.m
1 classdef Env < handle
2 properties
3 data
4 outer
5 end
6 methods
7 function env = Env(outer, binds, exprs)
8 if exist('OCTAVE_VERSION', 'builtin') ~= 0
9 env.data = Dict();
10 else
11 env.data = containers.Map();
12 end
13
14 if nargin == 0
15 env.outer = false;
16 else
17 % Workaround Octave calling bug when the first
18 % argument is the same type as the class (the class is
19 % not properly initialized in that case)
20 env.outer = outer{1};
21 end
22
23 if nargin > 1
24 %env = Env(outer);
25 for i=1:length(binds)
26 k = binds.get(i).name;
27 if strcmp(k, '&')
28 env.data(binds.get(i+1).name) = exprs.slice(i);
29 break;
30 else
31 env.data(k) = exprs.get(i);
32 end
33 end
34 end
35 end
36
37 function ret = set(env, k, v)
38 env.data(k.name) = v;
39 ret = v;
40 end
41 function ret = find(env, k)
42 if env.data.isKey(k.name)
43 ret = env;
44 else
45 if ~islogical(env.outer)
46 ret = env.outer.find(k);
47 else
48 ret = false;
49 end
50 end
51 end
52 function ret = get(env, k)
53 fenv = env.find(k);
54 if ~islogical(fenv)
55 ret = fenv.data(k.name);
56 else
57 if exist('OCTAVE_VERSION', 'builtin') ~= 0
58 error('ENV:notfound', ...
59 sprintf('''%s'' not found', k.name));
60 else
61 throw(MException('ENV:notfound', ...
62 sprintf('''%s'' not found', k.name)));
63 end
64 end
65 end
66 end
67 end