ir_reader: Add a pattern matching system and use it everywhere.

Previously, the IR reader was riddled with code that:
1. Checked for the right number of list elements (via a linked list walk)
2. Retrieved references to each component (via ->next->next pointers)
3. Downcasted as necessary to make sure that each sub-component was the
   right type (i.e. symbol, int, list).
4. Checking that the tag (i.e. "declare") was correct.

This was all very ad-hoc and a bit ugly.  Error checking had to be done
at both steps 1, 3, and 4.  Most code didn't even check the tag, relying
on the caller to do so.  Not all callers did.

The new pattern matching module performs the whole process in a single
straightforward function call, resulting in shorter, more readable code.

Unfortunately, MSVC does not support C99-style anonymous arrays, so the
pattern must be declared outside of the match call.
This commit is contained in:
Kenneth Graunke
2010-11-03 12:47:06 -07:00
parent 407184fe08
commit daeb0c646e
3 changed files with 297 additions and 311 deletions

View File

@@ -139,3 +139,49 @@ void s_list::print()
printf(")");
}
// --------------------------------------------------
bool
s_pattern::match(s_expression *expr)
{
switch (type)
{
case EXPR: *p_expr = expr; break;
case LIST: if (expr->is_list()) *p_list = (s_list *) expr; break;
case SYMBOL: if (expr->is_symbol()) *p_symbol = (s_symbol *) expr; break;
case NUMBER: if (expr->is_number()) *p_number = (s_number *) expr; break;
case INT: if (expr->is_int()) *p_int = (s_int *) expr; break;
case STRING:
s_symbol *sym = SX_AS_SYMBOL(expr);
if (sym != NULL && strcmp(sym->value(), literal) == 0)
return true;
return false;
};
return *p_expr == expr;
}
bool
s_match(s_expression *top, unsigned n, s_pattern *pattern, bool partial)
{
s_list *list = SX_AS_LIST(top);
if (list == NULL)
return false;
unsigned i = 0;
foreach_iter(exec_list_iterator, it, list->subexpressions) {
if (i >= n)
return partial; /* More actual items than the pattern expected */
s_expression *expr = (s_expression *) it.get();
if (expr == NULL || !pattern[i].match(expr))
return false;
i++;
}
if (i < n)
return false; /* Less actual items than the pattern expected */
return true;
}