#include <stdio.h>
#include <stdlib.h>
#include "bpp.h"
#include "scanner.h"


int FileStart = TRUE;
int LineNumber = 1;


///////////////////
// Match the token against a string
void Match(char *string)
{
	GetToken();
	if(strcmp(curToken->text,string) != 0)
	{


	}
}

///////////////////
// Go to the next line
void NextLine(int linetype)
{		
	while(curToken->type != linetype && curToken->type != TK_NEWLINE)
		GetToken();
}

///////////////////
// Restart the file scanner
void RestartScanner(FILE *file)
{
	fseek(file,0,SEEK_SET);
	yyrestart(file);
	FileStart = TRUE;
	LineNumber = 1;
	
	curToken->type = -1;	
	strcpy(curToken->text,"");	
	curToken->VarType = VAR_DEFAULT;
	LookToken->type = -1;
	strcpy(LookToken->text,"");
	LookToken->VarType = VAR_DEFAULT;
}

///////////////////
// Get a token
void GetToken(void)
{
	int temptype = VAR_DEFAULT;

	if(FileStart)
	{		
		// Current Token		
		curToken->type = yylex();
		strcpy(curToken->text,yytext);
		curToken->VarType = VarType;
		curToken->flags = 0;

		// Look Ahead
		LookToken->type = yylex();
		strcpy(LookToken->text,yytext);
		LookToken->VarType = VarType;
		LookToken->flags = 0;
	}
	else
	{
		memcpy(curToken,LookToken,sizeof(token_t));

		// Look Ahead
		LookToken->type = yylex();		
		strcpy(LookToken->text,yytext);		
		LookToken->VarType = VarType;
		LookToken->flags = 0;
	}

	if(curToken->type == TK_NEWLINE || curToken->type == TK_COMMENT)
		LineNumber++;	

	FileStart = FALSE;

	if(curToken->type == TK_CONTLINE)
	{
		GetToken();
		GetToken();
	}
}
