Browse Source

Eval+Env: Make use of macros for duplicate logic

master
Riyyi 1 year ago
parent
commit
27d6e24243
  1. 128
      src/eval.cpp
  2. 4
      src/eval.h
  3. 260
      src/functions.cpp
  4. 1
      src/step7_quote.cpp
  5. 128
      src/util.h

128
src/eval.cpp

@ -16,6 +16,7 @@
#include "eval.h" #include "eval.h"
#include "forward.h" #include "forward.h"
#include "types.h" #include "types.h"
#include "util.h"
namespace blaze { namespace blaze {
@ -80,7 +81,7 @@ ValuePtr Eval::evalImpl()
continue; // TCO continue; // TCO
} }
if (symbol == "quote") { if (symbol == "quote") {
return evalQuote(nodes, env); return evalQuote(nodes);
} }
if (symbol == "quasiquote") { if (symbol == "quasiquote") {
evalQuasiQuote(nodes, env); evalQuasiQuote(nodes, env);
@ -175,24 +176,17 @@ ValuePtr Eval::evalAst(ValuePtr ast, EnvironmentPtr env)
return ast; return ast;
} }
// -----------------------------------------
ValuePtr Eval::evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env) ValuePtr Eval::evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() != 2) { CHECK_ARG_COUNT_IS("def!", nodes.size(), 2);
Error::the().add(format("wrong number of arguments: def!, {}", nodes.size()));
return nullptr;
}
auto first_argument = *nodes.begin(); // First argument needs to be a Symbol
auto second_argument = *std::next(nodes.begin()); VALUE_CAST(symbol, Symbol, nodes.front());
// First element needs to be a Symbol
if (!is<Symbol>(first_argument.get())) {
Error::the().add(format("wrong argument type: symbol, {}", first_argument));
return nullptr;
}
std::string symbol = std::static_pointer_cast<Symbol>(first_argument)->symbol(); // Eval second argument
m_ast_stack.push(second_argument); m_ast_stack.push(*std::next(nodes.begin()));
m_env_stack.push(env); m_env_stack.push(env);
ValuePtr value = evalImpl(); ValuePtr value = evalImpl();
@ -202,15 +196,12 @@ ValuePtr Eval::evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
} }
// Modify existing environment // Modify existing environment
return env->set(symbol, value); return env->set(symbol->symbol(), value);
} }
ValuePtr Eval::evalQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env) ValuePtr Eval::evalQuote(const std::list<ValuePtr>& nodes)
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("quote", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: quote, {}", nodes.size()));
return nullptr;
}
return nodes.front(); return nodes.front();
} }
@ -242,10 +233,8 @@ static ValuePtr startsWith(ValuePtr ast, const std::string& symbol)
return nullptr; return nullptr;
} }
if (nodes.size() != 2) { // Dont count the Symbol as part of the arguments
Error::the().add(format("wrong number of arguments: {}, {}", symbol, nodes.size() - 1)); CHECK_ARG_COUNT_IS(symbol, nodes.size() - 1, 1);
return nullptr;
}
return *std::next(nodes.begin()); return *std::next(nodes.begin());
} }
@ -301,10 +290,7 @@ static ValuePtr evalQuasiQuoteImpl(ValuePtr ast)
void Eval::evalQuasiQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env) void Eval::evalQuasiQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("quasiquote", nodes.size(), 1, void());
Error::the().add(format("wrong number of arguments: quasiquote, {}", nodes.size()));
return;
}
auto result = evalQuasiQuoteImpl(nodes.front()); auto result = evalQuasiQuoteImpl(nodes.front());
@ -315,53 +301,31 @@ void Eval::evalQuasiQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
ValuePtr Eval::evalQuasiQuoteExpand(const std::list<ValuePtr>& nodes, EnvironmentPtr env) ValuePtr Eval::evalQuasiQuoteExpand(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("quasiquoteexpand", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: quasiquoteexpand, {}", nodes.size()));
return nullptr;
}
return evalQuasiQuoteImpl(nodes.front()); return evalQuasiQuoteImpl(nodes.front());
} }
// (let* (x 1) x)
void Eval::evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env) void Eval::evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() != 2) { CHECK_ARG_COUNT_IS("let*", nodes.size(), 2, void());
Error::the().add(format("wrong number of arguments: let*, {}", nodes.size()));
return;
}
auto first_argument = *nodes.begin();
auto second_argument = *std::next(nodes.begin());
// First argument needs to be a List or Vector // First argument needs to be a List or Vector
if (!is<Collection>(first_argument.get())) { VALUE_CAST(bindings, Collection, nodes.front(), void());
Error::the().add(format("wrong argument type: list, '{}'", first_argument)); auto binding_nodes = bindings->nodes();
return;
}
// Get the nodes out of the List or Vector
std::list<ValuePtr> binding_nodes;
auto bindings = std::static_pointer_cast<Collection>(first_argument);
binding_nodes = bindings->nodes();
// 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(); CHECK_ARG_COUNT_EVEN("bindings", binding_nodes.size(), void());
if (count % 2 != 0) {
Error::the().add(format("wrong number of arguments: {}, {}", "let* bindings", count));
return;
}
// Create new environment // Create new environment
auto let_env = Environment::create(env); auto let_env = Environment::create(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())) { VALUE_CAST(elt, Symbol, (*it), void());
Error::the().add(format("wrong argument type: symbol, '{}'", *it));
return;
}
std::string key = std::static_pointer_cast<Symbol>(*it)->symbol(); std::string key = elt->symbol();
m_ast_stack.push(*std::next(it)); m_ast_stack.push(*std::next(it));
m_env_stack.push(let_env); m_env_stack.push(let_env);
ValuePtr value = evalImpl(); ValuePtr value = evalImpl();
@ -369,18 +333,15 @@ void Eval::evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
} }
// TODO: Remove limitation of 3 arguments // TODO: Remove limitation of 3 arguments
// Eval all values in this new env, return last sexp of the result // Eval all arguments in this new env, return last sexp of the result
m_ast_stack.push(second_argument); m_ast_stack.push(*std::next(nodes.begin()));
m_env_stack.push(let_env); m_env_stack.push(let_env);
return; // TCO return; // TCO
} }
void Eval::evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env) void Eval::evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() == 0) { CHECK_ARG_COUNT_AT_LEAST("do", nodes.size(), 1, void());
Error::the().add(format("wrong number of arguments: do, {}", nodes.size()));
return;
}
// Evaluate all nodes except the last // Evaluate all nodes except the last
for (auto it = nodes.begin(); it != std::prev(nodes.end(), 1); ++it) { for (auto it = nodes.begin(); it != std::prev(nodes.end(), 1); ++it) {
@ -397,10 +358,7 @@ void Eval::evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
void Eval::evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env) void Eval::evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{ {
if (nodes.size() != 2 && nodes.size() != 3) { CHECK_ARG_COUNT_BETWEEN("if", nodes.size(), 2, 3, void());
Error::the().add(format("wrong number of arguments: if, {}", nodes.size()));
return;
}
auto first_argument = *nodes.begin(); auto first_argument = *nodes.begin();
auto second_argument = *std::next(nodes.begin()); auto second_argument = *std::next(nodes.begin());
@ -421,42 +379,28 @@ void Eval::evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
return; // TCO return; // TCO
} }
#define ARG_COUNT_CHECK(name, comparison, size) \ // (fn* (x) x)
if (comparison) { \
Error::the().add(format("wrong number of arguments: {}, {}", name, size)); \
return nullptr; \
}
#define AST_CHECK(type, value) \
if (!is<type>(value.get())) { \
Error::the().add(format("wrong argument type: {}, {}", #type, value)); \
return nullptr; \
}
#define AST_CAST(type, value, variable) \
AST_CHECK(type, value) \
auto variable = std::static_pointer_cast<type>(value);
ValuePtr Eval::evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env) ValuePtr Eval::evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
{ {
ARG_COUNT_CHECK("fn*", nodes.size() != 2, nodes.size()); CHECK_ARG_COUNT_IS("fn*", nodes.size(), 2);
auto first_argument = *nodes.begin();
auto second_argument = *std::next(nodes.begin());
// First element needs to be a List or Vector // First element needs to be a List or Vector
AST_CAST(Collection, first_argument, collection); VALUE_CAST(collection, Collection, nodes.front());
std::vector<std::string> bindings; std::vector<std::string> bindings;
for (auto node : collection->nodes()) { for (auto node : collection->nodes()) {
// All nodes need to be a Symbol // All nodes need to be a Symbol
AST_CAST(Symbol, node, symbol); VALUE_CAST(symbol, Symbol, node);
bindings.push_back(symbol->symbol()); bindings.push_back(symbol->symbol());
} }
return makePtr<Lambda>(bindings, second_argument, env); // TODO: Remove limitation of 3 arguments
// Wrap all other nodes in list and add that as lambda body
return makePtr<Lambda>(bindings, *std::next(nodes.begin()), env);
} }
//-----------------------------------------
ValuePtr Eval::apply(std::shared_ptr<List> evaluated_list) ValuePtr Eval::apply(std::shared_ptr<List> evaluated_list)
{ {
if (evaluated_list == nullptr) { if (evaluated_list == nullptr) {

4
src/eval.h

@ -28,14 +28,16 @@ public:
private: private:
ValuePtr evalImpl(); ValuePtr evalImpl();
ValuePtr evalAst(ValuePtr ast, EnvironmentPtr env); ValuePtr evalAst(ValuePtr ast, EnvironmentPtr env);
ValuePtr evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env); ValuePtr evalDef(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env); void evalLet(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr evalQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env); ValuePtr evalQuote(const std::list<ValuePtr>& nodes);
void evalQuasiQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env); void evalQuasiQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr evalQuasiQuoteExpand(const std::list<ValuePtr>& nodes, EnvironmentPtr env); ValuePtr evalQuasiQuoteExpand(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env); void evalDo(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
void evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env); void evalIf(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env); ValuePtr evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr apply(std::shared_ptr<List> evaluated_list); ValuePtr apply(std::shared_ptr<List> evaluated_list);
ValuePtr m_ast; ValuePtr m_ast;

260
src/functions.cpp

@ -24,17 +24,17 @@
#define FUNCTION_STRUCT_NAME(unique) __functionStruct##unique #define FUNCTION_STRUCT_NAME(unique) __functionStruct##unique
#define ADD_FUNCTION_IMPL(unique, symbol, lambda) \ #define ADD_FUNCTION_IMPL(unique, symbol, lambda) \
struct FUNCTION_STRUCT_NAME(unique) { \ struct FUNCTION_STRUCT_NAME(unique) { \
FUNCTION_STRUCT_NAME(unique) \ FUNCTION_STRUCT_NAME(unique) \
(std::string __symbol, FunctionType __lambda) \ (const std::string& __symbol, FunctionType __lambda) \
{ \ { \
s_functions.emplace(__symbol, __lambda); \ s_functions.emplace(__symbol, __lambda); \
} \ } \
}; \ }; \
static struct FUNCTION_STRUCT_NAME(unique) \ static struct FUNCTION_STRUCT_NAME(unique) \
FUNCTION_STRUCT_NAME(unique)( \ FUNCTION_STRUCT_NAME(unique)( \
symbol, \ symbol, \
[](std::list<ValuePtr> nodes) -> ValuePtr lambda); [](std::list<ValuePtr> nodes) -> ValuePtr lambda);
#define ADD_FUNCTION(symbol, lambda) ADD_FUNCTION_IMPL(__LINE__, symbol, lambda); #define ADD_FUNCTION(symbol, lambda) ADD_FUNCTION_IMPL(__LINE__, symbol, lambda);
@ -49,12 +49,8 @@ ADD_FUNCTION(
int64_t result = 0; int64_t result = 0;
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Number>(node.get())) { VALUE_CAST(number, Number, node);
Error::the().add(format("wrong argument type: number, '{}'", node)); result += number->number();
return nullptr;
}
result += std::static_pointer_cast<Number>(node)->number();
} }
return makePtr<Number>(result); return makePtr<Number>(result);
@ -67,19 +63,14 @@ ADD_FUNCTION(
return makePtr<Number>(0); return makePtr<Number>(0);
} }
for (auto node : nodes) {
if (!is<Number>(node.get())) {
Error::the().add(format("wrong argument type: number, '{}'", node));
return nullptr;
}
}
// Start with the first number // Start with the first number
int64_t result = std::static_pointer_cast<Number>(nodes.front())->number(); VALUE_CAST(number, Number, nodes.front());
int64_t result = number->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) {
result -= std::static_pointer_cast<Number>(*it)->number(); VALUE_CAST(number, Number, (*it));
result -= number->number();
} }
return makePtr<Number>(result); return makePtr<Number>(result);
@ -91,12 +82,8 @@ ADD_FUNCTION(
int64_t result = 1; int64_t result = 1;
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Number>(node.get())) { VALUE_CAST(number, Number, node);
Error::the().add(format("wrong argument type: number, '{}'", node)); result *= number->number();
return nullptr;
}
result *= std::static_pointer_cast<Number>(node)->number();
} }
return makePtr<Number>(result); return makePtr<Number>(result);
@ -105,24 +92,16 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"/", "/",
{ {
if (nodes.size() == 0) { CHECK_ARG_COUNT_AT_LEAST("/", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: /, 0"));
return nullptr;
}
for (auto node : nodes) {
if (!is<Number>(node.get())) {
Error::the().add(format("wrong argument type: number, '{}'", node));
return nullptr;
}
}
// Start with the first number // Start with the first number
double result = std::static_pointer_cast<Number>(nodes.front())->number(); VALUE_CAST(number, Number, nodes.front());
double result = number->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) {
result /= std::static_pointer_cast<Number>(*it)->number(); VALUE_CAST(number, Number, (*it));
result /= number->number();
} }
return makePtr<Number>((int64_t)result); return makePtr<Number>((int64_t)result);
@ -130,36 +109,28 @@ ADD_FUNCTION(
// // ----------------------------------------- // // -----------------------------------------
#define NUMBER_COMPARE(operator) \ #define NUMBER_COMPARE(operator) \
{ \ { \
bool result = true; \ bool result = true; \
\ \
if (nodes.size() < 2) { \ CHECK_ARG_COUNT_AT_LEAST(#operator, nodes.size(), 2); \
Error::the().add(format("wrong number of arguments: {}, {}", #operator, nodes.size())); \ \
return nullptr; \ /* Start with the first number */ \
} \ VALUE_CAST(number_node, Number, nodes.front()); \
\ int64_t number = number_node->number(); \
for (auto node : nodes) { \ \
if (!is<Number>(node.get())) { \ /* Skip the first node */ \
Error::the().add(format("wrong argument type: number, '{}'", node)); \ for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \
return nullptr; \ VALUE_CAST(current_number_node, Number, (*it)); \
} \ int64_t current_number = current_number_node->number(); \
} \ if (!(number operator current_number)) { \
\ result = false; \
/* Start with the first number */ \ break; \
int64_t number = std::static_pointer_cast<Number>(nodes.front())->number(); \ } \
\ number = current_number; \
/* Skip the first node */ \ } \
for (auto it = std::next(nodes.begin()); it != nodes.end(); ++it) { \ \
int64_t current_number = std::static_pointer_cast<Number>(*it)->number(); \ return makePtr<Constant>((result) ? Constant::True : Constant::False); \
if (!(number operator current_number)) { \
result = false; \
break; \
} \
number = current_number; \
} \
\
return makePtr<Constant>((result) ? Constant::True : Constant::False); \
} }
ADD_FUNCTION("<", NUMBER_COMPARE(<)); ADD_FUNCTION("<", NUMBER_COMPARE(<));
@ -200,12 +171,8 @@ ADD_FUNCTION(
bool result = true; bool result = true;
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Collection>(node.get())) { VALUE_CAST(collection, Collection, node);
Error::the().add(format("wrong argument type: collection, '{}'", node)); if (!collection->empty()) {
return nullptr;
}
if (!std::static_pointer_cast<Collection>(node)->empty()) {
result = false; result = false;
break; break;
} }
@ -214,13 +181,11 @@ ADD_FUNCTION(
return makePtr<Constant>((result) ? Constant::True : Constant::False); return makePtr<Constant>((result) ? Constant::True : Constant::False);
}); });
// FIXME: (count {1}) infinite loop
ADD_FUNCTION( ADD_FUNCTION(
"count", "count",
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("count", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: count, {}", nodes.size()));
return nullptr;
}
auto first_argument = nodes.front(); auto first_argument = nodes.front();
@ -284,10 +249,7 @@ ADD_FUNCTION("println", PRINTER_PRINT(false));
ADD_FUNCTION( ADD_FUNCTION(
"=", "=",
{ {
if (nodes.size() < 2) { CHECK_ARG_COUNT_AT_LEAST("=", nodes.size(), 2);
Error::the().add(format("wrong number of arguments: =, {}", nodes.size()));
return nullptr;
}
std::function<bool(ValuePtr, ValuePtr)> equal = std::function<bool(ValuePtr, ValuePtr)> equal =
[&equal](ValuePtr lhs, ValuePtr rhs) -> bool { [&equal](ValuePtr lhs, ValuePtr rhs) -> bool {
@ -369,17 +331,10 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"read-string", "read-string",
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("read-string", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: read-string, {}", nodes.size()));
return nullptr;
}
if (!is<String>(nodes.front().get())) {
Error::the().add(format("wrong argument type: string, '{}'", nodes.front()));
return nullptr;
}
std::string input = std::static_pointer_cast<String>(nodes.front())->data(); VALUE_CAST(node, String, nodes.front());
std::string input = node->data();
return read(input); return read(input);
}); });
@ -387,17 +342,10 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"slurp", "slurp",
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("slurp", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: slurp, {}", nodes.size()));
return nullptr;
}
if (!is<String>(nodes.front().get())) {
Error::the().add(format("wrong argument type: string, '{}'", nodes.front()));
return nullptr;
}
std::string path = std::static_pointer_cast<String>(nodes.front())->data(); VALUE_CAST(node, String, nodes.front());
std::string path = node->data();
auto file = ruc::File(path); auto file = ruc::File(path);
@ -407,10 +355,7 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"eval", "eval",
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("eval", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: eval, {}", nodes.size()));
return nullptr;
}
return eval(nodes.front(), nullptr); return eval(nodes.front(), nullptr);
}); });
@ -419,10 +364,7 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"atom", "atom",
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("atom", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: atom, {}", nodes.size()));
return nullptr;
}
return makePtr<Atom>(nodes.front()); return makePtr<Atom>(nodes.front());
}); });
@ -451,34 +393,20 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"deref", "deref",
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("deref", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: deref, {}", nodes.size()));
return nullptr;
}
if (!is<Atom>(nodes.front().get())) { VALUE_CAST(atom, Atom, nodes.front());
Error::the().add(format("wrong argument type: atom, '{}'", nodes.front()));
return nullptr;
}
return std::static_pointer_cast<Atom>(nodes.front())->deref(); return atom->deref();
}); });
// (reset! myatom 2) // (reset! myatom 2)
ADD_FUNCTION( ADD_FUNCTION(
"reset!", "reset!",
{ {
if (nodes.size() != 2) { CHECK_ARG_COUNT_IS("reset!", nodes.size(), 2);
Error::the().add(format("wrong number of arguments: reset!, {}", nodes.size()));
return nullptr;
}
if (!is<Atom>(nodes.front().get())) { VALUE_CAST(atom, Atom, nodes.front());
Error::the().add(format("wrong argument type: atom, '{}'", nodes.front()));
return nullptr;
}
auto atom = std::static_pointer_cast<Atom>(*nodes.begin());
auto value = *std::next(nodes.begin()); auto value = *std::next(nodes.begin());
atom->reset(value); atom->reset(value);
@ -490,25 +418,12 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"swap!", "swap!",
{ {
if (nodes.size() < 2) { CHECK_ARG_COUNT_AT_LEAST("swap!", nodes.size(), 2);
Error::the().add(format("wrong number of arguments: swap!, {}", nodes.size()));
return nullptr;
}
auto first_argument = *nodes.begin();
auto second_argument = *std::next(nodes.begin());
if (!is<Atom>(first_argument.get())) { VALUE_CAST(atom, Atom, nodes.front());
Error::the().add(format("wrong argument type: atom, '{}'", first_argument));
return nullptr;
}
if (!is<Callable>(second_argument.get())) { auto second_argument = *std::next(nodes.begin());
Error::the().add(format("wrong argument type: function, '{}'", second_argument)); IS_VALUE(Callable, second_argument);
return nullptr;
}
auto atom = std::static_pointer_cast<Atom>(first_argument);
// Remove atom and function from the argument list, add atom value // Remove atom and function from the argument list, add atom value
nodes.pop_front(); nodes.pop_front();
@ -532,21 +447,12 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"cons", "cons",
{ {
if (nodes.size() != 2) { CHECK_ARG_COUNT_IS("cons", nodes.size(), 2);
Error::the().add(format("wrong number of arguments: cons, {}", nodes.size()));
return nullptr;
}
auto first_argument = *nodes.begin();
auto second_argument = *std::next(nodes.begin());
if (!is<Collection>(second_argument.get())) { VALUE_CAST(collection, Collection, (*std::next(nodes.begin())));
Error::the().add(format("wrong argument type: list, '{}'", second_argument));
return nullptr;
}
auto result_nodes = std::static_pointer_cast<Collection>(second_argument)->nodes(); auto result_nodes = collection->nodes();
result_nodes.push_front(first_argument); result_nodes.push_front(nodes.front());
return makePtr<List>(result_nodes); return makePtr<List>(result_nodes);
}); });
@ -558,12 +464,8 @@ ADD_FUNCTION(
std::list<ValuePtr> result_nodes; std::list<ValuePtr> result_nodes;
for (auto node : nodes) { for (auto node : nodes) {
if (!is<Collection>(node.get())) { VALUE_CAST(collection, Collection, node);
Error::the().add(format("wrong argument type: list, '{}'", node)); auto argument_nodes = collection->nodes();
return nullptr;
}
auto argument_nodes = std::static_pointer_cast<Collection>(node)->nodes();
result_nodes.splice(result_nodes.end(), argument_nodes); result_nodes.splice(result_nodes.end(), argument_nodes);
} }
@ -574,19 +476,11 @@ ADD_FUNCTION(
ADD_FUNCTION( ADD_FUNCTION(
"vec", "vec",
{ {
if (nodes.size() != 1) { CHECK_ARG_COUNT_IS("vec", nodes.size(), 1);
Error::the().add(format("wrong number of arguments: vec, {}", nodes.size()));
return nullptr;
}
if (!is<Collection>(nodes.front().get())) {
Error::the().add(format("wrong argument type: list, '{}'", nodes.front()));
return nullptr;
}
auto result_nodes = std::static_pointer_cast<Collection>(nodes.front())->nodes(); VALUE_CAST(collection, Collection, nodes.front());
return makePtr<Vector>(result_nodes); return makePtr<Vector>(collection->nodes());
}); });
// ----------------------------------------- // -----------------------------------------

1
src/step7_quote.cpp

@ -119,6 +119,7 @@ auto main(int argc, char* argv[]) -> int
arg_parser.addOption(dump_reader, 'r', "dump-reader", nullptr, nullptr); arg_parser.addOption(dump_reader, 'r', "dump-reader", nullptr, nullptr);
arg_parser.addOption(pretty_print, 'c', "color", nullptr, nullptr); arg_parser.addOption(pretty_print, 'c', "color", nullptr, nullptr);
arg_parser.addOption(history_path, 'h', "history-path", nullptr, nullptr, nullptr, ruc::ArgParser::Required::Yes); arg_parser.addOption(history_path, 'h', "history-path", nullptr, nullptr, nullptr, ruc::ArgParser::Required::Yes);
// TODO: Add overload for addArgument(std::vector<std::string_view>)
arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No); arg_parser.addArgument(arguments, "arguments", nullptr, nullptr, ruc::ArgParser::Required::No);
arg_parser.parse(argc, argv); arg_parser.parse(argc, argv);

128
src/util.h

@ -6,9 +6,137 @@
#pragma once #pragma once
#include <memory> // std::static_pointer_cast
#include <string> #include <string>
#include <string_view> #include <string_view>
// -----------------------------------------
// TODO: Move these ruc/test/macro.h -> ruc/src/meta/macro.h
#define GET_2TH_ARG(arg1, arg2, ...) arg2
#define GET_3TH_ARG(arg1, arg2, arg3, ...) arg3
#define GET_4TH_ARG(arg1, arg2, arg3, arg4, ...) arg4
#define GET_5TH_ARG(arg1, arg2, arg3, arg4, arg5, ...) arg5
#define GET_6TH_ARG(arg1, arg2, arg3, arg4, arg5, arg6, ...) arg6
#define MACRO_CHOOSER_1(macro, ...) \
GET_2TH_ARG(__VA_ARGS__, macro##_1, )
#define MACRO_CHOOSER_2(macro, ...) \
GET_3TH_ARG(__VA_ARGS__, macro##_2, macro##_1, )
#define MACRO_CHOOSER_3(macro, ...) \
GET_4TH_ARG(__VA_ARGS__, macro##_3, macro##_2, macro##_1, )
#define MACRO_CHOOSER_4(macro, ...) \
GET_5TH_ARG(__VA_ARGS__, macro##_4, macro##_3, macro##_2, macro##_1, )
#define MACRO_CHOOSER_5(macro, ...) \
GET_6TH_ARG(__VA_ARGS__, macro##_5, macro##_4, macro##_3, macro##_2, macro##_1, )
// -----------------------------------------
#define CHECK_ARG_COUNT_IS_IMPL(name, size, expected, result) \
if (size != expected) { \
Error::the().add(format("wrong number of arguments: {}, {}", name, size)); \
return result; \
}
#define CHECK_ARG_COUNT_IS_3(name, size, expected) \
CHECK_ARG_COUNT_IS_IMPL(name, size, expected, nullptr)
#define CHECK_ARG_COUNT_IS_4(name, size, expected, result) \
CHECK_ARG_COUNT_IS_IMPL(name, size, expected, result)
#define CHECK_ARG_COUNT_IS(...) \
MACRO_CHOOSER_4(CHECK_ARG_COUNT_IS, __VA_ARGS__) \
(__VA_ARGS__)
// -----------------------------------------
#define CHECK_ARG_COUNT_AT_LEAST_IMPL(name, size, min, result) \
if (size < min) { \
Error::the().add(format("wrong number of arguments: {}, {}", name, size)); \
return result; \
}
#define CHECK_ARG_COUNT_AT_LEAST_3(name, size, min) \
CHECK_ARG_COUNT_AT_LEAST_IMPL(name, size, min, nullptr)
#define CHECK_ARG_COUNT_AT_LEAST_4(name, size, min, result) \
CHECK_ARG_COUNT_AT_LEAST_IMPL(name, size, min, result)
#define CHECK_ARG_COUNT_AT_LEAST(...) \
MACRO_CHOOSER_4(CHECK_ARG_COUNT_AT_LEAST, __VA_ARGS__) \
(__VA_ARGS__)
// -----------------------------------------
#define CHECK_ARG_COUNT_BETWEEN_IMPL(name, size, min, max, result) \
if (size < min || size > max) { \
Error::the().add(format("wrong number of arguments: {}, {}", name, size)); \
return result; \
}
#define CHECK_ARG_COUNT_BETWEEN_4(name, size, min, max) \
CHECK_ARG_COUNT_BETWEEN_IMPL(name, size, min, max, nullptr)
#define CHECK_ARG_COUNT_BETWEEN_5(name, size, min, max, result) \
CHECK_ARG_COUNT_BETWEEN_IMPL(name, size, min, max, result)
#define CHECK_ARG_COUNT_BETWEEN(...) \
MACRO_CHOOSER_5(CHECK_ARG_COUNT_BETWEEN, __VA_ARGS__) \
(__VA_ARGS__)
// -----------------------------------------
#define CHECK_ARG_COUNT_EVEN_IMPL(name, size, result) \
if (size % 2 != 0) { \
Error::the().add(format("wrong number of arguments: {}, {}", name, size)); \
return result; \
}
#define CHECK_ARG_COUNT_EVEN_2(name, size) \
CHECK_ARG_COUNT_EVEN_IMPL(name, size, nullptr)
#define CHECK_ARG_COUNT_EVEN_3(name, size, result) \
CHECK_ARG_COUNT_EVEN_IMPL(name, size, result)
#define CHECK_ARG_COUNT_EVEN(...) \
MACRO_CHOOSER_3(CHECK_ARG_COUNT_EVEN, __VA_ARGS__) \
(__VA_ARGS__)
// -----------------------------------------
#define IS_VALUE_IMPL(type, value, result) \
if (!is<type>(value.get())) { \
Error::the().add(format("wrong argument type: {}, {}", #type, value)); \
return result; \
}
#define IS_VALUE_2(type, value) \
IS_VALUE_IMPL(type, value, nullptr)
#define IS_VALUE_3(type, value, result) \
IS_VALUE_IMPL(type, value, result)
#define IS_VALUE(...) \
MACRO_CHOOSER_3(IS_VALUE, __VA_ARGS__) \
(__VA_ARGS__)
// -----------------------------------------
#define VALUE_CAST_IMPL(variable, type, value, result) \
IS_VALUE(type, value, result); \
auto variable = std::static_pointer_cast<type>(value);
#define VALUE_CAST_3(variable, type, value) \
VALUE_CAST_IMPL(variable, type, value, nullptr)
#define VALUE_CAST_4(variable, type, value, result) \
VALUE_CAST_IMPL(variable, type, value, result)
#define VALUE_CAST(...) \
MACRO_CHOOSER_4(VALUE_CAST, __VA_ARGS__) \
(__VA_ARGS__)
// -----------------------------------------
namespace blaze { namespace blaze {
template<typename It, typename C> template<typename It, typename C>

Loading…
Cancel
Save