Commit b41e1b3a authored by Ronald Charles Moore's avatar Ronald Charles Moore
Browse files

added better error checking

parent 411d0123
......@@ -6,7 +6,8 @@
PROGS := interpreter
TESTFILES := testInput.txt testOutput.txt
TESTINPUT := goodTest.input badTest.input
# Uncomment only one of the next two lines (choose your c++ compiler)
# CC=g++
CC := clang++
......@@ -36,8 +37,14 @@ clean:
test: $(PROGS) $(TESTFILES)
# Running interpreter in test mode
-./interpreter testInput.txt 2>&1 >tmp.txt
-./interpreter goodTest.input >tmp.txt 2>&1
# Checking output -- no news is good news!
@diff -qs goodTest.reference tmp.txt
-$(RM) tmp.txt
# Running interpreter in test mode
-./interpreter badTest.input 2>&1 >tmp.txt
# Checking output -- no news is good news!
diff testOutput.txt tmp.txt
-rm tmp.txt
@diff -qs badTest.reference tmp.txt
-$(RM) tmp.txt
42?
42^2
(42))
((42
(42 / 0)
(42 / (1-1))
42 +/- 3.14159
0:42?
INTERPRETER: 42
ERROR on line 0, column 3 : Bad Character(s) found.
42?--^
1:42^2
INTERPRETER: 42
ERROR on line 1, column 3 : Bad Character(s) found.
42^2--^
1:42^2
INTERPRETER: 2
2:(42))
INTERPRETER: 42
2:(42))
ERROR on line 2, column 5 : Expected Left Parenthesis or number
(42))----^
INTERPRETER: 0
3:((42
ERROR on line 4, column 1 : Expected Right Parenthesis
(42 / 0)^
ERROR on line 4, column 3 : Expected Right Parenthesis
(42 / 0)--^
ERROR on line 4, column 8 : Division by zero!
(42 / 0)-------^
INTERPRETER: 0
5:(42 / (1-1))
ERROR on line 5, column 12 : Division by zero!
(42 / (1-1))-----------^
ERROR on line 6, column 2 : Expected Right Parenthesis
42 +/- 3.14159-^
ERROR on line 6, column 5 : Expected Left Parenthesis or number
42 +/- 3.14159----^
INTERPRETER: -3.14159
End Of File!
......@@ -47,17 +47,6 @@ static int currentColumnNumber = 0;
static Token next_token; // again with the global variables...
static numberType currentNumber; // = zero....
static void printErrorMsg( const std::string Error )
{
std::cout << "ERROR on line " << currentLineNumber
<< ", column " << currentColumnNumber << " : "
<< Error << std::endl;
std::cout << currentLine;
for ( int col = 0; col < currentColumnNumber-1; col++ )
std::cout << '-';
std::cout << '^' << std::endl;
} // end printErrorMsg
// The Lexer
// ==========
static bool skippedWhiteSpace( ) { // return true if not at EOF, i.e. if skipped
......@@ -120,6 +109,18 @@ static Token gettok( ) {
return bad_tok;
} // end gettok
static void printErrorMsg( const std::string Error )
{
std::cout << "ERROR on line " << currentLineNumber
<< ", column " << currentColumnNumber << " : "
<< Error << std::endl;
std::cout << currentLine;
for ( int col = 0; col < currentColumnNumber-1; col++ )
std::cout << '-';
std::cout << '^' << std::endl;
next_token = gettok( ); // skip over badness -- move on!
} // end printErrorMsg
// PARSING!!!
// ===========
//
......@@ -223,10 +224,16 @@ int main( int argc, char **argv ) {
// else if 1 == argc ....
std::string fileName( argv[1] );
if ( "-" != fileName ) {
static std::ifstream ifs( fileName );
static std::ifstream ifs( fileName, std::ifstream::in );
if ( ! ifs.good( ) ) {
std::cerr << "Could not open file " << fileName
<< '\n';
return( -2 ); // return NOT OK status
}; // else if ifs.good() ...
input = &ifs;
}
// Prime the pump!
next_token = gettok( );
......@@ -236,6 +243,8 @@ int main( int argc, char **argv ) {
<< currentLine << std::endl;
numberType value = E( );
std::cout << "INTERPRETER: " << value << std::endl;
if ( bad_tok == next_token )
printErrorMsg( "Bad Character(s) found." );
};
std::cout << "End Of File!" << std::endl;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment