Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlei committed Nov 11, 2016
1 parent 9a959c2 commit 6b9cfe2
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 79 deletions.
17 changes: 17 additions & 0 deletions INSTALL
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
smop -- matlab to python compiler

Installation with pip
=====================

Install pip

sudo apt-get install python-pip

Install smop

pip install smop --user

or

sudo pip install smop

Done.

Installation with easy_install
==============================

Expand Down
20 changes: 7 additions & 13 deletions smop/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def del elif else except
exec finally for from global
if import in is lambda
not or pass print raise
return try while
return try while with
Data Float Int Numeric Oxphys
array close float int input
Expand Down Expand Up @@ -217,15 +217,15 @@ def _backend(self,level=0):
def _backend(self,level=0):
self.args.append(node.ident("*args"))
self.args.append(node.ident("**kwargs"))

s = """
@function
def %s(%s):
nargin = sys._getframe(1).f_locals["nargin"]
varargin = sys._getframe(1).f_locals["varargin"]
nargout = sys._getframe(1).f_locals["nargout"]
varargin = %s.varargin
nargin = %s.nargin
""" % (self.ident._backend(),
self.args._backend())
self.args._backend(),
self.ident._backend(),
self.ident._backend())
return s

@extend(node.funcall)
Expand Down Expand Up @@ -364,13 +364,7 @@ def _backend(self,level=0):

@extend(node.string)
def _backend(self,level=0):
if "\n" in self.value:
return '"""%s"""' % self.value
if options.strings == "C":
return 'r"%s"' % self.value.replace('"',r'\"')
if options.strings == "F":
return "r'%s'" % self.value.replace("'",r"''")
assert 0
return "'%s'" % str(self.value).encode("string_escape")

@extend(node.sub)
def _backend(self,level=0):
Expand Down
7 changes: 3 additions & 4 deletions smop/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,9 @@ def print_usage():

def function(f):
def helper(*args,**kwargs):
nargout = kwargs and kwargs["nargout"]
varargin = cellarray(args)
nargin=len(args)
return f(*args)
helper.nargin = len(args)
helper.varargin = cellarray(args)
return f(*args,**kwargs)
return helper

def error(s):
Expand Down
17 changes: 4 additions & 13 deletions smop/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,10 @@ def new():
id = r"[a-zA-Z_][a-zA-Z_0-9]*"

def unescape(s):
"""
ffd52d5fc5
"""
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
if s[0] == "'":
return s[1:-1].replace("''","'")
else:
return s[1:-1].decode("string_escape")

@TOKEN(mos)
def t_afterkeyword_STRING(t):
Expand Down
7 changes: 4 additions & 3 deletions smop/libscripts/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ MFILES = $(shell find $(SCRIPTS) -name \*.m -type f)
CYTHON = cython
PYTHON = python
CAT = cat
XFILES = -x white.m,summer.m,spring.m,rainbow.m,prism.m,pink.m,ocean.m,lines.m,jet.m
XFILES = -x white.m,summer.m,spring.m,rainbow.m,prism.m,pink.m,ocean.m,lines.m,jet.m,__gnuplot_open_stream__.m
FLAGS =
SMOP = $(PYTHON) ../main.py

V = 2.7

all: $(PYFILES)
grep Autogen libscripts.py | wc -l
$(CAT) *.py > libscripts.py

clean:
rm -f libscripts.py *.pyc *.py

%.py: %.m
$(SMOP) $^ $(FLAGS) $(XFILES)
($(PYTHON) $@ && $(CAT) $@ >> libscripts.py)
$(PYTHON) $@

.DELETE_ON_ERROR:

30 changes: 17 additions & 13 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,tarfile
import sys,cPickle,os #,tarfile
import getopt,re
import lexer
import parse
Expand All @@ -13,18 +13,22 @@
import callgraph
import networkx as nx
import pickle
import readline
try:
import readline
except:
import readline as pyreadline

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 (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 @@ -41,7 +45,7 @@ def main():
if options.link:
print >> fp, "from %s import *" % options.link
print >> fp, "#", options.filename

for i, options.filename in enumerate(options.filelist):
try:
if not options.filename.endswith((".m")):
Expand All @@ -52,15 +56,15 @@ def main():
print "\tExcluded: '%s'" % options.filename
continue
if options.verbose:
print options.filename
if tar:
buf = tar.extractfile(options.filename).read()
else:
buf = open(options.filename).read()
print i, options.filename
#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
#assert None not in stmt_list
if not stmt_list:
return
if not options.no_resolve:
Expand Down
5 changes: 2 additions & 3 deletions smop/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class syntax_error(error):
pass

precedence = (
("left", "COMMA"),
("right", "COMMA"),
("right","DOTDIVEQ","DOTMULEQ","EQ","EXPEQ",
"MULEQ","MINUSEQ","DIVEQ","PLUSEQ","OREQ","ANDEQ"),
("nonassoc","HANDLE"),
Expand Down Expand Up @@ -435,8 +435,7 @@ def p_ident_init_opt(p):
else:
p[0] = p[1]
if len(p) == 2:
# p[0].init = node.ident(name="None") if p[0].name != "varargin" else ""
pass
p[0].init = node.ident(name="None")
else:
p[0].init = p[3]

Expand Down
Loading

0 comments on commit 6b9cfe2

Please sign in to comment.