aboutsummaryrefslogtreecommitdiff
path: root/lpvm.c
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2016-07-01 10:09:27 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2016-07-01 10:09:27 -0300
commit5ff36d597a47830ea0e266cce253cfda2cf43348 (patch)
treefb141d517b569d2346a975e8172e3ddf25a33133 /lpvm.c
parent81f7263ac1274aa604c154a199c70a54909de6e0 (diff)
downloadlpeglabel-5ff36d597a47830ea0e266cce253cfda2cf43348.tar.gz
lpeglabel-5ff36d597a47830ea0e266cce253cfda2cf43348.tar.bz2
lpeglabel-5ff36d597a47830ea0e266cce253cfda2cf43348.zip
Updating code given that lpeglabel.T only throws one label
Diffstat (limited to 'lpvm.c')
-rw-r--r--lpvm.c46
1 files changed, 16 insertions, 30 deletions
diff --git a/lpvm.c b/lpvm.c
index a57c4b3..4d9797c 100644
--- a/lpvm.c
+++ b/lpvm.c
@@ -31,17 +31,6 @@ static void setlabelfail(Labelset *ls) {
31 loopset(i, ls->cs[i] = 0); 31 loopset(i, ls->cs[i] = 0);
32 ls->cs[IDXLFAIL] = 1; 32 ls->cs[IDXLFAIL] = 1;
33} 33}
34
35static void clearandsetlabel(Labelset *ls, byte b) {
36 loopset(i, ls->cs[i] = 0);
37 setlabel(ls->cs, b);
38}
39
40
41static int cs_disjoint (const Charset *cs1, const Charset *cs2) {
42 loopset(i, if ((cs1->cs[i] & cs2->cs[i]) != 0) return 0;)
43 return 1;
44}
45/* labeled failure end */ 34/* labeled failure end */
46 35
47/* 36/*
@@ -54,8 +43,8 @@ static int cs_disjoint (const Charset *cs1, const Charset *cs2) {
54typedef struct Stack { 43typedef struct Stack {
55 const char *s; /* saved position (or NULL for calls) */ 44 const char *s; /* saved position (or NULL for calls) */
56 const Instruction *p; /* next instruction */ 45 const Instruction *p; /* next instruction */
57 int caplevel;
58 const Labelset *ls; /* labeled failure */ 46 const Labelset *ls; /* labeled failure */
47 int caplevel;
59} Stack; 48} Stack;
60 49
61 50
@@ -165,7 +154,7 @@ static int removedyncap (lua_State *L, Capture *capture,
165** Opcode interpreter 154** Opcode interpreter
166*/ 155*/
167const char *match (lua_State *L, const char *o, const char *s, const char *e, 156const char *match (lua_State *L, const char *o, const char *s, const char *e,
168 Instruction *op, Capture *capture, int ptop, Labelset *labelf, const char **sfail) { /* labeled failure */ 157 Instruction *op, Capture *capture, int ptop, byte *labelf, const char **sfail) { /* labeled failure */
169 Stack stackbase[INITBACK]; 158 Stack stackbase[INITBACK];
170 Stack *stacklimit = stackbase + INITBACK; 159 Stack *stacklimit = stackbase + INITBACK;
171 Stack *stack = stackbase; /* point to first empty slot in stack */ 160 Stack *stack = stackbase; /* point to first empty slot in stack */
@@ -177,14 +166,13 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e,
177 setlabelfail(&lsfail); 166 setlabelfail(&lsfail);
178 stack->p = &giveup; stack->s = s; stack->caplevel = 0; stack++; 167 stack->p = &giveup; stack->s = s; stack->caplevel = 0; stack++;
179 lua_pushlightuserdata(L, stackbase); 168 lua_pushlightuserdata(L, stackbase);
180 /*printf("match: %s\n", s);*/ 169 for (;;) {
181 for (;;) { 170#if defined(DEBUG)
182/*#if defined(DEBUG)*/ 171 printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ",
183 /* printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ",
184 s, stack - getstackbase(L, ptop), ndyncap, captop); 172 s, stack - getstackbase(L, ptop), ndyncap, captop);
185 printinst(op, p);*/ 173 printinst(op, p);
186 /*printcaplist(capture, capture + captop);*/ 174 printcaplist(capture, capture + captop);
187/*#endif*/ 175#endif
188 assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); 176 assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop);
189 switch ((Opcode)p->i.code) { 177 switch ((Opcode)p->i.code) {
190 case IEnd: { 178 case IEnd: {
@@ -205,7 +193,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e,
205 case IAny: { 193 case IAny: {
206 if (s < e) { p++; s++; } 194 if (s < e) { p++; s++; }
207 else { 195 else {
208 setlabelfail(labelf); /* labeled failure */ 196 *labelf = LFAIL; /* labeled failure */
209 *sfail = s; 197 *sfail = s;
210 goto fail; 198 goto fail;
211 } 199 }
@@ -219,7 +207,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e,
219 case IChar: { 207 case IChar: {
220 if ((byte)*s == p->i.aux && s < e) { p++; s++; } 208 if ((byte)*s == p->i.aux && s < e) { p++; s++; }
221 else { 209 else {
222 setlabelfail(labelf); /* labeled failure */ 210 *labelf = LFAIL; /* labeled failure */
223 *sfail = s; 211 *sfail = s;
224 goto fail; 212 goto fail;
225 } 213 }
@@ -235,7 +223,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e,
235 if (testchar((p+1)->buff, c) && s < e) 223 if (testchar((p+1)->buff, c) && s < e)
236 { p += CHARSETINSTSIZE; s++; } 224 { p += CHARSETINSTSIZE; s++; }
237 else { 225 else {
238 setlabelfail(labelf); /* labeled failure */ 226 *labelf = LFAIL; /* labeled failure */
239 *sfail = s; 227 *sfail = s;
240 goto fail; 228 goto fail;
241 } 229 }
@@ -251,7 +239,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e,
251 case IBehind: { 239 case IBehind: {
252 int n = p->i.aux; 240 int n = p->i.aux;
253 if (n > s - o) { 241 if (n > s - o) {
254 setlabelfail(labelf); /* labeled failure */ 242 *labelf = LFAIL; /* labeled failure */
255 *sfail = s; 243 *sfail = s;
256 goto fail; 244 goto fail;
257 } 245 }
@@ -322,9 +310,8 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e,
322 continue; 310 continue;
323 } 311 }
324 case IThrow: { /* labeled failure */ 312 case IThrow: { /* labeled failure */
325 clearandsetlabel(labelf, p->i.aux); 313 *labelf = p->i.aux;
326 *sfail = s; 314 *sfail = s;
327 /*printf("s = %s, sfail = %s\n", s, *sfail);*/
328 goto fail; 315 goto fail;
329 } 316 }
330 case IFailTwice: 317 case IFailTwice:
@@ -332,14 +319,13 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e,
332 stack--; 319 stack--;
333 /* go through */ 320 /* go through */
334 case IFail: 321 case IFail:
335 setlabelfail(labelf); /* labeled failure */ 322 *labelf = LFAIL; /* labeled failure */
336 *sfail = s; 323 *sfail = s;
337 fail: { /* pattern failed: try to backtrack */ 324 fail: { /* pattern failed: try to backtrack */
338 do { /* remove pending calls */ 325 do { /* remove pending calls */
339 assert(stack > getstackbase(L, ptop)); 326 assert(stack > getstackbase(L, ptop));
340 s = (--stack)->s; 327 s = (--stack)->s;
341 /*printf("s = %s, disj = %d\n", s, cs_disjoint(stack->ls, labelf));*/ 328 } while (s == NULL || (stack->p != &giveup && !testlabel(stack->ls->cs, *labelf)));
342 } while (s == NULL || (stack->p != &giveup && cs_disjoint(stack->ls, labelf)));
343 if (ndyncap > 0) /* is there matchtime captures? */ 329 if (ndyncap > 0) /* is there matchtime captures? */
344 ndyncap -= removedyncap(L, capture, stack->caplevel, captop); 330 ndyncap -= removedyncap(L, capture, stack->caplevel, captop);
345 captop = stack->caplevel; 331 captop = stack->caplevel;
@@ -356,7 +342,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e,
356 fr -= rem; /* 'rem' items were popped from Lua stack */ 342 fr -= rem; /* 'rem' items were popped from Lua stack */
357 res = resdyncaptures(L, fr, s - o, e - o); /* get result */ 343 res = resdyncaptures(L, fr, s - o, e - o); /* get result */
358 if (res == -1) { /* fail? */ 344 if (res == -1) { /* fail? */
359 setlabelfail(labelf); /* labeled failure */ 345 *labelf = LFAIL; /* labeled failure */
360 *sfail = (const char *) s; /* TODO: ??? */ 346 *sfail = (const char *) s; /* TODO: ??? */
361 goto fail; 347 goto fail;
362 } 348 }