Browse Source

Env: Allow load order control for native functions

Riyyi 1 year ago
parent
commit
b65482eb68
  1. 16
      src/env/environment.cpp
  2. 15
      src/env/environment.h
  3. 29
      src/env/functions/collection-access.cpp
  4. 27
      src/env/functions/collection-constructor.cpp
  5. 47
      src/env/functions/collection-modify.cpp
  6. 19
      src/env/functions/compare.cpp
  7. 13
      src/env/functions/convert.cpp
  8. 11
      src/env/functions/format.cpp
  9. 11
      src/env/functions/meta.cpp
  10. 19
      src/env/functions/mutable.cpp
  11. 15
      src/env/functions/operators.cpp
  12. 21
      src/env/functions/other.cpp
  13. 53
      src/env/functions/predicate.cpp
  14. 19
      src/env/functions/repl.cpp
  15. 22
      src/env/macro.h
  16. 1
      src/repl.cpp

16
src/env/environment.cpp vendored

@ -72,6 +72,22 @@ EnvironmentPtr Environment::create(const ValuePtr lambda, ValueVector&& argument
// -----------------------------------------
void Environment::loadFunctions()
{
loadCollectionAccess();
loadCollectionConstructor();
loadCollectionModify();
loadCompare();
loadConvert();
loadFormat();
loadMeta();
loadMutable();
loadOperators();
loadOther();
loadPredicate();
loadRepl();
}
void Environment::registerFunction(const std::string& name, FunctionType function)
{
s_functions.insert_or_assign(name, function);

15
src/env/environment.h vendored

@ -24,6 +24,7 @@ public:
static EnvironmentPtr create(EnvironmentPtr outer);
static EnvironmentPtr create(const ValuePtr lambda, ValueVector&& arguments);
static void loadFunctions();
static void registerFunction(const std::string& name, FunctionType function);
static void installFunctions(EnvironmentPtr env);
@ -34,6 +35,20 @@ public:
private:
Environment() {}
// Outer environment native functions, "Core"
static void loadCollectionAccess();
static void loadCollectionConstructor();
static void loadCollectionModify();
static void loadCompare();
static void loadConvert();
static void loadFormat();
static void loadMeta();
static void loadMutable();
static void loadOperators();
static void loadOther();
static void loadPredicate();
static void loadRepl();
EnvironmentPtr m_outer { nullptr };
std::unordered_map<std::string, ValuePtr> m_values;

29
src/env/functions/collection-access.cpp vendored

@ -14,8 +14,10 @@
namespace blaze {
// (first (list 1 2 3)) -> 1
ADD_FUNCTION(
void Environment::loadCollectionAccess()
{
// (first (list 1 2 3)) -> 1
ADD_FUNCTION(
"first",
{
CHECK_ARG_COUNT_IS("first", SIZE(), 1);
@ -30,8 +32,8 @@ ADD_FUNCTION(
return (collection->empty()) ? makePtr<Constant>() : collection->front();
});
// (nth (list 1 2 3) 0) -> 1
ADD_FUNCTION(
// (nth (list 1 2 3) 0) -> 1
ADD_FUNCTION(
"nth",
{
CHECK_ARG_COUNT_IS("nth", SIZE(), 2);
@ -49,8 +51,8 @@ ADD_FUNCTION(
return collection_nodes[index];
});
// (rest (list 1 2 3)) -> (2 3)
ADD_FUNCTION(
// (rest (list 1 2 3)) -> (2 3)
ADD_FUNCTION(
"rest",
{
CHECK_ARG_COUNT_IS("rest", SIZE(), 1);
@ -65,10 +67,10 @@ ADD_FUNCTION(
return makePtr<List>(collection->rest());
});
// -----------------------------------------
// -----------------------------------------
// (get {:kw "value"} :kw) -> "value"
ADD_FUNCTION(
// (get {:kw "value"} :kw) -> "value"
ADD_FUNCTION(
"get",
{
CHECK_ARG_COUNT_AT_LEAST("get", SIZE(), 1);
@ -89,8 +91,8 @@ ADD_FUNCTION(
return (result) ? result : makePtr<Constant>();
});
// (keys {"foo" 3 :bar 5}) -> ("foo" :bar)
ADD_FUNCTION(
// (keys {"foo" 3 :bar 5}) -> ("foo" :bar)
ADD_FUNCTION(
"keys",
{
CHECK_ARG_COUNT_AT_LEAST("keys", SIZE(), 1);
@ -115,8 +117,8 @@ ADD_FUNCTION(
return makePtr<List>(nodes);
});
// (vals {"foo" 3 :bar 5}) -> (3 5)
ADD_FUNCTION(
// (vals {"foo" 3 :bar 5}) -> (3 5)
ADD_FUNCTION(
"vals",
{
CHECK_ARG_COUNT_AT_LEAST("vals", SIZE(), 1);
@ -135,5 +137,6 @@ ADD_FUNCTION(
return makePtr<List>(nodes);
});
}
} // namespace blaze

27
src/env/functions/collection-constructor.cpp vendored

@ -14,15 +14,17 @@
namespace blaze {
// (list 1 2) -> (1 2)
ADD_FUNCTION(
void Environment::loadCollectionConstructor()
{
// (list 1 2) -> (1 2)
ADD_FUNCTION(
"list",
{
return makePtr<List>(begin, end);
});
// (make-list 4 nil) -> (nil nil nil nil)
ADD_FUNCTION(
// (make-list 4 nil) -> (nil nil nil nil)
ADD_FUNCTION(
"make-list",
{
CHECK_ARG_COUNT_IS("make-list", SIZE(), 2);
@ -73,10 +75,10 @@ ADD_FUNCTION(
return makePtr<List>(std::move(nodes));
});
// -----------------------------------------
// -----------------------------------------
// (vec (list 1 2 3))
ADD_FUNCTION(
// (vec (list 1 2 3))
ADD_FUNCTION(
"vec",
{
CHECK_ARG_COUNT_IS("vec", SIZE(), 1);
@ -90,8 +92,8 @@ ADD_FUNCTION(
return makePtr<Vector>(collection->nodesCopy());
});
// (vector 1 2 3) -> [1 2 3]
ADD_FUNCTION(
// (vector 1 2 3) -> [1 2 3]
ADD_FUNCTION(
"vector",
{
auto result = makePtr<Vector>();
@ -99,10 +101,10 @@ ADD_FUNCTION(
return makePtr<Vector>(begin, end);
});
// -----------------------------------------
// -----------------------------------------
// (hash-map "foo" 5 :bar 10) -> {"foo" 5 :bar 10}
ADD_FUNCTION(
// (hash-map "foo" 5 :bar 10) -> {"foo" 5 :bar 10}
ADD_FUNCTION(
"hash-map",
{
CHECK_ARG_COUNT_EVEN("hash-map", SIZE());
@ -115,5 +117,6 @@ ADD_FUNCTION(
return makePtr<HashMap>(elements);
});
}
} // namespace blaze

47
src/env/functions/collection-modify.cpp vendored

@ -16,8 +16,10 @@
namespace blaze {
// (apply + 1 2 (list 3 4)) -> (+ 1 2 3 4) -> 10
ADD_FUNCTION(
void Environment::loadCollectionModify()
{
// (apply + 1 2 (list 3 4)) -> (+ 1 2 3 4) -> 10
ADD_FUNCTION(
"apply",
{
CHECK_ARG_COUNT_AT_LEAST("apply", SIZE(), 2);
@ -49,8 +51,8 @@ ADD_FUNCTION(
return value;
});
// (cons 1 (list 2 3))
ADD_FUNCTION(
// (cons 1 (list 2 3))
ADD_FUNCTION(
"cons",
{
CHECK_ARG_COUNT_IS("cons", SIZE(), 2);
@ -68,8 +70,8 @@ ADD_FUNCTION(
return makePtr<List>(result_nodes);
});
// (concat (list 1) (list 2 3)) -> (1 2 3)
ADD_FUNCTION(
// (concat (list 1) (list 2 3)) -> (1 2 3)
ADD_FUNCTION(
"concat",
{
size_t count = 0;
@ -89,9 +91,9 @@ ADD_FUNCTION(
return makePtr<List>(result_nodes);
});
// (conj '(1 2 3) 4 5 6) -> (6 5 4 1 2 3)
// (conj [1 2 3] 4 5 6) -> [1 2 3 4 5 6]
ADD_FUNCTION(
// (conj '(1 2 3) 4 5 6) -> (6 5 4 1 2 3)
// (conj [1 2 3] 4 5 6) -> [1 2 3 4 5 6]
ADD_FUNCTION(
"conj",
{
CHECK_ARG_COUNT_AT_LEAST("conj", SIZE(), 1);
@ -118,8 +120,8 @@ ADD_FUNCTION(
return makePtr<Vector>(nodes);
});
// (map (fn* (x) (* x 2)) (list 1 2 3)) -> (2 4 6)
ADD_FUNCTION(
// (map (fn* (x) (* x 2)) (list 1 2 3)) -> (2 4 6)
ADD_FUNCTION(
"map",
{
CHECK_ARG_COUNT_IS("map", SIZE(), 2);
@ -147,8 +149,8 @@ ADD_FUNCTION(
return makePtr<List>(nodes);
});
// (set-nth (list 1 2 3) 1 "foo") -> (1 "foo" 3)
ADD_FUNCTION(
// (set-nth (list 1 2 3) 1 "foo") -> (1 "foo" 3)
ADD_FUNCTION(
"set-nth",
{
CHECK_ARG_COUNT_IS("set-nth-element", SIZE(), 3);
@ -173,10 +175,10 @@ ADD_FUNCTION(
return makePtr<List>(collection_nodes);
});
// (seq '(1 2 3)) -> (1 2 3)
// (seq [1 2 3]) -> (1 2 3)
// (seq "foo") -> ("f" "o" "o")
ADD_FUNCTION(
// (seq '(1 2 3)) -> (1 2 3)
// (seq [1 2 3]) -> (1 2 3)
// (seq "foo") -> ("f" "o" "o")
ADD_FUNCTION(
"seq",
{
CHECK_ARG_COUNT_IS("seq", SIZE(), 1);
@ -223,10 +225,10 @@ ADD_FUNCTION(
return nullptr;
});
// -----------------------------------------
// -----------------------------------------
// (assoc {:a 1 :b 2} :a 3 :c 1) -> {:a 3 :b 2 :c 1}
ADD_FUNCTION(
// (assoc {:a 1 :b 2} :a 3 :c 1) -> {:a 3 :b 2 :c 1}
ADD_FUNCTION(
"assoc",
{
CHECK_ARG_COUNT_AT_LEAST("assoc", SIZE(), 1);
@ -245,8 +247,8 @@ ADD_FUNCTION(
return makePtr<HashMap>(elements);
});
// (dissoc {:a 1 :b 2 :c 3} :a :c :d) -> {:b 2}
ADD_FUNCTION(
// (dissoc {:a 1 :b 2 :c 3} :a :c :d) -> {:b 2}
ADD_FUNCTION(
"dissoc",
{
CHECK_ARG_COUNT_AT_LEAST("dissoc", SIZE(), 1);
@ -261,5 +263,6 @@ ADD_FUNCTION(
return makePtr<HashMap>(elements);
});
}
} // namespace blaze

19
src/env/functions/compare.cpp vendored

@ -14,6 +14,8 @@
namespace blaze {
void Environment::loadCompare()
{
#define NUMBER_COMPARE(operator) \
{ \
bool result = true; \
@ -38,16 +40,16 @@ namespace blaze {
return makePtr<Constant>((result) ? Constant::True : Constant::False); \
}
ADD_FUNCTION("<", NUMBER_COMPARE(<));
ADD_FUNCTION("<=", NUMBER_COMPARE(<=));
ADD_FUNCTION(">", NUMBER_COMPARE(>));
ADD_FUNCTION(">=", NUMBER_COMPARE(>=));
ADD_FUNCTION("<", NUMBER_COMPARE(<));
ADD_FUNCTION("<=", NUMBER_COMPARE(<=));
ADD_FUNCTION(">", NUMBER_COMPARE(>));
ADD_FUNCTION(">=", NUMBER_COMPARE(>=));
// -----------------------------------------
// -----------------------------------------
// (= 1 1) -> true
// (= "foo" "foo") -> true
ADD_FUNCTION(
// (= 1 1) -> true
// (= "foo" "foo") -> true
ADD_FUNCTION(
"=",
{
CHECK_ARG_COUNT_AT_LEAST("=", SIZE(), 2);
@ -128,5 +130,6 @@ ADD_FUNCTION(
return makePtr<Constant>((result) ? Constant::True : Constant::False);
});
}
} // namespace blaze

13
src/env/functions/convert.cpp vendored

@ -10,8 +10,10 @@
namespace blaze {
// (symbol "foo") -> foo
ADD_FUNCTION(
void Environment::loadConvert()
{
// (symbol "foo") -> foo
ADD_FUNCTION(
"symbol",
{
CHECK_ARG_COUNT_IS("symbol", SIZE(), 1);
@ -25,9 +27,9 @@ ADD_FUNCTION(
return makePtr<Symbol>(stringValue->data());
});
// (keyword "foo") -> :foo
// (keyword 123) -> :123
ADD_FUNCTION(
// (keyword "foo") -> :foo
// (keyword 123) -> :123
ADD_FUNCTION(
"keyword",
{
CHECK_ARG_COUNT_IS("symbol", SIZE(), 1);
@ -45,5 +47,6 @@ ADD_FUNCTION(
return makePtr<Keyword>(stringValue->data());
});
}
} // namespace blaze

11
src/env/functions/format.cpp vendored

@ -16,6 +16,8 @@
namespace blaze {
void Environment::loadFormat()
{
#define PRINTER_STRING(print_readably, concatenation) \
{ \
std::string result; \
@ -32,8 +34,8 @@ namespace blaze {
return makePtr<String>(result); \
}
ADD_FUNCTION("str", PRINTER_STRING(false, ""));
ADD_FUNCTION("pr-str", PRINTER_STRING(true, " "));
ADD_FUNCTION("str", PRINTER_STRING(false, ""));
ADD_FUNCTION("pr-str", PRINTER_STRING(true, " "));
#define PRINTER_PRINT(print_readably) \
{ \
@ -50,7 +52,8 @@ ADD_FUNCTION("pr-str", PRINTER_STRING(true, " "));
return makePtr<Constant>(); \
}
ADD_FUNCTION("prn", PRINTER_PRINT(true));
ADD_FUNCTION("println", PRINTER_PRINT(false));
ADD_FUNCTION("prn", PRINTER_PRINT(true));
ADD_FUNCTION("println", PRINTER_PRINT(false));
}
} // namespace blaze

11
src/env/functions/meta.cpp vendored

@ -12,8 +12,10 @@
namespace blaze {
// (meta [1 2 3])
ADD_FUNCTION(
void Environment::loadMeta()
{
// (meta [1 2 3])
ADD_FUNCTION(
"meta",
{
CHECK_ARG_COUNT_IS("meta", SIZE(), 1);
@ -31,8 +33,8 @@ ADD_FUNCTION(
return front->meta();
});
// (with-meta [1 2 3] "some metadata")
ADD_FUNCTION(
// (with-meta [1 2 3] "some metadata")
ADD_FUNCTION(
"with-meta",
{
CHECK_ARG_COUNT_IS("with-meta", SIZE(), 2);
@ -49,5 +51,6 @@ ADD_FUNCTION(
return front->withMeta(*(begin + 1));
});
}
} // namespace blaze

19
src/env/functions/mutable.cpp vendored

@ -15,8 +15,10 @@
namespace blaze {
// (atom 1)
ADD_FUNCTION(
void Environment::loadMutable()
{
// (atom 1)
ADD_FUNCTION(
"atom",
{
CHECK_ARG_COUNT_IS("atom", SIZE(), 1);
@ -24,8 +26,8 @@ ADD_FUNCTION(
return makePtr<Atom>(*begin);
});
// (deref myatom)
ADD_FUNCTION(
// (deref myatom)
ADD_FUNCTION(
"deref",
{
CHECK_ARG_COUNT_IS("deref", SIZE(), 1);
@ -35,8 +37,8 @@ ADD_FUNCTION(
return atom->deref();
});
// (reset! myatom 2)
ADD_FUNCTION(
// (reset! myatom 2)
ADD_FUNCTION(
"reset!",
{
CHECK_ARG_COUNT_IS("reset!", SIZE(), 2);
@ -49,8 +51,8 @@ ADD_FUNCTION(
return value;
});
// (swap! myatom (fn* [x y] (+ 1 x y)) 2) -> (deref (def! myatom (atom ((fn* [x y] (+ 1 x y)) (deref myatom) 2))))
ADD_FUNCTION(
// (swap! myatom (fn* [x y] (+ 1 x y)) 2) -> (deref (def! myatom (atom ((fn* [x y] (+ 1 x y)) (deref myatom) 2))))
ADD_FUNCTION(
"swap!",
{
CHECK_ARG_COUNT_AT_LEAST("swap!", SIZE(), 2);
@ -77,5 +79,6 @@ ADD_FUNCTION(
return atom->reset(value);
});
}
} // namespace blaze

15
src/env/functions/operators.cpp vendored

@ -12,7 +12,9 @@
namespace blaze {
ADD_FUNCTION(
void Environment::loadOperators()
{
ADD_FUNCTION(
"+",
{
int64_t result = 0;
@ -25,7 +27,7 @@ ADD_FUNCTION(
return makePtr<Number>(result);
});
ADD_FUNCTION(
ADD_FUNCTION(
"-",
{
if (SIZE() == 0) {
@ -45,7 +47,7 @@ ADD_FUNCTION(
return makePtr<Number>(result);
});
ADD_FUNCTION(
ADD_FUNCTION(
"*",
{
int64_t result = 1;
@ -58,7 +60,7 @@ ADD_FUNCTION(
return makePtr<Number>(result);
});
ADD_FUNCTION(
ADD_FUNCTION(
"/",
{
CHECK_ARG_COUNT_AT_LEAST("/", SIZE(), 1);
@ -76,8 +78,8 @@ ADD_FUNCTION(
return makePtr<Number>((int64_t)result);
});
// (% 5 2) -> 1
ADD_FUNCTION(
// (% 5 2) -> 1
ADD_FUNCTION(
"%",
{
CHECK_ARG_COUNT_IS("/", SIZE(), 2);
@ -87,5 +89,6 @@ ADD_FUNCTION(
return makePtr<Number>(divide->number() % by->number());
});
}
} // namespace blaze

21
src/env/functions/other.cpp vendored

@ -17,9 +17,11 @@
namespace blaze {
// (count '(1 2 3)) -> 3
// (count [1 2 3]) -> 3
ADD_FUNCTION(
void Environment::loadOther()
{
// (count '(1 2 3)) -> 3
// (count [1 2 3]) -> 3
ADD_FUNCTION(
"count",
{
CHECK_ARG_COUNT_IS("count", SIZE(), 1);
@ -40,10 +42,10 @@ ADD_FUNCTION(
return makePtr<Number>((int64_t)result);
});
// -----------------------------------------
// -----------------------------------------
// (throw x)
ADD_FUNCTION(
// (throw x)
ADD_FUNCTION(
"throw",
{
CHECK_ARG_COUNT_IS("throw", SIZE(), 1);
@ -53,10 +55,10 @@ ADD_FUNCTION(
return nullptr;
})
// -----------------------------------------
// -----------------------------------------
// (time-ms)
ADD_FUNCTION(
// (time-ms)
ADD_FUNCTION(
"time-ms",
{
CHECK_ARG_COUNT_IS("time-ms", SIZE(), 0);
@ -67,5 +69,6 @@ ADD_FUNCTION(
return makePtr<Number>(elapsed);
});
}
} // namespace blaze

53
src/env/functions/predicate.cpp vendored

@ -10,6 +10,8 @@
namespace blaze {
void Environment::loadPredicate()
{
#define IS_CONSTANT(name, constant) \
{ \
CHECK_ARG_COUNT_IS(name, SIZE(), 1); \
@ -19,12 +21,12 @@ namespace blaze {
&& std::static_pointer_cast<Constant>(*begin)->state() == constant); \
}
// (nil? nil)
ADD_FUNCTION("nil?", IS_CONSTANT("nil?", Constant::Nil));
ADD_FUNCTION("true?", IS_CONSTANT("true?", Constant::True));
ADD_FUNCTION("false?", IS_CONSTANT("false?", Constant::False));
// (nil? nil)
ADD_FUNCTION("nil?", IS_CONSTANT("nil?", Constant::Nil));
ADD_FUNCTION("true?", IS_CONSTANT("true?", Constant::True));
ADD_FUNCTION("false?", IS_CONSTANT("false?", Constant::False));
// -----------------------------------------
// -----------------------------------------
#define IS_TYPE(type) \
{ \
@ -44,18 +46,18 @@ ADD_FUNCTION("false?", IS_CONSTANT("false?", Constant::False));
return makePtr<Constant>(result); \
}
// (symbol? 'foo)
ADD_FUNCTION("atom?", IS_TYPE(Atom));
ADD_FUNCTION("keyword?", IS_TYPE(Keyword));
ADD_FUNCTION("list?", IS_TYPE(List));
ADD_FUNCTION("map?", IS_TYPE(HashMap));
ADD_FUNCTION("number?", IS_TYPE(Number));
ADD_FUNCTION("sequential?", IS_TYPE(Collection));
ADD_FUNCTION("string?", IS_TYPE(String));
ADD_FUNCTION("symbol?", IS_TYPE(Symbol));
ADD_FUNCTION("vector?", IS_TYPE(Vector));
ADD_FUNCTION(
// (symbol? 'foo)
ADD_FUNCTION("atom?", IS_TYPE(Atom));
ADD_FUNCTION("keyword?", IS_TYPE(Keyword));
ADD_FUNCTION("list?", IS_TYPE(List));
ADD_FUNCTION("map?", IS_TYPE(HashMap));
ADD_FUNCTION("number?", IS_TYPE(Number));
ADD_FUNCTION("sequential?", IS_TYPE(Collection));
ADD_FUNCTION("string?", IS_TYPE(String));
ADD_FUNCTION("symbol?", IS_TYPE(Symbol));
ADD_FUNCTION("vector?", IS_TYPE(Vector));
ADD_FUNCTION(
"fn?",
{
bool result = true;
@ -74,7 +76,7 @@ ADD_FUNCTION(
return makePtr<Constant>(result);
});
ADD_FUNCTION(
ADD_FUNCTION(
"macro?",
{
bool result = true;
@ -93,11 +95,11 @@ ADD_FUNCTION(
return makePtr<Constant>(result);
});
// -----------------------------------------
// -----------------------------------------
// (contains? {:foo 5} :foo) -> true
// (contains? {"bar" 5} "foo") -> false
ADD_FUNCTION(
// (contains? {:foo 5} :foo) -> true
// (contains? {"bar" 5} "foo") -> false
ADD_FUNCTION(
"contains?",
{
CHECK_ARG_COUNT_IS("contains?", SIZE(), 2);
@ -111,9 +113,9 @@ ADD_FUNCTION(
return makePtr<Constant>(hash_map->exists(*(begin + 1)));
});
// (empty? '() '()) -> true
// (empty? [] [1 2 3] []) -> false
ADD_FUNCTION(
// (empty? '() '()) -> true
// (empty? [] [1 2 3] []) -> false
ADD_FUNCTION(
"empty?",
{
bool result = true;
@ -128,5 +130,6 @@ ADD_FUNCTION(
return makePtr<Constant>((result) ? Constant::True : Constant::False);
});
}
} // namespace blaze

19
src/env/functions/repl.cpp vendored

@ -14,8 +14,10 @@
namespace blaze {
// REPL reader
ADD_FUNCTION(
void Environment::loadRepl()
{
// REPL reader
ADD_FUNCTION(
"read-string",
{
CHECK_ARG_COUNT_IS("read-string", SIZE(), 1);
@ -26,8 +28,8 @@ ADD_FUNCTION(
return read(input);
});
// Read file contents
ADD_FUNCTION(
// Read file contents
ADD_FUNCTION(
"slurp",
{
CHECK_ARG_COUNT_IS("slurp", SIZE(), 1);
@ -40,8 +42,8 @@ ADD_FUNCTION(
return makePtr<String>(file.data());
});
// Prompt readline
ADD_FUNCTION(
// Prompt readline
ADD_FUNCTION(
"readline",
{
CHECK_ARG_COUNT_IS("readline", SIZE(), 1);
@ -51,13 +53,14 @@ ADD_FUNCTION(
return readline(prompt->data());
});
// REPL eval
ADD_FUNCTION(
// REPL eval
ADD_FUNCTION(
"eval",
{
CHECK_ARG_COUNT_IS("eval", SIZE(), 1);
return eval(*begin, nullptr);
});
}
} // namespace blaze

22
src/env/macro.h vendored

@ -8,25 +8,9 @@
#include "env/environment.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 counter macro 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) \
(const std::string& __symbol, FunctionType __lambda) \
{ \
Environment::registerFunction(__symbol, __lambda); \
} \
}; \
static struct FUNCTION_STRUCT_NAME(unique) \
FUNCTION_STRUCT_NAME(unique)( \
#define ADD_FUNCTION(symbol, lambda) \
Environment::registerFunction( \
symbol, \
[](ValueVectorConstIt begin, ValueVectorConstIt end) -> ValuePtr lambda);
#define ADD_FUNCTION(symbol, lambda) ADD_FUNCTION_IMPL(__COUNTER__, symbol, lambda);
[](ValueVectorConstIt begin, ValueVectorConstIt end) -> blaze::ValuePtr lambda);
#define SIZE() std::distance(begin, end)

1
src/repl.cpp

@ -149,6 +149,7 @@ auto main(int argc, char* argv[]) -> int
std::signal(SIGINT, blaze::cleanup);
std::signal(SIGTERM, blaze::cleanup);
blaze::Environment::loadFunctions();
blaze::Environment::installFunctions(blaze::s_outer_env);
installLambdas(blaze::s_outer_env);
makeArgv(blaze::s_outer_env, arguments);

Loading…
Cancel
Save