8 Pass(
"Optimizer", debug), m_ast()
45 for (
auto& child : node.
list())
54 const auto keyword = node.
constList().front().keyword();
55 const auto condition = node.
constList()[1];
58 if (condition.nodeType() ==
NodeType::Symbol && condition.string() ==
"false")
65 const auto back = node.
constList().back();
80 for (
auto& child : node.
list())
89 for (
auto& child : node.
list())
91 if (child.nodeType() ==
NodeType::List && !child.constList().empty() &&
94 const Keyword kw = child.constList()[0].keyword();
107 const std::string name = child.constList()[1].string();
115 else if (child.comment().find(
"@deprecated") != std::string::npos)
119 if (
auto result = std::ranges::find_if(vec, [](
const std::string& line) {
120 return line.find(
"@deprecated") != std::string::npos;
124 advice = result->substr(result->find(
"@deprecated") + 11);
129 "Using a deprecated {} `{}'.{}",
131 !child.constList()[2].constList().empty() &&
133 child.constList()[2].constList()[0].keyword() ==
Keyword::Fun
137 advice.empty() ?
"" :
" " + advice);
Lots of utilities about string, filesystem and more.
Optimizes a given ArkScript AST.
void debug(const Logger::MessageAndLocation &data, Args &&... args)
Write a debug level log using fmtlib.
void trace(const char *fmt, Args &&... args)
Write a trace level log using fmtlib.
void warn(const char *fmt, Args &&... args)
Write a warn level log using fmtlib.
void traceStart(std::string &&trace_name)
A node of an Abstract Syntax Tree for ArkScript.
NodeType nodeType() const noexcept
Return the node type.
bool isListLike() const noexcept
Check if the node is a list like node.
const std::string & string() const noexcept
Return the string held by the value (if the node type allows it)
const std::vector< Node > & constList() const noexcept
Return the list of sub-nodes held by the node.
Namespace & arkNamespace() noexcept
Return the namespace held by the value (if the node type allows it)
std::ostream & debugPrint(std::ostream &os) const noexcept
Print a node to an output stream with added type annotations.
std::vector< Node > & list() noexcept
Return the list of sub-nodes held by the node.
Optimizer(unsigned debug) noexcept
Construct a new Optimizer.
void pruneUnusedGlobalVariables(Node &node)
Remove unused global variables from the AST.
const Node & ast() const noexcept
Returns the modified AST.
void countAndPruneDeadCode(Node &node)
Count the occurrences of each symbol in the AST, recursively, and prune if false/true,...
std::unordered_map< std::string, unsigned > m_sym_appearances
void process(const Node &ast)
Send the AST to the optimizer, then run the different optimization strategies on it.
An interface to describe compiler passes.
std::vector< std::string > splitString(const std::string &source, const char sep)
Cut a string into pieces, given a character separator.
ARK_API void trimWhitespace(std::string &line)
Remove whitespaces at the start and end of a string.
Keyword
The different keywords available.
std::shared_ptr< Node > ast