ArkScript
A small, fast, functional and scripting language for video games
Optimizer.hpp
Go to the documentation of this file.
1/**
2 * @file Optimizer.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Optimizes a given ArkScript AST
5 * @version 0.3
6 * @date 2020-10-27
7 *
8 * @copyright Copyright (c) 2020-2021
9 *
10 */
11
12#ifndef COMPILER_AST_OPTIMIZER_HPP
13#define COMPILER_AST_OPTIMIZER_HPP
14
15#include <functional>
16#include <unordered_map>
17#include <string>
18#include <cinttypes>
19
21#include <Ark/Exceptions.hpp>
22#include <Ark/Constants.hpp>
24
25namespace Ark::internal
26{
27 /**
28 * @brief The ArkScript AST optimizer
29 *
30 */
32 {
33 public:
34 /**
35 * @brief Construct a new Optimizer
36 *
37 */
38 explicit Optimizer(uint16_t options) noexcept;
39
40 /**
41 * @brief Send the AST to the optimizer, then run the different optimization strategies on it
42 *
43 * @param ast
44 */
45 void feed(const Node& ast);
46
47 /**
48 * @brief Returns the modified AST
49 *
50 * @return const Node&
51 */
52 const Node& ast() const noexcept;
53
54 private:
56 uint16_t m_options;
57 std::unordered_map<std::string, unsigned> m_sym_appearances;
58
59 /**
60 * @brief Generate a fancy error message
61 *
62 * @param message
63 * @param node
64 */
65 [[noreturn]] void throwOptimizerError(const std::string& message, const Node& node);
66
67 /**
68 * @brief Iterate over the AST and remove unused top level functions and constants
69 *
70 */
71 void remove_unused();
72
73 /**
74 * @brief Run a given functor on the global scope symbols
75 *
76 * @param node
77 * @param func
78 */
79 void runOnGlobalScopeVars(Node& node, const std::function<void(Node&, Node&, int)>& func);
80
81 /**
82 * @brief Count the occurences of each symbol in the AST, recursively
83 *
84 * @param node
85 */
86 void countOccurences(Node& node);
87 };
88}
89
90#endif
Constants used by ArkScript.
ArkScript homemade exceptions.
AST node used by the parser, optimizer and compiler.
A node of an Abstract Syntax Tree for ArkScript.
Definition: Node.hpp:29
The ArkScript AST optimizer.
Definition: Optimizer.hpp:32
void feed(const Node &ast)
Send the AST to the optimizer, then run the different optimization strategies on it.
Definition: Optimizer.cpp:9
void throwOptimizerError(const std::string &message, const Node &node)
Generate a fancy error message.
Definition: Optimizer.cpp:22
void remove_unused()
Iterate over the AST and remove unused top level functions and constants.
Definition: Optimizer.cpp:27
const Node & ast() const noexcept
Returns the modified AST.
Definition: Optimizer.cpp:17
void countOccurences(Node &node)
Count the occurences of each symbol in the AST, recursively.
Definition: Optimizer.cpp:73
void runOnGlobalScopeVars(Node &node, const std::function< void(Node &, Node &, int)> &func)
Run a given functor on the global scope symbols.
Definition: Optimizer.cpp:47
std::unordered_map< std::string, unsigned > m_sym_appearances
Definition: Optimizer.hpp:57
Create string error context for AST errors.