Line data Source code
1 : #include "../../../inc/visitors/codeGeneration/codeGeneration.h"
2 :
3 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_case.h"
4 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_default.h"
5 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_elseIf.h"
6 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_if.h"
7 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_switch.h"
8 : #include "../../../inc/parsingAnalysis/ast/conditionals/ast_ternary.h"
9 : #include <cstddef>
10 : #include <memory>
11 : #include <variant>
12 :
13 : namespace nicole {
14 :
15 : std::expected<std::shared_ptr<llvm::Value>, Error>
16 0 : CodeGeneration::visit(const AST_IF *node) const noexcept {
17 0 : if (!node) {
18 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_IF");
19 0 : }
20 0 : const auto condition{node->condition()->accept(*this)};
21 0 : if (!condition) {
22 0 : return createError(condition.error());
23 0 : }
24 0 : const auto body{node->body()->accept(*this)};
25 0 : if (!body) {
26 0 : return createError(body.error());
27 0 : }
28 0 : for (const auto &elseif_ : node->elseIf()) {
29 0 : const auto result{elseif_->accept(*this)};
30 0 : if (!result) {
31 0 : return createError(result.error());
32 0 : }
33 0 : }
34 0 : if (node->elseBody()) {
35 0 : const auto elseBody{node->elseBody()->accept(*this)};
36 0 : if (!elseBody) {
37 0 : return createError(elseBody.error());
38 0 : }
39 0 : }
40 0 : return {};
41 0 : }
42 :
43 : std::expected<std::shared_ptr<llvm::Value>, Error>
44 0 : CodeGeneration::visit(const AST_ELSE_IF *node) const noexcept {
45 0 : if (!node) {
46 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_ELSE_IF");
47 0 : }
48 0 : const auto condition{node->condition()->accept(*this)};
49 0 : if (!condition) {
50 0 : return createError(condition.error());
51 0 : }
52 0 : const auto result{node->body()->accept(*this)};
53 0 : if (!result) {
54 0 : return createError(result.error());
55 0 : }
56 0 : return {};
57 0 : }
58 :
59 : std::expected<std::shared_ptr<llvm::Value>, Error>
60 0 : CodeGeneration::visit(const AST_SWITCH *node) const noexcept {
61 0 : if (!node) {
62 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_SWITCH");
63 0 : }
64 0 : const auto condition{node->condition()->accept(*this)};
65 0 : if (!condition) {
66 0 : return createError(condition.error());
67 0 : }
68 0 : for (const auto &case_ : node->cases()) {
69 0 : const auto result{case_->accept(*this)};
70 0 : if (!result) {
71 0 : return createError(result.error());
72 0 : }
73 0 : }
74 0 : if (node->defaultCase()) {
75 0 : const auto defaultCase{node->defaultCase()->accept(*this)};
76 0 : if (!defaultCase) {
77 0 : return createError(defaultCase.error());
78 0 : }
79 0 : }
80 0 : return {};
81 0 : }
82 :
83 : std::expected<std::shared_ptr<llvm::Value>, Error>
84 0 : CodeGeneration::visit(const AST_CASE *node) const noexcept {
85 0 : if (!node) {
86 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_CASE");
87 0 : }
88 0 : const auto match{node->match()->accept(*this)};
89 0 : if (!match) {
90 0 : return createError(match.error());
91 0 : }
92 0 : const auto result{node->body()->accept(*this)};
93 0 : if (!result) {
94 0 : return createError(result.error());
95 0 : }
96 0 : return {};
97 0 : }
98 :
99 : std::expected<std::shared_ptr<llvm::Value>, Error>
100 0 : CodeGeneration::visit(const AST_DEFAULT *node) const noexcept {
101 0 : if (!node) {
102 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_DEFAULT");
103 0 : }
104 0 : const auto result{node->body()->accept(*this)};
105 0 : if (!result) {
106 0 : return createError(result.error());
107 0 : }
108 0 : return {};
109 0 : }
110 :
111 : std::expected<std::shared_ptr<llvm::Value>, Error>
112 0 : CodeGeneration::visit(const AST_TERNARY *node) const noexcept {
113 0 : if (!node) {
114 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_TERNARY");
115 0 : }
116 0 : const auto condition{node->condition()->accept(*this)};
117 0 : if (!condition) {
118 0 : return createError(condition.error());
119 0 : }
120 0 : const auto first{node->first()->accept(*this)};
121 0 : if (!first) {
122 0 : return createError(first.error());
123 0 : }
124 0 : const auto second{node->second()->accept(*this)};
125 0 : if (!second) {
126 0 : return createError(second.error());
127 0 : }
128 0 : return {};
129 0 : }
130 :
131 : std::expected<std::shared_ptr<llvm::Value>, Error>
132 0 : CodeGeneration::visit(const AST_CONDITION *node) const noexcept {
133 0 : if (!node) {
134 0 : return createError(ERROR_TYPE::NULL_NODE, "invalid AST_CONDITION");
135 0 : }
136 0 : return node->condition()->accept(*this);
137 0 : }
138 :
139 : } // namespace nicole
|