diff regexParser/generateLoop.cc @ 312:c9ac6f06e706

add loop
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 09 Feb 2016 10:59:22 +0900
parents regexParser/generateSequentialSearch.cc@1188debbef10
children 90ccd94906c0
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/regexParser/generateLoop.cc	Tue Feb 09 10:59:22 2016 +0900
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "generateSequentialSearch.h"
+#include "CharClass.h"
+#include "subsetConstruction.h"
+
+
+static void
+generateState1(StatePtr state,long stateNum, bool accept, FILE *fp, TransitionGeneratorPtr tg) {
+
+    fprintf(fp,"static TSValue state%lx(TSValue tsv) {\n",stateNum);
+    if (accept && state->accept) {
+        fputs("    tsv=tsv.tg->stateMatch(tsv);\n",fp);
+    }
+    fputs("    if (tsv.buff.buffptr >= tsv.buff.buffend) return tsv;\n",fp);
+    CharClassWalkerPtr ccw = createCharClassWalker(state->cc);
+    if (hasNext(ccw)) fputs("    unsigned char c = *tsv.buff.buffptr++;\n",fp);
+    fputs("    if (0) ;\n",fp);
+    while (hasNext(ccw)) {
+        CharClassPtr cc = getNext(ccw);
+        unsigned long begin = cc->cond.range.begin;
+        unsigned long end = cc->cond.range.end;
+        BitVector bi = cc->nextState;
+        if (begin == end) {
+            fprintf(fp,"    else if (c=='%c') { tsv.current=(TState*)state%lx;}\n",(unsigned char)begin, bi.bitContainer);
+        } else {
+            fprintf(fp,"    else if (c<'%c')  { tsv=tsv.tg->stateSkip(tsv);tsv.current=(TState*) state0;}\n",(unsigned char)begin);
+            fprintf(fp,"    else if (c<='%c') { tsv.current=(TState*) state%lx;} \n",(unsigned char)end,  bi.bitContainer);
+        }
+    }
+    free(ccw);
+    fprintf(fp,"    else { tsv=tsv.tg->stateSkip(tsv); tsv.current=(TState*)state0; }\n    return tsv;\n");
+    fputs("}\n\n",fp);
+} 
+
+void
+exportStateLoop(TransitionGeneratorPtr tg) {
+    StatePtr state = tg->stateList;
+    FILE *fp = fopen("state.cc","w");
+    if (fp==NULL) {
+        perror("");
+        fprintf(stderr,"cannot write state.cc\n");
+        exit(1);
+    }
+    fprintf(fp,"static TSValue state%lx(TSValue tsv) ;\n",0);
+    for (;state;state = state->next) {
+        if (state->bitState.bitContainer!=1)  // state1 always unused but state0
+            fprintf(fp,"static TSValue state%lx(TSValue tsv);\n",state->bitState.bitContainer);
+    }
+    fputs("\n",fp);
+
+    fprintf(fp,
+"\n"
+"static TSValue \n"
+"stateLoop(TSValue tsv) {\n"
+"    tsv.current = (TState*)state0;\n"
+"    while(tsv.buff.buffptr < tsv.buff.buffend) {\n"
+"        TSValue (*next)(TSValue) = (TSValue (*)(TSValue))tsv.current;\n"
+"        tsv = next(tsv);\n"
+"    }\n"
+"    return tsv;\n"
+"}\n\n" );
+
+
+    // initial state must not accept empty string
+    generateState1(tg->stateList,0L,false,fp,tg);
+    for (state = tg->stateList;state;state = state->next) {
+        if (state->bitState.bitContainer!=1)  // state1 always unused but state0
+            generateState1(state,state->bitState.bitContainer,true,fp,tg);
+    }
+    fprintf(fp,"#define state0 stateLoop\n");
+    fclose(fp);
+}
+