aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-dataloader/v2script/AbstractSyntaxTree.cpp
blob: 5518e5d15db92d2f341c0a8a48960136cb0cfdcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <concepts>
#include <iomanip>
#include <sstream>
#include <string>
#include <utility>
#include <vector>

#include <stddef.h>

#include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp>

#include <lexy/dsl/option.hpp>
#include <lexy/input_location.hpp>

using namespace ovdl::v2script::ast;

static void _handle_string_characters(std::string& string, bool allow_newline) {
   size_t position = 0;
   for (auto& c : string) {
      switch (c) {
         case '\r':
         case '\n':
            if (allow_newline) goto END_LOOP;
            c = ' ';
            break;
         default: break;
      }
   END_LOOP:
      position++;
   }
}

void ovdl::v2script::ast::copy_into_node_ptr_vector(const std::vector<NodePtr>& source, std::vector<NodeUPtr>& dest) {
   dest.clear();
   dest.reserve(source.size());
   for (auto&& p : source) {
      dest.push_back(NodeUPtr { p });
   }
}

AbstractStringNode::AbstractStringNode() : Node({}) {}
AbstractStringNode::AbstractStringNode(NodeLocation location, std::string&& name, bool allow_newline) : Node(location),
                                                                              _name(std::move(name)) {
   _handle_string_characters(_name, allow_newline);
}
AbstractStringNode::AbstractStringNode(NodeLocation location) : Node(location) {}
AbstractStringNode::AbstractStringNode(std::string&& name, bool allow_newline) : AbstractStringNode({}, std::move(name), allow_newline) {}

std::ostream& AbstractStringNode::print(std::ostream& stream, size_t indent) const {
   return stream << _name;
}

#define OVDL_AST_STRING_NODE_DEF(NAME, ...)                                                                                                     \
   NAME::NAME() : AbstractStringNode() {}                                                                                                      \
   NAME::NAME(std::string&& name, bool allow_newline) : AbstractStringNode(std::move(name), allow_newline) {}                                  \
   NAME::NAME(lexy::nullopt) : AbstractStringNode() {}                                                                                         \
   NAME::NAME(NodeLocation location) : AbstractStringNode(location) {}                                                                         \
   NAME::NAME(NodeLocation location, std::string&& name, bool allow_newline) : AbstractStringNode(location, std::move(name), allow_newline) {} \
   NAME::NAME(NodeLocation location, lexy::nullopt) : AbstractStringNode(location, {}, true) {}                                                \
   std::ostream& NAME::print(std::ostream& stream, size_t indent) const __VA_ARGS__

OVDL_AST_STRING_NODE_DEF(IdentifierNode, {
   return stream << _name;
});

OVDL_AST_STRING_NODE_DEF(StringNode, {
   return stream << '"' << _name << '"';
});

OVDL_AST_STRING_NODE_DEF(FactorNode, {
   return stream << "factor = " << _name;
});

OVDL_AST_STRING_NODE_DEF(MonthNode, {
   return stream << "months = " << _name;
});

OVDL_AST_STRING_NODE_DEF(NameNode, {
   return stream << "name = " << _name;
});

OVDL_AST_STRING_NODE_DEF(FireOnlyNode, {
   return stream << "fire_only_once = " << _name;
});

OVDL_AST_STRING_NODE_DEF(IdNode, {
   return stream << "id = " << _name;
});

OVDL_AST_STRING_NODE_DEF(TitleNode, {
   return stream << "title = " << _name;
});

OVDL_AST_STRING_NODE_DEF(DescNode, {
   return stream << "desc = " << _name;
});

OVDL_AST_STRING_NODE_DEF(PictureNode, {
   return stream << "picture = " << _name;
});

OVDL_AST_STRING_NODE_DEF(IsTriggeredNode, {
   return stream << "is_triggered_only = " << _name;
});

#undef OVDL_AST_STRING_NODE_DEF

AssignNode::AssignNode(NodeLocation location, NodeCPtr name, NodePtr init)
   : Node(location),
     _initializer(std::move(init)) {
   if (name->is_type<IdentifierNode>()) {
      _name = cast_node_cptr<IdentifierNode>(name)._name;
   }
}

std::ostream& Node::print_ptr(std::ostream& stream, NodeCPtr node, size_t indent) {
   return node != nullptr ? node->print(stream, indent) : stream << "<NULL>";
}

static std::ostream& print_newline_indent(std::ostream& stream, size_t indent) {
   return stream << "\n"
              << std::setw(indent) << std::setfill('\t') << "";
}

/* Starts with a newline and ends at the end of a line, and so
 * should be followed by a call to print_newline_indent.
 */
static std::ostream& print_nodeuptr_vector(const std::vector<NodeUPtr>& nodes,
   std::ostream& stream, size_t indent) {
   for (NodeUPtr const& node : nodes) {
      print_newline_indent(stream, indent);
      Node::print_ptr(stream, node.get(), indent);
   }
   return stream;
}

AbstractListNode::AbstractListNode(NodeLocation location, const std::vector<NodePtr>& statements) : Node(location) {
   copy_into_node_ptr_vector(statements, _statements);
}
AbstractListNode::AbstractListNode(const std::vector<NodePtr>& statements) : AbstractListNode({}, statements) {}
std::ostream& AbstractListNode::print(std::ostream& stream, size_t indent) const {
   stream << '{';
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << "}";
}

#define OVDL_AST_LIST_NODE_DEF(NAME, ...)                                                                                 \
   NAME::NAME(const std::vector<NodePtr>& statements) : AbstractListNode(statements) {}                                  \
   NAME::NAME(lexy::nullopt) : AbstractListNode() {}                                                                     \
   NAME::NAME(NodeLocation location, const std::vector<NodePtr>& statements) : AbstractListNode(location, statements) {} \
   NAME::NAME(NodeLocation location, lexy::nullopt) : AbstractListNode(location, {}) {}                                  \
   std::ostream& NAME::print(std::ostream& stream, size_t indent) const __VA_ARGS__

OVDL_AST_LIST_NODE_DEF(FileNode, {
   print_nodeuptr_vector(_statements, stream, indent);
   return print_newline_indent(stream, indent);
});

OVDL_AST_LIST_NODE_DEF(ListNode, {
   stream << '{';
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << "}";
});

OVDL_AST_LIST_NODE_DEF(ModifierNode, {
   stream << "modifier = {";
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << '}';
});

OVDL_AST_LIST_NODE_DEF(MtthNode, {
   stream << "mean_time_to_happen = {";
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << '}';
});

OVDL_AST_LIST_NODE_DEF(EventOptionNode, {
   stream << "option = {";
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << '}';
});

OVDL_AST_LIST_NODE_DEF(BehaviorListNode, {
   stream << "ai_chance = {"; // may be ai_chance or ai_will_do
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << '}';
});

OVDL_AST_LIST_NODE_DEF(DecisionListNode, {
   stream << "political_decisions = {";
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << '}';
});

#undef OVDL_AST_LIST_NODE_DEF

EventNode::EventNode(NodeLocation location, Type type, const std::vector<NodePtr>& statements) : Node(location),
                                                                         _type(type) {
   copy_into_node_ptr_vector(statements, _statements);
}
EventNode::EventNode(Type type, const std::vector<NodePtr>& statements) : EventNode({}, type, statements) {}
std::ostream& EventNode::print(std::ostream& stream, size_t indent) const {
   switch (_type) {
      case Type::Country: stream << "country_event = "; break;
      case Type::Province: stream << "province_event = "; break;
   }
   stream << '{';
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << '}';
}

DecisionNode::DecisionNode(NodeLocation location, NodePtr name, const std::vector<NodePtr>& statements) : Node(location),
                                                                                _name(std::move(name)) {
   copy_into_node_ptr_vector(statements, _statements);
}
DecisionNode::DecisionNode(NodePtr name, const std::vector<NodePtr>& statements) : DecisionNode({}, name, statements) {}
std::ostream& DecisionNode::print(std::ostream& stream, size_t indent) const {
   print_ptr(stream, _name.get(), indent) << " = {";
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << '}';
}

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) {
      Node::print_ptr(stream, _initializer.get(), indent + 1);
   }
   return stream;
}

ExecutionListNode::ExecutionListNode(NodeLocation location, ExecutionNode::Type type, const std::vector<NodePtr>& statements) : Node(location),
                                                                                                _type(type) {
   copy_into_node_ptr_vector(statements, _statements);
}
ExecutionListNode::ExecutionListNode(ExecutionNode::Type type, const std::vector<NodePtr>& 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 << "{ ";
   switch (_type) {
      case ExecutionNode::Type::Effect: stream << "effect = {"; break;
      case ExecutionNode::Type::Trigger: stream << "trigger = {"; break;
   }
   if (!_statements.empty()) {
      print_nodeuptr_vector(_statements, stream, indent + 1);
      print_newline_indent(stream, indent);
   }
   return stream << "}}";
}

Node::operator std::string() const {
   std::stringstream ss;
   ss << *this;
   return ss.str();
}

std::ostream& AssignNode::print(std::ostream& stream, size_t indent) const {
   stream << _name << " = ";
   return Node::print_ptr(stream, _initializer.get(), indent);
}