1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-19 09:38:28 +03:00
mal/impls/matlab/type_utils.m
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

105 lines
3.1 KiB
Matlab

classdef type_utils
properties (Constant = true)
nil = types.Nil();
end
methods(Static)
function ret = equal(a,b)
ret = false;
ota = class(a); otb = class(b);
if ~(strcmp(ota,otb) || ...
(type_utils.sequential_Q(a) && type_utils.sequential_Q(b)))
return;
end
switch (ota)
case {'types.List', 'types.Vector'}
if ~(length(a) == length(b))
return;
end
for i=1:length(a)
if ~(type_utils.equal(a.get(i), b.get(i)))
return;
end
end
ret = true;
case 'types.HashMap'
if ~(length(a) == length(b))
return;
end
ks1 = a.keys();
for i=1:length(ks1)
k = ks1{i};
if ~(b.data.isKey(k))
return;
end
if ~(type_utils.equal(a.data(k), b.data(k)))
return;
end
end
ret = true;
case 'char'
ret = strcmp(a,b);
otherwise
ret = a == b;
end
end
function ret = sequential_Q(obj)
ret = strcmp(class(obj), 'types.List') || ...
strcmp(class(obj), 'types.Vector');
end
function ret = list_Q(obj)
ret = strcmp(class(obj), 'types.List');
end
function ret = vector_Q(obj)
ret = strcmp(class(obj), 'types.Vector');
end
function ret = hash_map_Q(obj)
ret = strcmp(class(obj), 'types.HashMap');
end
function ret = keyword(str)
if type_utils.keyword_Q(str)
ret = str;
else
ret = sprintf('%c%s', 255, str);
end
end
function ret = keyword_Q(obj)
ret = length(obj) > 1 && strcmp(obj(1), sprintf('%c', 255));
end
function ret = string_Q(obj)
ret = strcmp(class(obj), 'char') && ~type_utils.keyword_Q(obj);
end
function ret = number_Q(obj)
ret = strcmp(class(obj), 'double');
end
function ret = fn_Q(obj)
ret = isa(obj,'function_handle') || ...
(isa(obj,'types.Function') && ~obj.is_macro);
end
function ret = macro_Q(obj)
ret = isa(obj,'types.Function') && obj.is_macro;
end
function print_stack(err)
for i=1:numel(err.stack)
stack = err.stack(i);
if exist('OCTAVE_VERSION', 'builtin') ~= 0
fprintf(' %s at line %d column %d (%s)\n', ...
stack.name, stack.line, stack.column, stack.file);
else
fprintf(' %s at line %d (%s)\n', ...
stack.name, stack.line, stack.file);
end
end
end
end
end