Nicolas Winkler 6 anni fa
parent
commit
abf85dcd80
6 ha cambiato i file con 30 aggiunte e 61 eliminazioni
  1. 0 58
      src/Util.h
  2. 2 1
      src/ast/Ast.cpp
  3. 18 1
      src/ast/Ast.h
  4. 7 0
      src/ast/AstVisitor.cpp
  5. 2 0
      src/ast/AstVisitor.h
  6. 1 1
      src/ast/syntax.y

+ 0 - 58
src/Util.h

@@ -1,58 +0,0 @@
-#ifndef QLOW_UTIL_H
-#define QLOW_UTIL_H
-
-#include <vector>
-#include <memory>
-#include <sstream>
-#include <typeinfo>
-
-
-namespace qlow
-{
-    
-    template<typename T>
-    using OwningList = std::vector<std::unique_ptr<T>>;
-    
-    
-    
-    /// I don't like this, but I lack better ideas at the moment.
-    /// TODO: find better solution
-    /*!
-    * \brief tries to cast a unique_ptr and throws if it fails
-    */
-    template<typename T, typename U>
-    std::unique_ptr<T> unique_dynamic_cast(std::unique_ptr<U>&& p)
-    {
-        U* released = p.release();
-        if (T* casted = dynamic_cast<T*>(released); casted)
-            return std::unique_ptr<T> (casted);
-        else {
-            delete released;
-            //throw "failed unique dynamic cast";
-            throw std::string("invalid unique_dynamic_cast from ") + typeid(p.get()).name() + " to " + typeid(T).name();
-        }
-    }
-
-
-    
-    namespace util
-    {
-        inline std::string toString(const void* a)
-        {
-            std::ostringstream o;
-            o << a;
-            return o.str();
-        }
-    }
-}
-
-
-
-
-
-
-
-
-
-#endif // QLOW_UTIL_H
-

+ 2 - 1
src/ast/Ast.cpp

@@ -54,6 +54,7 @@ ACCEPT_DEFINITION(ReturnStatement, StructureVisitor)
 ACCEPT_DEFINITION(LocalVariableStatement, StructureVisitor)
 ACCEPT_DEFINITION(AddressExpression, StructureVisitor)
 ACCEPT_DEFINITION(IntConst, StructureVisitor)
+ACCEPT_DEFINITION(StringConst, StructureVisitor)
 ACCEPT_DEFINITION(UnaryOperation, StructureVisitor)
 ACCEPT_DEFINITION(BinaryOperation, StructureVisitor)
 ACCEPT_DEFINITION(NewExpression, StructureVisitor)
@@ -66,7 +67,7 @@ Statement::~Statement(void)
 }
 
 
-qlow::ast::IntConst::IntConst(std::string&& val, const qlow::CodePosition& p) :
+qlow::ast::IntConst::IntConst(const std::string& val, const qlow::CodePosition& p) :
     AstObject{ p },
     Expression{ p },
     value{ strtoull(val.c_str(), nullptr, 0) }

+ 18 - 1
src/ast/Ast.h

@@ -75,7 +75,9 @@ namespace qlow
         struct ReturnStatement;
         struct LocalVariableStatement;
         struct AddressExpression;
+
         struct IntConst;
+        struct StringConst;
 
         struct Operation;
         struct UnaryOperation;
@@ -497,7 +499,22 @@ struct qlow::ast::IntConst : public Expression
         Expression(p),
         value{ v } {}
         
-    IntConst(std::string&& val, const CodePosition& p);
+    IntConst(const std::string& val, const CodePosition& p);
+    virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
+};
+
+
+struct qlow::ast::StringConst : public Expression
+{
+    std::string value;
+    
+    StringConst(std::string value, const CodePosition& p) :
+        AstObject(p),
+        Expression(p),
+        value{ std::move(value) }
+    {
+    }
+
     virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
 };
 

+ 7 - 0
src/ast/AstVisitor.cpp

@@ -368,6 +368,13 @@ std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::IntConst& ast,
 }
 
 
+std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::StringConst& ast, sem::Scope& scope)
+{
+    // TODO implement
+    return nullptr;
+}
+
+
 std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::UnaryOperation& ast, sem::Scope& scope)
 {
     auto argument = unique_dynamic_cast<sem::Expression>(ast.expr->accept(*this, scope));

+ 2 - 0
src/ast/AstVisitor.h

@@ -49,6 +49,7 @@ class qlow::StructureVisitor :
         ast::LocalVariableStatement,
         ast::AddressExpression,
         ast::IntConst,
+        ast::StringConst,
         ast::UnaryOperation,
         ast::BinaryOperation,
         ast::NewExpression,
@@ -75,6 +76,7 @@ public:
     ReturnType visit(ast::LocalVariableStatement& ast, sem::Scope& scope) override;
     ReturnType visit(ast::AddressExpression& ast, sem::Scope& scope) override;
     ReturnType visit(ast::IntConst& ast, sem::Scope& scope) override;
+    ReturnType visit(ast::StringConst& ast, sem::Scope& scope) override;
     ReturnType visit(ast::UnaryOperation& ast, sem::Scope& scope) override;
     ReturnType visit(ast::BinaryOperation& ast, sem::Scope& scope) override;
     ReturnType visit(ast::NewExpression& ast, sem::Scope& scope) override;

+ 1 - 1
src/ast/syntax.y

@@ -599,7 +599,7 @@ expression:
     }
     |
     INT_LITERAL {
-        $$ = new IntConst(std::move(*$1), @$);
+        $$ = new IntConst(*$1, @$);
         delete $1;
     };/*
     |