Skip to content

Commit

Permalink
feat(error): replaced Node::operator<< with Node::debugPrint, using N…
Browse files Browse the repository at this point in the history
…ode::repr in error messages as it is more suited
  • Loading branch information
SuperFola committed May 6, 2024
1 parent f0ee7c1 commit ce12a4c
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- macros can be declared inside a begin block within a cond macro and used in the scope surrounding the cond macro
- `arkscript --version` and `arkscript --help` now output ArkScript version with the commit hash
- `void Value::toString(std::ostream&, VM&)` now becomes `std::string Value::toString(VM&)`
- removed `Node::operator<<` to replace it with `Node::debugPrint`

### Removed
- removed unused `NodeType::Closure`
Expand Down
10 changes: 7 additions & 3 deletions include/Ark/Compiler/AST/Node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ namespace Ark::internal
*/
[[nodiscard]] std::string repr() const noexcept;

friend ARK_API std::ostream& operator<<(std::ostream& os, const Node& N) noexcept;
/**
* @brief Print a node to an output stream with added type annotations
* @param os
* @return
*/
[[nodiscard]] std::ostream& debugPrint(std::ostream& os) const noexcept;

friend bool operator==(const Node& A, const Node& B);
friend bool operator<(const Node& A, const Node& B);
friend bool operator!(const Node& A);
Expand All @@ -186,8 +192,6 @@ namespace Ark::internal
std::string m_after_comment; ///< Comment after node
};

ARK_API std::ostream& operator<<(std::ostream& os, const std::vector<Node>& node) noexcept;

const Node& getTrueNode();
const Node& getFalseNode();
const Node& getNilNode();
Expand Down
30 changes: 15 additions & 15 deletions src/arkreactor/Compiler/AST/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,21 +206,21 @@ namespace Ark::internal
return data;
}

std::ostream& operator<<(std::ostream& os, const Node& node) noexcept
std::ostream& Node::debugPrint(std::ostream& os) const noexcept
{
switch (node.m_type)
switch (m_type)
{
case NodeType::Symbol:
os << "Symbol:" << node.string();
os << "Symbol:" << string();
break;

case NodeType::Capture:
os << "Capture:" << node.string();
os << "Capture:" << string();
break;

case NodeType::Keyword:
os << "Keyword:";
switch (node.keyword())
switch (keyword())
{
case Keyword::Fun: os << "Fun"; break;
case Keyword::Let: os << "Let"; break;
Expand All @@ -235,40 +235,40 @@ namespace Ark::internal
break;

case NodeType::String:
os << "String:" << node.string();
os << "String:" << string();
break;

case NodeType::Number:
os << "Number:" << node.number();
os << "Number:" << number();
break;

case NodeType::List:
os << "( ";
for (const auto& i : node.constList())
os << i << " ";
for (const auto& i : constList())
i.debugPrint(os) << " ";
os << ")";
break;

case NodeType::Field:
os << "( Field ";
for (const auto& i : node.constList())
os << i << " ";
for (const auto& i : constList())
i.debugPrint(os) << " ";
os << ")";
break;

case NodeType::Macro:
os << "( Macro ";
for (const auto& i : node.constList())
os << i << " ";
for (const auto& i : constList())
i.debugPrint(os) << " ";
os << ")";
break;

case NodeType::Spread:
os << "Spread:" << node.string();
os << "Spread:" << string();
break;

case NodeType::Unused:
os << "Unused:" << node.string();
os << "Unused:" << string();
break;

default:
Expand Down
7 changes: 1 addition & 6 deletions src/arkreactor/Compiler/AST/Optimizer.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include <Ark/Compiler/AST/Optimizer.hpp>

#include <sstream>

namespace Ark::internal
{
Optimizer::Optimizer(uint16_t options) noexcept :
Expand All @@ -23,10 +21,7 @@ namespace Ark::internal

void Optimizer::throwOptimizerError(const std::string& message, const Node& node)
{
std::stringstream ss;
ss << node;

throw CodeError(message, node.filename(), node.line(), node.col(), ss.str());
throw CodeError(message, node.filename(), node.line(), node.col(), node.repr());
}

void Optimizer::remove_unused()
Expand Down
4 changes: 1 addition & 3 deletions src/arkreactor/Compiler/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,7 @@ namespace Ark

void Compiler::throwCompilerError(const std::string& message, const Node& node)
{
std::stringstream ss;
ss << node;
throw CodeError(message, node.filename(), node.line(), node.col(), ss.str());
throw CodeError(message, node.filename(), node.line(), node.col(), node.repr());
}

void Compiler::compileExpression(const Node& x, int p, bool is_result_unused, bool is_terminal, const std::string& var_name)
Expand Down
2 changes: 1 addition & 1 deletion src/arkreactor/Compiler/Macros/Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace Ark::internal
if (m_debug >= 3)
{
std::cout << "(MacroProcessor) AST after processing macros\n";
std::cout << m_ast << '\n';
m_ast.debugPrint(std::cout) << '\n';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/arkreactor/Exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ namespace Ark::Diagnostics
message,
node.filename(),
(node.filename() == ARK_NO_NAME_FILE) ? "" : Utils::readFile(node.filename()),
node,
node.repr(),
node.line(),
node.col(),
size);
Expand Down
4 changes: 2 additions & 2 deletions tests/unittests/ParserSuite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ std::string astToString(Ark::internal::Parser& parser)

std::stringstream ss;
for (auto it = parser.ast().constList().begin() + 1, end = parser.ast().constList().end(); it != end; ++it)
ss << *it << "\n";
it->debugPrint(ss) << "\n";

const auto& imports = parser.imports();

Expand Down Expand Up @@ -96,4 +96,4 @@ ut::suite<"Parser"> parser_suite = [] {
}
});
};
};
};

0 comments on commit ce12a4c

Please sign in to comment.