From 1d2c5ce39d12adcb584d586952a59e15f2495f67 Mon Sep 17 00:00:00 2001 From: Spartan322 Date: Sat, 2 Sep 2023 23:56:26 -0400 Subject: Add Node Line/Column Generator Fix Errors.hpp dependency on v2script/Parser.hpp Add node location print to headless/main.cpp Add Node::line_col << operator Add Node::cast_to WARNING: Takes advantage of non-standard behavior in unordered_multimap THIS IS A HACK FOR NOW Only GCC unordered_multimap::equal_range sees elements backwards Prefer moving off of unordered_multimap to something like EASTL hash_multimap --- .../v2script/AbstractSyntaxTree.cpp | 48 +++++++++++++++------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp') diff --git a/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp b/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp index f9fb716..8dc1800 100644 --- a/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp +++ b/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -8,6 +9,8 @@ #include +#include + using namespace ovdl::v2script::ast; void ovdl::v2script::ast::copy_into_node_ptr_vector(const std::vector& source, std::vector& dest) { @@ -18,13 +21,17 @@ void ovdl::v2script::ast::copy_into_node_ptr_vector(const std::vector& } } -AbstractStringNode::AbstractStringNode(std::string&& name) : _name(std::move(name)) {} +AbstractStringNode::AbstractStringNode(NodeLocation location, std::string&& name) : Node(location), + _name(std::move(name)) {} +AbstractStringNode::AbstractStringNode(std::string&& name) : AbstractStringNode({}, std::move(name)) {} + std::ostream& AbstractStringNode::print(std::ostream& stream, size_t indent) const { return stream << _name; } -#define OVDL_AST_STRING_NODE_DEF(NAME, ...) \ - NAME::NAME(std::string&& name) : AbstractStringNode(std::move(name)) {} \ +#define OVDL_AST_STRING_NODE_DEF(NAME, ...) \ + NAME::NAME(std::string&& name) : AbstractStringNode(std::move(name)) {} \ + NAME::NAME(NodeLocation location, std::string&& name) : AbstractStringNode(location, std::move(name)) {} \ std::ostream& NAME::print(std::ostream& stream, size_t indent) const __VA_ARGS__ OVDL_AST_STRING_NODE_DEF(IdentifierNode, { @@ -73,8 +80,9 @@ OVDL_AST_STRING_NODE_DEF(IsTriggeredNode, { #undef OVDL_AST_STRING_NODE_DEF -AssignNode::AssignNode(NodeCPtr name, NodePtr init) - : _initializer(std::move(init)) { +AssignNode::AssignNode(NodeLocation location, NodeCPtr name, NodePtr init) + : Node(location), + _initializer(std::move(init)) { if (name->is_type()) { _name = cast_node_cptr(name)._name; } @@ -101,9 +109,10 @@ static std::ostream& print_nodeuptr_vector(const std::vector& nodes, return stream; } -AbstractListNode::AbstractListNode(const std::vector& statements) { +AbstractListNode::AbstractListNode(NodeLocation location, const std::vector& statements) : Node(location) { copy_into_node_ptr_vector(statements, _statements); } +AbstractListNode::AbstractListNode(const std::vector& statements) : AbstractListNode({}, statements) {} std::ostream& AbstractListNode::print(std::ostream& stream, size_t indent) const { stream << '{'; if (!_statements.empty()) { @@ -113,8 +122,9 @@ std::ostream& AbstractListNode::print(std::ostream& stream, size_t indent) const return stream << "}"; } -#define OVDL_AST_LIST_NODE_DEF(NAME, ...) \ - NAME::NAME(const std::vector& statements) : AbstractListNode(statements) {} \ +#define OVDL_AST_LIST_NODE_DEF(NAME, ...) \ + NAME::NAME(const std::vector& statements) : AbstractListNode(statements) {} \ + NAME::NAME(NodeLocation location, const std::vector& statements) : AbstractListNode(location, statements) {} \ std::ostream& NAME::print(std::ostream& stream, size_t indent) const __VA_ARGS__ OVDL_AST_LIST_NODE_DEF(FileNode, { @@ -178,9 +188,11 @@ OVDL_AST_LIST_NODE_DEF(DecisionListNode, { #undef OVDL_AST_LIST_NODE_DEF -EventNode::EventNode(Type type, const std::vector& statements) : _type(type) { +EventNode::EventNode(NodeLocation location, Type type, const std::vector& statements) : Node(location), + _type(type) { copy_into_node_ptr_vector(statements, _statements); } +EventNode::EventNode(Type type, const std::vector& statements) : EventNode({}, type, statements) {} std::ostream& EventNode::print(std::ostream& stream, size_t indent) const { switch (_type) { case Type::Country: stream << "country_event = "; break; @@ -194,9 +206,11 @@ std::ostream& EventNode::print(std::ostream& stream, size_t indent) const { return stream << '}'; } -DecisionNode::DecisionNode(NodePtr name, const std::vector& statements) : _name(std::move(name)) { +DecisionNode::DecisionNode(NodeLocation location, NodePtr name, const std::vector& statements) : Node(location), + _name(std::move(name)) { copy_into_node_ptr_vector(statements, _statements); } +DecisionNode::DecisionNode(NodePtr name, const std::vector& statements) : DecisionNode({}, name, statements) {} std::ostream& DecisionNode::print(std::ostream& stream, size_t indent) const { print_ptr(stream, _name.get(), indent) << " = {"; if (!_statements.empty()) { @@ -206,10 +220,12 @@ std::ostream& DecisionNode::print(std::ostream& stream, size_t indent) const { return stream << '}'; } -ExecutionNode::ExecutionNode(Type type, NodePtr name, NodePtr init) : _type(type), - _name(std::move(name)), - _initializer(std::move(init)) { +ExecutionNode::ExecutionNode(NodeLocation location, Type type, NodePtr name, NodePtr init) : Node(location), + _type(type), + _name(std::move(name)), + _initializer(std::move(init)) { } +ExecutionNode::ExecutionNode(Type type, NodePtr name, NodePtr init) : ExecutionNode({}, type, name, init) {} std::ostream& ExecutionNode::print(std::ostream& stream, size_t indent) const { print_ptr(stream, _name.get(), indent) << " = "; if (_initializer) { @@ -218,9 +234,11 @@ std::ostream& ExecutionNode::print(std::ostream& stream, size_t indent) const { return stream; } -ExecutionListNode::ExecutionListNode(ExecutionNode::Type type, const std::vector& statements) : _type(type) { +ExecutionListNode::ExecutionListNode(NodeLocation location, ExecutionNode::Type type, const std::vector& statements) : Node(location), + _type(type) { copy_into_node_ptr_vector(statements, _statements); } +ExecutionListNode::ExecutionListNode(ExecutionNode::Type type, const std::vector& statements) : ExecutionListNode({}, type, statements) {} std::ostream& ExecutionListNode::print(std::ostream& stream, size_t indent) const { // Only way to make a valid declared parsable file stream << "{ "; @@ -244,4 +262,4 @@ Node::operator std::string() const { std::ostream& AssignNode::print(std::ostream& stream, size_t indent) const { stream << _name << " = "; return Node::print_ptr(stream, _initializer.get(), indent); -} +} \ No newline at end of file -- cgit v1.2.3-56-ga3b1