From b41e1b3a6f972de9ae4a6016b2e766492a834fa4 Mon Sep 17 00:00:00 2001
From: "Prof. Ronald Moore" <ronald.moore@h-da.de>
Date: Fri, 1 May 2020 23:17:03 +0200
Subject: [PATCH] added better error checking

---
 .../cplusplus/interpreter/Makefile            | 15 ++++++---
 .../cplusplus/interpreter/badTest.input       |  8 +++++
 .../cplusplus/interpreter/badTest.reference   | 33 +++++++++++++++++++
 .../{testInput.txt => goodTest.input}         |  0
 .../{testOutput.txt => goodTest.reference}    |  0
 .../cplusplus/interpreter/interpreter.cpp     | 33 ++++++++++++-------
 6 files changed, 73 insertions(+), 16 deletions(-)
 create mode 100644 recursiveDescentParsers/cplusplus/interpreter/badTest.input
 create mode 100644 recursiveDescentParsers/cplusplus/interpreter/badTest.reference
 rename recursiveDescentParsers/cplusplus/interpreter/{testInput.txt => goodTest.input} (100%)
 rename recursiveDescentParsers/cplusplus/interpreter/{testOutput.txt => goodTest.reference} (100%)

diff --git a/recursiveDescentParsers/cplusplus/interpreter/Makefile b/recursiveDescentParsers/cplusplus/interpreter/Makefile
index 1413587..bd7ef7e 100644
--- a/recursiveDescentParsers/cplusplus/interpreter/Makefile
+++ b/recursiveDescentParsers/cplusplus/interpreter/Makefile
@@ -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
+	
 
diff --git a/recursiveDescentParsers/cplusplus/interpreter/badTest.input b/recursiveDescentParsers/cplusplus/interpreter/badTest.input
new file mode 100644
index 0000000..99faffe
--- /dev/null
+++ b/recursiveDescentParsers/cplusplus/interpreter/badTest.input
@@ -0,0 +1,8 @@
+42?
+42^2
+(42))
+((42
+(42 / 0)
+(42 / (1-1))
+42 +/- 3.14159
+
diff --git a/recursiveDescentParsers/cplusplus/interpreter/badTest.reference b/recursiveDescentParsers/cplusplus/interpreter/badTest.reference
new file mode 100644
index 0000000..c2830f4
--- /dev/null
+++ b/recursiveDescentParsers/cplusplus/interpreter/badTest.reference
@@ -0,0 +1,33 @@
+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!
diff --git a/recursiveDescentParsers/cplusplus/interpreter/testInput.txt b/recursiveDescentParsers/cplusplus/interpreter/goodTest.input
similarity index 100%
rename from recursiveDescentParsers/cplusplus/interpreter/testInput.txt
rename to recursiveDescentParsers/cplusplus/interpreter/goodTest.input
diff --git a/recursiveDescentParsers/cplusplus/interpreter/testOutput.txt b/recursiveDescentParsers/cplusplus/interpreter/goodTest.reference
similarity index 100%
rename from recursiveDescentParsers/cplusplus/interpreter/testOutput.txt
rename to recursiveDescentParsers/cplusplus/interpreter/goodTest.reference
diff --git a/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp b/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp
index 2963c1a..6753b12 100644
--- a/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp
+++ b/recursiveDescentParsers/cplusplus/interpreter/interpreter.cpp
@@ -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;
 	
-- 
GitLab