int CcSyntaxSymSet_New(CcSyntaxSymSet_t * self, const CcBitArray_t * s) { CcBitArray_t * cur; for (cur = self->start; cur < self->used; ++cur) if (CcBitArray_Equal(cur, s)) return cur - self->start; if (self->used >= self->last) { cur = CcRealloc(self->start, sizeof(CcBitArray_t) * (self->last - self->start) * 2); self->used = cur + (self->used - self->start); self->last = cur + (self->last - self->start) * 2; self->start = cur; } cur = self->used++; CcBitArray_Clone(cur, s); return cur - self->start; }
static void ATest(FILE * fp, CcBitArray_t * ba0, CcBitArray_t * ba1) { CcBitArray_t ba2; int idx, cnt, numbits = CcBitArray_getCount(ba0); CcBitArray_Dump(ba0, fp, "BitArray 0: ", "\n"); CcBitArray_Dump(ba1, fp, "BitArray 1: ", "\n"); cnt = 0; for (idx = 0; idx < numbits; ++idx) if (CcBitArray_Get(ba0, idx) && CcBitArray_Get(ba1, idx)) cnt = 1; COCO_ASSERT((CcBitArray_Intersect(ba0, ba1) == cnt)); COCO_ASSERT((CcBitArray_Clone(&ba2, ba0))); COCO_ASSERT((CcBitArray_Equal(ba0, &ba2))); cnt = 0; for (idx = 0; idx < numbits; ++idx) { COCO_ASSERT(CcBitArray_Get(ba0, idx) == CcBitArray_Get(&ba2, idx)); if (CcBitArray_Get(ba0, idx)) ++cnt; } COCO_ASSERT((CcBitArray_Elements(ba0) == cnt)); COCO_ASSERT((CcBitArray_Or(&ba2, ba1) == 0)); CcBitArray_Dump(&ba2, fp, "Or: ", "\n"); for (idx = 0; idx < numbits; ++idx) COCO_ASSERT((CcBitArray_Get(ba0, idx) || CcBitArray_Get(ba1, idx)) == CcBitArray_Get(&ba2, idx)); CcBitArray_Destruct(&ba2); COCO_ASSERT((CcBitArray_Clone(&ba2, ba0))); COCO_ASSERT((CcBitArray_And(&ba2, ba1) == 0)); CcBitArray_Dump(&ba2, fp, "And: ", "\n"); for (idx = 0; idx < numbits; ++idx) COCO_ASSERT((CcBitArray_Get(ba0, idx) && CcBitArray_Get(ba1, idx)) == CcBitArray_Get(&ba2, idx)); CcBitArray_Destruct(&ba2); COCO_ASSERT((CcBitArray_Clone(&ba2, ba0))); CcBitArray_Subtract(&ba2, ba1); CcBitArray_Dump(&ba2, fp, "Subtract: ", "\n"); for (idx = 0; idx < numbits; ++idx) COCO_ASSERT((CcBitArray_Get(ba0, idx) && !CcBitArray_Get(ba1, idx)) == CcBitArray_Get(&ba2, idx)); COCO_ASSERT(!CcBitArray_Intersect(ba1, &ba2)); CcBitArray_Destruct(&ba2); fprintf(fp, "\n"); }
static void SCSOS_GenCode(CcCSharpBaseOutputScheme_t * self, CcOutput_t * output, CcNode_t * p, const CcBitArray_t * IsChecked) { int err; CcsBool_t equal, useSwitch; int index; CcNode_t * p2; CcBitArray_t s1, s2, isChecked; CcNodeNT_t * pnt; CcNodeT_t * pt; CcNodeWT_t * pwt; CcNodeSEM_t * psem; CcNodeSYNC_t * psync; CcSyntax_t * syntax = &self->base.globals->syntax; CcArrayList_t * terminals = &self->base.globals->symtab.terminals; CcBitArray_Clone(&isChecked, IsChecked); while (p != NULL) { if (p->base.type == node_nt) { pnt = (CcNodeNT_t *)p; if (pnt->pos) { CcPrintfIL(output, "%s(%s);", pnt->sym->name, pnt->pos->text); } else { CcPrintfIL(output, "%s();", pnt->sym->name); } } else if (p->base.type == node_t) { pt = (CcNodeT_t *)p; if (CcBitArray_Get(&isChecked, pt->sym->kind)) CcPrintfIL(output, "Get();"); else CcPrintfIL(output, "Expect(%d);", pt->sym->kind); } else if (p->base.type == node_wt) { pwt = (CcNodeWT_t *)p; CcSyntax_Expected(syntax, &s1, p->next, self->curSy); CcBitArray_Or(&s1, syntax->allSyncSets); CcPrintfIL(output, "ExpectWeak(%d, %d);", pwt->sym->kind, CcSyntaxSymSet_New(&self->symSet, &s1)); CcBitArray_Destruct(&s1); } else if (p->base.type == node_any) { CcPrintfIL(output, "Get();"); } else if (p->base.type == node_eps) { } else if (p->base.type == node_rslv) { } else if (p->base.type == node_sem) { psem = (CcNodeSEM_t *)p; CcSource(output, psem->pos); } else if (p->base.type == node_sync) { psync = (CcNodeSYNC_t *)p; err = CcSyntax_SyncError(syntax, self->curSy); CcBitArray_Clone(&s1, psync->set); SCSOS_GenCond(self, output, "while (!(", ")) {", &s1, p); output->indent += 4; CcPrintfIL(output, "SynErr(%d); Get();", err); output->indent -= 4; CcPrintfIL(output, "}"); CcBitArray_Destruct(&s1); } else if (p->base.type == node_alt) { CcSyntax_First(syntax, &s1, p); equal = CcBitArray_Equal(&s1, &isChecked); CcBitArray_Destruct(&s1); useSwitch = SCSOS_UseSwitch(self, p); if (useSwitch) CcPrintfIL(output, "switch (la.kind) {"); p2 = p; while (p2 != NULL) { CcSyntax_Expected(syntax, &s1, p2->sub, self->curSy); if (useSwitch) { CcPrintfI(output, ""); for (index = 0; index < terminals->Count; ++index) if (CcBitArray_Get(&s1, index)) CcPrintf(output, "case %d: ", index); CcPrintfL(output,"{"); } else if (p2 == p) { SCSOS_GenCond(self, output, "if (", ") {", &s1, p2->sub); } else if (p2->down == NULL && equal) { CcPrintfIL(output, "} else {"); } else { SCSOS_GenCond(self, output, "} else if (", ") {", &s1, p2->sub); } CcBitArray_Or(&s1, &isChecked); output->indent += 4; SCSOS_GenCode(self, output, p2->sub, &s1); if (useSwitch) CcPrintfIL(output, "break;"); output->indent -= 4; if (useSwitch) CcPrintfIL(output, "}"); p2 = p2->down; CcBitArray_Destruct(&s1); } if (equal) { CcPrintfIL(output, "}"); } else { err = CcSyntax_AltError(syntax, self->curSy); if (useSwitch) { CcPrintfIL(output, "default: SynErr(%d); break;", err); CcPrintfIL(output, "}"); } else { CcPrintfIL(output, "} else SynErr(%d);", err); } } } else if (p->base.type == node_iter) { p2 = p->sub; if (p2->base.type == node_wt) { CcSyntax_Expected(syntax, &s1, p2->next, self->curSy); CcSyntax_Expected(syntax, &s2, p->next, self->curSy); CcPrintfIL(output, "while (WeakSeparator(%d, %d, %d)) {", ((CcNodeWT_t *)p2)->sym->kind, CcSyntaxSymSet_New(&self->symSet, &s1), CcSyntaxSymSet_New(&self->symSet, &s2)); CcBitArray_Destruct(&s1); CcBitArray_Destruct(&s2); CcBitArray(&s1, terminals->Count); if (p2->up || p2->next == NULL) p2 = NULL; else p2 = p2->next; } else { CcSyntax_First(syntax, &s1, p2); SCSOS_GenCond(self, output, "while (", ") {", &s1, p2); } output->indent += 4; SCSOS_GenCode(self, output, p2, &s1); output->indent -= 4; CcPrintfIL(output, "}"); CcBitArray_Destruct(&s1); } else if (p->base.type == node_opt) { CcSyntax_First(syntax, &s1, p->sub); SCSOS_GenCond(self, output, "if (", ") {", &s1, p->sub); output->indent += 4; SCSOS_GenCode(self, output, p->sub, &s1); output->indent -= 4; CcPrintfIL(output, "}"); CcBitArray_Destruct(&s1); } if (p->base.type != node_eps && p->base.type != node_sem && p->base.type != node_sync) CcBitArray_SetAll(&isChecked, FALSE); if (p->up) break; p = p->next; } CcBitArray_Destruct(&isChecked); }