static void buildgrammar (lua_State *L, TTree *grammar, int frule, int n) { int i; TTree *nd = sib1(grammar); /* auxiliary pointer to traverse the tree */ for (i = 0; i < n; i++) { /* add each rule into new tree */ int ridx = frule + 2*i + 1; /* index of i-th rule */ int rulesize; TTree *rn = gettree(L, ridx, &rulesize); nd->tag = TRule; nd->key = 0; nd->cap = i; /* rule number */ nd->u.ps = rulesize + 1; /* point to next rule */ memcpy(sib1(nd), rn, rulesize * sizeof(TTree)); /* copy rule */ mergektable(L, ridx, sib1(nd)); /* merge its ktable into new one */ nd = sib2(nd); /* move to next rule */ } nd->tag = TTrue; /* finish list of rules */ }
/* ** Create a new 'ktable' to the pattern at the top of the stack, adding ** all elements from pattern 'p' (if not 0) plus element 'idx' to it. ** Return index of new element. */ static int addtonewktable (lua_State *L, int p, int idx) { newktable(L, 1); if (p) mergektable(L, p, NULL); return addtoktable(L, idx); }