Skip to content

Commit

Permalink
Removed trailing underlines
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlei committed Oct 19, 2014
1 parent b75a8d2 commit ed0d004
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 145 deletions.
23 changes: 17 additions & 6 deletions smop/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def _backend(self,level=0):
self.args[2]._backend(),
self.args[1]._backend())
if self.op == ":":
return "arange_(%s)" % self.args._backend()
return "arange(%s)" % self.args._backend()

if self.op == "end":
# if self.args:
Expand All @@ -160,11 +160,12 @@ def _backend(self,level=0):
return "end()"

if self.op == ".":
#import pdb; pdb.set_trace()
try:
is_parens = self.args[1].op != "parens"
is_parens = self.args[1].op == "parens"
except:
is_parens = False
if is_parens:
if not is_parens:
return "%s%s" % (self.args[0]._backend(),
self.args[1]._backend())
else:
Expand Down Expand Up @@ -228,9 +229,19 @@ def _backend(self,level=0):
# s = ''
#if self.args.__class__ is node.funcall:
# self.args.nargout = self.nargout
if (self.ret.__class__ is node.ident and
if self.ret.__class__ is node.expr and self.ret.op == "." :
try:
if self.ret.args[1].op == 'parens':
s = "setattr(%s,%s,%s)" % (self.ret.args[0]._backend(),
self.ret.args[1].args[0]._backend(),
self.args._backend())
except:
s = "%s%s = copy(%s)" % (self.ret.args[0]._backend(),
self.ret.args[1]._backend(),
self.args._backend())
elif (self.ret.__class__ is node.ident and
self.args.__class__ is node.ident):
s = "%s=copy_(%s)" % (self.ret._backend(),
s = "%s=copy(%s)" % (self.ret._backend(),
self.args._backend())
else:
s = "%s=%s" % (self.ret._backend(),
Expand Down Expand Up @@ -298,7 +309,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

@extend(node.stmt_list)
def _backend(self,level=0):
Expand Down
6 changes: 3 additions & 3 deletions smop/go.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from runtime import *

def main():
ai = matlabarray(zeros_(10,10,dtype=int),dtype=int)
af = copy_(ai)
ai = matlabarray(zeros (10,10,dtype=int),dtype=int)
af = copy(ai)

ai[1,1]=2
ai[2,2]=3
Expand All @@ -20,7 +20,7 @@ def main():
af[10,10]=5

t0 = time.clock()
mv = a.solver_(ai,af,0)
mv = a.solver(ai,af,0)
t1 = time.clock()
print t1-t0
print mv.shape
Expand Down
16 changes: 13 additions & 3 deletions smop/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lexer,parse,resolve,backend,options,node,graphviz
import networkx as nx
import readline
from runtime import *
#from version import __version__
__version__ = version.__version__

Expand Down Expand Up @@ -101,7 +102,7 @@ def main():
print "? for help"
while 1:
try:
buf = raw_input("=>> ")
buf = raw_input("octave: ")
if not buf:
continue
while buf[-1] == "\\":
Expand All @@ -118,9 +119,18 @@ def main():
t = parse.parse(buf if buf[-1]=='\n' else buf+'\n')
if not t:
continue
print "t=", repr(t)
print 60*"-"
resolve.resolve(t,symtab)
_ = backend.backend(t)
exec _
print "t=", repr(t)
print 60*"-"
print "symtab:",symtab
s = backend.backend(t)
print "python:",s.strip()
try:
print eval(s)
except SyntaxError:
exec s
except EOFError:
return
except Exception as ex:
Expand Down
10 changes: 5 additions & 5 deletions smop/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ def resolve(t, symtab=None, fp=None, func_name=None):
G.node[n]["label"] = "%s\\n%s" % (n, u.props)

for u in node.postorder(t):
if u.__class__ is node.func_decl:
u.ident.name += "_"
elif u.__class__ is node.funcall:
#if u.__class__ is node.func_decl:
# u.ident.name += "_"
if u.__class__ is node.funcall:
try:
if u.func_expr.props in "UR": # upd,ref
u.__class__ = node.arrayref
else:
u.func_expr.name += "_"
#else:
# u.func_expr.name += "_"
except:
pass

Expand Down
86 changes: 43 additions & 43 deletions smop/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,50 +381,50 @@ def __str__(self):
raise NotImplementedError


def abs_(a):
def abs(a):
"""
Unless the argument is already as subclass of ndarray,
convert the argument to ndarray, then apply numpy.abs
"""
return np.abs(np.asanyarray(a))

def arange_(start,stop,step=1,**kwargs):
def arange(start,stop,step=1,**kwargs):
"""
>>> a=arange_(1,10) # 1:10
>>> size_(a)
>>> a=arange(1,10) # 1:10
>>> size(a)
matlabarray([[ 1, 10]])
"""
return matlabarray(np.arange(start,
stop+1,
step,
**kwargs).reshape(1,-1),**kwargs)

def ceil_(a):
def ceil(a):
"""
Unless the argument is already as subclass of ndarray,
convert the argument to ndarray, then apply numpy.ceil
"""
return np.ceil(np.asanyarray(a))

def cell_(*args):
def cell(*args):
if len(args) == 1:
args += args
return cellarray(np.zeros(args,dtype=object,order="F"))

def copy_(a):
def copy(a):
return matlabarray(np.asanyarray(a).copy(order="F"))

def disp_(*args):
def disp(*args):
print (args)

false = False

def false_(*args):
def false(*args):
if len(args) == 1:
args += args
return np.zeros(args,dtype=bool,order="F")

def find_(a,n=None,d=None,nargout=1):
def find(a,n=None,d=None,nargout=1):
if d:
raise NotImplementedError

Expand All @@ -446,21 +446,21 @@ def find_(a,n=None,d=None,nargout=1):
matlabarray((j+1).reshape(-1,1)))
raise NotImplementedError

def floor_(a):
return np.floor_(np.asanyarray(a))
def floor(a):
return np.floor(np.asanyarray(a))

def fopen_(*args):
def fopen(*args):
try:
fp = open(*args)
assert fp != -1
return fp
except:
return -1

def fullfile_(*args):
def fullfile(*args):
return os.path.join(*args)

def intersect_(a,b,nargout=1):
def intersect(a,b,nargout=1):
if nargout == 1:
c = sorted(set(a) & set(b))
if isinstance(a,str):
Expand All @@ -473,40 +473,40 @@ def intersect_(a,b,nargout=1):
return np.array(c)
raise NotImplementedError

#def inf_(*args):
#def inf(*args):
# t = np.empty(np.prod(args))
# t[:] = np.inf
# return t.reshape(args)
#
#Inf_ = inf_
#
#def int_(t):
#def int(t):
# return np.int(t)
#
#def int32_(t):
#def int32(t):
# return np.int(t)
#

def iscellstr_(a):
def iscellstr(a):
return isinstance(a,cellarray) and all(isinstance(t,str) for t in a)

def ischar_(a):
def ischar(a):
try:
return a.dtype == "|S1"
except AttributeError:
return False

def isempty_(a):
def isempty(a):
try:
return 0 in np.asarray(a).shape
except AttributeError:
return False

def isequal_(a,b):
def isequal(a,b):
return np.array_equal(np.asanyarray(a),
np.asanyarray(b))

def isscalar_(a):
def isscalar(a):
"""np.isscalar returns True if a.__class__ is a scalar
type (i.e., int, and also immutable containers str and
tuple, but not list.) Our requirements are different"""
Expand All @@ -515,47 +515,47 @@ def isscalar_(a):
except AttributeError:
return np.isscalar(a)

def length_(a):
def length(a):
try:
return max(np.asarray(a).shape)
except ValueError:
return 1

try:
def load_(a):
def load(a):
return loadmat(a) # FIXME
except:
pass

def max_(a, d=0, nargout=0):
def max(a, d=0, nargout=0):
if d or nargout:
raise NotImplementedError
return np.amax(a)

def min_(a, d=0, nargout=0):
def min(a, d=0, nargout=0):
if d or nargout:
raise NotImplementedError
return np.amin(a)

def mod_(a,b):
def mod(a,b):
try:
return a % b
except ZeroDivisionError:
return a
def ndims_(a):
def ndims(a):
return np.asarray(a).ndim

def numel_(a):
def numel(a):
return np.asarray(a).size

def ones_(*args,**kwargs):
def ones(*args,**kwargs):
if not args:
return 1.0
if len(args) == 1:
args += args
return matlabarray(np.ones(args,order="F",**kwargs))

def rand_(*args,**kwargs):
def rand(*args,**kwargs):
if not args:
return np.random.rand()
if len(args) == 1:
Expand All @@ -565,18 +565,18 @@ def rand_(*args,**kwargs):
except:
pass

def ravel_(a):
def ravel(a):
return np.asanyarray(a).reshape(-1,1)

def round_(a):
def round(a):
return np.round(np.asanyarray(a))

def rows_(a):
def rows(a):
return np.asarray(a).shape[0]

def size_(a, b=0, nargout=1):
def size(a, b=0, nargout=1):
"""
>>> size_(zeros_(3,3)) + 1
>>> size(zeros(3,3)) + 1
matlabarray([[4, 4]])
"""
s = np.asarray(a).shape
Expand All @@ -591,34 +591,34 @@ def size_(a, b=0, nargout=1):
except IndexError:
return 1

def strread_(s, format="", nargout=1):
def strread(s, format="", nargout=1):
if format == "":
a = [float(x) for x in s.split()]
return tuple(a) if nargout > 1 else np.asanyarray([a])
raise NotImplementedError

def strrep_(a,b,c):
def strrep(a,b,c):
if isinstance(a,str):
return a.replace(b,c)
raise NotImplementedError # cell arrays

def sum_(a, dim=None):
def sum(a, dim=None):
if dim is None:
return np.asanyarray(a).sum()
else:
return np.asanyarray(a).sum(dim-1)

def toupper_(a):
def toupper(a):
return char(str(a.data).upper())

true = True

def true_(*args):
def true(*args):
if len(args) == 1:
args += args
return matlabarray(np.ones(args,dtype=bool,order="F"))

def zeros_(*args,**kwargs):
def zeros(*args,**kwargs):
if not args:
return 0.0
if len(args) == 1:
Expand Down
Loading

0 comments on commit ed0d004

Please sign in to comment.