Mercurial > hg > Members > shinya > pyrect
changeset 83:68cefeb3bee1
experimentation, use table-lookup at first state's transition.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 10 Nov 2010 22:48:33 +0900 |
parents | 3dc381c90870 |
children | f5c4193913a1 |
files | pyrect/translator/grep_translator.py pyrect/translator/template/grep.c |
diffstat | 2 files changed, 20 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/pyrect/translator/grep_translator.py Wed Nov 10 22:00:24 2010 +0900 +++ b/pyrect/translator/grep_translator.py Wed Nov 10 22:48:33 2010 +0900 @@ -43,21 +43,20 @@ def emit_initialization(self): self.emit("#include <stdio.h>") - self.emit("#define GREP grep") self.emit("#include <stdlib.h>") self.emit("#include <sys/mman.h>") self.emit("#include <sys/types.h>") self.emit("#include <sys/stat.h>") self.emit("#include <fcntl.h>") self.emit("#include <unistd.h>") - self.emit("#include <string.h>") + self.emit("#include <string.h>", 2) self.emit("typedef unsigned char UCHAR;") - self.emit("typedef unsigned char *UCHARP;") + self.emit("typedef unsigned char *UCHARP;", 2) self.emit('void reject(%s);' % self.interface) - self.emit("void matcher(%s);" % self.interface, 2) - self.emit('void accept(%s);' % self.interface) + self.emit("void matcher(%s);" % self.interface) + self.emit('void accept(%s);' % self.interface, 2) key = None @@ -86,6 +85,15 @@ else: self.skip_boost = False + self.emiti("void (*first_state_tbl[256])(UCHARP, UCHARP, UCHARP) = {") + tbl = ["booster"] * 256 + for c, n in self.cg.map[self.cg.start].iteritems(): + if type(c) != Character: continue + else: tbl[c.char] = self.state_name(n) + tbl[0] = tbl[10] = tbl[13] = "reject" + self.emit(", ".join(tbl)) + self.emitd("};", 2) + grepsource = open(self.BASE_DIR + "/template/grep.c") self.emit(grepsource.read()) @@ -210,7 +218,7 @@ else: self.emit( "%s(%s);" % (self.state_name(self.cg.start), self.args)) self.emit( "return;") - self.emitd("}") + self.emitd("}", 2) return def emit_accept_state(self): @@ -258,6 +266,11 @@ self.emitd("}", 2) return + if cur_state == self.cg.start: + self.emit( "return first_state_tbl[*buf++](%s);" % self.args) + self.emitd("}", 2) + return + default = self.state_name(self.cg.start) for eol in self.eols: transition[eol] = "reject"
--- a/pyrect/translator/template/grep.c Wed Nov 10 22:00:24 2010 +0900 +++ b/pyrect/translator/template/grep.c Wed Nov 10 22:48:33 2010 +0900 @@ -53,7 +53,7 @@ fprintf(stderr, "can't open %s:", argv[i]); continue; } - GREP(argv[1], fd, argc > 3 ? argv[i] : NULL); + grep(argv[1], fd, argc > 3 ? argv[i] : NULL); close(fd); } }