Browse Source

Eval+Env: Make use of macros for duplicate logic

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

4
src/eval.h

@ -28,14 +28,16 @@ public:
private:
ValuePtr evalImpl();
ValuePtr evalAst(ValuePtr ast, EnvironmentPtr env);
ValuePtr evalDef(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);
ValuePtr evalQuasiQuoteExpand(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);
ValuePtr evalFn(const std::list<ValuePtr>& nodes, EnvironmentPtr env);
ValuePtr apply(std::shared_ptr<List> evaluated_list);
ValuePtr m_ast;

260
src/functions.cpp

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

128
src/util.h

@ -6,9 +6,137 @@
#pragma once
#include <memory> // std::static_pointer_cast
#include <string>
#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 {
template<typename It, typename C>

Loading…
Cancel
Save