changeset 34:50b10929be29

change compile-method to full-source-compile.
author Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp>
date Sun, 11 Jul 2010 22:50:37 +0900
parents e9e90c006760
children 74a40ad4fa14
files pyrect/cbc_translator.py pyrect/grep_translator.py pyrect/jitgrep.py pyrect/template/grep.c
diffstat 4 files changed, 74 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/pyrect/cbc_translator.py	Sat Jul 10 15:37:41 2010 +0900
+++ b/pyrect/cbc_translator.py	Sun Jul 11 22:50:37 2010 +0900
@@ -1,1 +1,31 @@
+#!/usr/bin/env python
 
+from dfareg import Regexp, CallGraph
+from c_translator import CTranslator
+
+class CbCTranslateExeption(Exception):
+    pass
+
+class CbCTranslator(CTranslator):
+    """
+    CbCTranslator
+    >>> string = \"(A|B)*C\"
+    >>> reg = Regexp(string)
+    >>> dfacg = CallGraph(reg.dfa)
+    >>> ct = CbCTranslator(string, dfacg)
+    >>> ct.translate()
+    >>> ct.debug = True
+    >>> ct.translate()
+    """
+    def __init__(self, regexp, cg):
+        if cg.type == "NFA": raise CbCTranslateExeption("can't translate CbC from NFA")
+        CTranslator.__init__(self, regexp, cg)
+        self.funType = '__code '
+        self.callType = 'goto '
+        self.breakStatement = ''
+
+def test():
+    import doctest
+    doctest.testmod()
+
+if __name__ == '__main__' : test()
--- a/pyrect/grep_translator.py	Sat Jul 10 15:37:41 2010 +0900
+++ b/pyrect/grep_translator.py	Sun Jul 11 22:50:37 2010 +0900
@@ -25,6 +25,7 @@
         self.callType = 'return '
         self.breakStatement = ''
         self.begline = False
+        self.bufsize = 1024
 
     def emit_accept_state(self):
         self.emit ("""
@@ -39,12 +40,21 @@
 }\n""" % self.funType)
 
     def emit_initialization(self):
+        self.emit("#include <stdio.h>\n")
+        self.emit("#include <stdlib.h>\n")
+        self.emit("#include <string.h>\n\n")
+
+        self.emit("#define LINEBUFSIZE 1024\n")
+        self.emit("#define READBUFSIZE %d\n\n" % (0 if self.bufsize <= 0 else self.bufsize))
+        self.emit("char readbuf[%d];\n\n" % (0 if self.bufsize <= 0 else self.bufsize))
+
         self.emit("%sDFA(char* s);\n" % (self.funType))
-        self.emit("extern int grepmain(int argc, char* argv[]);\n")
         for state in self.cg.map.iterkeys():
             self.emit(self.funType + self.modify_state_name(state) + "(char* s);\n")
         self.emit(self.funType + 'accept(char* s);\n')
         self.emit(self.funType + 'reject(char* s);\n')
+        grepsource = open("template/grep.c")
+        self.emit(grepsource.read())
 
     def emit_filter(self):
         pass
--- a/pyrect/jitgrep.py	Sat Jul 10 15:37:41 2010 +0900
+++ b/pyrect/jitgrep.py	Sun Jul 11 22:50:37 2010 +0900
@@ -9,28 +9,29 @@
 from dfareg import Regexp, CallGraph
 
 def main(argv):
-    myusage = "%prog [--build-lib] [--time] [--debug] [--cc=compiler] [-Olevel] regexp [file ..]"
+    myusage = """%prog [--buf-size=size] [--dump]
+                  [--time] [--debug] [--cc=compiler]
+                  [-Olevel] regexp [file..] [--out=file]"""
     psr = OptionParser(usage=myusage)
 
     redirect = ""
     srcpath = "/tmp/jitgrep_dfa.c"
     binpath = "/tmp/jitgrep"
-    libgrep = os.path.dirname(__file__) + "/template/libgrep.so"
 
     psr.add_option("--cc", action="store", type="string", dest="cc", default="gcc", metavar="FILE",
                    help="Choose compiler (default is gcc).")
+    psr.add_option("--buf-size=size", action="store", type="string", dest="bufsize", default="1M" , help="Set read-buffer size (e.x. 1024, 1024K, 2M)")
     psr.add_option("--CFLAGS", action="store", type="string", dest="cflags", default="-O3 -fomit-frame-pointer", help="Print compile/matching time.")
     psr.add_option("--time", action="store_true", dest="time", default=False, help="Print compile/matching time.")
     psr.add_option("--debug", action="store_true", dest="debug", default=False, help="Dump commands, not evalute matching (except interactive mode).")
-    psr.add_option("--build-lib", action="store_true", dest="rebuild", default=False, help="Building libgrep.so, although which exists.")
+    psr.add_option("--dump", action="store_true", dest="dump", default=False, help="Dump generated grep-source.")
     psr.add_option("--out", action="store", type="string", dest="out", default="", metavar="FILE", help="Output file.")
 
     (opts, args) = psr.parse_args(argv)
-    libgrep = ".".join([libgrep, opts.cc])
 
     if len(args) < 2:
         psr.print_usage()
-        return
+        exit(0)
 
     if opts.cc == "cbc":
         cbc = True
@@ -48,27 +49,37 @@
     else:
         begline = False
 
-    if (opts.time) : start_time = time.time()
+    try:
+        if opts.bufsize[-1] == 'K':
+            bufsize = int(opts.bufsize[:-1]) * 2**10
+        elif opts.bufsize[-1] == 'M':
+            bufsize = int(opts.bufsize[:-1]) * 2**20
+        else:
+            bufsize = int(opts.bufsize)
+    except ValueError:
+        psr.print_usage()
+        exit(0)
+
+    if opts.time : start_time = time.time()
     reg = Regexp(string)
     dfacg = CallGraph(reg.dfa)
     grept = GREPTranslator(string, dfacg)
     grept.begline = begline
+    grept.bufsize = bufsize
 
-    tmpsrc = open(srcpath, 'w')
-    grept.translate(tmpsrc)
-    tmpsrc.close()
+    if opts.dump:
+        grept.translate()
+        exit(0)
+    else:
+        tmpsrc = open(srcpath, 'w')
+        grept.translate(tmpsrc)
+        tmpsrc.close()
+
     if (opts.time):
         end_time = time.time()
         print("Translation: " + str(end_time - start_time) + " Sec.")
 
-    if opts.rebuild or not os.path.exists(libgrep):
-        cmd = " ".join([opts.cc, opts.cflags, "-c -fPIC -shared template/grep.c -o", libgrep])
-        if opts.debug:
-            print cmd
-        else:
-            os.system(cmd)
-
-    cmd = " ".join([opts.cc, opts.cflags, srcpath, libgrep, "-o", binpath])
+    cmd = " ".join([opts.cc, opts.cflags, srcpath, "-o", binpath])
     if opts.debug:
         print("compile command", cmd)
     else:
@@ -104,6 +115,7 @@
 
     if not opts.debug:
         #os.remove(srcpath)
-        os.remove(binpath)
+        #os.remove(binpath)
+        pass
 
 if __name__ == '__main__': main(sys.argv)
--- a/pyrect/template/grep.c	Sat Jul 10 15:37:41 2010 +0900
+++ b/pyrect/template/grep.c	Sun Jul 11 22:50:37 2010 +0900
@@ -1,17 +1,9 @@
 /* Excerpted from 'The Practice of Programming' */
 /* by Brian W. Kernighan and Rob Pike */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define BUFSIZE 1024
-
-extern int match(char *regexp, char *text);
-
 int grep(char * regexp, FILE *f, char *name) {
   int n, nmatch;
-  char buf[BUFSIZE];
+  char buf[LINEBUFSIZE];
   nmatch = 0;
   while (fgets(buf, sizeof buf, f) != NULL) {
     n = strlen(buf);
@@ -46,6 +38,8 @@
         fprintf(stderr, "can't open %s:", argv[i]);
         continue;
       }
+      if (READBUFSIZE > 0)
+        setvbuf(f, readbuf, _IOFBF, READBUFSIZE);
       if (grep(argv[1], f, argc > 3 ? argv[i] : NULL) > 0)
         nmatch++;
       fclose(f);