|
|
|
@ -7,7 +7,6 @@
|
|
|
|
|
#include <memory> // std::static_pointer_cast |
|
|
|
|
#include <string> |
|
|
|
|
|
|
|
|
|
#include "ruc/format/color.h" |
|
|
|
|
#include "ruc/format/format.h" |
|
|
|
|
|
|
|
|
|
#include "ast.h" |
|
|
|
@ -18,11 +17,34 @@
|
|
|
|
|
#include "types.h" |
|
|
|
|
#include "util.h" |
|
|
|
|
|
|
|
|
|
// At the top-level you cant invoke any function, but you can create variables.
|
|
|
|
|
// Using a struct's constructor you can work around this limitation.
|
|
|
|
|
// Also the line number in the file is used to make the struct names unique.
|
|
|
|
|
|
|
|
|
|
#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, \
|
|
|
|
|
[](std::list<ASTNodePtr> nodes) -> ASTNodePtr lambda); |
|
|
|
|
|
|
|
|
|
#define ADD_FUNCTION(symbol, lambda) ADD_FUNCTION_IMPL(__LINE__, symbol, lambda); |
|
|
|
|
|
|
|
|
|
namespace blaze { |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::add() |
|
|
|
|
static std::unordered_map<std::string, FunctionType> s_functions; |
|
|
|
|
|
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"+", |
|
|
|
|
{ |
|
|
|
|
auto add = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
int64_t result = 0; |
|
|
|
|
|
|
|
|
|
for (auto node : nodes) { |
|
|
|
@ -35,14 +57,11 @@ void GlobalEnvironment::add()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return makePtr<Number>(result); |
|
|
|
|
}; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
m_values.emplace("+", makePtr<Function>(add)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::sub() |
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"-", |
|
|
|
|
{ |
|
|
|
|
auto sub = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
if (nodes.size() == 0) { |
|
|
|
|
return makePtr<Number>(0); |
|
|
|
|
} |
|
|
|
@ -63,14 +82,11 @@ void GlobalEnvironment::sub()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return makePtr<Number>(result); |
|
|
|
|
}; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
m_values.emplace("-", makePtr<Function>(sub)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::mul() |
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"*", |
|
|
|
|
{ |
|
|
|
|
auto mul = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
int64_t result = 1; |
|
|
|
|
|
|
|
|
|
for (auto node : nodes) { |
|
|
|
@ -83,16 +99,13 @@ void GlobalEnvironment::mul()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return makePtr<Number>(result); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
m_values.emplace("*", makePtr<Function>(mul)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::div() |
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"/", |
|
|
|
|
{ |
|
|
|
|
auto div = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
if (nodes.size() == 0) { |
|
|
|
|
Error::the().add(format("wrong number of arguments: {}, 0", m_current_key)); |
|
|
|
|
Error::the().add(format("wrong number of arguments: /, 0")); |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -112,19 +125,16 @@ void GlobalEnvironment::div()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return makePtr<Number>((int64_t)result); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
m_values.emplace("/", makePtr<Function>(div)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
// // -----------------------------------------
|
|
|
|
|
|
|
|
|
|
#define NUMBER_COMPARE(symbol, comparison_symbol) \ |
|
|
|
|
auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
|
|
|
|
|
#define NUMBER_COMPARE(operator) \ |
|
|
|
|
{ \
|
|
|
|
|
bool result = true; \
|
|
|
|
|
\
|
|
|
|
|
if (nodes.size() < 2) { \
|
|
|
|
|
Error::the().add(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); \
|
|
|
|
|
Error::the().add(format("wrong number of arguments: {}, {}", #operator, nodes.size() - 1)); \
|
|
|
|
|
return nullptr; \
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
@ -141,7 +151,7 @@ void GlobalEnvironment::div()
|
|
|
|
|
/* 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 comparison_symbol current_number) { \
|
|
|
|
|
if (!(number operator current_number)) { \
|
|
|
|
|
result = false; \
|
|
|
|
|
break; \
|
|
|
|
|
} \
|
|
|
|
@ -149,35 +159,18 @@ void GlobalEnvironment::div()
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
return makePtr<Value>((result) ? Value::True : Value::False); \
|
|
|
|
|
}; \
|
|
|
|
|
\
|
|
|
|
|
m_values.emplace(symbol, makePtr<Function>(lambda)); |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::lt() |
|
|
|
|
{ |
|
|
|
|
NUMBER_COMPARE("<", >=); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::lte() |
|
|
|
|
{ |
|
|
|
|
NUMBER_COMPARE("<=", >); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::gt() |
|
|
|
|
{ |
|
|
|
|
NUMBER_COMPARE(">", <=); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::gte() |
|
|
|
|
{ |
|
|
|
|
NUMBER_COMPARE(">=", <); |
|
|
|
|
} |
|
|
|
|
ADD_FUNCTION("<", NUMBER_COMPARE(<)); |
|
|
|
|
ADD_FUNCTION("<=", NUMBER_COMPARE(<=)); |
|
|
|
|
ADD_FUNCTION(">", NUMBER_COMPARE(>)); |
|
|
|
|
ADD_FUNCTION(">=", NUMBER_COMPARE(>=)); |
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::list() |
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"list", |
|
|
|
|
{ |
|
|
|
|
auto list = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
auto list = makePtr<List>(); |
|
|
|
|
|
|
|
|
|
for (auto node : nodes) { |
|
|
|
@ -185,14 +178,11 @@ void GlobalEnvironment::list()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return list; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
m_values.emplace("list", makePtr<Function>(list)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::isList() |
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"list?", |
|
|
|
|
{ |
|
|
|
|
auto is_list = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
bool result = true; |
|
|
|
|
|
|
|
|
|
for (auto node : nodes) { |
|
|
|
@ -203,14 +193,11 @@ void GlobalEnvironment::isList()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return makePtr<Value>((result) ? Value::True : Value::False); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
m_values.emplace("list?", makePtr<Function>(is_list)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::isEmpty() |
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"empty?", |
|
|
|
|
{ |
|
|
|
|
auto is_empty = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
bool result = true; |
|
|
|
|
|
|
|
|
|
for (auto node : nodes) { |
|
|
|
@ -226,16 +213,13 @@ void GlobalEnvironment::isEmpty()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return makePtr<Value>((result) ? Value::True : Value::False); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
m_values.emplace("empty?", makePtr<Function>(is_empty)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::count() |
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"count", |
|
|
|
|
{ |
|
|
|
|
auto count = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
if (nodes.size() != 1) { |
|
|
|
|
Error::the().add(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); |
|
|
|
|
Error::the().add(format("wrong number of arguments: count, {}", nodes.size() - 1)); |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -255,15 +239,12 @@ void GlobalEnvironment::count()
|
|
|
|
|
|
|
|
|
|
// FIXME: Add numeric_limits check for implicit cast: size_t > int64_t
|
|
|
|
|
return makePtr<Number>((int64_t)result); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
m_values.emplace("count", makePtr<Function>(count)); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
#define PRINTER_STRING(symbol, concatenation, print_readably) \ |
|
|
|
|
auto lambda = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
|
|
|
|
|
#define PRINTER_STRING(print_readably, concatenation) \ |
|
|
|
|
{ \
|
|
|
|
|
std::string result; \
|
|
|
|
|
\
|
|
|
|
|
Printer printer; \
|
|
|
|
@ -276,22 +257,13 @@ void GlobalEnvironment::count()
|
|
|
|
|
} \
|
|
|
|
|
\
|
|
|
|
|
return makePtr<String>(result); \
|
|
|
|
|
}; \
|
|
|
|
|
\
|
|
|
|
|
m_values.emplace(symbol, makePtr<Function>(lambda)); |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::str() |
|
|
|
|
{ |
|
|
|
|
PRINTER_STRING("str", "", false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::prStr() |
|
|
|
|
{ |
|
|
|
|
PRINTER_STRING("pr-str", " ", true); |
|
|
|
|
} |
|
|
|
|
ADD_FUNCTION("str", PRINTER_STRING(false, "")); |
|
|
|
|
ADD_FUNCTION("pr-str", PRINTER_STRING(true, " ")); |
|
|
|
|
|
|
|
|
|
#define PRINTER_PRINT(symbol, print_readably) \ |
|
|
|
|
auto lambda = [](std::list<ASTNodePtr> nodes) -> ASTNodePtr { \
|
|
|
|
|
#define PRINTER_PRINT(print_readably) \ |
|
|
|
|
{ \
|
|
|
|
|
Printer printer; \
|
|
|
|
|
for (auto it = nodes.begin(); it != nodes.end(); ++it) { \
|
|
|
|
|
print("{}", printer.printNoErrorCheck(*it, print_readably)); \
|
|
|
|
@ -303,27 +275,18 @@ void GlobalEnvironment::prStr()
|
|
|
|
|
print("\n"); \
|
|
|
|
|
\
|
|
|
|
|
return makePtr<Value>(Value::Nil); \
|
|
|
|
|
}; \
|
|
|
|
|
\
|
|
|
|
|
m_values.emplace(symbol, makePtr<Function>(lambda)); |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::prn() |
|
|
|
|
{ |
|
|
|
|
PRINTER_PRINT("prn", true); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::println() |
|
|
|
|
{ |
|
|
|
|
PRINTER_PRINT("println", false); |
|
|
|
|
} |
|
|
|
|
ADD_FUNCTION("prn", PRINTER_PRINT(true)); |
|
|
|
|
ADD_FUNCTION("println", PRINTER_PRINT(false)); |
|
|
|
|
|
|
|
|
|
// -----------------------------------------
|
|
|
|
|
|
|
|
|
|
void GlobalEnvironment::equal() |
|
|
|
|
ADD_FUNCTION( |
|
|
|
|
"=", |
|
|
|
|
{ |
|
|
|
|
auto lambda = [this](std::list<ASTNodePtr> nodes) -> ASTNodePtr { |
|
|
|
|
if (nodes.size() < 2) { |
|
|
|
|
Error::the().add(format("wrong number of arguments: {}, {}", m_current_key, nodes.size() - 1)); |
|
|
|
|
Error::the().add(format("wrong number of arguments: =, {}", nodes.size() - 1)); |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -402,9 +365,13 @@ void GlobalEnvironment::equal()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return makePtr<Value>((result) ? Value::True : Value::False); |
|
|
|
|
}; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
m_values.emplace("=", makePtr<Function>(lambda)); |
|
|
|
|
void installFunctions(EnvironmentPtr env) |
|
|
|
|
{ |
|
|
|
|
for (const auto& [name, lambda] : s_functions) { |
|
|
|
|
env->set(name, makePtr<Function>(name, lambda)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace blaze
|
|
|
|
|