Implement question mark desugaring

This commit is contained in:
2024-10-26 16:49:18 +01:00
parent 7e7a7f1cb7
commit ac4e9911d8
2 changed files with 61 additions and 0 deletions

View File

@@ -53,12 +53,35 @@ static void desugar_plus(parse_term_t *term)
term->subexpr.contents = alternatives;
}
static void desugar_qmark(parse_term_t *term)
{
parse_sequence_t *alternatives = malloc(2 * sizeof(parse_sequence_t));
alternatives[0].count = alternatives[0].capacity = 1;
alternatives[0].contents = malloc(sizeof(parse_term_t));
alternatives[0].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
alternatives[0].contents[0].type = PARSE_TERM_EMPTY;
alternatives[1].count = alternatives[0].capacity = 1;
alternatives[1].contents = malloc(sizeof(parse_term_t));
memcpy(&alternatives[1].contents[0], term, sizeof(parse_term_t));
alternatives[1].contents[0].quantifier = PARSE_QUANTIFIER_NONE;
term->quantifier = PARSE_QUANTIFIER_NONE;
term->type = PARSE_TERM_SUBEXPR;
term->subexpr.count = term->subexpr.capacity = 2;
term->subexpr.contents = alternatives;
}
static void desugar_term(parse_term_t *term)
{
switch (term->quantifier) {
case PARSE_QUANTIFIER_PLUS:
desugar_plus(term);
break;
case PARSE_QUANTIFIER_QMARK:
desugar_qmark(term);
break;
case PARSE_QUANTIFIER_NONE:
case PARSE_QUANTIFIER_STAR:
break;