Menu

[73c16a]: / CPP Converter / Input.h  Maximize  Restore  History

Download this file

174 lines (159 with data), 6.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#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
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.