/* SI 413 Fall 2012
 * Lab 05
 * Recursive descent parser for pat language.
 * YOUR NAME HERE
 * PARTNER'S NAME HERE
 */

#include "pat.hpp"
#include <cstdlib>
using namespace std;

// These are the prototypes for your recursive descent function.
// Of course you need to implement these below!
void stmt();
void seq(); void seqtail();
void catseq(); void cattail();
void opseq(); void optail();
void atom();

int next = -1; // store next token

// These are the colored output streams to make things all pretty.
colorout resout(1, 'u');
colorout errout(2, 'r');

// --------- Helper Functions

// Gets called if we see an unexpected token
void perror(const char* funcname) { 
  errout << "Parse error in " << funcname << endl; 
  exit(2); 
}

// Returns the next token without actually consuming it
int peek() { 
  if (next == -1) next = yylex(); 
  return next; 
}

// Consumes the next token, and makes sure it matches the argument.
// If so, the semantic value of the token (a string) is returned.
string match(int tok) { 
  if (tok == peek()) {
    next = -1; 
    return yylval;
  }
  else {
    errout << "Token match error" << endl;
    exit(3);
  }
}

// ---------------- Recursive descent functions

void stmt() {
  // YOU NEED TO FILL THIS IN!
  perror("stmt (Not implemented yet!)");
}

//-- Main method
int main(void) {
  // This checks whether the output is a terminal.
  bool tty = isatty(0) && isatty(2);

  while(true) {
    if (tty) cerr << "> " << flush;
    if (peek() == 0) break;
    stmt();
    if (tty) cerr << "(Parse OK)" << endl;
  }

  if (tty) cerr << "Goodbye" << endl;
  return 0;
}
