Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-math committed Nov 25, 2023
1 parent cb6e91e commit ff37ae0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
27 changes: 27 additions & 0 deletions TokenList.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
classdef TokenList
properties
tokens
end

methods
function self = TokenList(tokens)
self.tokens = tokens;
end
function subs = subsref(self, subs, varargin)
if strcmp(subs(1).type, '()')
assert(numel(subs(1).subs) == 1 && numel(subs(1).subs{1}) == 1);
subs = builtin('subsref', self.tokens, subs);
else
subs = builtin('subsref', self, subs);
end
end
function n = numel(self, varargin)
if ~isempty(varargin)
n = 1;
else
n = numel(self.tokens);
end
end
end
end

2 changes: 1 addition & 1 deletion main.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function compareFile(file1, file2)
tokens.append(Token(type, token));
lastToken = type;
end
tokens = tokens.toList([]);
tokens = TokenList(tokens.toList([]));
end
function [j, type, token] = nextToken(s, j, table, lastToken)
while j <= numel(s) && s(j) == ' '
Expand Down
11 changes: 11 additions & 0 deletions mruntime.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,17 @@ def isequal(a: Any, b: Any) -> bool:
return True


class TokenList:
def __init__(self, tokens: list[str]) -> None:
self.tokens = tokens

def __getitem__(self, i: int) -> str:
return self.tokens[i]

def __len__(self) -> int:
return len(self.tokens)


newline = "\n"
numel = len
true = True
Expand Down
2 changes: 1 addition & 1 deletion test_m/py/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def tokenize(s, table): # retval: tokens
[j, type, token] = nextToken(s, j, table, lastToken)
mparen(tokens.append, Token(type, token))
lastToken = type
tokens = mparen(tokens.toList, [])
tokens = TokenList(mparen(tokens.toList, []))
return tokens
def nextToken(s, j, table, lastToken): # retval: [j, type, token]
nargin = 4
Expand Down

0 comments on commit ff37ae0

Please sign in to comment.