Compare commits
4
Commits
ba7281b6f1
...
27d6e24243
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
27d6e24243 | ||
|
|
b4742ef380 | ||
|
|
082a029957 | ||
|
|
80e0437a5c |
@@ -19,6 +19,7 @@ AllowShortLambdasOnASingleLine: All
|
||||
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
IndentPPDirectives: BeforeHash
|
||||
RequiresClausePosition: SingleLine
|
||||
|
||||
BraceWrapping:
|
||||
AfterEnum: false
|
||||
|
||||
@@ -107,3 +107,7 @@ add_dependencies(test5 ${PROJECT})
|
||||
add_custom_target(test6
|
||||
COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step6_file.mal -- ./${PROJECT})
|
||||
add_dependencies(test6 ${PROJECT})
|
||||
|
||||
add_custom_target(test7
|
||||
COMMAND env STEP=step_env MAL_IMPL=js ../vendor/mal/runtest.py --deferrable --optional ../vendor/mal/tests/step7_quote.mal -- ./${PROJECT})
|
||||
add_dependencies(test7 ${PROJECT})
|
||||
|
||||
+20
-1
@@ -16,6 +16,11 @@
|
||||
|
||||
namespace blaze {
|
||||
|
||||
Collection::Collection(const std::list<ValuePtr>& nodes)
|
||||
: m_nodes(nodes)
|
||||
{
|
||||
}
|
||||
|
||||
void Collection::add(ValuePtr node)
|
||||
{
|
||||
if (node == nullptr) {
|
||||
@@ -27,6 +32,20 @@ void Collection::add(ValuePtr node)
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
List::List(const std::list<ValuePtr>& nodes)
|
||||
: Collection(nodes)
|
||||
{
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
Vector::Vector(const std::list<ValuePtr>& nodes)
|
||||
: Collection(nodes)
|
||||
{
|
||||
}
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
void HashMap::add(const std::string& key, ValuePtr value)
|
||||
{
|
||||
if (value == nullptr) {
|
||||
@@ -81,7 +100,7 @@ Function::Function(const std::string& name, FunctionType function)
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
Lambda::Lambda(std::vector<std::string> bindings, ValuePtr body, EnvironmentPtr env)
|
||||
Lambda::Lambda(const std::vector<std::string>& bindings, ValuePtr body, EnvironmentPtr env)
|
||||
: m_bindings(bindings)
|
||||
, m_body(body)
|
||||
, m_env(env)
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <concepts> // std::derived_from, std::same_as
|
||||
#include <cstdint> // int64_t, uint8_t
|
||||
#include <functional> // std::function
|
||||
#include <list>
|
||||
@@ -52,18 +53,29 @@ protected:
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
template<typename T>
|
||||
concept IsValue = std::same_as<Value, T> || std::derived_from<T, Value>;
|
||||
|
||||
class Collection : public Value {
|
||||
public:
|
||||
virtual ~Collection() = default;
|
||||
|
||||
void add(ValuePtr node);
|
||||
|
||||
const std::list<ValuePtr>& nodes() const { return m_nodes; }
|
||||
size_t size() const { return m_nodes.size(); }
|
||||
bool empty() const { return m_nodes.size() == 0; }
|
||||
|
||||
const std::list<ValuePtr>& nodes() const { return m_nodes; }
|
||||
|
||||
protected:
|
||||
Collection() = default;
|
||||
Collection(const std::list<ValuePtr>& nodes);
|
||||
|
||||
template<IsValue... Ts>
|
||||
Collection(std::shared_ptr<Ts>... nodes)
|
||||
{
|
||||
m_nodes = { nodes... };
|
||||
}
|
||||
|
||||
private:
|
||||
virtual bool isCollection() const override { return true; }
|
||||
@@ -77,6 +89,14 @@ private:
|
||||
class List final : public Collection {
|
||||
public:
|
||||
List() = default;
|
||||
List(const std::list<ValuePtr>& nodes);
|
||||
|
||||
template<IsValue... Ts>
|
||||
List(std::shared_ptr<Ts>... nodes)
|
||||
: Collection(nodes...)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~List() = default;
|
||||
|
||||
private:
|
||||
@@ -89,6 +109,14 @@ private:
|
||||
class Vector final : public Collection {
|
||||
public:
|
||||
Vector() = default;
|
||||
Vector(const std::list<ValuePtr>& nodes);
|
||||
|
||||
template<IsValue... Ts>
|
||||
Vector(std::shared_ptr<Ts>... nodes)
|
||||
: Collection(nodes...)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~Vector() = default;
|
||||
|
||||
private:
|
||||
@@ -236,10 +264,10 @@ private:
|
||||
|
||||
class Lambda final : public Callable {
|
||||
public:
|
||||
Lambda(std::vector<std::string> bindings, ValuePtr body, EnvironmentPtr env);
|
||||
Lambda(const std::vector<std::string>& bindings, ValuePtr body, EnvironmentPtr env);
|
||||
virtual ~Lambda() = default;
|
||||
|
||||
std::vector<std::string> bindings() const { return m_bindings; }
|
||||
const std::vector<std::string>& bindings() const { return m_bindings; }
|
||||
ValuePtr body() const { return m_body; }
|
||||
EnvironmentPtr env() const { return m_env; }
|
||||
|
||||
|
||||
+146
-74
@@ -16,6 +16,7 @@
|
||||
#include "eval.h"
|
||||
#include "forward.h"
|
||||
#include "types.h"
|
||||
#include "util.h"
|
||||
|
||||
namespace blaze {
|
||||
|
||||
@@ -79,6 +80,16 @@ ValuePtr Eval::evalImpl()
|
||||
evalLet(nodes, env);
|
||||
continue; // TCO
|
||||
}
|
||||
if (symbol == "quote") {
|
||||
return evalQuote(nodes);
|
||||
}
|
||||
if (symbol == "quasiquote") {
|
||||
evalQuasiQuote(nodes, env);
|
||||
continue; // TCO
|
||||
}
|
||||
if (symbol == "quasiquoteexpand") {
|
||||
return evalQuasiQuoteExpand(nodes, env);
|
||||
}
|
||||
if (symbol == "do") {
|
||||
evalDo(nodes, env);
|
||||
continue; // TCO
|
||||
@@ -165,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 argument needs to be a Symbol
|
||||
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();
|
||||
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();
|
||||
|
||||
@@ -192,48 +196,136 @@ 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)
|
||||
{
|
||||
CHECK_ARG_COUNT_IS("quote", nodes.size(), 1);
|
||||
|
||||
return nodes.front();
|
||||
}
|
||||
|
||||
static bool isSymbol(ValuePtr value, const std::string& symbol)
|
||||
{
|
||||
if (!is<Symbol>(value.get())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto valueSymbol = std::static_pointer_cast<Symbol>(value)->symbol();
|
||||
|
||||
if (valueSymbol != symbol) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static ValuePtr startsWith(ValuePtr ast, const std::string& symbol)
|
||||
{
|
||||
if (!is<List>(ast.get())) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto nodes = std::static_pointer_cast<List>(ast)->nodes();
|
||||
|
||||
if (nodes.empty() || !isSymbol(nodes.front(), symbol)) {
|
||||
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());
|
||||
}
|
||||
|
||||
static ValuePtr evalQuasiQuoteImpl(ValuePtr ast)
|
||||
{
|
||||
if (is<HashMap>(ast.get()) || is<Symbol>(ast.get())) {
|
||||
return makePtr<List>(makePtr<Symbol>("quote"), ast);
|
||||
}
|
||||
|
||||
if (!is<Collection>(ast.get())) {
|
||||
return ast;
|
||||
}
|
||||
|
||||
// `~2 or `(unquote 2)
|
||||
const auto unquote = startsWith(ast, "unquote"); // x
|
||||
if (unquote) {
|
||||
return unquote;
|
||||
}
|
||||
|
||||
// `~@(list 2 2 2) or `(splice-unquote (list 2 2 2))
|
||||
const auto splice_unquote = startsWith(ast, "splice-unquote"); // (list 2 2 2)
|
||||
if (splice_unquote) {
|
||||
return splice_unquote;
|
||||
}
|
||||
|
||||
ValuePtr result = makePtr<List>();
|
||||
|
||||
auto nodes = std::static_pointer_cast<Collection>(ast)->nodes();
|
||||
|
||||
// `() or `(1 ~2 3) or `(1 ~@(list 2 2 2) 3)
|
||||
for (auto it = nodes.rbegin(); it != nodes.rend(); ++it) {
|
||||
const auto elt = *it;
|
||||
|
||||
const auto splice_unquote = startsWith(elt, "splice-unquote"); // (list 2 2 2)
|
||||
if (splice_unquote) {
|
||||
// (cons 1 (concat (list 2 2 2) (cons 3 ())))
|
||||
result = makePtr<List>(makePtr<Symbol>("concat"), splice_unquote, result);
|
||||
continue;
|
||||
}
|
||||
|
||||
// (cons 1 (cons 2 (cons 3 ())))
|
||||
result = makePtr<List>(makePtr<Symbol>("cons"), evalQuasiQuoteImpl(elt), result);
|
||||
}
|
||||
|
||||
if (is<List>(ast.get())) {
|
||||
return result;
|
||||
}
|
||||
|
||||
// Wrap result in (vec) for Vector types
|
||||
return makePtr<List>(makePtr<Symbol>("vec"), result);
|
||||
}
|
||||
|
||||
void Eval::evalQuasiQuote(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
|
||||
{
|
||||
CHECK_ARG_COUNT_IS("quasiquote", nodes.size(), 1, void());
|
||||
|
||||
auto result = evalQuasiQuoteImpl(nodes.front());
|
||||
|
||||
m_ast_stack.push(result);
|
||||
m_env_stack.push(env);
|
||||
return; // TCO
|
||||
}
|
||||
|
||||
ValuePtr Eval::evalQuasiQuoteExpand(const std::list<ValuePtr>& nodes, EnvironmentPtr env)
|
||||
{
|
||||
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: collection, '{}'", 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();
|
||||
@@ -241,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) {
|
||||
@@ -269,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());
|
||||
@@ -293,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) {
|
||||
|
||||
@@ -28,11 +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);
|
||||
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;
|
||||
|
||||
+82
-133
@@ -27,7 +27,7 @@
|
||||
#define ADD_FUNCTION_IMPL(unique, symbol, lambda) \
|
||||
struct FUNCTION_STRUCT_NAME(unique) { \
|
||||
FUNCTION_STRUCT_NAME(unique) \
|
||||
(std::string __symbol, FunctionType __lambda) \
|
||||
(const std::string& __symbol, FunctionType __lambda) \
|
||||
{ \
|
||||
s_functions.emplace(__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);
|
||||
@@ -134,24 +113,16 @@ ADD_FUNCTION(
|
||||
{ \
|
||||
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; \
|
||||
} \
|
||||
} \
|
||||
CHECK_ARG_COUNT_AT_LEAST(#operator, nodes.size(), 2); \
|
||||
\
|
||||
/* Start with the first number */ \
|
||||
int64_t number = std::static_pointer_cast<Number>(nodes.front())->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) { \
|
||||
int64_t current_number = std::static_pointer_cast<Number>(*it)->number(); \
|
||||
VALUE_CAST(current_number_node, Number, (*it)); \
|
||||
int64_t current_number = current_number_node->number(); \
|
||||
if (!(number operator current_number)) { \
|
||||
result = false; \
|
||||
break; \
|
||||
@@ -172,13 +143,7 @@ ADD_FUNCTION(">=", NUMBER_COMPARE(>=));
|
||||
ADD_FUNCTION(
|
||||
"list",
|
||||
{
|
||||
auto list = makePtr<List>();
|
||||
|
||||
for (auto node : nodes) {
|
||||
list->add(node);
|
||||
}
|
||||
|
||||
return list;
|
||||
return makePtr<List>(nodes);
|
||||
});
|
||||
|
||||
ADD_FUNCTION(
|
||||
@@ -206,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;
|
||||
}
|
||||
@@ -220,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();
|
||||
|
||||
@@ -290,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 {
|
||||
@@ -375,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;
|
||||
}
|
||||
CHECK_ARG_COUNT_IS("read-string", nodes.size(), 1);
|
||||
|
||||
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);
|
||||
});
|
||||
@@ -393,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;
|
||||
}
|
||||
CHECK_ARG_COUNT_IS("slurp", nodes.size(), 1);
|
||||
|
||||
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);
|
||||
|
||||
@@ -413,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);
|
||||
});
|
||||
@@ -425,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());
|
||||
});
|
||||
@@ -457,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);
|
||||
@@ -496,25 +418,12 @@ ADD_FUNCTION(
|
||||
ADD_FUNCTION(
|
||||
"swap!",
|
||||
{
|
||||
if (nodes.size() < 2) {
|
||||
Error::the().add(format("wrong number of arguments: reset!, {}", nodes.size()));
|
||||
return nullptr;
|
||||
}
|
||||
CHECK_ARG_COUNT_AT_LEAST("swap!", nodes.size(), 2);
|
||||
|
||||
VALUE_CAST(atom, Atom, nodes.front());
|
||||
|
||||
auto first_argument = *nodes.begin();
|
||||
auto second_argument = *std::next(nodes.begin());
|
||||
|
||||
if (!is<Atom>(first_argument.get())) {
|
||||
Error::the().add(format("wrong argument type: atom, '{}'", first_argument));
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
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);
|
||||
IS_VALUE(Callable, second_argument);
|
||||
|
||||
// Remove atom and function from the argument list, add atom value
|
||||
nodes.pop_front();
|
||||
@@ -534,6 +443,46 @@ ADD_FUNCTION(
|
||||
return atom->reset(value);
|
||||
});
|
||||
|
||||
// (cons 1 (list 2 3))
|
||||
ADD_FUNCTION(
|
||||
"cons",
|
||||
{
|
||||
CHECK_ARG_COUNT_IS("cons", nodes.size(), 2);
|
||||
|
||||
VALUE_CAST(collection, Collection, (*std::next(nodes.begin())));
|
||||
|
||||
auto result_nodes = collection->nodes();
|
||||
result_nodes.push_front(nodes.front());
|
||||
|
||||
return makePtr<List>(result_nodes);
|
||||
});
|
||||
|
||||
// (concat (list 1) (list 2 3))
|
||||
ADD_FUNCTION(
|
||||
"concat",
|
||||
{
|
||||
std::list<ValuePtr> result_nodes;
|
||||
|
||||
for (auto node : nodes) {
|
||||
VALUE_CAST(collection, Collection, node);
|
||||
auto argument_nodes = collection->nodes();
|
||||
result_nodes.splice(result_nodes.end(), argument_nodes);
|
||||
}
|
||||
|
||||
return makePtr<List>(result_nodes);
|
||||
});
|
||||
|
||||
// (vec (list 1 2 3))
|
||||
ADD_FUNCTION(
|
||||
"vec",
|
||||
{
|
||||
CHECK_ARG_COUNT_IS("vec", nodes.size(), 1);
|
||||
|
||||
VALUE_CAST(collection, Collection, nodes.front());
|
||||
|
||||
return makePtr<Vector>(collection->nodes());
|
||||
});
|
||||
|
||||
// -----------------------------------------
|
||||
|
||||
void installFunctions(EnvironmentPtr env)
|
||||
|
||||
+5
-3
@@ -180,13 +180,12 @@ ValuePtr Reader::readHashMap()
|
||||
auto hash_map = makePtr<HashMap>();
|
||||
while (!isEOF() && peek().type != Token::Type::BraceClose) {
|
||||
auto key = readImpl();
|
||||
auto value = readImpl();
|
||||
|
||||
if (key == nullptr && value == nullptr) {
|
||||
if (key == nullptr || isEOF()) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (key == nullptr || value == nullptr) {
|
||||
if (peek().type == Token::Type::BraceClose) {
|
||||
Error::the().add("hash-map requires an even-sized list");
|
||||
return nullptr;
|
||||
}
|
||||
@@ -196,12 +195,15 @@ ValuePtr Reader::readHashMap()
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto value = readImpl();
|
||||
|
||||
std::string keyString = is<String>(key.get()) ? std::static_pointer_cast<String>(key)->data() : std::static_pointer_cast<Keyword>(key)->keyword();
|
||||
hash_map->add(keyString, value);
|
||||
}
|
||||
|
||||
if (!consumeSpecific(Token { .type = Token::Type::BraceClose })) { // }
|
||||
Error::the().add("expected '}', got EOF");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return hash_map;
|
||||
|
||||
+1
-1
@@ -24,7 +24,7 @@
|
||||
#include "readline.h"
|
||||
#include "settings.h"
|
||||
|
||||
#if 1
|
||||
#if 0
|
||||
namespace blaze {
|
||||
|
||||
static EnvironmentPtr s_outer_env = Environment::create();
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Riyyi
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <csignal> // std::signal
|
||||
#include <cstdlib> // std::exit
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
#include "ruc/argparser.h"
|
||||
#include "ruc/format/color.h"
|
||||
|
||||
#include "ast.h"
|
||||
#include "environment.h"
|
||||
#include "error.h"
|
||||
#include "eval.h"
|
||||
#include "forward.h"
|
||||
#include "lexer.h"
|
||||
#include "printer.h"
|
||||
#include "reader.h"
|
||||
#include "readline.h"
|
||||
#include "settings.h"
|
||||
|
||||
#if 1
|
||||
namespace blaze {
|
||||
|
||||
static EnvironmentPtr s_outer_env = Environment::create();
|
||||
|
||||
static auto cleanup(int signal) -> void
|
||||
{
|
||||
print("\033[0m\n");
|
||||
std::exit(signal);
|
||||
}
|
||||
|
||||
auto read(std::string_view input) -> ValuePtr
|
||||
{
|
||||
Lexer lexer(input);
|
||||
lexer.tokenize();
|
||||
if (Settings::the().get("dump-lexer") == "1") {
|
||||
lexer.dump();
|
||||
}
|
||||
|
||||
Reader reader(std::move(lexer.tokens()));
|
||||
reader.read();
|
||||
if (Settings::the().get("dump-reader") == "1") {
|
||||
reader.dump();
|
||||
}
|
||||
|
||||
return reader.node();
|
||||
}
|
||||
|
||||
auto eval(ValuePtr ast, EnvironmentPtr env) -> ValuePtr
|
||||
{
|
||||
if (env == nullptr) {
|
||||
env = s_outer_env;
|
||||
}
|
||||
|
||||
Eval eval(ast, env);
|
||||
eval.eval();
|
||||
|
||||
return eval.ast();
|
||||
}
|
||||
|
||||
static auto print(ValuePtr exp) -> std::string
|
||||
{
|
||||
Printer printer;
|
||||
|
||||
return printer.print(exp, true);
|
||||
}
|
||||
|
||||
static auto rep(std::string_view input, EnvironmentPtr env) -> std::string
|
||||
{
|
||||
Error::the().clearErrors();
|
||||
Error::the().setInput(input);
|
||||
|
||||
return print(eval(read(input), env));
|
||||
}
|
||||
|
||||
static std::string_view lambdaTable[] = {
|
||||
"(def! not (fn* (cond) (if cond false true)))",
|
||||
"(def! load-file (fn* (filename) \
|
||||
(eval (read-string (str \"(do \" (slurp filename) \"\nnil)\")))))",
|
||||
};
|
||||
|
||||
static auto installLambdas(EnvironmentPtr env) -> void
|
||||
{
|
||||
for (auto function : lambdaTable) {
|
||||
rep(function, env);
|
||||
}
|
||||
}
|
||||
|
||||
static auto makeArgv(EnvironmentPtr env, std::vector<std::string> arguments) -> void
|
||||
{
|
||||
auto list = makePtr<List>();
|
||||
if (arguments.size() > 1) {
|
||||
for (auto it = arguments.begin() + 1; it != arguments.end(); ++it) {
|
||||
list->add(makePtr<String>(*it));
|
||||
}
|
||||
}
|
||||
env->set("*ARGV*", list);
|
||||
}
|
||||
|
||||
} // namespace blaze
|
||||
|
||||
auto main(int argc, char* argv[]) -> int
|
||||
{
|
||||
bool dump_lexer = false;
|
||||
bool dump_reader = false;
|
||||
bool pretty_print = false;
|
||||
std::string_view history_path = "~/.blaze-history";
|
||||
std::vector<std::string> arguments;
|
||||
|
||||
// CLI arguments
|
||||
ruc::ArgParser arg_parser;
|
||||
arg_parser.addOption(dump_lexer, 'l', "dump-lexer", nullptr, nullptr);
|
||||
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);
|
||||
|
||||
// Set settings
|
||||
blaze::Settings::the().set("dump-lexer", dump_lexer ? "1" : "0");
|
||||
blaze::Settings::the().set("dump-reader", dump_reader ? "1" : "0");
|
||||
blaze::Settings::the().set("pretty-print", pretty_print ? "1" : "0");
|
||||
|
||||
// Signal callbacks
|
||||
std::signal(SIGINT, blaze::cleanup);
|
||||
std::signal(SIGTERM, blaze::cleanup);
|
||||
|
||||
installFunctions(blaze::s_outer_env);
|
||||
installLambdas(blaze::s_outer_env);
|
||||
makeArgv(blaze::s_outer_env, arguments);
|
||||
|
||||
if (arguments.size() > 0) {
|
||||
rep(format("(load-file \"{}\")", arguments.front()), blaze::s_outer_env);
|
||||
return 0;
|
||||
}
|
||||
|
||||
blaze::Readline readline(pretty_print, history_path);
|
||||
|
||||
std::string input;
|
||||
while (readline.get(input)) {
|
||||
if (pretty_print) {
|
||||
print("\033[0m");
|
||||
}
|
||||
print("{}\n", rep(input, blaze::s_outer_env));
|
||||
}
|
||||
|
||||
if (pretty_print) {
|
||||
print("\033[0m");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
+128
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user