Nicolas Winkler 7 years ago
parent
commit
c2c9efbb19
5 changed files with 161 additions and 9 deletions
  1. 2 0
      src/Ast.cpp
  2. 43 6
      src/Ast.h
  3. 14 0
      src/AstVisitor.cpp
  4. 99 0
      src/AstVisitor.h
  5. 3 3
      src/Semantic.h

+ 2 - 0
src/Ast.cpp

@@ -7,3 +7,5 @@ Statement::~Statement(void)
 {
 }
 
+
+

+ 43 - 6
src/Ast.h

@@ -27,14 +27,18 @@
 #include <memory>
 #include <utility>
 
+//#include "AstVisitor.h"
 
 namespace qlow
 {
+    class AstVisitor;
     namespace ast
     {
         template<typename T>
         using List = std::vector<std::unique_ptr<T>>;
 
+        // base class
+        struct AstObject;
 
         struct Class;
 
@@ -62,8 +66,12 @@ namespace qlow
 }
 
 
+struct qlow::ast::AstObject : public Visitable<AstVisitor>
+{
+};
+
 
-struct qlow::ast::Class
+struct qlow::ast::Class : public AstObject
 {
     std::string name;
     List<FeatureDeclaration> features;
@@ -72,10 +80,12 @@ struct qlow::ast::Class
         name(name), features(std::move(features))
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
-struct qlow::ast::FeatureDeclaration
+struct qlow::ast::FeatureDeclaration : public AstObject
 {
     std::string name;
     std::string type;
@@ -84,6 +94,8 @@ struct qlow::ast::FeatureDeclaration
         name(name), type(type)
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -93,6 +105,8 @@ struct qlow::ast::FieldDeclaration : public FeatureDeclaration
         FeatureDeclaration(type, name)
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -116,10 +130,12 @@ struct qlow::ast::MethodDefinition : public FeatureDeclaration
         body(std::move(body))
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
-struct qlow::ast::VariableDeclaration 
+struct qlow::ast::VariableDeclaration  : public AstObject
 {
     std::string type;
     std::string name;
@@ -127,6 +143,8 @@ struct qlow::ast::VariableDeclaration
         type(type), name(name)
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -137,10 +155,12 @@ struct qlow::ast::ArgumentDeclaration :
         VariableDeclaration(type, name)
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
-struct qlow::ast::DoEndBlock
+struct qlow::ast::DoEndBlock : public AstObject
 {
     List<Statement> statements;
     
@@ -148,17 +168,22 @@ struct qlow::ast::DoEndBlock
         statements(std::move(statements))
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
-struct qlow::ast::Statement
+struct qlow::ast::Statement : public virtual AstObject
 {
     virtual ~Statement(void);
+
+    void accept(AstVisitor& v);
 };
 
 
-struct qlow::ast::Expression
+struct qlow::ast::Expression : public virtual AstObject
 {
+    void accept(AstVisitor& v);
 };
 
 
@@ -179,6 +204,8 @@ struct qlow::ast::FeatureCall : public Expression, public Statement
         target(std::move(target)), name(name), arguments(std::move(arguments))
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -191,6 +218,8 @@ struct qlow::ast::AssignmentStatement : public Statement
         target(target), expr(std::move(expr))
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -202,6 +231,8 @@ struct qlow::ast::NewVariableStatement : public Statement
        name(name), type(type)
     {
     } 
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -216,6 +247,8 @@ struct qlow::ast::Operation : public Expression
         op(op)
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -236,6 +269,8 @@ struct qlow::ast::UnaryOperation : public Operation
         expr(std::move(expr))
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -249,6 +284,8 @@ struct qlow::ast::BinaryOperation : public Operation
         left(std::move(left)), right(std::move(right))
     {
     }
+
+    void accept(AstVisitor& v);
 };
 
 

+ 14 - 0
src/AstVisitor.cpp

@@ -0,0 +1,14 @@
+#include "AstVisitor.h"
+
+
+using namespace qlow;
+
+std::unique_ptr<sem::SemanticObject> AstVisitor::visit(ast::Class& ast)
+{
+    auto c = std::make_unique<sem::Class> ();
+    c->name = ast.name;
+
+    return c;
+}
+
+

+ 99 - 0
src/AstVisitor.h

@@ -0,0 +1,99 @@
+#ifndef QLOW_AST_VISITOR_H
+#define QLOW_AST_VISITOR_H
+
+#include "Visitor.h"
+#include "Ast.h"
+#include "Semantic.h"
+
+#include <memory>
+
+
+namespace qlow
+{
+    namespace ast
+    {
+        template<typename T>
+        using List = std::vector<std::unique_ptr<T>>;
+
+        // base class
+        struct AstObject;
+
+        struct Class;
+
+        struct FeatureDeclaration;
+
+        struct FieldDeclaration;
+        struct MethodDefinition;
+
+        struct VariableDeclaration;
+        struct ArgumentDeclaration;
+
+        struct DoEndBlock;
+
+        struct Statement;
+        struct Expression;
+
+        struct FeatureCall;
+        struct AssignmentStatement;
+        struct NewVariableStatement;
+
+        struct Operation;
+        struct UnaryOperation;
+        struct BinaryOperation;
+    }
+}
+
+
+namespace qlow
+{
+    class AstVisitor;
+}
+
+
+class qlow::AstVisitor :
+    public Visitor<
+        std::unique_ptr<sem::SemanticObject>,
+
+        ast::Class,
+        ast::FeatureDeclaration,
+        ast::FieldDeclaration,
+        ast::MethodDefinition,
+        ast::VariableDeclaration,
+        ast::ArgumentDeclaration,
+        ast::DoEndBlock,
+        ast::Statement,
+        ast::Expression,
+        ast::FeatureCall,
+        ast::AssignmentStatement,
+        ast::NewVariableStatement,
+        ast::Operation,
+        ast::UnaryOperation,
+        ast::BinaryOperation
+    >
+{
+public:
+    using ReturnType = std::unique_ptr<sem::SemanticObject>;
+
+    ReturnType visit(ast::Class& ast) override;
+    ReturnType visit(ast::FeatureDeclaration& ast) override;
+    ReturnType visit(ast::FieldDeclaration& ast) override;
+    ReturnType visit(ast::MethodDefinition& ast) override;
+    ReturnType visit(ast::VariableDeclaration& ast) override;
+    ReturnType visit(ast::ArgumentDeclaration& ast) override;
+    ReturnType visit(ast::DoEndBlock& ast) override;
+    ReturnType visit(ast::Statement& ast) override;
+    ReturnType visit(ast::Expression& ast) override;
+    ReturnType visit(ast::FeatureCall& ast) override;
+    ReturnType visit(ast::AssignmentStatement& ast) override;
+    ReturnType visit(ast::NewVariableStatement& ast) override;
+    ReturnType visit(ast::Operation& ast) override;
+    ReturnType visit(ast::UnaryOperation& ast) override;
+    ReturnType visit(ast::BinaryOperation& ast) override;
+};
+
+
+
+
+#endif // QLOW_AST_VISITOR_H
+
+

+ 3 - 3
src/Semantic.h

@@ -11,8 +11,9 @@ namespace qlow
     {
 
         template<typename T>
-        using std::map<std::string, std::unique_ptr<T>> SymbolTable;
+        using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
 
+        struct SemanticObject;
         struct Class;
 
         struct Field;
@@ -32,8 +33,7 @@ struct Class
 struct Field
 {
     Class* type;
-
-
+    std::string name;
 }