view src/jitgrep.py @ 14:55684cb51347

add LICENSE
author Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
date Sun, 04 Jul 2010 11:06:41 +0900
parents 41391400fe68
children 104ee9208b17
line wrap: on
line source

#!/usr/bin/env python

import sys
import os
import re
from optparse import OptionParser
from grep_translator import GREPTranslator
from dfareg import Regexp, CallGraph

def main(argv):
    myusage = "%prog [--time] [--debug] [--cc=compiler] [-Olevel] regexp [file ..]"
    psr = OptionParser(usage=myusage)

    optimize = ""
    debug = False
    time = False
    argv_iter = argv
    for args in argv_iter:
       matchp = re.match("^-O[0-9]?$", args)
       if matchp:
           optimize = matchp.group(0)
           argv.remove(optimize)
       elif args == "--debug":
           argv.remove("--debug")
           debug = True
       elif args == "--time":
           time = True;
           argv.remove("--time")

    psr.add_option("--cc", action="store", type="string", dest="compiler", default="gcc", help="string", metavar="FILE")
    (opts, args) = psr.parse_args(argv)

    if debug: print("option", opts)
    if debug: print("args", args)

    string = argv[1]
    reg = Regexp(string)
    dfacg = CallGraph(reg.dfa)
    tje = GREPTranslator(string, dfacg)

    srcpath = "/tmp/jitgrep_emit.c"
    binpath = "/tmp/jitgrep_emit"

    tmpsrc = open(srcpath, 'w')
    tje.translate(tmpsrc)
    tmpsrc.close()

    cmd = " ".join([opts.compiler, optimize, srcpath, "-o", binpath])
    if debug:
        print("compile command", cmd)
    else:
        os.system(cmd)

    # print("argc=" + str(len(argv)))
    # print(argv)

    if len(argv) == 2:
        while True:
            try:
                os.system(binpath + ' ' + raw_input())
            except (KeyboardInterrupt, EOFError):
                break
    else:
        cmd = binpath + ' dummy_option ' + ' '.join(argv[2:])
        if debug:
            print("exec command", cmd)
        else:
            os.system(cmd)

    if not debug:
        os.remove(srcpath)
        os.remove(binpath)

if __name__ == '__main__': main(sys.argv)