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) void Environment::registerFunction(const std::string& name, FunctionType function)
{ {
s_functions.insert_or_assign(name, 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(EnvironmentPtr outer);
static EnvironmentPtr create(const ValuePtr lambda, ValueVector&& arguments); static EnvironmentPtr create(const ValuePtr lambda, ValueVector&& arguments);
static void loadFunctions();
static void registerFunction(const std::string& name, FunctionType function); static void registerFunction(const std::string& name, FunctionType function);
static void installFunctions(EnvironmentPtr env); static void installFunctions(EnvironmentPtr env);
@ -34,6 +35,20 @@ public:
private: private:
Environment() {} 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 }; EnvironmentPtr m_outer { nullptr };
std::unordered_map<std::string, ValuePtr> m_values; std::unordered_map<std::string, ValuePtr> m_values;

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

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

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

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

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

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

19
src/env/functions/compare.cpp vendored

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

13
src/env/functions/convert.cpp vendored

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

11
src/env/functions/format.cpp vendored

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

11
src/env/functions/meta.cpp vendored

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

19
src/env/functions/mutable.cpp vendored

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

15
src/env/functions/operators.cpp vendored

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

21
src/env/functions/other.cpp vendored

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

53
src/env/functions/predicate.cpp vendored

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

19
src/env/functions/repl.cpp vendored

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

22
src/env/macro.h vendored

@ -8,25 +8,9 @@
#include "env/environment.h" #include "env/environment.h"
// At the top-level you cant invoke any function, but you can create variables. #define ADD_FUNCTION(symbol, lambda) \
// Using a struct's constructor you can work around this limitation. Environment::registerFunction( \
// 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)( \
symbol, \ symbol, \
[](ValueVectorConstIt begin, ValueVectorConstIt end) -> ValuePtr lambda); [](ValueVectorConstIt begin, ValueVectorConstIt end) -> blaze::ValuePtr lambda);
#define ADD_FUNCTION(symbol, lambda) ADD_FUNCTION_IMPL(__COUNTER__, symbol, lambda);
#define SIZE() std::distance(begin, end) #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(SIGINT, blaze::cleanup);
std::signal(SIGTERM, blaze::cleanup); std::signal(SIGTERM, blaze::cleanup);
blaze::Environment::loadFunctions();
blaze::Environment::installFunctions(blaze::s_outer_env); blaze::Environment::installFunctions(blaze::s_outer_env);
installLambdas(blaze::s_outer_env); installLambdas(blaze::s_outer_env);
makeArgv(blaze::s_outer_env, arguments); makeArgv(blaze::s_outer_env, arguments);

Loading…
Cancel
Save