From f61f49dc581df78ec5cd964ff35aca9a80c1008a Mon Sep 17 00:00:00 2001 From: Victor Lei Date: Mon, 15 Aug 2016 17:53:14 +0300 Subject: [PATCH] misc fixes --- smop/Makefile | 4 ++-- smop/backend.py | 16 ++++++++++------ smop/core.py | 6 +++--- smop/main.py | 1 + smop/parse.py | 24 ++++++++++++------------ 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/smop/Makefile b/smop/Makefile index 5bfeffc6..5c857340 100644 --- a/smop/Makefile +++ b/smop/Makefile @@ -7,13 +7,13 @@ SCRIPTS = $(OCTAVE)/scripts CYTHON = cython PYTHON = python -XFILES = -x inputParser.m,quadgk.m,quadl.m,triplequad.m,dblquad.m +XFILES = -x inputParser.m,quadgk.m,quadl.m,triplequad.m,dblquad.m,reallog.m,nthroot.m,factorial.m FLAGS = V = 2.7 ALL = \*.m factorial.py: factorial.m - $(PYTHON) main.py -v -loctave $^ -o- $(FLAGS) + $(PYTHON) main.py -v $^ $(FLAGS) all: mybench.py $(PYTHON) -c "import mybench ; mybench.mybench()" diff --git a/smop/backend.py b/smop/backend.py index ac143381..7eabf95a 100644 --- a/smop/backend.py +++ b/smop/backend.py @@ -126,15 +126,17 @@ def _backend(self,level=0): @extend(node.expr) def _backend(self,level=0): if self.op in ("!","not"): # ??? - return "not %s" % self.args[0] + return "not %s" % self.args[0]._backend() if self.op in ("&","and"): return "logical_and(%s)" % self.args._backend() if self.op == "&&": - return " and ".join(t._backend() for t in self.args) + return "%s and %s" % (self.args[0]._backend(), + self.args[1]._backend()) if self.op in ("|","or"): return "logical_or(%s)" % self.args._backend() if self.op == "||": - return " or ".join(t._backend() for t in self.args) + return "%s or %s" % (self.args[0]._backend(), + self.args[1]._backend()) if self.op == '@': # FIXMEj return self.args[0]._backend() @@ -221,9 +223,11 @@ def _backend(self,level=0): s = """ @function -def {0}({1}): - nargin = {0}.nargin""".format(self.ident._backend(), - self.args._backend()) +def {0}({1}{2}nargin=-1): +""".format(self.ident._backend(), + self.args._backend(), + "," if self.args else "") + return s @extend(node.funcall) diff --git a/smop/core.py b/smop/core.py index 1b6f5990..baf2eeaa 100644 --- a/smop/core.py +++ b/smop/core.py @@ -709,9 +709,9 @@ def print_usage(): raise Exception def function(f): - def helper(*args): - helper.func_dict["nargin"]=len(args) - return f(*args) + def helper(*args,**kwargs): + kwargs["nargin"]=len(args) + return f(*args,**kwargs) return helper def error(s): diff --git a/smop/main.py b/smop/main.py index 6121fa71..be0e8033 100644 --- a/smop/main.py +++ b/smop/main.py @@ -48,6 +48,7 @@ def main(): if options.verbose: print options.filename buf = open(options.filename).read().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: diff --git a/smop/parse.py b/smop/parse.py index 98b93fc6..5fb64389 100644 --- a/smop/parse.py +++ b/smop/parse.py @@ -1,5 +1,5 @@ # SMOP compiler -- Simple Matlab/Octave to Python compiler -# Copyright 2011-2014 Victor Leikehman +# Copyright 2011-2016 Victor Leikehman import copy import pprint @@ -80,7 +80,7 @@ def p_end_function(p): """ p[0] = p[1] p[0].append(node.return_stmt(ret=ret_expr)) - p[0].append(node.comment_stmt("\nif __name__ == '__main__':")) + p[0].append(node.comment_stmt("\nif __name__ == '__main__':\n pass")) @exceptions @@ -857,16 +857,16 @@ def parse(buf): else: return None # p[i] is a func decl - for j in range(i+1,len(p)): - if i < j and isinstance(p[j], node.func_stmt): - p.insert(j,node.return_stmt(ret=p[i].ret)) - j += 1 - i = j - p.append(node.return_stmt(ret=p[i].ret)) - - if "2" in options.debug: - for i,pi in enumerate(p): - print i,pi.__class__.__name__,str(pi)[:50] +# for j in range(i+1,len(p)): +# if i < j and isinstance(p[j], node.func_stmt): +# p.insert(j,node.return_stmt(ret=p[i].ret)) +# j += 1 +# i = j +# p.append(node.return_stmt(ret=p[i].ret)) +# +# if "2" in options.debug: +# for i,pi in enumerate(p): +# print i,pi.__class__.__name__,str(pi)[:50] return p