77dae072b343446807524ad4a8d3af906dcd2277
1 % this is just being used as a namespace
3 methods
(Static
= true
)
4 function tokens
= tokenize
(str
)
5 re
= '[\s,]*(~@|[\[\]{}()''`~^@]|"(?:\\.|[^\\"])*"|;.*|[^\s\[\]{}(''"`,;)]*)';
6 % extract the capture group (to ignore spaces and commas)
7 tokens
= cellfun
(@
(x
) x
(1), regexp
(str
, re
, 'tokens'));
10 function atm
= read_atom
(rdr
)
12 %fprintf('in read_atom: %s\n', token);
13 if not
(isempty(regexp
(token
, '^-?[0-9]+$', 'match')))
14 atm
= str2double
(token
);
15 elseif strcmp(token
(1), '"')
16 atm
= token
(2:length(token
)-1);
17 elseif strcmp(token
, 'nil')
19 elseif strcmp(token
, 'true')
21 elseif strcmp(token
, 'false')
24 atm
= types.Symbol
(token
);
28 function lst
= read_list
(rdr
)
29 %fprintf('in read_list\n');
32 if not
(strcmp(token
, '('))
33 error('expected ''(''');
38 error('expected '')''');
40 if strcmp(token
, ')'), break, end
41 lst
{length(lst
)+1} = reader.read_form
(rdr
);
47 function ast
= read_form
(rdr
)
48 %fprintf('in read_form\n');
52 error('unexpected '')''');
54 ast
= reader.read_list
(rdr
);
56 ast
= reader.read_atom
(rdr
);
60 function ast
= read_str
(str
)
61 %fprintf('in read_str\n');
62 tokens
= reader.tokenize
(str
);
64 ast
= reader.read_form
(rdr
);