123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581 |
- // =============================================================================
- //
- // This file is part of the qlow compiler.
- //
- // Copyright (C) 2014-2015 Nicolas Winkler
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- // =============================================================================
- #ifndef QLOW_AST_H
- #define QLOW_AST_H
- #include <string>
- #include <vector>
- #include <memory>
- #include <utility>
- #include <map>
- #include "Visitor.h"
- #include "Util.h"
- #include "ErrorReporting.h"
- namespace qlow
- {
- struct CodePosition;
-
- class StructureVisitor;
- namespace ast
- {
- class Ast;
- // base class
- struct AstObject;
- struct Class;
- struct Type;
- struct ClassType;
- struct ArrayType;
- struct PointerType;
-
- struct FeatureDeclaration;
- struct FieldDeclaration;
- struct MethodDefinition;
- struct VariableDeclaration;
- struct ArgumentDeclaration;
- struct Statement;
-
- struct DoEndBlock;
- struct IfElseBlock;
- struct WhileBlock;
- struct Expression;
- struct FeatureCall;
- struct AssignmentStatement;
- struct ReturnStatement;
- struct LocalVariableStatement;
- struct AddressExpression;
- struct IntConst;
- struct Operation;
- struct UnaryOperation;
- struct BinaryOperation;
-
- struct NewArrayExpression;
-
- struct CastExpression;
- }
- namespace sem
- {
- struct SemanticObject;
- struct Class;
-
- class Scope;
- // template<typename T>
- // using SymbolTable = std::map<std::string, std::unique_ptr<T>>;
- }
- }
- class qlow::ast::Ast
- {
- OwningList<AstObject> objects;
- public:
- inline const OwningList<AstObject>& getObjects(void) const { return objects; }
- inline OwningList<AstObject>& getObjects(void) { return objects; }
- void merge(Ast&& other);
- };
- struct qlow::ast::AstObject :
- public Visitable<std::unique_ptr<sem::SemanticObject>, sem::Scope&, StructureVisitor>
- {
- CodePosition pos;
-
- inline AstObject(const CodePosition& cp) :
- pos{ cp } {}
-
- virtual ~AstObject(void);
- };
- struct qlow::ast::Class : public AstObject
- {
- std::string name;
- OwningList<FeatureDeclaration> features;
-
- inline Class(const std::string& name, OwningList<FeatureDeclaration>& features, const CodePosition& cp) :
- AstObject{ cp },
- name{ name }, features(std::move(features))
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&) override;
- };
- struct qlow::ast::Type : public AstObject
- {
- inline Type(const CodePosition& cp) :
- AstObject{ cp }
- {
- }
-
- virtual std::string asString(void) const = 0;
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor&, sem::Scope&) override;
- };
- struct qlow::ast::ClassType : public ast::Type
- {
- std::string typeName;
-
- inline ClassType(const std::string& typeName, const CodePosition& cp) :
- Type{ cp },
- typeName{ typeName }
- {
- }
-
- inline ClassType(std::string&& typeName, const CodePosition& cp) :
- Type{ cp },
- typeName{ std::move(typeName) }
- {
- }
-
- inline std::string asString(void) const override { return typeName; }
- };
- struct qlow::ast::ArrayType : public ast::Type
- {
- std::unique_ptr<ast::Type> arrayType;
-
- inline ArrayType(std::unique_ptr<ast::Type> arrayType, const CodePosition& cp) :
- Type{ cp },
- arrayType{ std::move(arrayType) }
- {
- }
-
- inline std::string asString(void) const override {
- return std::string("[") + arrayType->asString() + "]";
- }
- };
- struct qlow::ast::PointerType : public ast::Type
- {
- std::unique_ptr<ast::Type> derefType;
-
- inline PointerType(std::unique_ptr<ast::Type> derefType, const CodePosition& cp) :
- Type{ cp },
- derefType{ std::move(derefType) }
- {
- }
-
- inline std::string asString(void) const override {
- return derefType->asString() + "*";
- }
- };
- struct qlow::ast::FeatureDeclaration : public AstObject
- {
- std::string name;
- std::unique_ptr<ast::Type> type;
- inline FeatureDeclaration(std::unique_ptr<ast::Type> type, const std::string& name, const CodePosition& cp) :
- AstObject{ cp },
- name{ name },
- type{ std::move(type) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::FieldDeclaration : public FeatureDeclaration
- {
- inline FieldDeclaration(std::unique_ptr<ast::Type> type, const std::string& name, const CodePosition& cp) :
- FeatureDeclaration{ std::move(type), name, cp }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::MethodDefinition : public FeatureDeclaration
- {
- OwningList<ArgumentDeclaration> arguments;
-
- /// pointer to method body. If this is a null pointer, the method has only
- /// been declared and not defined (with extern)
- std::unique_ptr<DoEndBlock> body;
- inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
- std::unique_ptr<DoEndBlock> body, const CodePosition& cp) :
- FeatureDeclaration{ std::move(type), name, cp },
- body{ std::move(body) }
- {
- }
- inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
- OwningList<ArgumentDeclaration>&& arguments, std::unique_ptr<DoEndBlock> body, const CodePosition& cp) :
- FeatureDeclaration{ std::move(type), name, cp },
- arguments(std::move(arguments)),
- body{ std::move(body) }
- {
- }
-
- inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
- const CodePosition& cp) :
- FeatureDeclaration{ std::move(type), name, cp },
- body{ nullptr }
- {
- }
-
- inline MethodDefinition(std::unique_ptr<ast::Type> type, const std::string& name,
- OwningList<ArgumentDeclaration>&& arguments, const CodePosition& cp) :
- FeatureDeclaration{ std::move(type), name, cp },
- arguments(std::move(arguments)),
- body{ nullptr }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::VariableDeclaration : public AstObject
- {
- std::unique_ptr<ast::Type> type;
- std::string name;
- inline VariableDeclaration(std::unique_ptr<ast::Type> type, std::string&& name, const CodePosition& cp) :
- AstObject{ cp },
- type{ std::move(type) },
- name{ std::move(name) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::ArgumentDeclaration :
- public VariableDeclaration
- {
- inline ArgumentDeclaration(std::unique_ptr<ast::Type> type, std::string&& name, const CodePosition& cp) :
- VariableDeclaration{ std::move(type), std::move(name), cp }
- {
- }
- //virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::Statement : public virtual AstObject
- {
- inline Statement(const CodePosition& cp) :
- AstObject{ cp } {}
-
- virtual ~Statement(void);
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::DoEndBlock : public Statement
- {
- OwningList<Statement> statements;
-
- inline DoEndBlock(OwningList<Statement>&& statements, const CodePosition& cp) :
- AstObject{ cp },
- Statement{ cp },
- statements(std::move(statements))
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::IfElseBlock : public Statement
- {
- std::unique_ptr<Expression> condition;
- std::unique_ptr<DoEndBlock> ifBlock;
- std::unique_ptr<DoEndBlock> elseBlock;
-
- inline IfElseBlock(std::unique_ptr<Expression> condition,
- std::unique_ptr<DoEndBlock> ifBlock,
- std::unique_ptr<DoEndBlock> elseBlock,
- const CodePosition& cp) :
- AstObject{ cp },
- Statement{ cp },
- condition{ std::move(condition) },
- ifBlock{ std::move(ifBlock) },
- elseBlock{ std::move(elseBlock) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::WhileBlock : public Statement
- {
- std::unique_ptr<Expression> condition;
- std::unique_ptr<DoEndBlock> body;
-
- inline WhileBlock(std::unique_ptr<Expression> condition,
- std::unique_ptr<DoEndBlock> body,
- const CodePosition& cp) :
- AstObject{ cp },
- Statement{ cp },
- condition{ std::move(condition) },
- body{ std::move(body) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::Expression : public virtual AstObject
- {
- inline Expression(const CodePosition& cp) :
- AstObject{ cp } {}
-
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::FeatureCall : public Expression, public Statement
- {
- std::unique_ptr<Expression> target;
- std::string name;
- OwningList<Expression> arguments;
- inline FeatureCall(std::unique_ptr<Expression> target, const std::string& name, const CodePosition& cp) :
- AstObject{ cp },
- Expression{ cp }, Statement{ cp },
- target(std::move(target)), name(name)
- {
- }
- inline FeatureCall(std::unique_ptr<Expression> target, const std::string& name,
- OwningList<Expression>&& arguments, const CodePosition& cp) :
- AstObject{ cp },
- Expression{ cp }, Statement{ cp },
- target(std::move(target)), name(name), arguments(std::move(arguments))
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::ReturnStatement : public Statement
- {
- std::unique_ptr<Expression> expr;
- inline ReturnStatement(std::unique_ptr<Expression>&& expr, const CodePosition& cp) :
- AstObject{ cp },
- Statement{ cp },
- expr{ std::move(expr) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::AssignmentStatement : public Statement
- {
- std::unique_ptr<Expression> target;
- std::unique_ptr<Expression> expr;
- inline AssignmentStatement(std::unique_ptr<Expression>&& target, std::unique_ptr<Expression>&& expr, const CodePosition& cp) :
- AstObject{ cp },
- Statement{ cp },
- target{ std::move(target) }, expr{ std::move(expr) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::LocalVariableStatement : public Statement
- {
- std::string name;
- std::unique_ptr<ast::Type> type;
- inline LocalVariableStatement(std::string&& name, std::unique_ptr<Type> type, const CodePosition& cp) :
- AstObject{ cp },
- Statement{ cp },
- name{ name },
- type{ std::move(type) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::AddressExpression : public Expression
- {
- std::unique_ptr<Expression> target;
- inline AddressExpression(std::unique_ptr<Expression> target,
- const CodePosition& cp) :
- AstObject{ cp },
- Expression{ cp },
- target{ std::move(target) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::IntConst : public Expression
- {
- unsigned long long value;
-
- IntConst(unsigned long long v, const CodePosition& p) :
- AstObject(p),
- Expression(p),
- value{ v } {}
-
- IntConst(std::string&& val, const CodePosition& p);
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::Operation : public Expression
- {
- std::string opString;
- CodePosition opPos;
- inline Operation(const std::string& opString, const CodePosition& cp,
- const CodePosition& opPos) :
- AstObject{ cp },
- Expression{ cp },
- opString{ opString },
- opPos{ opPos }
- {
- }
- };
- struct qlow::ast::UnaryOperation : public Operation
- {
- enum Side
- {
- PREFIX,
- SUFFIX,
- };
- Side side;
- std::unique_ptr<Expression> expr;
- inline UnaryOperation(std::unique_ptr<Expression> expr, Side side,
- const std::string& op, const CodePosition& cp,
- const CodePosition& opPos
- ) :
- AstObject{ cp },
- Operation{ op, cp, opPos },
- side{ side },
- expr{ std::move(expr) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::BinaryOperation : public Operation
- {
- std::unique_ptr<Expression> left;
- std::unique_ptr<Expression> right;
- inline BinaryOperation(std::unique_ptr<Expression> left,
- std::unique_ptr<Expression> right,
- const std::string& op,
- const CodePosition& cp,
- const CodePosition& opPos
- ) :
- AstObject{ cp },
- Operation{ op, cp, opPos },
- left{ std::move(left) },
- right{ std::move(right) }
- {
- }
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::NewArrayExpression : public Expression
- {
- std::unique_ptr<ast::Type> type;
- std::unique_ptr<Expression> length;
- inline NewArrayExpression(std::unique_ptr<ast::Type> type,
- std::unique_ptr<Expression> length,
- const CodePosition& cp) :
- AstObject{ cp },
- Expression{ cp },
- type{ std::move(type) },
- length{ std::move(length) }
- {
- }
-
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- struct qlow::ast::CastExpression : public Expression
- {
- std::unique_ptr<ast::Expression> expression;
- std::unique_ptr<ast::Type> targetType;
-
- inline CastExpression(std::unique_ptr<ast::Expression> expression,
- std::unique_ptr<ast::Type> targetType,
- const CodePosition& cp) :
- AstObject{ cp },
- Expression{ cp },
- expression{ std::move(expression) },
- targetType{ std::move(targetType) }
- {
- }
-
- virtual std::unique_ptr<sem::SemanticObject> accept(StructureVisitor& v, sem::Scope&);
- };
- #endif // QLOW_AST_H
|