Browse Source

Everywhere: Convert List and Vector to an std::list<> datatype

master
Riyyi 2 years ago
parent
commit
35a32678d0
  1. 10
      src/ast.h
  2. 68
      src/eval.cpp
  3. 6
      src/eval.h
  4. 42
      src/functions.cpp
  5. 8
      src/printer.cpp
  6. 6
      src/reader.cpp

10
src/ast.h

@ -8,13 +8,13 @@
#include <cstdint> // int64_t, uint8_t
#include <functional> // std::function
#include <memory> // std::shared_ptr
#include <list>
#include <memory> // std::shared_ptr
#include <span>
#include <string>
#include <string_view>
#include <typeinfo> // typeid
#include <unordered_map>
#include <vector>
#include "ruc/format/formatter.h"
@ -57,7 +57,7 @@ public:
void addNode(ASTNodePtr node);
const std::vector<ASTNodePtr>& nodes() const { return m_nodes; }
const std::list<ASTNodePtr>& nodes() const { return m_nodes; }
size_t size() const { return m_nodes.size(); }
bool empty() const { return m_nodes.size() == 0; }
@ -65,7 +65,7 @@ protected:
Collection() {}
private:
std::vector<ASTNodePtr> m_nodes;
std::list<ASTNodePtr> m_nodes;
};
// -----------------------------------------
@ -199,7 +199,7 @@ private:
// -----------------------------------------
using Lambda = std::function<ASTNodePtr(std::span<ASTNodePtr>)>;
using Lambda = std::function<ASTNodePtr(std::list<ASTNodePtr>)>;
class Function final : public ASTNode {
public:

68
src/eval.cpp

@ -4,10 +4,11 @@
* SPDX-License-Identifier: MIT
*/
#include <iterator> // sd::advance, std::next
#include <list>
#include <memory> // std::static_pointer_cast
#include <span> // std::span
#include <string>
#include <vector>
#include "ast.h"
#include "environment.h"
@ -47,8 +48,9 @@ ASTNodePtr Eval::evalImpl(ASTNodePtr ast, EnvironmentPtr env)
// Environment
auto nodes = list->nodes();
if (is<Symbol>(nodes[0].get())) {
auto symbol = std::static_pointer_cast<Symbol>(nodes[0])->symbol();
if (is<Symbol>(nodes.front().get())) {
auto symbol = std::static_pointer_cast<Symbol>(nodes.front())->symbol();
nodes.pop_front();
if (symbol == "def!") {
return evalDef(nodes, env);
}
@ -116,21 +118,24 @@ ASTNodePtr Eval::evalAst(ASTNodePtr ast, EnvironmentPtr env)
return ast;
}
ASTNodePtr Eval::evalDef(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr env)
ASTNodePtr Eval::evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
{
if (nodes.size() != 3) {
Error::the().addError(format("wrong number of arguments: def!, {}", nodes.size() - 1));
if (nodes.size() != 2) {
Error::the().addError(format("wrong number of arguments: def!, {}", nodes.size()));
return nullptr;
}
auto first_argument = *nodes.begin();
auto second_argument = *std::next(nodes.begin());
// First element needs to be a Symbol
if (!is<Symbol>(nodes[1].get())) {
Error::the().addError(format("wrong type argument: symbol, {}", nodes[1]));
if (!is<Symbol>(first_argument.get())) {
Error::the().addError(format("wrong type argument: symbol, {}", first_argument));
return nullptr;
}
std::string symbol = std::static_pointer_cast<Symbol>(nodes[1])->symbol();
ASTNodePtr value = evalImpl(nodes[2], env);
std::string symbol = std::static_pointer_cast<Symbol>(first_argument)->symbol();
ASTNodePtr value = evalImpl(second_argument, env);
// Dont overwrite symbols after an error
if (Error::the().hasAnyError()) {
@ -141,27 +146,30 @@ ASTNodePtr Eval::evalDef(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr en
return env->set(symbol, value);
}
ASTNodePtr Eval::evalLet(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr env)
ASTNodePtr Eval::evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env)
{
if (nodes.size() != 3) {
Error::the().addError(format("wrong number of arguments: let*, {}", nodes.size() - 1));
if (nodes.size() != 2) {
Error::the().addError(format("wrong number of arguments: let*, {}", nodes.size()));
return nullptr;
}
auto first_argument = *nodes.begin();
auto second_argument = *std::next(nodes.begin());
// First argument needs to be a List or Vector
if (!is<List>(nodes[1].get()) && !is<Vector>(nodes[1].get())) {
Error::the().addError(format("wrong argument type: list, '{}'", nodes[1]));
if (!is<List>(first_argument.get()) && !is<Vector>(first_argument.get())) {
Error::the().addError(format("wrong argument type: list, '{}'", first_argument));
return nullptr;
}
// Get the nodes out of the List or Vector
std::vector<ASTNodePtr> binding_nodes;
if (is<List>(nodes[1].get())) {
auto bindings = std::static_pointer_cast<List>(nodes[1]);
std::list<ASTNodePtr> binding_nodes;
if (is<List>(first_argument.get())) {
auto bindings = std::static_pointer_cast<List>(first_argument);
binding_nodes = bindings->nodes();
}
else {
auto bindings = std::static_pointer_cast<Vector>(nodes[1]);
auto bindings = std::static_pointer_cast<Vector>(first_argument);
binding_nodes = bindings->nodes();
}
@ -175,21 +183,21 @@ ASTNodePtr Eval::evalLet(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr en
// Create new environment
auto let_env = makePtr<Environment>(env);
for (size_t i = 0; i < count; i += 2) {
for (auto it = binding_nodes.begin(); it != binding_nodes.end(); std::advance(it, 2)) {
// First element needs to be a Symbol
if (!is<Symbol>(binding_nodes[i].get())) {
Error::the().addError(format("wrong argument type: symbol, '{}'", binding_nodes[i]));
if (!is<Symbol>(*it->get())) {
Error::the().addError(format("wrong argument type: symbol, '{}'", *it));
return nullptr;
}
std::string key = std::static_pointer_cast<Symbol>(binding_nodes[i])->symbol();
ASTNodePtr value = evalImpl(binding_nodes[i + 1], let_env);
std::string key = std::static_pointer_cast<Symbol>(*it)->symbol();
ASTNodePtr value = evalImpl(*std::next(it), let_env);
let_env->set(key, value);
}
// TODO: Remove limitation of 3 arguments
// Eval all values in this new env, return last sexp of the result
return evalImpl(nodes[2], let_env);
return evalImpl(second_argument, let_env);
}
ASTNodePtr Eval::apply(std::shared_ptr<List> evaluated_list)
@ -200,17 +208,17 @@ ASTNodePtr Eval::apply(std::shared_ptr<List> evaluated_list)
auto nodes = evaluated_list->nodes();
if (!is<Function>(nodes[0].get())) {
Error::the().addError(format("invalid function: {}", nodes[0]));
if (!is<Function>(nodes.front().get())) {
Error::the().addError(format("invalid function: {}", nodes.front()));
return nullptr;
}
// car
auto lambda = std::static_pointer_cast<Function>(nodes[0])->lambda();
auto lambda = std::static_pointer_cast<Function>(nodes.front())->lambda();
// cdr
std::span<ASTNodePtr> span { nodes.data() + 1, nodes.size() - 1 };
nodes.pop_front();
return lambda(span);
return lambda(nodes);
}
} // namespace blaze

6
src/eval.h

@ -6,7 +6,7 @@
#pragma once
#include <vector>
#include <list>
#include "ast.h"
#include "environment.h"
@ -25,8 +25,8 @@ public:
private:
ASTNodePtr evalImpl(ASTNodePtr ast, EnvironmentPtr env);
ASTNodePtr evalAst(ASTNodePtr ast, EnvironmentPtr env);
ASTNodePtr evalDef(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr env);
ASTNodePtr evalLet(const std::vector<ASTNodePtr>& nodes, EnvironmentPtr env);
ASTNodePtr evalDef(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
ASTNodePtr evalLet(const std::list<ASTNodePtr>& nodes, EnvironmentPtr env);
ASTNodePtr apply(std::shared_ptr<List> evaluated_list);
ASTNodePtr m_ast;

42
src/functions.cpp

@ -21,7 +21,7 @@ namespace blaze {
void GlobalEnvironment::add()
{
auto add = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto add = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
int64_t result = 0;
for (auto node : nodes) {
@ -41,7 +41,7 @@ void GlobalEnvironment::add()
void GlobalEnvironment::sub()
{
auto sub = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto sub = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
if (nodes.size() == 0) {
return makePtr<Number>(0);
}
@ -54,7 +54,7 @@ void GlobalEnvironment::sub()
}
// Start with the first number
int64_t result = std::static_pointer_cast<Number>(nodes[0])->number();
int64_t result = std::static_pointer_cast<Number>(nodes.front())->number();
// Skip the first node
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) {
@ -69,7 +69,7 @@ void GlobalEnvironment::sub()
void GlobalEnvironment::mul()
{
auto mul = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto mul = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
int64_t result = 1;
for (auto node : nodes) {
@ -89,7 +89,7 @@ void GlobalEnvironment::mul()
void GlobalEnvironment::div()
{
auto div = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto div = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
if (nodes.size() == 0) {
Error::the().addError(format("wrong number of arguments: {}, 0", m_current_key));
return nullptr;
@ -103,7 +103,7 @@ void GlobalEnvironment::div()
}
// Start with the first number
double result = std::static_pointer_cast<Number>(nodes[0])->number();
double result = std::static_pointer_cast<Number>(nodes.front())->number();
// Skip the first node
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) {
@ -119,7 +119,7 @@ void GlobalEnvironment::div()
// -----------------------------------------
#define NUMBER_COMPARE(symbol, comparison_symbol) \
auto lambda = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr { \
auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
bool result = true; \
\
if (nodes.size() < 2) { \
@ -135,7 +135,7 @@ void GlobalEnvironment::div()
} \
\
/* Start with the first number */ \
int64_t number = std::static_pointer_cast<Number>(nodes[0])->number(); \
int64_t number = std::static_pointer_cast<Number>(nodes.front())->number(); \
\
/* Skip the first node */ \
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \
@ -176,7 +176,7 @@ void GlobalEnvironment::gte()
void GlobalEnvironment::list()
{
auto list = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto list = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
auto list = makePtr<List>();
for (auto node : nodes) {
@ -191,7 +191,7 @@ void GlobalEnvironment::list()
void GlobalEnvironment::isList()
{
auto is_list = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto is_list = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
bool result = true;
for (auto node : nodes) {
@ -209,7 +209,7 @@ void GlobalEnvironment::isList()
void GlobalEnvironment::isEmpty()
{
auto is_empty = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto is_empty = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
bool result = true;
for (auto node : nodes) {
@ -232,7 +232,7 @@ void GlobalEnvironment::isEmpty()
void GlobalEnvironment::count()
{
auto count = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto count = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
if (nodes.size() > 1) {
Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1));
return nullptr;
@ -259,7 +259,7 @@ void GlobalEnvironment::count()
// -----------------------------------------
#define PRINTER_STRING(symbol, concatenation, print_readably) \
auto lambda = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr { \
auto lambda = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
std::string result; \
\
Printer printer; \
@ -287,7 +287,7 @@ void GlobalEnvironment::prStr()
}
#define PRINTER_PRINT(symbol, print_readably) \
auto lambda = [](std::span<ASTNodePtr> nodes) -> ASTNodePtr { \
auto lambda = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
Printer printer; \
for (auto it = nodes.begin(); it != nodes.end(); ++it) { \
print("{}", printer.printNoErrorCheck(*it, print_readably)); \
@ -317,7 +317,7 @@ void GlobalEnvironment::println()
void GlobalEnvironment::equal()
{
auto lambda = [this](std::span<ASTNodePtr> nodes) -> ASTNodePtr {
auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr {
if (nodes.size() < 2) {
Error::the().addError(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1));
return nullptr;
@ -334,8 +334,10 @@ void GlobalEnvironment::equal()
return false;
}
for (size_t i = 0; i < lhs_nodes.size(); ++i) {
if (!equal(lhs_nodes[i], rhs_nodes[i])) {
auto lhs_it = lhs_nodes.begin();
auto rhs_it = rhs_nodes.begin();
for (; lhs_it != lhs_nodes.end(); ++lhs_it, ++rhs_it) {
if (!equal(*lhs_it, *rhs_it)) {
return false;
}
}
@ -386,8 +388,10 @@ void GlobalEnvironment::equal()
};
bool result = true;
for (size_t i = 0; i < nodes.size() - 1; ++i) {
if (!equal(nodes[i], nodes[i + 1])) {
auto it = nodes.begin();
auto it_next = std::next(nodes.begin());
for (; it_next != nodes.end(); ++it, ++it_next) {
if (!equal(*it, *it_next)) {
result = false;
break;
}

8
src/printer.cpp

@ -77,8 +77,8 @@ void Printer::printImpl(ASTNodePtr node, bool print_readably)
m_first_node = false;
m_previous_node_is_list = true;
auto nodes = std::static_pointer_cast<List>(node)->nodes();
for (size_t i = 0; i < nodes.size(); ++i) {
printImpl(nodes[i]);
for (auto node : nodes) {
printImpl(node);
m_previous_node_is_list = false;
}
m_print += ')';
@ -89,8 +89,8 @@ void Printer::printImpl(ASTNodePtr node, bool print_readably)
m_first_node = false;
m_previous_node_is_list = true;
auto nodes = std::static_pointer_cast<Vector>(node)->nodes();
for (size_t i = 0; i < nodes.size(); ++i) {
printImpl(nodes[i]);
for (auto node : nodes) {
printImpl(node);
m_previous_node_is_list = false;
}
m_print += ']';

6
src/reader.cpp

@ -378,15 +378,15 @@ void Reader::dumpImpl(ASTNodePtr node)
ASTNode* node_raw_ptr = node.get();
if (is<List>(node_raw_ptr)) {
auto list = std::static_pointer_cast<List>(node);
auto nodes = std::static_pointer_cast<List>(node)->nodes();
print("{}", indentation);
print(fg(ruc::format::TerminalColor::Blue), "ListContainer");
print(" <");
print(fg(ruc::format::TerminalColor::Blue), "()");
print(">\n");
m_indentation++;
for (size_t i = 0; i < list->nodes().size(); ++i) {
dumpImpl(list->nodes()[i]);
for (auto node : nodes) {
dumpImpl(node);
}
m_indentation--;
return;

Loading…
Cancel
Save