Compare commits

...
3 Commits
Author SHA1 Message Date
Riyyi 9271b9fe01 Meta: Add to-dos 2023-03-24 23:40:02 +01:00
Riyyi 6c12b199e8 Eval: Add error message for invalid function calls 2023-03-24 23:30:22 +01:00
Riyyi 17fddc1cf4 Printer: Fix HashMap printing spacing 2023-03-24 23:24:18 +01:00
5 changed files with 25 additions and 13 deletions
+1
View File
@@ -79,6 +79,7 @@ Function::Function(Lambda lambda)
void Formatter<blaze::ASTNode*>::format(Builder& builder, blaze::ASTNode* value) const
{
// TODO: Call into Printer::dumpImp(), instead of doing it manually
if (is<blaze::String>(value)) {
return Formatter<std::string>::format(builder, static_cast<blaze::String*>(value)->data());
}
+2
View File
@@ -42,6 +42,8 @@ class GlobalEnvironment final : public Environment {
public:
GlobalEnvironment()
{
// TODO: Add more native functions
// TODO: Move the functions to their own file
auto add = [](std::span<ASTNode*> nodes) -> ASTNode* {
int64_t result = 0;
+2 -1
View File
@@ -42,7 +42,7 @@ ASTNode* Eval::evalAst(ASTNode* ast, Environment* env)
if (is<Symbol>(ast)) {
auto result = env->lookup(static_cast<Symbol*>(ast)->symbol());
if (!result) {
Error::the().addError(format("'{}' not found", ast));
Error::the().addError(format("symbols function definition is void: {}", ast));
}
return result;
}
@@ -79,6 +79,7 @@ ASTNode* Eval::apply(List* evaluated_list)
auto nodes = evaluated_list->nodes();
if (!is<Function>(nodes[0])) {
Error::the().addError(format("invalid function: {}", nodes[0]));
return nullptr;
}
+19 -12
View File
@@ -4,10 +4,12 @@
* SPDX-License-Identifier: MIT
*/
#include "lexer.h"
#include <iterator> // std::next
#include "ruc/format/print.h"
#include "error.h"
#include "lexer.h"
#include "printer.h"
#include "types.h"
@@ -53,9 +55,9 @@ void Printer::dumpImpl(ASTNode* node)
print("(");
m_firstNode = false;
m_previousNodeIsList = true;
List* list = static_cast<List*>(node);
for (size_t i = 0; i < list->nodes().size(); ++i) {
dumpImpl(list->nodes()[i]);
auto nodes = static_cast<List*>(node)->nodes();
for (size_t i = 0; i < nodes.size(); ++i) {
dumpImpl(nodes[i]);
m_previousNodeIsList = false;
}
print(")");
@@ -65,9 +67,9 @@ void Printer::dumpImpl(ASTNode* node)
print("[");
m_firstNode = false;
m_previousNodeIsList = true;
Vector* vector = static_cast<Vector*>(node);
for (size_t i = 0; i < vector->nodes().size(); ++i) {
dumpImpl(vector->nodes()[i]);
auto nodes = static_cast<Vector*>(node)->nodes();
for (size_t i = 0; i < nodes.size(); ++i) {
dumpImpl(nodes[i]);
m_previousNodeIsList = false;
}
print("]");
@@ -77,15 +79,20 @@ void Printer::dumpImpl(ASTNode* node)
print("{{");
m_firstNode = false;
m_previousNodeIsList = true;
HashMap* hash_map = static_cast<HashMap*>(node);
for (auto element : hash_map->elements()) {
print("{} ", element.first.front() == 0x7f ? ":" + element.first.substr(1) : element.first); // 127
dumpImpl(element.second);
m_previousNodeIsList = false;
auto elements = static_cast<HashMap*>(node)->elements();
for (auto it = elements.begin(); it != elements.end(); ++it) {
print("{} ", it->first.front() == 0x7f ? ":" + it->first.substr(1) : it->first); // 127
dumpImpl(it->second);
if (it != elements.end() && std::next(it) != elements.end()) {
print(" ");
}
}
m_previousNodeIsList = false;
print("}}");
}
else if (is<String>(node)) {
// TODO: Implement string readably printing
printSpacing();
print("{}", static_cast<String*>(node)->data());
}
+1
View File
@@ -38,6 +38,7 @@ void Reader::read()
m_node = readImpl();
// TODO: Move these to the appropriate functions
// Error checking
if (m_invalid_syntax) {