Skip to content

Instantly share code, notes, and snippets.

@ampli
Created October 14, 2018 04:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ampli/01a2b82fbe23e3231bcfcb9d50cbd4bd to your computer and use it in GitHub Desktop.
Save ampli/01a2b82fbe23e3231bcfcb9d50cbd4bd to your computer and use it in GitHub Desktop.
#define EXP_STACKSIZE 1000
static void push_elist(E_list *s[], int *i, E_list *e)
{
assert(*i < EXP_STACKSIZE);
s[(*i)++] = e;
}
static E_list *pop_elist(E_list *s[], int *i)
{
return s[--(*i)];
}
static E_list *next_E_list(E_list *s[], int *si)
{
E_list *p = pop_elist(s, si);
Exp *t = p->e;
if (t->type != CONNECTOR_type)
{
for (E_list *l = t->u.l; l != NULL; l = l->next)
push_elist(s, si, l);
}
return p;
}
void free_Exp(Exp *e)
{
if (e == NULL) return;
int si;
E_list *s[EXP_STACKSIZE];
/* Make the expression look like E_list. */
s[0] = &(E_list){ NULL, e };
si = 1;
while (si > 0)
{
E_list *p = next_E_list(s, &si);
free(p->e);
if (e != p->e) free(p);
}
}
void free_E_list(E_list *l)
{
if (l == NULL) return;
int si;
E_list *s[EXP_STACKSIZE];
s[0] = l;
si = 1;
while (si > 0)
{
E_list *p = next_E_list(s, &si);
free(p->e);
free(p);
}
}
int size_of_expression(Exp * e)
{
if (e == NULL) return 0;
E_list *s[1000];
int si;
int size = 0;
/* Make the expression look like E_list. */
s[0] = &(E_list){ NULL, e };
si = 1;
while (si > 0)
{
E_list *p = next_E_list(s, &si);
if (p->e->type == CONNECTOR_type) size++;
}
return size;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment