schema_editor/comdel/parser/astnode.h

345 lines
6.0 KiB
C++

#ifndef ASTNODE_H
#define ASTNODE_H
#include "token.h"
#include <optional>
#include <vector>
/**
* 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<long long> intValue;
std::optional<std::string> stringValue;
std::optional<bool> boolValue;
std::optional<std::string> 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<bool>(_value);
return value;
}
static Value ofInt(long long _value) {
Value value;
value.type = INT;
value.intValue = std::optional<long long>(_value);
return value;
}
static Value ofString(std::string _value) {
Value value;
value.type = STRING;
value.stringValue = std::optional<std::string>(_value);
return value;
}
static Value ofIdentifier(std::string _value) {
Value value;
value.type = IDENTIFIER;
value.identifierValue = std::optional<std::string>(_value);
return value;
}
};
struct Condition
{
bool negated;
IdentifierNode functionName;
std::vector<Value> 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<IfStmt> 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<EnumerationNode> enumeration;
std::vector<Rule> rules;
};
struct PropertyNode: public AstNode
{
IdentifierNode key;
Value value;
};
struct DisplayItemNode: AstNode
{
IdentifierNode type;
std::vector<PropertyNode> values;
};
struct DisplayNode: AstNode
{
std::vector<DisplayItemNode> 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<Value> defaultValue;
std::optional<Popup> popup;
};
struct ConnectionNode: AstNode
{
IdentifierNode component;
IdentifierNode pin;
IdentifierNode bus;
std::vector<AttributeNode> attributes;
std::vector<IdentifierNode> wires;
};
struct ComponentNode: AstNode
{
enum ComponentType {
OTHER,
PROCESSOR,
MEMORY
};
IdentifierNode name;
StringNode tooltip;
StringNode source;
ComponentType type;
std::vector<Rule> rules;
StringNode instanceName;
CountNode count;
DisplayNode display;
std::vector<PinNode> pin;
std::vector<AttributeNode> attributes;
};
struct BusNode: AstNode
{
enum BusType {
AUTOMATIC,
REGULAR
};
BusType type;
IdentifierNode name;
StringNode tooltip;
CountNode count;
DisplayNode display;
std::vector<WireNode> wires;
};
struct LibraryNode: AstNode
{
StringNode name;
StringNode libraryInfo;
StringNode header;
StringNode componentDirectory;
std::vector<AddressSpace> addressSpaces;
std::vector<ComponentNode> components;
std::vector<BusNode> buses;
std::vector<ConnectionNode> connections;
std::vector<PropertyNode> messages;
};
#endif // ASTNODE_H