Mercurial > hg > Members > shinya > pyrect
changeset 94:492f543703d5
improve jump-table initialize (when enable table-lookup). C99's range-initialize is awesome.
author | Ryoma SHINYA <shinya@firefly.cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 06 Dec 2010 04:48:06 +0900 |
parents | 82a8232625c3 |
children | f42e37e2fe93 |
files | pyrect/translator/goto_grep_translator.py pyrect/translator/grep_translator.py |
diffstat | 2 files changed, 13 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/pyrect/translator/goto_grep_translator.py Mon Dec 06 04:46:38 2010 +0900 +++ b/pyrect/translator/goto_grep_translator.py Mon Dec 06 04:48:06 2010 +0900 @@ -177,7 +177,7 @@ self.emit("buf = memchr(buf, %d, (end - buf));" % ord(key)) self.emit("if (buf == NULL) return;") emit_next() - self.demit("}", 1) + self.demit("", 1) return skip = dict() @@ -274,7 +274,7 @@ def emit_state(self, cur_state, transition): if self.filter_only: return - self.demiti("%s:" % self.state_name(cur_state)) + self.demiti("%s:;" % self.state_name(cur_state)) if cur_state in self.cg.accepts: self.emit( "buf--;") @@ -291,15 +291,17 @@ cur_state == default): if self.skip_boost and default == self.cg.start: default = "booster" - tbl = [default] * 256 + default = self.state_name(default) + tbl = dict() for eol in self.eols: tbl[eol.char] = "reject" for c, n in transition.iteritems(): tbl[c.char] = self.state_name(n) - self.emit( "static const void *%s_table[256] = {%s};" - % (self.state_name(cur_state), ", ".join(["&&"+x for x in tbl]))) - self.emit( "goto *%s_table[*buf++];" % (self.state_name(cur_state), self.args)) - self.demit("", 1) + self.emit( "static const void *%s_table[256] = {[0 ... 255] &&%s, %s};" + % (self.state_name(cur_state), default, + ", ".join(["[%d] &&%s" % (i, s) for (i, s) in tbl.items()]))) + self.emit( "goto *%s_table[*buf++];" % self.state_name(cur_state)) + self.demit("", 1) return for eol in self.eols:
--- a/pyrect/translator/grep_translator.py Mon Dec 06 04:46:38 2010 +0900 +++ b/pyrect/translator/grep_translator.py Mon Dec 06 04:48:06 2010 +0900 @@ -267,13 +267,14 @@ self.state_name(cur_state) == default): if self.skip_boost and default == self.state_name(self.cg.start): default = "booster" - tbl = [default] * 256 + tbl = dict() for eol in self.eols: tbl[eol.char] = "reject" for c, n in transition.iteritems(): tbl[c.char] = self.state_name(n) - self.emit( "static void (*%s_table[256])(UCHARP, UCHARP, UCHARP) = {%s};" - % (self.state_name(cur_state), ", ".join(tbl))) + self.emit( "static void (*%s_table[256])(UCHARP, UCHARP, UCHARP) = {[0 ... 255] = (void*)%s, %s};" + % (self.state_name(cur_state), default, + ", ".join("[%d] = %s" % (i, s) for (i, s) in tbl.items()))) self.emit( "return %s_table[*buf++](%s);" % (self.state_name(cur_state), self.args)) self.demit("}", 2) return