#ifndef INPUT_H_INCLUDED
#define INPUT_H_INCLUDED
FILE* Input;
char LookChar = '\0';
string Value;
int Token;
bool DoScan = true;// Determins whether or not we should scan
// All of the keywords used in the program
string Keyword[] = {"begin", "end", "main", "number",// Look up
"text", "for", "loop", "while", "until",// Look up
"do", "forever", "if", "else", "break",// Look up
"continue", "asm", "switch", "case",// Look up
"return", "datatype", "list", "void",
"length", "value", "sizeof", "from", "to", "noreturn",
"nohalt"};// Look up
#include "Tokens.h"
void ReadChar(void) {
LookChar = fgetc(Input);
if(LookChar == '\n') {// If we found a newline
LineNum++;// Increment the line that we are on
}// End of If statement
}
void GetChar(void) {
if(TempChar != ' ') {
LookChar = TempChar;
TempChar = ' ';
} else {
ReadChar();
if(LookChar == '/') {
TempChar = fgetc(Input);
if (TempChar == '*') {// Now, if the temporary character is a astric sign
LookChar = $BlocCom; // Show we found a block comment
TempChar = ' '; // Clear the temporary character
// Now, if the temporary character is a slash
} else if (TempChar == '/') {// Look up
LookChar = $LineCom; // Show we found a Line comment
TempChar = ' '; // Clear the temporary character
} else {// If we dont need the next character
fseek(Input,-1,SEEK_CUR);
TempChar = ' ';
}// End of If statement
}
}
}
// Skips all block comments found in the code
void SkipBlock(void) {// Look up
do {// Loop while we have not found the ending '/'
do {// Loop while we have not found the beggining '*'
GetChar();// Read the next character
// if there is a comment inside of another, allow it
if (LookChar == $BlocCom) SkipBlock();// Look up
} while (LookChar != '*');// Loop while we havent found a '*'
GetChar();// Get the next character
} while (LookChar != '/');// Loop until the end of the comment is found
GetChar();// Skip the ending '/'
}// End of SkipBLock(void)
// Skips all line comments found in the code
void SkipLine(void) {// Look up
do {// Loop until we found the newline character
ReadChar();// Skip the next character
} while ((LookChar != '\n')&&(LookChar != EOF));// Until we we find the end of the line
ReadChar();// Skip that character
}// End of SkipLine(void)
void SkipWhite(void) {
while((LookChar == ' ') || (LookChar == '\t') ||
(LookChar == '\n') || (LookChar == '\r') ||
(LookChar == $BlocCom) ||
(LookChar == $LineCom) || (LookChar == EOF)) {
if(LookChar == $BlocCom) {// If we found a block comment
SkipBlock();// Skip the block comment
} else if(LookChar == $LineCom) {// Or if we found a line comment
SkipLine();// Skip the line comment
} else if(LookChar == EOF) {
break;
} else {
GetChar();
}
}
}
void Match(string c) {
if(LookChar == c.c_str()[0]) {
GetChar();
SkipWhite();
} else Expected("\""+c+"\"",ERROR_MATCH);
}
void MatchStr(string Str) {
if(Value != Str) {
Expected("\""+Str+"\"",ERROR_MATCH);
}
}
string GetName(bool Skip = true) {
if(!isalpha(LookChar)) Expected("Name",ERROR_ID);
Value = "";
while(isalpha(LookChar) || isdigit(LookChar) || (LookChar=='_')) {
Value += LookChar;
ReadChar();
}
// if (Lookup(Value) == 0) {// Look up
// Token = $identifier;// The identifier is not a reserved word
// } else {// If it is a reserved word
// Token = Lookup(Value);// The keyword is not an identifier
// }// End of If statement
if(Skip) SkipWhite();
return Value;
}
string GetNum(void) {
if((!isdigit(LookChar))&&(LookChar!='-')) Expected("Number",ERROR_NUMBER);
Value = "";
if(LookChar=='-') {
Value += LookChar;
GetChar();
}
while(isdigit(LookChar)) {
Value += LookChar;
GetChar();
}
if(LookChar == '.') {
Value += LookChar;
GetChar();
if(!isdigit(LookChar)) Expected("Valid Decimal Number",ERROR_NUMBER);
while(isdigit(LookChar)) {
Value += LookChar;
GetChar();
}
}
Token = $DT_number;
SkipWhite();
return Value;
}
// Get a string from the file
string GetString(void) {// Look up
Value = "";// Clear the value
int Curr_Line = LineNum;// Save the line number
if(LookChar == '\"') {// If we started the string
ReadChar();// Get the next character
} else {// If we did not start the string
Abort("Invalid string",ERROR_STR);// Report the error
}// End of IF statement
Value = "\"";// Add the begining of the string
while(LookChar != '\"') {// While we are still in the string
switch(LookChar) {// Process the next character
case EOF:// If we hit the end of the file
LineNum=Curr_Line;// reset the line number
Abort("You did not end your string",ERROR_STR);// Report the error
break;// Leave the switch statement
case '\n':// IF we found a newline
SkipWhite();// Skip the whitespace
break;// Leave the switch statement
default:// If we found a regular character
Value+=LookChar;// add it to the string
GetChar();// Read the next character
}// End of Switch statement
}// End of While loop
Value+=LookChar;// Add the ending of the string
Match("\"");// Make sure we ended it
Token = $DT_string;// Show that we found a string
return Value;// Return the string
}// End of GetString(void)
#endif // INPUT_H_INCLUDED