Browse Source

translating to better type system

Nicolas Winkler 6 years ago
parent
commit
c3e250e12f
6 changed files with 35 additions and 33 deletions
  1. 12 3
      src/AstVisitor.cpp
  2. 4 4
      src/Scope.cpp
  3. 6 11
      src/Scope.h
  4. 1 1
      src/Semantic.h
  5. 6 8
      src/TypeVisitor.cpp
  6. 6 6
      src/TypeVisitor.h

+ 12 - 3
src/AstVisitor.cpp

@@ -8,11 +8,16 @@
 using namespace qlow;
 using namespace qlow;
 
 
 
 
+[[deprecated]]
 sem::Class* StructureVisitor::getType(const std::string& type, sem::Scope& scope)
 sem::Class* StructureVisitor::getType(const std::string& type, sem::Scope& scope)
 {
 {
     auto t = scope.getType(type);
     auto t = scope.getType(type);
-    if (t)
-        return t.value().typeClass;
+    if (t) {
+        if (auto* ct = dynamic_cast<sem::ClassType*>(t.value()); ct)
+            return ct->getClass();
+        else
+            return nullptr;
+    }
     else
     else
         return nullptr;
         return nullptr;
 }
 }
@@ -39,7 +44,11 @@ std::unique_ptr<sem::SemanticObject> StructureVisitor::visit(ast::FieldDeclarati
     f->name = ast.name;
     f->name = ast.name;
     auto type = scope.getType(ast.type);
     auto type = scope.getType(ast.type);
     if (type) {
     if (type) {
-        f->type = type.value().typeClass;
+        if (auto* ct = dynamic_cast<sem::ClassType*>(type.value()); ct) {
+            f->type = ct->getClass();
+        }
+        else
+            throw "TODO implement";
     }
     }
     else {
     else {
         throw sem::SemanticException(sem::SemanticException::UNKNOWN_TYPE,
         throw sem::SemanticException(sem::SemanticException::UNKNOWN_TYPE,

+ 4 - 4
src/Scope.cpp

@@ -20,11 +20,11 @@ sem::Method* sem::GlobalScope::getMethod(const std::string& name)
 }
 }
 
 
 
 
-std::optional<sem::Type> sem::GlobalScope::getType(const std::string& name)
+std::optional<sem::Type*> sem::GlobalScope::getType(const std::string& name)
 {
 {
     auto t = classes.find(name);
     auto t = classes.find(name);
     if (t != classes.end())
     if (t != classes.end())
-        return std::make_optional(Type{ t->second.get() });
+        return std::make_optional(new ClassType{ t->second.get() });
     return std::nullopt;
     return std::nullopt;
 }
 }
 
 
@@ -81,7 +81,7 @@ std::string sem::ClassScope::toString(void)
 }
 }
 
 
 
 
-std::optional<sem::Type> sem::ClassScope::getType(const std::string& name)
+std::optional<sem::Type*> sem::ClassScope::getType(const std::string& name)
 {
 {
     return parentScope.getType(name);
     return parentScope.getType(name);
 }
 }
@@ -115,7 +115,7 @@ sem::Method * sem::LocalScope::getMethod(const std::string& name)
 }
 }
 
 
 
 
-std::optional<sem::Type> sem::LocalScope::getType(const std::string& name)
+std::optional<sem::Type*> sem::LocalScope::getType(const std::string& name)
 {
 {
     return parentScope.getType(name);
     return parentScope.getType(name);
 }
 }

+ 6 - 11
src/Scope.h

@@ -5,6 +5,8 @@
 #include <map>
 #include <map>
 #include <memory>
 #include <memory>
 
 
+#include "Type.h"
+
 namespace qlow
 namespace qlow
 {
 {
     namespace sem
     namespace sem
@@ -19,7 +21,6 @@ namespace qlow
         struct Class;
         struct Class;
         struct Method;
         struct Method;
         struct Variable;
         struct Variable;
-        struct Type;
 
 
         class Scope;
         class Scope;
         class GlobalScope;
         class GlobalScope;
@@ -29,19 +30,13 @@ namespace qlow
 }
 }
 
 
 
 
-struct qlow::sem::Type
-{
-    Class* typeClass;
-};
-
-
 class qlow::sem::Scope
 class qlow::sem::Scope
 {
 {
 public:
 public:
     virtual ~Scope(void);
     virtual ~Scope(void);
     virtual Variable* getVariable(const std::string& name) = 0;
     virtual Variable* getVariable(const std::string& name) = 0;
     virtual Method* getMethod(const std::string& name) = 0;
     virtual Method* getMethod(const std::string& name) = 0;
-    virtual std::optional<Type> getType(const std::string& name) = 0;
+    virtual std::optional<Type*> getType(const std::string& name) = 0;
 
 
     virtual std::string toString(void) = 0;
     virtual std::string toString(void) = 0;
 };
 };
@@ -54,7 +49,7 @@ public:
 public:
 public:
     virtual Variable* getVariable(const std::string& name);
     virtual Variable* getVariable(const std::string& name);
     virtual Method* getMethod(const std::string& name);
     virtual Method* getMethod(const std::string& name);
-    virtual std::optional<Type> getType(const std::string& name);
+    virtual std::optional<Type*> getType(const std::string& name);
 
 
     virtual std::string toString(void);
     virtual std::string toString(void);
 };
 };
@@ -71,7 +66,7 @@ public:
     }
     }
     virtual Variable* getVariable(const std::string& name);
     virtual Variable* getVariable(const std::string& name);
     virtual Method* getMethod(const std::string& name);
     virtual Method* getMethod(const std::string& name);
-    virtual std::optional<Type> getType(const std::string& name);
+    virtual std::optional<Type*> getType(const std::string& name);
     virtual std::string toString(void);
     virtual std::string toString(void);
 };
 };
 
 
@@ -90,7 +85,7 @@ public:
 
 
     virtual Variable* getVariable(const std::string& name);
     virtual Variable* getVariable(const std::string& name);
     virtual Method* getMethod(const std::string& name);
     virtual Method* getMethod(const std::string& name);
-    virtual std::optional<Type> getType(const std::string& name);
+    virtual std::optional<Type*> getType(const std::string& name);
     virtual std::string toString(void);
     virtual std::string toString(void);
 };
 };
 
 

+ 1 - 1
src/Semantic.h

@@ -113,7 +113,7 @@ struct qlow::sem::Field : public Variable
 struct qlow::sem::Method : public SemanticObject
 struct qlow::sem::Method : public SemanticObject
 {
 {
     Class* containingType;
     Class* containingType;
-    Type returnType;
+    std::unique_ptr<Type> returnType;
     std::string name;
     std::string name;
     ast::MethodDefinition* astNode;
     ast::MethodDefinition* astNode;
     std::unique_ptr<DoEndBlock> body;
     std::unique_ptr<DoEndBlock> body;

+ 6 - 8
src/TypeVisitor.cpp

@@ -4,34 +4,32 @@ using namespace qlow;
 
 
 
 
 
 
-sem::Type sem::TypeVisitor::visit(sem::Expression& expr, const sem::SymbolTable<sem::Class>& classes)
+std::unique_ptr<sem::Type> sem::TypeVisitor::visit(sem::Expression& expr, const sem::SymbolTable<sem::Class>& classes)
 {
 {
 }
 }
 
 
 
 
-sem::Type sem::TypeVisitor::visit(sem::UnaryOperation& expr, const sem::SymbolTable<sem::Class>& classes)
+std::unique_ptr<sem::Type> sem::TypeVisitor::visit(sem::UnaryOperation& expr, const sem::SymbolTable<sem::Class>& classes)
 {
 {
     return visit(*expr.arg, classes);
     return visit(*expr.arg, classes);
 }
 }
 
 
 
 
-sem::Type sem::TypeVisitor::visit(sem::BinaryOperation& expr, const sem::SymbolTable<sem::Class>& classes)
+std::unique_ptr<sem::Type> sem::TypeVisitor::visit(sem::BinaryOperation& expr, const sem::SymbolTable<sem::Class>& classes)
 {
 {
     return visit(*expr.left, classes);
     return visit(*expr.left, classes);
 }
 }
 
 
 
 
-sem::Type sem::TypeVisitor::visit(sem::FeatureCallExpression& expr, const sem::SymbolTable<sem::Class>& classes)
+std::unique_ptr<sem::Type> sem::TypeVisitor::visit(sem::FeatureCallExpression& expr, const sem::SymbolTable<sem::Class>& classes)
 {
 {
     return Type{ expr.callee->returnType };
     return Type{ expr.callee->returnType };
 }
 }
 
 
 
 
-sem::Type sem::TypeVisitor::visit(sem::IntConst& expr, const sem::SymbolTable<sem::Class>& classes)
+std::unique_ptr<sem::Type> sem::TypeVisitor::visit(sem::IntConst& expr, const sem::SymbolTable<sem::Class>& classes)
 {
 {
-    //return Type{ new sem::Class() };
-    //TODO implement
-    return Type { nullptr };
+    return std::make_unique<NativeType>();
 }
 }
 
 
 
 

+ 6 - 6
src/TypeVisitor.h

@@ -15,7 +15,7 @@ namespace qlow
 
 
 class qlow::sem::TypeVisitor :
 class qlow::sem::TypeVisitor :
     public Visitor<
     public Visitor<
-        qlow::sem::Type,
+        std::unique_ptr<qlow::sem::Type>,
         const sem::SymbolTable<sem::Class>,
         const sem::SymbolTable<sem::Class>,
         
         
         sem::Expression,
         sem::Expression,
@@ -27,11 +27,11 @@ class qlow::sem::TypeVisitor :
 {
 {
     
     
 public:
 public:
-    qlow::sem::Type visit(sem::Expression& expr, const sem::SymbolTable<sem::Class>& classes) override;
-    qlow::sem::Type visit(sem::UnaryOperation& expr, const sem::SymbolTable<sem::Class>& classes) override;
-    qlow::sem::Type visit(sem::BinaryOperation& expr, const sem::SymbolTable<sem::Class>& classes) override;
-    qlow::sem::Type visit(sem::FeatureCallExpression& expr, const sem::SymbolTable<sem::Class>& classes) override;
-    qlow::sem::Type visit(sem::IntConst& expr, const sem::SymbolTable<sem::Class>& classes) override;
+    std::unique_ptr<qlow::sem::Type> visit(sem::Expression& expr, const sem::SymbolTable<sem::Class>& classes) override;
+    std::unique_ptr<qlow::sem::Type> visit(sem::UnaryOperation& expr, const sem::SymbolTable<sem::Class>& classes) override;
+    std::unique_ptr<qlow::sem::Type> visit(sem::BinaryOperation& expr, const sem::SymbolTable<sem::Class>& classes) override;
+    std::unique_ptr<qlow::sem::Type> visit(sem::FeatureCallExpression& expr, const sem::SymbolTable<sem::Class>& classes) override;
+    std::unique_ptr<qlow::sem::Type> visit(sem::IntConst& expr, const sem::SymbolTable<sem::Class>& classes) override;
 };
 };
 
 
 #endif // QLOW_SEM_TYPEVISITOR_H
 #endif // QLOW_SEM_TYPEVISITOR_H