diff options
| author | Joel Martin <github@martintribe.org> | 2015-02-07 20:32:06 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-02-07 20:32:24 -0600 |
| commit | 9a54ea186c2c7646d014b97774adfc418e9ab9c9 (patch) | |
| tree | 7fcb4abf81594a0075c5356724140e65554eefe6 | |
| parent | 882b43289ecc0c13a030605b68be52107b1c64b5 (diff) | |
| download | mal-9a54ea186c2c7646d014b97774adfc418e9ab9c9.tar.gz mal-9a54ea186c2c7646d014b97774adfc418e9ab9c9.zip | |
matlab: step0 and step1 basics.
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | matlab/+types/Symbol.m | 10 | ||||
| -rw-r--r-- | matlab/Makefile | 13 | ||||
| -rw-r--r-- | matlab/Reader.m | 27 | ||||
| -rw-r--r-- | matlab/printer.m | 27 | ||||
| -rw-r--r-- | matlab/reader.m | 66 | ||||
| -rw-r--r-- | matlab/step0_repl.m | 25 | ||||
| -rw-r--r-- | matlab/step1_read_print.m | 31 | ||||
| l--------- | matlab/types | 1 |
9 files changed, 203 insertions, 1 deletions
@@ -11,7 +11,7 @@ PYTHON = python # IMPLS = bash c clojure coffee cs go haskell java js lua make mal \ - ocaml perl php ps python r racket ruby rust scala vb + ocaml matlab perl php ps python r racket ruby rust scala vb step0 = step0_repl step1 = step1_read_print @@ -62,6 +62,7 @@ lua_STEP_TO_PROG = lua/$($(1)).lua make_STEP_TO_PROG = make/$($(1)).mk mal_STEP_TO_PROG = mal/$($(1)).mal ocaml_STEP_TO_PROG = ocaml/$($(1)) +matlab_STEP_TO_PROG = matlab/$($(1)).m perl_STEP_TO_PROG = perl/$($(1)).pl php_STEP_TO_PROG = php/$($(1)).php ps_STEP_TO_PROG = ps/$($(1)).ps @@ -87,6 +88,7 @@ lua_RUNSTEP = ../$(2) $(3) make_RUNSTEP = make -f ../$(2) $(3) mal_RUNSTEP = $(call $(MAL_IMPL)_RUNSTEP,$(1),$(call $(MAL_IMPL)_STEP_TO_PROG,stepA),../$(2),") #" ocaml_RUNSTEP = ../$(2) $(3) +matlab_RUNSTEP = matlab -nodisplay -nosplash -nodesktop -nojvm -r "run('../$(2)');quit;" perl_RUNSTEP = perl ../$(2) --raw $(3) php_RUNSTEP = php ../$(2) $(3) ps_RUNSTEP = $(4)gs -q -I./ -dNODISPLAY -- ../$(2) $(3)$(4) diff --git a/matlab/+types/Symbol.m b/matlab/+types/Symbol.m new file mode 100644 index 0000000..1a06037 --- /dev/null +++ b/matlab/+types/Symbol.m @@ -0,0 +1,10 @@ +classdef Symbol + properties + name + end + methods + function sym = Symbol(name) + sym.name = name; + end + end +end diff --git a/matlab/Makefile b/matlab/Makefile new file mode 100644 index 0000000..db69428 --- /dev/null +++ b/matlab/Makefile @@ -0,0 +1,13 @@ +SOURCES_BASE = Reader.m types/Symbol.m reader.m printer.m +#SOURCES_LISP = env.m core.m stepA_interop.m +SOURCES_LISP = step1_read_print.m +SOURCES = $(SOURCES_BASE) $(SOURCES_LISP) + + +.PHONY: stats tests $(TESTS) + +stats: $(SOURCES) + @wc $^ + +stats-lisp: $(SOURCES_LISP) + @wc $^ diff --git a/matlab/Reader.m b/matlab/Reader.m new file mode 100644 index 0000000..c18ea54 --- /dev/null +++ b/matlab/Reader.m @@ -0,0 +1,27 @@ +classdef Reader < handle + properties + tokens + position + end + methods + function rdr = Reader(tokens) + rdr.tokens = tokens; + rdr.position = 1; + end + function tok = next(rdr) + rdr.position = rdr.position + 1; + if rdr.position-1 > length(rdr.tokens) + tok = false; + else + tok = rdr.tokens{rdr.position-1}; + end + end + function tok = peek(rdr) + if rdr.position > length(rdr.tokens) + tok = false; + else + tok = rdr.tokens{rdr.position}; + end + end + end +end diff --git a/matlab/printer.m b/matlab/printer.m new file mode 100644 index 0000000..b164093 --- /dev/null +++ b/matlab/printer.m @@ -0,0 +1,27 @@ +% this is just being used as a namespace +classdef printer + methods (Static = true) + function str = pr_str(obj, print_readably) + switch class(obj) + case 'types.Symbol' + str = obj.name; + case 'double' + str = num2str(obj); + case 'char' + str = strcat('"', obj, '"'); + case 'cell' + strs = cellfun(@(x) printer.pr_str(x, print_readably), ... + obj, 'UniformOutput', false); + str = strcat('(', strjoin(strs, ' '), ')'); + case 'logical' + if eq(obj, true) + str = 'true'; + else + str = 'false'; + end + otherwise + str = '#<unknown>'; + end + end + end +end diff --git a/matlab/reader.m b/matlab/reader.m new file mode 100644 index 0000000..8c752c9 --- /dev/null +++ b/matlab/reader.m @@ -0,0 +1,66 @@ +% this is just being used as a namespace +classdef reader + methods (Static = true) + function tokens = tokenize(str) + re = '[\s,]*(~@|[\[\]{}()''`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}(''"`,;)]*)'; + % extract the capture group (to ignore spaces and commas) + tokens = cellfun(@(x) x(1), regexp(str, re, 'tokens')); + end + + function atm = read_atom(rdr) + token = rdr.next(); + %fprintf('in read_atom: %s\n', token); + if not(isempty(regexp(token, '^-?[0-9]+$', 'match'))) + atm = str2double(token); + elseif strcmp(token(1), '"') + atm = token(2:length(token)-1); + %else if token eq 'nil' + elseif strcmp(token, 'true') + atm = true; + elseif strcmp(token, 'false') + atm = false; + else + atm = types.Symbol(token); + end + end + + function lst = read_list(rdr) + %fprintf('in read_list\n'); + lst = {}; + token = rdr.next(); + if not(strcmp(token, '(')) + error('expected ''('''); + end + token = rdr.peek(); + while true + if eq(token, false) + error('expected '')'''); + end + if strcmp(token, ')'), break, end + lst{length(lst)+1} = reader.read_form(rdr); + token = rdr.peek(); + end + rdr.next(); + end + + function ast = read_form(rdr) + %fprintf('in read_form\n'); + token = rdr.peek(); + switch token(1) + case ')' + error('unexpected '')'''); + case '(' + ast = reader.read_list(rdr); + otherwise + ast = reader.read_atom(rdr); + end + end + + function ast = read_str(str) + %fprintf('in read_str\n'); + tokens = reader.tokenize(str); + rdr = Reader(tokens); + ast = reader.read_form(rdr); + end + end +end diff --git a/matlab/step0_repl.m b/matlab/step0_repl.m new file mode 100644 index 0000000..22025c0 --- /dev/null +++ b/matlab/step0_repl.m @@ -0,0 +1,25 @@ +function step0_repl(varargin), main(varargin), end + +function ret = READ(str) + ret = str; +end + +function ret = EVAL(ast, env) + ret = ast; +end + +function ret = PRINT(ast) + ret = ast; +end + +function ret = rep(str) + ret = PRINT(EVAL(READ(str), '')); +end + +function main(args) + while (true) + line = input('user> ', 's'); + fprintf('%s\n', rep(line)); + end +end + diff --git a/matlab/step1_read_print.m b/matlab/step1_read_print.m new file mode 100644 index 0000000..486b725 --- /dev/null +++ b/matlab/step1_read_print.m @@ -0,0 +1,31 @@ +function step1_read_print(varargin), main(varargin), end + +function ret = READ(str) + ret = reader.read_str(str); +end + +function ret = EVAL(ast, env) + ret = ast; +end + +function ret = PRINT(ast) + ret = printer.pr_str(ast, true); +end + +function ret = rep(str) + ret = PRINT(EVAL(READ(str), '')); +end + +function main(args) + %cleanObj = onCleanup(@() disp('*** here1 ***')); + while (true) + line = input('user> ', 's'); + if strcmp(strtrim(line),''), continue, end + try + fprintf('%s\n', rep(line)); + catch err + fprintf('Error: %s\n', err.message); + fprintf('%s\n', getReport(err, 'extended')); + end + end +end diff --git a/matlab/types b/matlab/types new file mode 120000 index 0000000..bb26791 --- /dev/null +++ b/matlab/types @@ -0,0 +1 @@ ++types/
\ No newline at end of file |
