diff -r e15a577a0bfe -r d1ac217c9165 src/script/ast.h --- a/src/script/ast.h Tue Feb 03 04:03:19 2015 +0200 +++ b/src/script/ast.h Tue Feb 03 15:26:07 2015 +0200 @@ -1,23 +1,75 @@ #pragma once #include #include +#include "../main.h" namespace Script { - using AstPointer = QSharedPointer; - - enum AstNodeType - { - - }; - - class AstNode + namespace Ast { - public: - AstNode (AstPointer parent); + using NodePointer = QSharedPointer; + using RootPointer = QSharedPointer; + using MacroPointer = QSharedPointer; + + enum NodeType + { + ROOT, + MACRO, + }; + + class BaseNode + { + public: + BaseNode (NodePointer parent, NodeType type); + virtual ~BaseNode() = 0; + + NodeType type() const { return m_type; } + NodePointer parent() const { return m_parent; } + QVector children() const { return m_children; } + void addChild (NodePointer child) { m_children.append (child); } + void dump(); + virtual QString describe() const = 0; + + private: + QVector m_children; + NodePointer m_parent; + NodeType m_type; + }; - private: - QVector m_children; - AstPointer m_parent; - }; + class RootNode : public BaseNode + { + public: + RootNode() : BaseNode (NodePointer(), ROOT) {} + QString describe() const override; + }; + + class MacroNode : public BaseNode + { + public: + MacroNode (NodePointer parent, QString macroName) : + BaseNode (parent, MACRO), + m_macroName (macroName) {} + + QString macroName() const { return m_macroName; } + QString describe() const override; + + private: + QString m_macroName; + }; + + template + QSharedPointer spawn (NodePointer parent, Args&&... args) + { + static_assert (std::is_base_of::value, + "Script::Ast::spawn can only be used for AST types."); + QSharedPointer ast (new T (parent, args...)); + parent->addChild (ast); + return ast; + } + + inline RootPointer spawnRoot() + { + return RootPointer (new RootNode()); + } + } }