소스 검색

refactoring

Nicolas Winkler 6 년 전
부모
커밋
30102b7566
5개의 변경된 파일104개의 추가작업 그리고 40개의 파일을 삭제
  1. 1 1
      src/Ast.h
  2. 64 27
      src/Logging.cpp
  3. 29 9
      src/Logging.h
  4. 4 3
      src/Semantic.h
  5. 6 0
      src/main.cpp

+ 1 - 1
src/Ast.h

@@ -87,7 +87,7 @@ namespace qlow
  */
 struct qlow::CodePosition
 {
-    const char* filename;
+    const char* filename = "";
     int first_line;
     int last_line;
     int first_column;

+ 64 - 27
src/Logging.cpp

@@ -1,39 +1,76 @@
 #include "Logging.h"
 
-using qlow::Out;
+#include "Ast.h"
 
+using qlow::Logger;
 
-Out Out::stdout(std::cout);
 
+Logger Logger::instance(std::cout);
 
-Out::Out(std::ostream& target) :
-    std::ostream(this),
-    target(target),
-    indentVal(0),
-    firstChar(true)
+
+Logger::Logger(std::ostream& target) :
+    std::ostream{ this },
+    target{ target },
+    indentVal{ 0 },
+    firstChar{ true }
 {
 }
 
 
-int Out::overflow(int c)
+void Logger::foreground(Color color, bool bright)
 {
-    // needs indenting on first char
-    if (firstChar)
-        for (int i = 0; i < indentVal; i++)
-            target.put(' ');
-
-    if (logType <= logLevel)
-        target.put(char(c));
-
-    if (c == '\n') {
-        // remove formatting
-        target << "\033[0m";
-        logType = LogLevel::NONE;
-        firstChar = true;
-    }
-    else
-        firstChar = false;
+    using std::string;
+    string cchar = string(1, '0' + color);
+    string isbright = bright ? "9" : "3";
+    *this << "\033[" << isbright << cchar << "m";
+}
+
+
+void Logger::background(Color color, bool bright)
+{
+    using std::string;
+    string cchar = string(1, '0' + color);
+    string isbright = bright ? "10" : "4";
+    *this << "\033[" << isbright << cchar << "m"; 
+}
 
+
+void Logger::bold(void)
+{
+    *this << "\033[1m";
+}
+
+
+void Logger::removeFormatting(void)
+{
+    *this << "\033[0m";
+}
+
+
+void Logger::logError(const std::string& errMsg)
+{
+    bold();
+    foreground(RED, true);
+    *this << "error: ";
+    removeFormatting();
+    *this << errMsg << std::endl;
+}
+
+
+void Logger::logError(const std::string& errMsg, const qlow::CodePosition& cp)
+{
+    bold();
+    *this << cp.filename << ":" << cp.first_line << ":" << cp.first_column << ": ";
+    foreground(RED, true);
+    *this << "error: ";
+    removeFormatting();
+    *this << errMsg << std::endl;
+}
+
+
+int Logger::overflow(int c)
+{
+    target.put(char(c));
     return 0;
 }
 
@@ -44,7 +81,7 @@ std::ostream& operator << (std::ostream& o, qlow::LogLevel l)
     {
         case qlow::LogLevel::WARNING:
             try {
-                dynamic_cast<qlow::Out&>(o).setLogType(l);
+                dynamic_cast<qlow::Logger&>(o).setLogType(l);
             }
             catch(...) {}
             return o << "\033[33m";
@@ -53,7 +90,7 @@ std::ostream& operator << (std::ostream& o, qlow::LogLevel l)
 }
 
 
-/*std::streamsize Out::xsputn(const char* s, std::streamsize n)
+/*std::streamsize Logger::xsputn(const char* s, std::streamsize n)
 {
     target.write(s, n);
 }*/
@@ -61,7 +98,7 @@ std::ostream& operator << (std::ostream& o, qlow::LogLevel l)
 
 /*int main()
 {
-    qlow::Out& l = Out::stdout;
+    qlow::Logger& l = Logger::stdout;
     l << "aha" << std::endl;
     l.indent(10);
     l << "aha" << std::endl;

+ 29 - 9
src/Logging.h

@@ -4,11 +4,10 @@
 #include <iostream>
 #include <stack>
 
+
 namespace qlow
 {
-    // indenting ostream
-    class Out;
-
+    class Logger;
 
     enum class LogLevel
     {
@@ -20,10 +19,12 @@ namespace qlow
         TRACE,
         OFF,
     };
+    
+    struct CodePosition;
 }
 
 
-class qlow::Out :
+class qlow::Logger :
     public std::ostream,
     private std::streambuf
 {
@@ -35,17 +36,36 @@ protected:
     // type of current logging
     LogLevel logType = LogLevel::OFF;
     LogLevel logLevel = LogLevel::INFO;
+    
+    static Logger instance;
+
+    // color
+    enum Color {
+        BLACK = 0,
+        RED,
+        GREEN,
+        YELLOW,
+        BLUE,
+        MAGENTA,
+        CYAN,
+        WHITE
+    };
+
+    void foreground(Color color, bool bright);
+    void background(Color color, bool bright);
+    void bold(void);
+    void removeFormatting(void);
 
 public:
-    Out(std::ostream& target);
-    ~Out(void) = default;
+    explicit Logger(std::ostream& target);
+    ~Logger(void) = default;
 
-    inline void indent(int width = 4) { indentVal += width; }
-    inline void unindent(int width = 4) { indentVal -= width; }
+    void logError(const std::string& errMsg);
+    void logError(const std::string& errMsg, const qlow::CodePosition& cp);
 
     inline void setLogType(LogLevel l) { logType = l; }
 
-    static Out stdout;
+    inline static Logger& getInstance(void) { return instance; }
 
 protected:
     virtual int overflow(int c);

+ 4 - 3
src/Semantic.h

@@ -48,9 +48,10 @@ namespace qlow
 
         class SemanticException;
     }
-    
+
     class ExpressionVisitor;
     class StatementVisitor;
+
     namespace gen
     {
         class FunctionGenerator;
@@ -95,7 +96,7 @@ struct qlow::sem::Class : public SemanticObject
         llvmType{ nullptr }
     {
     }
-    
+
     virtual std::string toString(void) const override;
 };
 
@@ -106,7 +107,7 @@ struct qlow::sem::Variable : public SemanticObject
     std::string name;
 
     /// if this is a local variable, this stores a reference to the llvm
-    /// instance of this variable.
+    /// instance of this variable. If it is a parameter, the parameter value
     llvm::Value* allocaInst;
     
     Variable(void) = default;

+ 6 - 0
src/main.cpp

@@ -6,6 +6,9 @@
 #include "Semantic.h"
 #include "CodeGeneration.h"
 
+#include "Driver.h"
+
+
 extern std::unique_ptr<std::vector<std::unique_ptr<qlow::ast::Class>>> parsedClasses;
 extern FILE* qlow_parser_in;
 extern int qlow_parser_parse(void);
@@ -24,6 +27,9 @@ int main(int argc, char** argv)
         }
     }
     
+    qlow::Driver driver(argc, argv);
+    driver.run();
+    
     {
     const char* filename = argv[optind];