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 1.1
6 * @date 2024-07-09
7 *
8 * @copyright Copyright (c) 2020-2024
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
19#include <Ark/Compiler/Pass.hpp>
21
22namespace Ark::internal
23{
24 /**
25 * @brief The ArkScript AST optimizer
26 *
27 */
28 class 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 */
69 };
70}
71
72#endif
AST node used by the parser, optimizer and compiler.
Interface for a compiler pass (take in an AST, output an AST)
A node of an Abstract Syntax Tree for ArkScript.
Definition Node.hpp:31
The ArkScript AST optimizer.
Definition Optimizer.hpp:29
Optimizer(unsigned debug) noexcept
Construct a new Optimizer.
Definition Optimizer.cpp:5
void pruneUnusedGlobalVariables(Node &node)
Remove unused global variables from the AST.
Definition Optimizer.cpp:85
const Node & ast() const noexcept override
Returns the modified AST.
Definition Optimizer.cpp:28
void countAndPruneDeadCode(Node &node)
Count the occurrences of each symbol in the AST, recursively, and prune if false/true,...
Definition Optimizer.cpp:33
std::unordered_map< std::string, unsigned > m_sym_appearances
Definition Optimizer.hpp:54
void process(const Node &ast) override
Send the AST to the optimizer, then run the different optimization strategies on it.
Definition Optimizer.cpp:9
An interface to describe compiler passes.
Definition Pass.hpp:23