Browse Source

Everywhere: Rename addNode and addError functions -> add

master
Riyyi 2 years ago
parent
commit
c53c0af5a2
  1. 2
      src/ast.cpp
  2. 2
      src/ast.h
  3. 2
      src/environment.cpp
  4. 4
      src/error.h
  5. 40
      src/eval.cpp
  6. 84
      src/functions.cpp
  7. 2
      src/lexer.cpp
  8. 60
      src/reader.cpp

2
src/ast.cpp

@ -16,7 +16,7 @@
namespace blaze { namespace blaze {
void Collection::addNode(ASTNodePtr node) void Collection::add(ASTNodePtr node)
{ {
m_nodes.push_back(node); m_nodes.push_back(node);
} }

2
src/ast.h

@ -56,7 +56,7 @@ public:
virtual bool isCollection() const override { return true; } virtual bool isCollection() const override { return true; }
void addNode(ASTNodePtr node); void add(ASTNodePtr node);
const std::list<ASTNodePtr>& nodes() const { return m_nodes; } const std::list<ASTNodePtr>& nodes() const { return m_nodes; }
size_t size() const { return m_nodes.size(); } size_t size() const { return m_nodes.size(); }

2
src/environment.cpp

@ -45,7 +45,7 @@ EnvironmentPtr Environment::create(const ASTNodePtr lambda, std::list<ASTNodePtr
auto list = makePtr<List>(); auto list = makePtr<List>();
for (; it != arguments.end(); ++it) { for (; it != arguments.end(); ++it) {
list->addNode(*it); list->add(*it);
} }
env->set(bindings[i + 1], list); env->set(bindings[i + 1], list);

4
src/error.h

@ -24,8 +24,8 @@ public:
m_token_errors.clear(); m_token_errors.clear();
m_other_errors.clear(); m_other_errors.clear();
} }
void addError(Token error) { m_token_errors.push_back(error); } void add(Token error) { m_token_errors.push_back(error); }
void addError(const std::string& error) { m_other_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 hasTokenError() { return m_token_errors.size() > 0; }
bool hasOtherError() { return m_other_errors.size() > 0; } bool hasOtherError() { return m_other_errors.size() > 0; }

40
src/eval.cpp

@ -82,7 +82,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
if (is<Symbol>(ast_raw_ptr)) { if (is<Symbol>(ast_raw_ptr)) {
auto result = env->get(std::static_pointer_cast<Symbol>(ast)->symbol()); auto result = env->get(std::static_pointer_cast<Symbol>(ast)->symbol());
if (!result) { if (!result) {
Error::the().addError(format("'{}' not found", ast)); Error::the().add(format("'{}' not found", ast));
return nullptr; return nullptr;
} }
return result; return result;
@ -95,7 +95,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
if (eval_node == nullptr) { if (eval_node == nullptr) {
return nullptr; return nullptr;
} }
result->addNode(eval_node); result->add(eval_node);
} }
return result; return result;
} }
@ -107,7 +107,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
if (eval_node == nullptr) { if (eval_node == nullptr) {
return nullptr; return nullptr;
} }
result->addNode(eval_node); result->add(eval_node);
} }
return result; return result;
} }
@ -130,7 +130,7 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env) ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() != 2) { 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; return nullptr;
} }
@ -139,7 +139,7 @@ ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
// First element needs to be a Symbol // First element needs to be a Symbol
if (!is<Symbol>(first_argument.get())) { if (!is<Symbol>(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; return nullptr;
} }
@ -158,7 +158,7 @@ ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
ASTNodePtr Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env) ASTNodePtr Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() != 2) { 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; return nullptr;
} }
@ -167,7 +167,7 @@ ASTNodePtr Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
// First argument needs to be a List or Vector // First argument needs to be a List or Vector
if (!is<Collection>(first_argument.get())) { if (!is<Collection>(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; return nullptr;
} }
@ -179,7 +179,7 @@ ASTNodePtr Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
// List or Vector needs to have an even number of elements // List or Vector needs to have an even number of elements
size_t count = binding_nodes.size(); size_t count = binding_nodes.size();
if (count % 2 != 0) { 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; return nullptr;
} }
@ -189,7 +189,7 @@ ASTNodePtr Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
for (auto it = binding_nodes.begin(); it != binding_nodes.end(); std::advance(it, 2)) { for (auto it = binding_nodes.begin(); it != binding_nodes.end(); std::advance(it, 2)) {
// First element needs to be a Symbol // First element needs to be a Symbol
if (!is<Symbol>(*it->get())) { if (!is<Symbol>(*it->get())) {
Error::the().addError(format("wrong argument type: symbol, '{}'", *it)); Error::the().add(format("wrong argument type: symbol, '{}'", *it));
return nullptr; return nullptr;
} }
@ -206,7 +206,7 @@ ASTNodePtr Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
ASTNodePtr Eval::evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env) ASTNodePtr Eval::evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() == 0) { 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; return nullptr;
} }
@ -222,7 +222,7 @@ ASTNodePtr Eval::evalDo(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
ASTNodePtr Eval::evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env) ASTNodePtr Eval::evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() != 2 && nodes.size() != 3) { 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; return nullptr;
} }
@ -240,16 +240,16 @@ ASTNodePtr Eval::evalIf(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
} }
} }
#define ARG_COUNT_CHECK(name, size, comparison) \ #define ARG_COUNT_CHECK(name, size, comparison) \
if (size comparison) { \ if (size comparison) { \
Error::the().addError(format("wrong number of arguments: {}, {}", name, size)); \ Error::the().add(format("wrong number of arguments: {}, {}", name, size)); \
return nullptr; \ return nullptr; \
} }
#define AST_CHECK(type, value) \ #define AST_CHECK(type, value) \
if (!is<type>(value.get())) { \ if (!is<type>(value.get())) { \
Error::the().addError(format("wrong argument type: {}, {}", #type, value)); \ Error::the().add(format("wrong argument type: {}, {}", #type, value)); \
return nullptr; \ return nullptr; \
} }
#define AST_CAST(type, value, variable) \ #define AST_CAST(type, value, variable) \
@ -285,7 +285,7 @@ ASTNodePtr Eval::apply(std::shared_ptr<List> evaluated_list)
auto nodes = evaluated_list->nodes(); auto nodes = evaluated_list->nodes();
if (!is<Function>(nodes.front().get()) && !is<Lambda>(nodes.front().get())) { if (!is<Function>(nodes.front().get()) && !is<Lambda>(nodes.front().get())) {
Error::the().addError(format("invalid function: {}", nodes.front())); Error::the().add(format("invalid function: {}", nodes.front()));
return nullptr; return nullptr;
} }

84
src/functions.cpp

@ -27,7 +27,7 @@ void GlobalEnvironment::add()
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Number>(node.get())) { if (!is<Number>(node.get())) {
Error::the().addError(format("wrong argument type: number, '{}'", node)); Error::the().add(format("wrong argument type: number, '{}'", node));
return nullptr; return nullptr;
} }
@ -49,7 +49,7 @@ void GlobalEnvironment::sub()
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Number>(node.get())) { if (!is<Number>(node.get())) {
Error::the().addError(format("wrong argument type: number, '{}'", node)); Error::the().add(format("wrong argument type: number, '{}'", node));
return nullptr; return nullptr;
} }
} }
@ -75,7 +75,7 @@ void GlobalEnvironment::mul()
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Number>(node.get())) { if (!is<Number>(node.get())) {
Error::the().addError(format("wrong argument type: number, '{}'", node)); Error::the().add(format("wrong argument type: number, '{}'", node));
return nullptr; return nullptr;
} }
@ -92,13 +92,13 @@ void GlobalEnvironment::div()
{ {
auto div = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { auto div = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
if (nodes.size() == 0) { 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; return nullptr;
} }
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Number>(node.get())) { if (!is<Number>(node.get())) {
Error::the().addError(format("wrong argument type: number, '{}'", node)); Error::the().add(format("wrong argument type: number, '{}'", node));
return nullptr; return nullptr;
} }
} }
@ -119,38 +119,38 @@ void GlobalEnvironment::div()
// ----------------------------------------- // -----------------------------------------
#define NUMBER_COMPARE(symbol, comparison_symbol) \ #define NUMBER_COMPARE(symbol, comparison_symbol) \
auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \ auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
bool result = true; \ bool result = true; \
\ \
if (nodes.size() < 2) { \ 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; \ return nullptr; \
} \ } \
\ \
for (auto node : nodes) { \ for (auto node : nodes) { \
if (!is<Number>(node.get())) { \ if (!is<Number>(node.get())) { \
Error::the().addError(format("wrong argument type: number, '{}'", node)); \ Error::the().add(format("wrong argument type: number, '{}'", node)); \
return nullptr; \ return nullptr; \
} \ } \
} \ } \
\ \
/* Start with the first number */ \ /* Start with the first number */ \
int64_t number = std::static_pointer_cast<Number>(nodes.front())->number(); \ int64_t number = std::static_pointer_cast<Number>(nodes.front())->number(); \
\ \
/* Skip the first node */ \ /* Skip the first node */ \
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \ for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \
int64_t current_number = std::static_pointer_cast<Number>(*it)->number(); \ int64_t current_number = std::static_pointer_cast<Number>(*it)->number(); \
if (number comparison_symbol current_number) { \ if (number comparison_symbol current_number) { \
result = false; \ result = false; \
break; \ break; \
} \ } \
number = current_number; \ number = current_number; \
} \ } \
\ \
return makePtr<Value>((result) ? Value::True : Value::False); \ return makePtr<Value>((result) ? Value::True : Value::False); \
}; \ }; \
\ \
m_values.emplace(symbol, makePtr<Function>(lambda)); m_values.emplace(symbol, makePtr<Function>(lambda));
void GlobalEnvironment::lt() void GlobalEnvironment::lt()
@ -181,7 +181,7 @@ void GlobalEnvironment::list()
auto list = makePtr<List>(); auto list = makePtr<List>();
for (auto node : nodes) { for (auto node : nodes) {
list->addNode(node); list->add(node);
} }
return list; return list;
@ -215,7 +215,7 @@ void GlobalEnvironment::isEmpty()
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Collection>(node.get())) { if (!is<Collection>(node.get())) {
Error::the().addError(format("wrong argument type: collection, '{}'", node)); Error::the().add(format("wrong argument type: collection, '{}'", node));
return nullptr; return nullptr;
} }
@ -235,7 +235,7 @@ void GlobalEnvironment::count()
{ {
auto count = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { auto count = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
if (nodes.size() != 1) { 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; return nullptr;
} }
@ -249,7 +249,7 @@ void GlobalEnvironment::count()
result = std::static_pointer_cast<Collection>(first_argument)->size(); result = std::static_pointer_cast<Collection>(first_argument)->size();
} }
else { else {
Error::the().addError(format("wrong argument type: collection, '{}'", first_argument)); Error::the().add(format("wrong argument type: collection, '{}'", first_argument));
return nullptr; return nullptr;
} }
@ -323,7 +323,7 @@ void GlobalEnvironment::equal()
{ {
auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
if (nodes.size() < 2) { 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; return nullptr;
} }

2
src/lexer.cpp

@ -160,7 +160,7 @@ bool Lexer::consumeString()
} }
if (character != '"') { 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 }); m_tokens.push_back({ Token::Type::String, m_line, column, text });

60
src/reader.cpp

@ -50,7 +50,7 @@ void Reader::read()
case Token::Type::Comment: case Token::Type::Comment:
break; break;
default: default:
Error::the().addError("more than one sexp in input"); Error::the().add("more than one sexp in input");
break; break;
}; };
} }
@ -70,21 +70,21 @@ ASTNodePtr Reader::readImpl()
return readList(); return readList();
break; break;
case Token::Type::ParenClose: // ) case Token::Type::ParenClose: // )
Error::the().addError("invalid read syntax: ')'"); Error::the().add("invalid read syntax: ')'");
return nullptr; return nullptr;
break; break;
case Token::Type::BracketOpen: // [ case Token::Type::BracketOpen: // [
return readVector(); return readVector();
break; break;
case Token::Type::BracketClose: // ] case Token::Type::BracketClose: // ]
Error::the().addError("invalid read syntax: ']'"); Error::the().add("invalid read syntax: ']'");
return nullptr; return nullptr;
break; break;
case Token::Type::BraceOpen: // { case Token::Type::BraceOpen: // {
return readHashMap(); return readHashMap();
break; break;
case Token::Type::BraceClose: // } case Token::Type::BraceClose: // }
Error::the().addError("invalid read syntax: '}'"); Error::the().add("invalid read syntax: '}'");
return nullptr; return nullptr;
break; break;
case Token::Type::Quote: // ' case Token::Type::Quote: // '
@ -129,13 +129,13 @@ ASTNodePtr Reader::readSpliceUnquote()
ignore(); // ~@ ignore(); // ~@
if (isEOF()) { if (isEOF()) {
Error::the().addError("expected form, got EOF"); Error::the().add("expected form, got EOF");
return nullptr; return nullptr;
} }
auto list = makePtr<List>(); auto list = makePtr<List>();
list->addNode(makePtr<Symbol>("splice-unquote")); list->add(makePtr<Symbol>("splice-unquote"));
list->addNode(readImpl()); list->add(readImpl());
return list; return list;
} }
@ -146,11 +146,11 @@ ASTNodePtr Reader::readList()
auto list = makePtr<List>(); auto list = makePtr<List>();
while (!isEOF() && peek().type != Token::Type::ParenClose) { while (!isEOF() && peek().type != Token::Type::ParenClose) {
list->addNode(readImpl()); list->add(readImpl());
} }
if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // ) if (!consumeSpecific(Token { .type = Token::Type::ParenClose })) { // )
Error::the().addError("expected ')', got EOF"); Error::the().add("expected ')', got EOF");
return nullptr; return nullptr;
} }
@ -163,11 +163,11 @@ ASTNodePtr Reader::readVector()
auto vector = makePtr<Vector>(); auto vector = makePtr<Vector>();
while (!isEOF() && peek().type != Token::Type::BracketClose) { while (!isEOF() && peek().type != Token::Type::BracketClose) {
vector->addNode(readImpl()); vector->add(readImpl());
} }
if (!consumeSpecific(Token { .type = Token::Type::BracketClose })) { // ] if (!consumeSpecific(Token { .type = Token::Type::BracketClose })) { // ]
Error::the().addError("expected ']', got EOF"); Error::the().add("expected ']', got EOF");
} }
return vector; return vector;
@ -187,12 +187,12 @@ ASTNodePtr Reader::readHashMap()
} }
if (key == nullptr || value == nullptr) { 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; return nullptr;
} }
if (!is<String>(key.get()) && !is<Keyword>(key.get())) { if (!is<String>(key.get()) && !is<Keyword>(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; return nullptr;
} }
@ -201,7 +201,7 @@ ASTNodePtr Reader::readHashMap()
} }
if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // } if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // }
Error::the().addError("expected '}', got EOF"); Error::the().add("expected '}', got EOF");
} }
return hash_map; return hash_map;
@ -212,13 +212,13 @@ ASTNodePtr Reader::readQuote()
ignore(); // ' ignore(); // '
if (isEOF()) { if (isEOF()) {
Error::the().addError("expected form, got EOF"); Error::the().add("expected form, got EOF");
return nullptr; return nullptr;
} }
auto list = makePtr<List>(); auto list = makePtr<List>();
list->addNode(makePtr<Symbol>("quote")); list->add(makePtr<Symbol>("quote"));
list->addNode(readImpl()); list->add(readImpl());
return list; return list;
} }
@ -228,13 +228,13 @@ ASTNodePtr Reader::readQuasiQuote()
ignore(); // ` ignore(); // `
if (isEOF()) { if (isEOF()) {
Error::the().addError("expected form, got EOF"); Error::the().add("expected form, got EOF");
return nullptr; return nullptr;
} }
auto list = makePtr<List>(); auto list = makePtr<List>();
list->addNode(makePtr<Symbol>("quasiquote")); list->add(makePtr<Symbol>("quasiquote"));
list->addNode(readImpl()); list->add(readImpl());
return list; return list;
} }
@ -244,13 +244,13 @@ ASTNodePtr Reader::readUnquote()
ignore(); // ~ ignore(); // ~
if (isEOF()) { if (isEOF()) {
Error::the().addError("expected form, got EOF"); Error::the().add("expected form, got EOF");
return nullptr; return nullptr;
} }
auto list = makePtr<List>(); auto list = makePtr<List>();
list->addNode(makePtr<Symbol>("unquote")); list->add(makePtr<Symbol>("unquote"));
list->addNode(readImpl()); list->add(readImpl());
return list; return list;
} }
@ -261,17 +261,17 @@ ASTNodePtr Reader::readWithMeta()
ignore(); // first token ignore(); // first token
if (isEOF()) { // second token if (isEOF()) { // second token
Error::the().addError("expected form, got EOF"); Error::the().add("expected form, got EOF");
return nullptr; return nullptr;
} }
retreat(); retreat();
auto list = makePtr<List>(); auto list = makePtr<List>();
list->addNode(makePtr<Symbol>("with-meta")); list->add(makePtr<Symbol>("with-meta"));
ASTNodePtr first = readImpl(); ASTNodePtr first = readImpl();
ASTNodePtr second = readImpl(); ASTNodePtr second = readImpl();
list->addNode(second); list->add(second);
list->addNode(first); list->add(first);
return list; return list;
} }
@ -281,13 +281,13 @@ ASTNodePtr Reader::readDeref()
ignore(); // @ ignore(); // @
if (isEOF()) { if (isEOF()) {
Error::the().addError("expected form, got EOF"); Error::the().add("expected form, got EOF");
return nullptr; return nullptr;
} }
auto list = makePtr<List>(); auto list = makePtr<List>();
list->addNode(makePtr<Symbol>("deref")); list->add(makePtr<Symbol>("deref"));
list->addNode(readImpl()); list->add(readImpl());
return list; return list;
} }

Loading…
Cancel
Save