Skip to content

Commit

Permalink
bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlei committed Aug 21, 2016
1 parent a379717 commit cb236c9
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 26 deletions.
10 changes: 8 additions & 2 deletions smop/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down
21 changes: 11 additions & 10 deletions smop/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
17 changes: 12 additions & 5 deletions smop/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
25 changes: 18 additions & 7 deletions smop/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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()
2 changes: 1 addition & 1 deletion smop/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion smop/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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__])
Expand Down
10 changes: 10 additions & 0 deletions smop/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,26 @@ 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:
p[0] = node.expr_list()
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):
Expand Down

0 comments on commit cb236c9

Please sign in to comment.