#ifndef __FILTER_C__
#define __FILTER_C__ 1

#include "filter.h"

static IMPAST *separate_perms(IMPAST *Program) {
  IMPAST *new, *parent;

  if (Program == NULL) return Program;
  if (Program->opcode == OP_LINE) return Program; // No perms present!

  new = malloc(sizeof(IMPAST *) + sizeof(IAST_PERMS));
  new->perms.opcode = ASTOP_PERMS;
  new->perms.perms = Program;
  parent = NULL;
  while (Program && Program->opcode != OP_LINE) {
    // Everything before the first LINE directive must be perms.
    // We'll bundle all the perms into a group so that when we
    // output the program we can replace the entire perms
    // declarations with a #include if we want to.
    parent = Program;
    Program = Program->next_icode;
  }
  parent->next_icode = NULL; // end of perms
  new->next_icode = Program;
  return new;
}

IMPAST *filter_icode(IMPAST *Program) {
  IMPAST *new;

  // Apply a sequence of individual rewrites...

  new = separate_perms(Program);
  
  return new;
}

#endif // __FILTER_C__

 
