1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-11 00:52:44 +03:00
mal/matlab/Env.m
Joel Martin 4769962955 matlab: support Octave 4.0.0
- Workarounds for Octave 4.0.0:
    - no containers.Map yet so use new Dict
      structure when running under Octave.
    - weird error when +FOO/ directory and FOO.m file both exist so
      rename types.m to type_utils.m
    - no getReport so implement custom stack printer
    - workaround weird issue that happens when a class initializer is
      called with the first argument of the same class as per
      Env(outer, ...). The class is not properly initialized. So for
      now, wrap the outer in a cell as Env({outer}, ...)
    - missing MException object types so when in Octave, use a global
      variable to store the error object.
    - missing native2unicode so just use 0xff character for keyword
      prefix.
    - workaround some function calling/passing differences. For
      example, the "@" anonymous function symbol does not seem to work
      for conveying an existing function but only for defininign a new
      one. E.g. this works:
          @(a,b) a+b
      but this doesn't
          @my_adder_fn.
      so just do this:
          @(a,b) my_adder_fn(a,b)

- Add Dockerfile with Octave

- Active Travis for matlab implementation using Octave
2015-12-31 13:55:18 -06:00

68 lines
2.0 KiB
Matlab

classdef Env < handle
properties
data
outer
end
methods
function env = Env(outer, binds, exprs)
if exist('OCTAVE_VERSION', 'builtin') ~= 0
env.data = Dict();
else
env.data = containers.Map();
end
if nargin == 0
env.outer = false;
else
% Workaround Octave calling bug when the first
% argument is the same type as the class (the class is
% not properly initialized in that case)
env.outer = outer{1};
end
if nargin > 1
%env = Env(outer);
for i=1:length(binds)
k = binds.get(i).name;
if strcmp(k, '&')
env.data(binds.get(i+1).name) = exprs.slice(i);
break;
else
env.data(k) = exprs.get(i);
end
end
end
end
function ret = set(env, k, v)
env.data(k.name) = v;
ret = v;
end
function ret = find(env, k)
if env.data.isKey(k.name)
ret = env;
else
if ~islogical(env.outer)
ret = env.outer.find(k);
else
ret = false;
end
end
end
function ret = get(env, k)
fenv = env.find(k);
if ~islogical(fenv)
ret = fenv.data(k.name);
else
if exist('OCTAVE_VERSION', 'builtin') ~= 0
error('ENV:notfound', ...
sprintf('''%s'' not found', k.name));
else
throw(MException('ENV:notfound', ...
sprintf('''%s'' not found', k.name)));
end
end
end
end
end