Refactored all of the desugaring code to no longer depend on memory being initialized to 0. Everything is about 12% faster now.

This commit is contained in:
Dan Hirsch 2013-05-25 03:35:42 +02:00
parent d71215d494
commit ec404ca8fe
36 changed files with 411 additions and 417 deletions

View file

@ -25,18 +25,19 @@ static bool opt_isValidCF(void *env) {
static HParsedToken* reshape_optional(const HParseResult *p) {
assert(p->ast);
assert(p->ast->token_type == TT_SEQUENCE);
assert(p->ast->seq->used > 0);
HParsedToken *res = p->ast->seq->elements[0];
if(res)
return res;
if (p->ast->seq->used > 0) {
HParsedToken *res = p->ast->seq->elements[0];
if(res)
return res;
}
HParsedToken *ret = h_arena_malloc(p->arena, sizeof(HParsedToken));
ret->token_type = TT_NONE;
return ret;
}
static HCFChoice* desugar_optional(HAllocator *mm__, void *env) {
static void desugar_optional(HAllocator *mm__, HCFStack *stk__, void *env) {
HParser *p = (HParser*) env;
/* optional(A) =>
@ -44,28 +45,14 @@ static HCFChoice* desugar_optional(HAllocator *mm__, void *env) {
-> \epsilon
*/
HCFChoice *ret = h_new(HCFChoice, 1);
HCFChoice *a = h_desugar(mm__, p);
HCFChoice *eps = desugar_epsilon(mm__, NULL);
ret->type = HCF_CHOICE;
ret->seq = h_new(HCFSequence*, 3); /* enough for 2 productions */
ret->seq[0] = h_new(HCFSequence, 1);
ret->seq[0]->items = h_new(HCFChoice*, 2);
ret->seq[0]->items[0] = a;
ret->seq[0]->items[1] = NULL;
ret->seq[1] = h_new(HCFSequence, 1);
ret->seq[1]->items = h_new(HCFChoice*, 2);
ret->seq[1]->items[0] = eps;
ret->seq[1]->items[1] = NULL;
ret->seq[2] = NULL;
ret->reshape = reshape_optional;
return ret;
HCFS_BEGIN_CHOICE() {
HCFS_BEGIN_SEQ() {
HCFS_DESUGAR(p);
} HCFS_END_SEQ();
HCFS_BEGIN_SEQ() {
} HCFS_END_SEQ();
HCFS_THIS_CHOICE->reshape = reshape_optional;
} HCFS_END_CHOICE();
}
static bool h_svm_action_optional(HArena *arena, HSVMContext *ctx, void *env) {