aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/vm/Codegen.cpp
blob: b84078ea60fbc94510ce8fe937ac3f74d9bba725 (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
#include "Codegen.hpp"

#include <cctype>
#include <string_view>

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

#include <dryad/node.hpp>
#include <dryad/tree.hpp>

#include <range/v3/algorithm/equal.hpp>

#include "tsl/ordered_map.h"
#include <lauf/asm/builder.h>

using namespace OpenVic::Vm;
using namespace ovdl::v2script::ast;
using namespace ovdl::v2script;

using namespace std::string_view_literals;

bool ichar_equals(char a, char b) {
   return std::tolower(static_cast<unsigned char>(a)) == std::tolower(static_cast<unsigned char>(b));
}

void Codegen::generate_effect_from(Parser const& parser, Node* node) {
   bool is_prepping_args = false;
   tsl::ordered_map<std::string_view, std::string_view> prepped_arguments;

   dryad::visit_tree(
      node, //
      [&](dryad::child_visitor<NodeKind> visitor, AssignStatement* statement) {
         auto const* left = dryad::node_try_cast<FlatValue>(statement->left());
         if (!left) {
            return;
         }

         auto const* right = dryad::node_try_cast<FlatValue>(statement->right());
         if (!right) {
            is_prepping_args = true; // TODO: determine if scope or list-arg effect
            visitor(right);
         } else if (ranges::equal(right->value().view(), "yes"sv, ichar_equals)) {
            // TODO: insert vic2 bytecode scope object address here
            // TODO: calls a builtin for Victoria 2 effects?
         } else if (!ranges::equal(right->value().view(), "no"sv, ichar_equals)) {
            // TODO: single argument execution
         }
      },
      [&](FlatValue* value) {
         // TODO: handle right side
      }
   );
}

void Codegen::generate_condition_from(Parser const& parser, Node* node) {
   bool is_prepping_args = false;
   tsl::ordered_map<std::string_view, std::string_view> prepped_arguments;

   dryad::visit_tree(
      node, //
      [&](dryad::child_visitor<NodeKind> visitor, AssignStatement* statement) {
         auto const* left = dryad::node_try_cast<FlatValue>(statement->left());
         if (!left) {
            return;
         }

         auto const* right = dryad::node_try_cast<FlatValue>(statement->right());
         if (!right) {
            is_prepping_args = true; // TODO: determine if scope or list-arg effect
            visitor(right);
         } else if (ranges::equal(right->value().view(), "yes"sv, ichar_equals)) {
            // TODO: insert vic2 bytecode scope object address here
            // TODO: calls a builtin for Victoria 2 triggers?
         } else if (!ranges::equal(right->value().view(), "no"sv, ichar_equals)) {
            // TODO: single argument execution
         }
      },
      [&](FlatValue* value) {
         // TODO: handle right side
      }
   );
}