From c53c0af5a27ee91c54d79d306ef9b7f41de3feb2 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 2 Apr 2023 15:30:26 +0200 Subject: [PATCH] Everywhere: Rename addNode and addError functions -> add --- src/ast.cpp | 2 +- src/ast.h | 2 +- src/environment.cpp | 2 +- src/error.h | 4 +-- src/eval.cpp | 40 ++++++++++----------- src/functions.cpp | 84 ++++++++++++++++++++++----------------------- src/lexer.cpp | 2 +- src/reader.cpp | 60 ++++++++++++++++---------------- 8 files changed, 98 insertions(+), 98 deletions(-) diff --git a/src/ast.cpp b/src/ast.cpp index 2431b10..dd32077 100644 --- a/src/ast.cpp +++ b/src/ast.cpp @@ -16,7 +16,7 @@ namespace blaze { -void Collection::addNode(ASTNodePtr node) +void Collection::add(ASTNodePtr node) { m_nodes.push_back(node); } diff --git a/src/ast.h b/src/ast.h index aa2060b..7774298 100644 --- a/src/ast.h +++ b/src/ast.h @@ -56,7 +56,7 @@ public: virtual bool isCollection() const override { return true; } - void addNode(ASTNodePtr node); + void add(ASTNodePtr node); const std::list& nodes() const { return m_nodes; } size_t size() const { return m_nodes.size(); } diff --git a/src/environment.cpp b/src/environment.cpp index 498be83..964fb8d 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -45,7 +45,7 @@ EnvironmentPtr Environment::create(const ASTNodePtr lambda, std::list(); for (; it != arguments.end(); ++it) { - list->addNode(*it); + list->add(*it); } env->set(bindings[i + 1], list); diff --git a/src/error.h b/src/error.h index 90eea3c..08c9884 100644 --- a/src/error.h +++ b/src/error.h @@ -24,8 +24,8 @@ public: m_token_errors.clear(); m_other_errors.clear(); } - void addError(Token error) { m_token_errors.push_back(error); } - void addError(const std::string& error) { m_other_errors.push_back(error); } + void add(Token error) { m_token_errors.push_back(error); } + void add(const std::string& error) { m_other_errors.push_back(error); } bool hasTokenError() { return m_token_errors.size() > 0; } bool hasOtherError() { return m_other_errors.size() > 0; } diff --git a/src/eval.cpp b/src/eval.cpp index 8d762c8..74ee110 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -82,7 +82,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env) if (is(ast_raw_ptr)) { auto result = env->get(std::static_pointer_cast(ast)->symbol()); if (!result) { - Error::the().addError(format("'{}' not found", ast)); + Error::the().add(format("'{}' not found", ast)); return nullptr; } return result; @@ -95,7 +95,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env) if (eval_node == nullptr) { return nullptr; } - result->addNode(eval_node); + result->add(eval_node); } return result; } @@ -107,7 +107,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env) if (eval_node == nullptr) { return nullptr; } - result->addNode(eval_node); + result->add(eval_node); } return result; } @@ -130,7 +130,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env) ASTNodePtr Eval::evalDef(const std::list& nodes, EnvironmentPtr env) { if (nodes.size() != 2) { - Error::the().addError(format("wrong number of arguments: def!, {}", nodes.size())); + Error::the().add(format("wrong number of arguments: def!, {}", nodes.size())); return nullptr; } @@ -139,7 +139,7 @@ ASTNodePtr Eval::evalDef(const std::list& nodes, EnvironmentPtr env) // First element needs to be a Symbol if (!is(first_argument.get())) { - Error::the().addError(format("wrong argument type: symbol, {}", first_argument)); + Error::the().add(format("wrong argument type: symbol, {}", first_argument)); return nullptr; } @@ -158,7 +158,7 @@ ASTNodePtr Eval::evalDef(const std::list& nodes, EnvironmentPtr env) ASTNodePtr Eval::evalLet(const std::list& nodes, EnvironmentPtr env) { if (nodes.size() != 2) { - Error::the().addError(format("wrong number of arguments: let*, {}", nodes.size())); + Error::the().add(format("wrong number of arguments: let*, {}", nodes.size())); return nullptr; } @@ -167,7 +167,7 @@ ASTNodePtr Eval::evalLet(const std::list& nodes, EnvironmentPtr env) // First argument needs to be a List or Vector if (!is(first_argument.get())) { - Error::the().addError(format("wrong argument type: collection, '{}'", first_argument)); + Error::the().add(format("wrong argument type: collection, '{}'", first_argument)); return nullptr; } @@ -179,7 +179,7 @@ ASTNodePtr Eval::evalLet(const std::list& nodes, EnvironmentPtr env) // List or Vector needs to have an even number of elements size_t count = binding_nodes.size(); if (count % 2 != 0) { - Error::the().addError(format("wrong number of arguments: {}, {}", "let* bindings", count)); + Error::the().add(format("wrong number of arguments: {}, {}", "let* bindings", count)); return nullptr; } @@ -189,7 +189,7 @@ ASTNodePtr Eval::evalLet(const std::list& nodes, EnvironmentPtr env) for (auto it = binding_nodes.begin(); it != binding_nodes.end(); std::advance(it, 2)) { // First element needs to be a Symbol if (!is(*it->get())) { - Error::the().addError(format("wrong argument type: symbol, '{}'", *it)); + Error::the().add(format("wrong argument type: symbol, '{}'", *it)); return nullptr; } @@ -206,7 +206,7 @@ ASTNodePtr Eval::evalLet(const std::list& nodes, EnvironmentPtr env) ASTNodePtr Eval::evalDo(const std::list& nodes, EnvironmentPtr env) { if (nodes.size() == 0) { - Error::the().addError(format("wrong number of arguments: do, {}", nodes.size())); + Error::the().add(format("wrong number of arguments: do, {}", nodes.size())); return nullptr; } @@ -222,7 +222,7 @@ ASTNodePtr Eval::evalDo(const std::list& nodes, EnvironmentPtr env) ASTNodePtr Eval::evalIf(const std::list& nodes, EnvironmentPtr env) { if (nodes.size() != 2 && nodes.size() != 3) { - Error::the().addError(format("wrong number of arguments: if, {}", nodes.size())); + Error::the().add(format("wrong number of arguments: if, {}", nodes.size())); return nullptr; } @@ -240,16 +240,16 @@ ASTNodePtr Eval::evalIf(const std::list& nodes, EnvironmentPtr env) } } -#define ARG_COUNT_CHECK(name, size, comparison) \ - if (size comparison) { \ - Error::the().addError(format("wrong number of arguments: {}, {}", name, size)); \ - return nullptr; \ +#define ARG_COUNT_CHECK(name, size, comparison) \ + if (size comparison) { \ + Error::the().add(format("wrong number of arguments: {}, {}", name, size)); \ + return nullptr; \ } -#define AST_CHECK(type, value) \ - if (!is(value.get())) { \ - Error::the().addError(format("wrong argument type: {}, {}", #type, value)); \ - return nullptr; \ +#define AST_CHECK(type, value) \ + if (!is(value.get())) { \ + Error::the().add(format("wrong argument type: {}, {}", #type, value)); \ + return nullptr; \ } #define AST_CAST(type, value, variable) \ @@ -285,7 +285,7 @@ ASTNodePtr Eval::apply(std::shared_ptr evaluated_list) auto nodes = evaluated_list->nodes(); if (!is(nodes.front().get()) && !is(nodes.front().get())) { - Error::the().addError(format("invalid function: {}", nodes.front())); + Error::the().add(format("invalid function: {}", nodes.front())); return nullptr; } diff --git a/src/functions.cpp b/src/functions.cpp index 6c30982..13c9aa1 100644 --- a/src/functions.cpp +++ b/src/functions.cpp @@ -27,7 +27,7 @@ void GlobalEnvironment::add() for (auto node : nodes) { if (!is(node.get())) { - Error::the().addError(format("wrong argument type: number, '{}'", node)); + Error::the().add(format("wrong argument type: number, '{}'", node)); return nullptr; } @@ -49,7 +49,7 @@ void GlobalEnvironment::sub() for (auto node : nodes) { if (!is(node.get())) { - Error::the().addError(format("wrong argument type: number, '{}'", node)); + Error::the().add(format("wrong argument type: number, '{}'", node)); return nullptr; } } @@ -75,7 +75,7 @@ void GlobalEnvironment::mul() for (auto node : nodes) { if (!is(node.get())) { - Error::the().addError(format("wrong argument type: number, '{}'", node)); + Error::the().add(format("wrong argument type: number, '{}'", node)); return nullptr; } @@ -92,13 +92,13 @@ void GlobalEnvironment::div() { auto div = [this](std::list nodes) -> ASTNodePtr { if (nodes.size() == 0) { - Error::the().addError(format("wrong number of arguments: {}, 0", m_current_key)); + Error::the().add(format("wrong number of arguments: {}, 0", m_current_key)); return nullptr; } for (auto node : nodes) { if (!is(node.get())) { - Error::the().addError(format("wrong argument type: number, '{}'", node)); + Error::the().add(format("wrong argument type: number, '{}'", node)); return nullptr; } } @@ -119,38 +119,38 @@ void GlobalEnvironment::div() // ----------------------------------------- -#define NUMBER_COMPARE(symbol, comparison_symbol) \ - auto lambda = [this](std::list nodes) -> ASTNodePtr { \ - bool result = true; \ - \ - if (nodes.size() < 2) { \ - Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); \ - return nullptr; \ - } \ - \ - for (auto node : nodes) { \ - if (!is(node.get())) { \ - Error::the().addError(format("wrong argument type: number, '{}'", node)); \ - return nullptr; \ - } \ - } \ - \ - /* Start with the first number */ \ - int64_t number = std::static_pointer_cast(nodes.front())->number(); \ - \ - /* Skip the first node */ \ - for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \ - int64_t current_number = std::static_pointer_cast(*it)->number(); \ - if (number comparison_symbol current_number) { \ - result = false; \ - break; \ - } \ - number = current_number; \ - } \ - \ - return makePtr((result) ? Value::True : Value::False); \ - }; \ - \ +#define NUMBER_COMPARE(symbol, comparison_symbol) \ + auto lambda = [this](std::list nodes) -> ASTNodePtr { \ + bool result = true; \ + \ + if (nodes.size() < 2) { \ + Error::the().add(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); \ + return nullptr; \ + } \ + \ + for (auto node : nodes) { \ + if (!is(node.get())) { \ + Error::the().add(format("wrong argument type: number, '{}'", node)); \ + return nullptr; \ + } \ + } \ + \ + /* Start with the first number */ \ + int64_t number = std::static_pointer_cast(nodes.front())->number(); \ + \ + /* Skip the first node */ \ + for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \ + int64_t current_number = std::static_pointer_cast(*it)->number(); \ + if (number comparison_symbol current_number) { \ + result = false; \ + break; \ + } \ + number = current_number; \ + } \ + \ + return makePtr((result) ? Value::True : Value::False); \ + }; \ + \ m_values.emplace(symbol, makePtr(lambda)); void GlobalEnvironment::lt() @@ -181,7 +181,7 @@ void GlobalEnvironment::list() auto list = makePtr(); for (auto node : nodes) { - list->addNode(node); + list->add(node); } return list; @@ -215,7 +215,7 @@ void GlobalEnvironment::isEmpty() for (auto node : nodes) { if (!is(node.get())) { - Error::the().addError(format("wrong argument type: collection, '{}'", node)); + Error::the().add(format("wrong argument type: collection, '{}'", node)); return nullptr; } @@ -235,7 +235,7 @@ void GlobalEnvironment::count() { auto count = [this](std::list nodes) -> ASTNodePtr { if (nodes.size() != 1) { - Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); + Error::the().add(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); return nullptr; } @@ -249,7 +249,7 @@ void GlobalEnvironment::count() result = std::static_pointer_cast(first_argument)->size(); } else { - Error::the().addError(format("wrong argument type: collection, '{}'", first_argument)); + Error::the().add(format("wrong argument type: collection, '{}'", first_argument)); return nullptr; } @@ -323,7 +323,7 @@ void GlobalEnvironment::equal() { auto lambda = [this](std::list nodes) -> ASTNodePtr { if (nodes.size() < 2) { - Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); + Error::the().add(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); return nullptr; } diff --git a/src/lexer.cpp b/src/lexer.cpp index bdbc84c..e2cecc7 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -160,7 +160,7 @@ bool Lexer::consumeString() } if (character != '"') { - Error::the().addError({ Token::Type::Error, m_line, column, "expected '\"', got EOF" }); + Error::the().add({ Token::Type::Error, m_line, column, "expected '\"', got EOF" }); } m_tokens.push_back({ Token::Type::String, m_line, column, text }); diff --git a/src/reader.cpp b/src/reader.cpp index 1549a6b..101a32a 100644 --- a/src/reader.cpp +++ b/src/reader.cpp @@ -50,7 +50,7 @@ void Reader::read() case Token::Type::Comment: break; default: - Error::the().addError("more than one sexp in input"); + Error::the().add("more than one sexp in input"); break; }; } @@ -70,21 +70,21 @@ ASTNodePtr Reader::readImpl() return readList(); break; case Token::Type::ParenClose: // ) - Error::the().addError("invalid read syntax: ')'"); + Error::the().add("invalid read syntax: ')'"); return nullptr; break; case Token::Type::BracketOpen: // [ return readVector(); break; case Token::Type::BracketClose: // ] - Error::the().addError("invalid read syntax: ']'"); + Error::the().add("invalid read syntax: ']'"); return nullptr; break; case Token::Type::BraceOpen: // { return readHashMap(); break; case Token::Type::BraceClose: // } - Error::the().addError("invalid read syntax: '}'"); + Error::the().add("invalid read syntax: '}'"); return nullptr; break; case Token::Type::Quote: // ' @@ -129,13 +129,13 @@ ASTNodePtr Reader::readSpliceUnquote() ignore(); // ~@ if (isEOF()) { - Error::the().addError("expected form, got EOF"); + Error::the().add("expected form, got EOF"); return nullptr; } auto list = makePtr(); - list->addNode(makePtr("splice-unquote")); - list->addNode(readImpl()); + list->add(makePtr("splice-unquote")); + list->add(readImpl()); return list; } @@ -146,11 +146,11 @@ ASTNodePtr Reader::readList() auto list = makePtr(); while (!isEOF() && peek().type != Token::Type::ParenClose) { - list->addNode(readImpl()); + list->add(readImpl()); } if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // ) - Error::the().addError("expected ')', got EOF"); + Error::the().add("expected ')', got EOF"); return nullptr; } @@ -163,11 +163,11 @@ ASTNodePtr Reader::readVector() auto vector = makePtr(); while (!isEOF() && peek().type != Token::Type::BracketClose) { - vector->addNode(readImpl()); + vector->add(readImpl()); } if (!consumeSpecific(Token { .type = Token::Type::BracketClose })) { // ] - Error::the().addError("expected ']', got EOF"); + Error::the().add("expected ']', got EOF"); } return vector; @@ -187,12 +187,12 @@ ASTNodePtr Reader::readHashMap() } if (key == nullptr || value == nullptr) { - Error::the().addError("hash-map requires an even-sized list"); + Error::the().add("hash-map requires an even-sized list"); return nullptr; } if (!is(key.get()) && !is(key.get())) { - Error::the().addError(format("{} is not a string or keyword", key)); + Error::the().add(format("{} is not a string or keyword", key)); return nullptr; } @@ -201,7 +201,7 @@ ASTNodePtr Reader::readHashMap() } if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // } - Error::the().addError("expected '}', got EOF"); + Error::the().add("expected '}', got EOF"); } return hash_map; @@ -212,13 +212,13 @@ ASTNodePtr Reader::readQuote() ignore(); // ' if (isEOF()) { - Error::the().addError("expected form, got EOF"); + Error::the().add("expected form, got EOF"); return nullptr; } auto list = makePtr(); - list->addNode(makePtr("quote")); - list->addNode(readImpl()); + list->add(makePtr("quote")); + list->add(readImpl()); return list; } @@ -228,13 +228,13 @@ ASTNodePtr Reader::readQuasiQuote() ignore(); // ` if (isEOF()) { - Error::the().addError("expected form, got EOF"); + Error::the().add("expected form, got EOF"); return nullptr; } auto list = makePtr(); - list->addNode(makePtr("quasiquote")); - list->addNode(readImpl()); + list->add(makePtr("quasiquote")); + list->add(readImpl()); return list; } @@ -244,13 +244,13 @@ ASTNodePtr Reader::readUnquote() ignore(); // ~ if (isEOF()) { - Error::the().addError("expected form, got EOF"); + Error::the().add("expected form, got EOF"); return nullptr; } auto list = makePtr(); - list->addNode(makePtr("unquote")); - list->addNode(readImpl()); + list->add(makePtr("unquote")); + list->add(readImpl()); return list; } @@ -261,17 +261,17 @@ ASTNodePtr Reader::readWithMeta() ignore(); // first token if (isEOF()) { // second token - Error::the().addError("expected form, got EOF"); + Error::the().add("expected form, got EOF"); return nullptr; } retreat(); auto list = makePtr(); - list->addNode(makePtr("with-meta")); + list->add(makePtr("with-meta")); ASTNodePtr first = readImpl(); ASTNodePtr second = readImpl(); - list->addNode(second); - list->addNode(first); + list->add(second); + list->add(first); return list; } @@ -281,13 +281,13 @@ ASTNodePtr Reader::readDeref() ignore(); // @ if (isEOF()) { - Error::the().addError("expected form, got EOF"); + Error::the().add("expected form, got EOF"); return nullptr; } auto list = makePtr(); - list->addNode(makePtr("deref")); - list->addNode(readImpl()); + list->add(makePtr("deref")); + list->add(readImpl()); return list; }