Skip to content

Commit

Permalink
misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlei committed Aug 15, 2016
1 parent 52561dc commit f61f49d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
4 changes: 2 additions & 2 deletions smop/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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()"

Expand Down
16 changes: 10 additions & 6 deletions smop/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions smop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions smop/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 12 additions & 12 deletions smop/parse.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit f61f49d

Please sign in to comment.