From f49bc77e105bef9a12499f65d4d2eebf968a78b2 Mon Sep 17 00:00:00 2001
From: "Prof. Ronald Moore" <ronald.moore@h-da.de>
Date: Tue, 11 May 2021 18:26:17 +0200
Subject: [PATCH] Got test9 to work (by borrowing 'main' hack from test8)!

Signed-off-by: Prof. Ronald Moore <ronald.moore@h-da.de>
---
 LLVM/cplusplus/Makefile        | 14 +++++++-------
 LLVM/cplusplus/tests/test9.cpp |  9 +++++++++
 LLVM/cplusplus/tests/test9.kal |  2 +-
 LLVM/cplusplus/toy9.cpp        |  5 +++--
 4 files changed, 20 insertions(+), 10 deletions(-)
 create mode 100644 LLVM/cplusplus/tests/test9.cpp

diff --git a/LLVM/cplusplus/Makefile b/LLVM/cplusplus/Makefile
index 6fb1122..e760074 100644
--- a/LLVM/cplusplus/Makefile
+++ b/LLVM/cplusplus/Makefile
@@ -26,7 +26,6 @@ LLVM_FLAGS := `llvm-config --cxxflags --ldflags --system-libs --libs all `
 
 CFLAGS :=  $(LLVM_FLAGS) -rdynamic -O3
 
-
 ###  Gnu Make Preliminaries -- See also
 # https://www.gnu.org/software/make/manual/html_node/Special-Targets.html
 
@@ -56,10 +55,10 @@ $(PROGS): %: %.cpp
 	$(CC) -g $< $(CFLAGS) -o $@
 
 clean: testclean
-	$(RM) -fv *~ $(PROGS) ModuleMaker.bc fib test8 test8.o test9
+	$(RM) -fv *~ $(PROGS) ModuleMaker.bc fib test8 test9 
 
 testclean:
-	rm -fv *~ *.output test8.o test9
+	rm -fv *~ *.output test8.o test9.o
 
 tests: $(TESTS)
 
@@ -96,14 +95,15 @@ test7: toy7 tests/test7.kal
 	echo && echo "\n===================> Testing toy7...\n"
 	-./toy7 <tests/test7.kal
 
-test8: toy8 tests/test8.kal toy8.cpp
+test8: toy8 tests/test8.kal tests/test8.cpp 
 	echo && echo "\n===================> Testing toy8... (expect 42, of course)\n"
 	-./toy8 <tests/test8.kal
 	-clang++ tests/test8.cpp test8.o -o test8
 	-./test8
 
-test9: toy9 tests/test9.kal
-	echo && echo "\n===================> Testing toy9...\n"
-	-./toy9 <tests/test9.kal |& clang -x ir - -o test9
+test9: toy9 tests/test9.kal tests/test9.cpp
+	echo && echo "\n===================> Testing toy9...(expect fib(12) == 144)\n"
+	-./toy9 <tests/test9.kal |& clang -x ir -c - -o test9.o
+	-clang++ tests/test9.cpp test9.o -o test9
 	-./test9
 
diff --git a/LLVM/cplusplus/tests/test9.cpp b/LLVM/cplusplus/tests/test9.cpp
new file mode 100644
index 0000000..0530592
--- /dev/null
+++ b/LLVM/cplusplus/tests/test9.cpp
@@ -0,0 +1,9 @@
+#include <iostream>
+
+extern "C" {
+    double repl( );
+}
+
+int main() {
+    std::cout << "REPL output =" << repl( ) << std::endl;
+}
diff --git a/LLVM/cplusplus/tests/test9.kal b/LLVM/cplusplus/tests/test9.kal
index 122c1a0..fba0973 100644
--- a/LLVM/cplusplus/tests/test9.kal
+++ b/LLVM/cplusplus/tests/test9.kal
@@ -4,4 +4,4 @@ def fib(x)
   else
     fib(x-1)+fib(x-2);
 
-fib(2)
+fib(12)
diff --git a/LLVM/cplusplus/toy9.cpp b/LLVM/cplusplus/toy9.cpp
index e75a21a..f34fd3d 100644
--- a/LLVM/cplusplus/toy9.cpp
+++ b/LLVM/cplusplus/toy9.cpp
@@ -801,8 +801,8 @@ static std::unique_ptr<FunctionAST> ParseDefinition() {
 static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
   SourceLocation FnLoc = CurLoc;
   if (auto E = ParseExpression()) {
-    // Make an anonymous proto.
-    auto Proto = std::make_unique<PrototypeAST>(FnLoc, "main",   //  "__anon_expr",
+    // Make an top level proto called "repl"
+    auto Proto = std::make_unique<PrototypeAST>(FnLoc, "repl",   //  "__anon_expr",
                                                  std::vector<std::string>());
     return std::make_unique<FunctionAST>(std::move(Proto), std::move(E));
   }
@@ -1343,6 +1343,7 @@ static void HandleExtern() {
 
 static void HandleTopLevelExpression() {
   // Evaluate a top-level expression into an anonymous function.
+  // ...which is not really anonymous but rather "repl" ...
   if (auto FnAST = ParseTopLevelExpr()) {
     if (!FnAST->codegen()) {
       fprintf(stderr, "Error generating code for top level expr");
-- 
GitLab