Browse Source

improving printing

Nicolas Winkler 6 years ago
parent
commit
c1ea466720
9 changed files with 55 additions and 10 deletions
  1. 1 1
      src/CMakeLists.txt
  2. 0 1
      src/Driver.cpp
  3. 14 2
      src/ErrorReporting.cpp
  4. 2 0
      src/ErrorReporting.h
  5. 11 1
      src/Printer.cpp
  6. 11 2
      src/Printer.h
  7. 1 0
      src/ast/syntax.y
  8. 14 2
      src/main.cpp
  9. 1 1
      src/makefile

+ 1 - 1
src/CMakeLists.txt

@@ -10,7 +10,7 @@ check_ipo_supported(RESULT ipo_supported)
 set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
-set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -g")
+set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Wextra -g -DDEBUGGING")
 
 find_package(BISON 3.0.0 REQUIRED)
 find_package(FLEX 2.4.0 REQUIRED)

+ 0 - 1
src/Driver.cpp

@@ -72,7 +72,6 @@ Driver::Driver(int argc, char** argv) :
 int Driver::run(void)
 {
     Printer& printer = Printer::getInstance();
-    
 #ifdef DEBUGGING
     printer << "starting parser" << std::endl;
 #endif

+ 14 - 2
src/ErrorReporting.cpp

@@ -2,12 +2,12 @@
 
 #include <map>
 #include <fstream>
+#include <sstream>
 
 using qlow::CompileError;
 using qlow::SyntaxError;
 using qlow::SemanticError;
 
-
 namespace qlow
 {
     void reportError(const CompileError& ce)
@@ -45,7 +45,7 @@ namespace qlow
     void printError(Printer& printer, const std::string& msg, const CodePosition& cp)
     {
         printer.bold();
-        printer << cp.filename << ":" << cp.first_line << ":" << cp.first_column << ": ";
+        printer << cp.getReportFormat() << ": "; //cp.filename << ":" << cp.first_line << ":" << cp.first_column << ": ";
         printer.foreground(Printer::RED, true);
         printer << "error: ";
         printer.removeFormatting();
@@ -54,6 +54,18 @@ namespace qlow
 }
 
 
+std::string qlow::CodePosition::getReportFormat(void) const
+{
+    std::ostringstream s;
+    if (filename == nullptr) {
+        s << first_line << ":" << first_column;
+    }
+    else {
+        s << filename << ":" << first_line << ":" << first_column;
+    }
+    return s.str();
+}
+
 
 CompileError::~CompileError(void)
 {

+ 2 - 0
src/ErrorReporting.h

@@ -34,6 +34,8 @@ struct qlow::CodePosition
     int last_column;
     
     inline bool isMultiline(void) const { return first_line != last_line; }
+
+    std::string getReportFormat(void) const;
 };
 
 

+ 11 - 1
src/Printer.cpp

@@ -1,6 +1,6 @@
 #include "Printer.h"
 
-#include "Ast.h"
+//#include "Ast.h"
 #include <cstdio>
 
 using qlow::Printer;
@@ -17,6 +17,7 @@ Printer Printer::instance(std::cout, isatty(fileno(stdout)));
 
 void Printer::foreground(Color color, bool bright)
 {
+    if (!isTty) return;
     using std::string;
     string cchar = string(1, '0' + color);
     string isbright = bright ? "9" : "3";
@@ -26,6 +27,7 @@ void Printer::foreground(Color color, bool bright)
 
 void Printer::background(Color color, bool bright)
 {
+    if (!isTty) return;
     using std::string;
     string cchar = string(1, '0' + color);
     string isbright = bright ? "10" : "4";
@@ -35,16 +37,24 @@ void Printer::background(Color color, bool bright)
 
 void Printer::bold(void)
 {
+    if (!isTty) return;
     target << "\033[1m";
 }
 
 
 void Printer::removeFormatting(void)
 {
+    if (!isTty) return;
     target << "\033[0m";
 }
 
 
+int Printer::sync(void)
+{
+    return target.eof() ? EOF : 0;
+}
+
+
 int Printer::overflow(int c)
 {
     target.put(char(c));

+ 11 - 2
src/Printer.h

@@ -17,9 +17,17 @@ class qlow::Printer :
     bool isTty;
 
     static Printer instance;
-public:
+
     inline Printer(std::ostream& target, bool isTty) :
-        target{ target }, isTty{ isTty } {}
+        std::ostream{ static_cast<std::streambuf*> (this) },
+        target{ target },
+        isTty{ isTty } {}
+public:
+
+    Printer(const Printer&) = delete;
+    Printer(Printer&&) = delete;
+    Printer& operator=(const Printer&) = delete;
+    Printer& operator=(Printer&&) = delete;
 
     enum Color {
         BLACK = 0,
@@ -39,6 +47,7 @@ public:
 
     inline static Printer& getInstance(void) { return instance; }
 protected:
+    int sync(void) override;
     int overflow(int c) override;
 };
 

+ 1 - 0
src/ast/syntax.y

@@ -83,6 +83,7 @@ do                                                        \
         YYRHSLOC(Rhs, 0).last_line;                       \
       (Cur).first_column = (Cur).last_column =            \
         YYRHSLOC(Rhs, 0).last_column;                     \
+      (Cur).filename = YYRHSLOC(Rhs, 0).filename;         \
     }                                                     \
 while (0)
 %}

+ 14 - 2
src/main.cpp

@@ -1,6 +1,7 @@
 #include <iostream>
 
-#include <unistd.h>
+#include <exception>
+#include <cstdlib>
 
 #include "Ast.h"
 #include "Semantic.h"
@@ -9,12 +10,23 @@
 
 #include "Driver.h"
 
-int main(int argc, char** argv)
+int main(int argc, char** argv) try
 {
+    std::set_terminate ([] () {
+        std::cerr << "terminated" << std::endl;
+        abort();
+    });
+
     qlow::Driver driver(argc, argv);
     return driver.run();
 
     return 0;
 }
+catch (float f) {
+    std::cerr << "uncaught float" << std::endl;
+}
+catch(...) {
+    std::cerr << "uncaught exception" << std::endl;
+}
 
 

+ 1 - 1
src/makefile

@@ -8,7 +8,7 @@ LLVMCONFIG := llvm-config
 INCLUDEDIRS := -I$(shell $(LLVMCONFIG) --includedir) -I.. -Isem/ -Iast/ -I.
 CXXFLAGS := -std=c++17 $(INCLUDEDIRS) -w # -Wall -Wextra
 
-ifndef STATIC
+ifdef STATIC
 LDFLAGS := $(shell $(LLVMCONFIG) --link-static --ldflags --system-libs --libs all) -static -dead-strip -s
 else
 LDFLAGS := $(shell $(LLVMCONFIG) --ldflags --system-libs --libs all)