From cb236c9fe26b08f1e741987486c5ae8103824434 Mon Sep 17 00:00:00 2001 From: Victor Lei Date: Sun, 21 Aug 2016 07:19:46 +0300 Subject: [PATCH] bugfixes --- smop/Makefile | 10 ++++++++-- smop/backend.py | 21 +++++++++++---------- smop/lexer.py | 17 ++++++++++++----- smop/main.py | 25 ++++++++++++++++++------- smop/node.py | 2 +- smop/options.py | 3 ++- smop/parse.py | 10 ++++++++++ 7 files changed, 62 insertions(+), 26 deletions(-) diff --git a/smop/Makefile b/smop/Makefile index 3b21d3ed..69859e9a 100644 --- a/smop/Makefile +++ b/smop/Makefile @@ -5,8 +5,14 @@ SCRIPTS = $(OCTAVE)/scripts CYTHON = cython PYTHON = python -#XFILES = -x inputParser.m,quadgk.m,quadl.m,triplequad.m,dblquad.m,uiputfile.m,uigetfile.m,qmr.m,installed_packages.m,stft.m,legend.m,__plt_get_axis_arg__.m,rotate.m,print.m,__ghostscript__.m,__gnuplot_print__.m,set.m,edit.m,what.m,usejava.m,javachk.m,__go_draw_axes__.m,interp3.m,interp2.m,interpn.m,randi.m,interp1.m,spdiags.m,importdata.m,stemleaf.m +XFILES = -x inputParser + +#m,uiputfile.m,uigetfile.m,qmr.m + +#quadgk.m,quadl.m,triplequad.m,dblquad.m +#installed_packages.m,stft.m,legend.m,__plt_get_axis_arg__.m,rotate.m,print.m,__ghostscript__.m,__gnuplot_print__.m,set.m,edit.m,what.m,usejava.m,javachk.m,__go_draw_axes__.m,interp3.m,interp2.m,interpn.m,randi.m,interp1.m,spdiags.m,importdata.m,stemleaf.m FLAGS = +MYFLAGS=--ignore=999 V = 2.7 @@ -21,7 +27,7 @@ all: make -B FLAGS=-CTN liboctave.py | wc liboctave.py: - find $(SCRIPTS) -name \*.m | xargs $(PYTHON) main.py -o $@ $(FLAGS) $(XFILES) $^ + find $(SCRIPTS) -name \*.m | xargs $(PYTHON) main.py -o $@ $(MYFLAGS) $(FLAGS) $(XFILES) $^ #$(PYTHON) $@ clean: diff --git a/smop/backend.py b/smop/backend.py index 13f5c9aa..38a89aaa 100644 --- a/smop/backend.py +++ b/smop/backend.py @@ -220,17 +220,18 @@ def _backend(self,level=0): @extend(node.func_stmt) def _backend(self,level=0): - if len(self.args) == 1 and self.args[0].name == "varargin": - s = "\ndef %s(*args):" % self.ident._backend() - s += "\n varargin = cellarray(args)" - return s - + if (self.args and + isinstance(self.args[-1],node.ident) and + self.args and + self.args[-1].name == "varargin"): + self.args[-1].name = "*varargin" s = """ @function -def {0}({1}): +def %s(%s): nargin = sys._getframe(1).f_locals["nargin"] -""".format(self.ident._backend(), - self.args._backend()) + varargin = cellarray(varargin) +""" % (self.ident._backend(), + self.args._backend()) return s @@ -255,7 +256,7 @@ def _backend(self,level=0): @extend(node.ident) def _backend(self,level=0): - return self.name if self.name not in reserved else self.name+'_' + return (self.name if self.name not in reserved else self.name+'_') + ("=" + self.init._backend() if self.init is not None else '') @extend(node.if_stmt) def _backend(self,level=0): @@ -374,7 +375,7 @@ def _backend(self,level=0): # return "" # if s[0] in "%#": # return "\n"+s.replace("%","#") - return '"%s"' % self.value.encode("string_escape").replace('"','\\"') + return '"%s"' % self.value #.encode("string_escape").replace('"','\\"') @extend(node.sub) def _backend(self,level=0): diff --git a/smop/lexer.py b/smop/lexer.py index f3675ddb..ba543840 100644 --- a/smop/lexer.py +++ b/smop/lexer.py @@ -100,10 +100,16 @@ def unescape(s): """ ffd52d5fc5 """ - if s[0] == "'": - return s[1:-1].replace("''","'").decode("string_escape") - else: - return s[1:-1].replace('""','"').decode("string_escape") + try: + if s == r"'\'" or s == r'"\"': + return s[1:-1] + if s[0] == "'": + return s[1:-1].replace("''","'").decode("string_escape") + else: + return s[1:-1].replace('""','"').decode("string_escape") + except ValueError: + print s + raise @TOKEN(mos) def t_afterkeyword_STRING(t): @@ -242,7 +248,8 @@ def t_NEWLINE(t): return t def t_ERROR_STMT(t): - r"%!error.*\n" + r"%!(error|warning|test).*\n" + t.lexer.lineno += 1 # keep multiline comments def t_COMMENT(t): diff --git a/smop/main.py b/smop/main.py index 5ff22d27..ff5f9c4e 100644 --- a/smop/main.py +++ b/smop/main.py @@ -2,7 +2,7 @@ # Copyright 2011-2016 Victor Leikehman from os.path import splitext,basename import version -import sys,cPickle,glob,os +import sys,cPickle,glob,os,tarfile import getopt,re import lexer import parse @@ -17,9 +17,14 @@ import graphviz def main(): + tar = None if not options.filelist: options.parser.print_help() return + if (len(options.filelist) == 1 and + options.filelist[0].endswith(".tar")): + tar = tarfile.open(options.filelist[0]) + options.filelist = tar.getnames() if options.output == "-": fp = sys.stdout elif options.output: @@ -39,19 +44,24 @@ def main(): for i, options.filename in enumerate(options.filelist): try: - if not options.filename.endswith((".m",".tst")): - print "\tIgnored file: '%s' (unexpected file type)" % options.filename + if not options.filename.endswith((".m")): + if options.verbose: + print "\tIgnored: '%s' (unexpected file type)" % options.filename continue if os.path.basename(options.filename) in options.xfiles: - print "\tExcluded file: '%s'" % options.filename + print "\tExcluded: '%s'" % options.filename continue if options.verbose: print options.filename - buf = open(options.filename).read().replace("\r\n","\n") + if tar: + buf = tar.extractfile(options.filename).read() + else: + buf = open(options.filename).read() + buf = buf.replace("\r\n","\n") buf = buf.decode("ascii",errors="ignore") stmt_list=parse.parse(buf if buf[-1]=='\n' else buf+'\n') #assert None not in stmt_list - if not stmt_list and options.strict: + if not stmt_list: return if not options.no_resolve: G = resolve.resolve(stmt_list) @@ -60,8 +70,9 @@ def main(): print >> fp, s except Exception as e: print "\tFailed: ",options.filename - if options.strict: + if not options.ignore: raise + options.ignore -= 1 if __name__ == "__main__": main() diff --git a/smop/node.py b/smop/node.py index 487de3b5..e1f1f6a4 100644 --- a/smop/node.py +++ b/smop/node.py @@ -123,7 +123,7 @@ class number(atom,recordtype("number","value lineno lexpos",default=None)): def __str__(self): return str(self.value) -class ident(atom,recordtype("ident","name lineno column lexpos defs props", +class ident(atom,recordtype("ident","name lineno column lexpos defs props init", default=None)): def __str__(self): return self.name diff --git a/smop/options.py b/smop/options.py index 785db723..f093c2b8 100644 --- a/smop/options.py +++ b/smop/options.py @@ -9,7 +9,7 @@ smop [options][file.m ...file.m][-l file.py...] or - python -m smop.main [options][file.m ...file.m][-l file.py...] + smop [options] library.tar [-l file.py...] """, description= """ SMOP is Small Matlab and Octave to Python compiler. @@ -88,6 +88,7 @@ write Octave test suite. When disabled, behaves like regular comments.""") +parser.add_argument("-I", "--ignore", type=int) args = parser.parse_args(namespace=sys.modules[__name__]) diff --git a/smop/parse.py b/smop/parse.py index ed5ada9a..4f880fd5 100644 --- a/smop/parse.py +++ b/smop/parse.py @@ -137,6 +137,7 @@ def p_args_opt(p): | LPAREN RPAREN | LPAREN expr_list RPAREN """ + flag = False if len(p) == 1: p[0] = node.expr_list() elif len(p) == 3: @@ -144,9 +145,18 @@ def p_args_opt(p): elif len(p) == 4: assert isinstance(p[2],node.expr_list) p[0] = p[2] + flag = True else: assert 0 + if flag: + t = p[2][-1] + if isinstance(t,node.ident) and t.name=="varargin": + t.name = "*varargin" + for t in p[2]: + if isinstance(t,node.ident) and t.name != '*varargin': + t.init = node.ident("None") + @exceptions def p_break_stmt(p):