#ifndef ASTNODE_H #define ASTNODE_H #include "token.h" #include #include /** * AST base class, all AST node classes extand this class. Class contains basic * information about a nodes location in file. */ class AstNode { public: Span span; AstNode() = default; virtual ~AstNode(); // this is defined in Ast.cpp just so that the // compiler doesn't complain about not knowing which // object file to include the vtable in AstNode(AstNode&&) = default; AstNode& operator=(AstNode&&) = default; AstNode(const AstNode&) = default; AstNode& operator=(const AstNode&) = default; }; struct StringNode: public AstNode { std::string value; }; struct IdentifierNode: public AstNode { std::string value; }; struct NumberNode: public AstNode { long long int value; NumberNode(std::string expression); NumberNode(): value(0) {} }; struct CountNode: public AstNode { NumberNode first; NumberNode second; CountNode(NumberNode first, NumberNode second): first(first), second(second) {} CountNode() {} }; struct AddressSpace: public AstNode { IdentifierNode name; NumberNode start; NumberNode end; }; class Value: AstNode { public: enum ValueType { INT, STRING, BOOL, IDENTIFIER }; private: ValueType type; std::optional intValue; std::optional stringValue; std::optional boolValue; std::optional identifierValue; public: ValueType getType() { return type; } long long asInt() { if(is(INT)) { return intValue.value(); } throw "cannot convert type to int"; } std::string asString() { if(is(STRING)) { return stringValue.value(); } throw "cannot convert type to string"; } std::string asIdentifier() { if(is(IDENTIFIER)) { return identifierValue.value(); } throw "cannot convert type to identifier"; } bool asBool() { if(is(BOOL)) { return boolValue.value(); } throw "cannot convert type to bool"; } bool is(ValueType type) { return this->type == type; } static Value ofBool(bool _value) { Value value; value.type = BOOL; value.boolValue = std::optional(_value); return value; } static Value ofInt(long long _value) { Value value; value.type = INT; value.intValue = std::optional(_value); return value; } static Value ofString(std::string _value) { Value value; value.type = STRING; value.stringValue = std::optional(_value); return value; } static Value ofIdentifier(std::string _value) { Value value; value.type = IDENTIFIER; value.identifierValue = std::optional(_value); return value; } }; struct Condition { bool negated; IdentifierNode functionName; std::vector params; }; class Action: AstNode { public: enum ActionType { ERROR, WARNING }; ActionType type; StringNode message; }; struct IfStmt: AstNode { Condition condition; Action action; }; struct Rule: AstNode { std::vector statements; }; struct EnumerationNode: AstNode { StringNode key; Value value; }; struct Popup: AstNode { enum PopupType { AUTOMATIC, ON_DEMAND }; PopupType type; StringNode title; StringNode text; bool enumerated; std::vector enumeration; std::vector rules; }; struct PropertyNode: public AstNode { IdentifierNode key; Value value; }; struct DisplayItemNode: AstNode { IdentifierNode type; std::vector values; }; struct DisplayNode: AstNode { std::vector items; }; struct PinConnectionNode: AstNode { enum ConnectionType { CHECK_ONLY, AUTOMATICALLY }; StringNode message; ConnectionType type; }; struct PinNode: AstNode { enum PinType { IN_OUT, IN, OUT }; IdentifierNode name; PinType type; StringNode tooltip; PinConnectionNode connection; DisplayNode display; }; struct WireNode: public AstNode { enum WireType { WIRE, WIRED_AND, WIRED_OR, R_WIRE }; WireType type; IdentifierNode name; NumberNode size; /* bool isTerminatedWith; long terminatedWith; bool isIfUntrminated; long ifUnterminated; */ }; struct AttributeNode: AstNode { Value::ValueType type; IdentifierNode name; std::optional defaultValue; std::optional popup; }; struct ConnectionNode: AstNode { IdentifierNode component; IdentifierNode pin; IdentifierNode bus; std::vector attributes; std::vector wires; }; struct ComponentNode: AstNode { enum ComponentType { OTHER, PROCESSOR, MEMORY }; IdentifierNode name; StringNode tooltip; StringNode source; ComponentType type; std::vector rules; StringNode instanceName; CountNode count; DisplayNode display; std::vector pin; std::vector attributes; }; struct BusNode: AstNode { enum BusType { AUTOMATIC, REGULAR }; BusType type; IdentifierNode name; StringNode tooltip; CountNode count; DisplayNode display; std::vector wires; }; struct LibraryNode: AstNode { StringNode name; StringNode libraryInfo; StringNode header; StringNode componentDirectory; std::vector addressSpaces; std::vector components; std::vector buses; std::vector connections; std::vector messages; }; #endif // ASTNODE_H