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 * @date 2024-07-09
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef COMPILER_AST_OPTIMIZER_HPP
12#define COMPILER_AST_OPTIMIZER_HPP
13
14#include <functional>
15#include <unordered_map>
16#include <string>
17
18#include <Ark/Platform.hpp>
19#include <Ark/Compiler/Pass.hpp>
21
22namespace Ark::internal
23{
24 /**
25 * @brief The ArkScript AST optimizer
26 *
27 */
28 class ARK_API Optimizer final : public Pass
29 {
30 public:
31 /**
32 * @brief Construct a new Optimizer
33 *
34 * @param debug level of debug
35 */
36 explicit Optimizer(unsigned debug) noexcept;
37
38 /**
39 * @brief Send the AST to the optimizer, then run the different optimization strategies on it
40 *
41 * @param ast
42 */
43 void process(const Node& ast) override;
44
45 /**
46 * @brief Returns the modified AST
47 *
48 * @return const Node&
49 */
50 [[nodiscard]] const Node& ast() const noexcept override;
51
52 private:
54 std::unordered_map<std::string, unsigned> m_sym_appearances;
55
56 /**
57 * @brief Count the occurrences of each symbol in the AST, recursively, and prune if false/true, while false/true
58 *
59 * @param node
60 */
61 void countAndPruneDeadCode(Node& node);
62
63 /**
64 * @brief Remove unused global variables from the AST
65 *
66 * @param node
67 */
68 void pruneUnusedGlobalVariables(Node& node);
69 };
70}
71
72#endif
#define ARK_API
Definition Module.hpp:28
AST node used by the parser, optimizer and compiler.
Interface for a compiler pass (take in an AST, output an AST)
ArkScript configuration macros.
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:30
The ArkScript AST optimizer.
Definition Optimizer.hpp:29
std::unordered_map< std::string, unsigned > m_sym_appearances
Definition Optimizer.hpp:54
An interface to describe compiler passes.
Definition Pass.hpp:23