Artifact [db722c77bd]

Not logged in

Artifact db722c77bd22e7eb464d1848e8d654b733701d60:


# SMOP compiler -- Simple Matlab/Octave to Python compiler
# Copyright 2011 Victor Leikehman
#
# This file is part of SMOP compiler.
# 
# SMOP compiler is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# SMOP compiler is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with SMOP compiler.  If not, see <http://www.gnu.org/licenses/>.

import sys
import getopt
import networkx as nx
import smop
from smop.compiler import parse,resolve,backend

def usage():
    print "Such and such"

def main():
    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], "ho:vV", 
                                       ["help",
                                        "output=",
                                        "verbose",
                                    "version"])
    except getopt.GetoptError, err:
        # print help information and exit:
        print str(err) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)

    output = None
    verbose = False
    for o, a in opts:
        if o in ("-v", "--verbose"):
            verbose = True
        elif o in ("-V", "--version"):
            print "SMOP compiler version",smop.__version__
            sys.exit()
        elif o in ("-h", "--help"):
            usage()
            sys.exit()
        elif o in ("-o", "--output"):
            output = a
        else:
            assert False, "unhandled option"

    if not args:
        defs = nx.DiGraph() if nx else {}
        symtab = {}
        exec "from smop.runtime import *"
        while 1:
            try:
                inp = raw_input("? ")
                if not inp:
                    continue
                #import pdb;pdb.set_trace()
                inp += '\n'
                t = parse.parse(inp)
                #print t
                resolve.resolve(t,symtab,defs)
                #print defs
                cmd = t.python()
                exec cmd.strip()
            except EOFError:
                return
            except Exception as e:
                print e
    func_list = []
    for f in args:
        if verbose:
            print f
        funcs = parse.fparse(f)
        if verbose:
            for func in funcs:
                print "\t",func.head.ident.name
        func_list.append(funcs)

    if not output:
        output = "a.py"
    fp = open(output,"w")
    print >> fp, "import numpy\nfrom smop.runtime import *"
                
    for tree in func_list: 
        # Each function is resolved separately
        defs = resolve.resolve(tree)
        print >> fp,tree.python()

if __name__ == "__main__":
    main()