Everywhere: Add docstring support

This commit is contained in:
Riyyi
2023-11-19 21:44:17 +01:00
parent 25871cd5d5
commit 536e55e75a
24 changed files with 401 additions and 62 deletions
+14
View File
@@ -21,6 +21,8 @@ void Environment::loadCollectionAccess()
// (count {:foo 2 :bar 3}) -> 2
ADD_FUNCTION(
"count",
"",
"",
{
CHECK_ARG_COUNT_IS("count", SIZE(), 1);
@@ -48,6 +50,8 @@ void Environment::loadCollectionAccess()
// (first (list 1 2 3)) -> 1
ADD_FUNCTION(
"first",
"",
"",
{
CHECK_ARG_COUNT_IS("first", SIZE(), 1);
@@ -64,6 +68,8 @@ void Environment::loadCollectionAccess()
// (nth (list 1 2 3) 0) -> 1
ADD_FUNCTION(
"nth",
"",
"",
{
CHECK_ARG_COUNT_IS("nth", SIZE(), 2);
@@ -83,6 +89,8 @@ void Environment::loadCollectionAccess()
// (rest (list 1 2 3)) -> (2 3)
ADD_FUNCTION(
"rest",
"",
"",
{
CHECK_ARG_COUNT_IS("rest", SIZE(), 1);
@@ -101,6 +109,8 @@ void Environment::loadCollectionAccess()
// (get {:kw "value"} :kw) -> "value"
ADD_FUNCTION(
"get",
"",
"",
{
CHECK_ARG_COUNT_AT_LEAST("get", SIZE(), 1);
@@ -123,6 +133,8 @@ void Environment::loadCollectionAccess()
// (keys {"foo" 3 :bar 5}) -> ("foo" :bar)
ADD_FUNCTION(
"keys",
"",
"",
{
CHECK_ARG_COUNT_AT_LEAST("keys", SIZE(), 1);
@@ -149,6 +161,8 @@ void Environment::loadCollectionAccess()
// (vals {"foo" 3 :bar 5}) -> (3 5)
ADD_FUNCTION(
"vals",
"",
"",
{
CHECK_ARG_COUNT_AT_LEAST("vals", SIZE(), 1);
+10
View File
@@ -19,6 +19,8 @@ void Environment::loadCollectionConstructor()
// (list 1 2) -> (1 2)
ADD_FUNCTION(
"list",
"",
"",
{
return makePtr<List>(begin, end);
});
@@ -26,6 +28,8 @@ void Environment::loadCollectionConstructor()
// (make-list 4 nil) -> (nil nil nil nil)
ADD_FUNCTION(
"make-list",
"",
"",
{
CHECK_ARG_COUNT_IS("make-list", SIZE(), 2);
@@ -80,6 +84,8 @@ void Environment::loadCollectionConstructor()
// (vec (list 1 2 3))
ADD_FUNCTION(
"vec",
"",
"",
{
CHECK_ARG_COUNT_IS("vec", SIZE(), 1);
@@ -95,6 +101,8 @@ void Environment::loadCollectionConstructor()
// (vector 1 2 3) -> [1 2 3]
ADD_FUNCTION(
"vector",
"",
"",
{
auto result = makePtr<Vector>();
@@ -106,6 +114,8 @@ void Environment::loadCollectionConstructor()
// (hash-map "foo" 5 :bar 10) -> {"foo" 5 :bar 10}
ADD_FUNCTION(
"hash-map",
"",
"",
{
CHECK_ARG_COUNT_EVEN("hash-map", SIZE());
+18
View File
@@ -21,6 +21,8 @@ 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);
@@ -54,6 +56,8 @@ void Environment::loadCollectionModify()
// (cons 1 (list 2 3))
ADD_FUNCTION(
"cons",
"",
"",
{
CHECK_ARG_COUNT_IS("cons", SIZE(), 2);
@@ -73,6 +77,8 @@ void Environment::loadCollectionModify()
// (concat (list 1) (list 2 3)) -> (1 2 3)
ADD_FUNCTION(
"concat",
"",
"",
{
size_t count = 0;
for (auto it = begin; it != end; ++it) {
@@ -95,6 +101,8 @@ void Environment::loadCollectionModify()
// (conj [1 2 3] 4 5 6) -> [1 2 3 4 5 6]
ADD_FUNCTION(
"conj",
"",
"",
{
CHECK_ARG_COUNT_AT_LEAST("conj", SIZE(), 1);
@@ -123,6 +131,8 @@ void Environment::loadCollectionModify()
// (map (fn* (x) (* x 2)) (list 1 2 3)) -> (2 4 6)
ADD_FUNCTION(
"map",
"",
"",
{
CHECK_ARG_COUNT_IS("map", SIZE(), 2);
@@ -152,6 +162,8 @@ void Environment::loadCollectionModify()
// (set-nth (list 1 2 3) 1 "foo") -> (1 "foo" 3)
ADD_FUNCTION(
"set-nth",
"",
"",
{
CHECK_ARG_COUNT_IS("set-nth-element", SIZE(), 3);
@@ -180,6 +192,8 @@ void Environment::loadCollectionModify()
// (seq "foo") -> ("f" "o" "o")
ADD_FUNCTION(
"seq",
"",
"",
{
CHECK_ARG_COUNT_IS("seq", SIZE(), 1);
@@ -230,6 +244,8 @@ void Environment::loadCollectionModify()
// (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);
@@ -250,6 +266,8 @@ void Environment::loadCollectionModify()
// (dissoc {:a 1 :b 2 :c 3} :a :c :d) -> {:b 2}
ADD_FUNCTION(
"dissoc",
"",
"",
{
CHECK_ARG_COUNT_AT_LEAST("dissoc", SIZE(), 1);
+6 -4
View File
@@ -40,10 +40,10 @@ void Environment::loadCompare()
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(>=));
// -----------------------------------------
@@ -51,6 +51,8 @@ void Environment::loadCompare()
// (= "foo" "foo") -> true
ADD_FUNCTION(
"=",
"",
"",
{
CHECK_ARG_COUNT_AT_LEAST("=", SIZE(), 2);
+12 -2
View File
@@ -18,6 +18,8 @@ void Environment::loadConvert()
// (number-to-string 123) -> "123"
ADD_FUNCTION(
"number-to-string",
"",
"",
{
CHECK_ARG_COUNT_IS("number-to-string", SIZE(), 1);
@@ -37,6 +39,8 @@ void Environment::loadConvert()
// (string-to-char "123") -> 49
ADD_FUNCTION(
"string-to-char",
"",
"",
{
CHECK_ARG_COUNT_IS("string-to-char", SIZE(), 1);
@@ -49,6 +53,8 @@ void Environment::loadConvert()
// (string-to-number "123") -> 123
ADD_FUNCTION(
"string-to-number",
"",
"",
{
CHECK_ARG_COUNT_IS("string-to-number", SIZE(), 1);
@@ -83,14 +89,16 @@ void Environment::loadConvert()
// (string-to-list "foo") -> (102 111 111)
// (string-to-vector "foo") -> [102 111 111]
ADD_FUNCTION("string-to-list", STRING_TO_COLLECTION("string-to-list", List));
ADD_FUNCTION("string-to-vector", STRING_TO_COLLECTION("string-to-vector", Vector));
ADD_FUNCTION("string-to-list", "", "", STRING_TO_COLLECTION("string-to-list", List));
ADD_FUNCTION("string-to-vector", "", "", STRING_TO_COLLECTION("string-to-vector", Vector));
// -------------------------------------
// (symbol "foo") -> foo
ADD_FUNCTION(
"symbol",
"",
"",
{
CHECK_ARG_COUNT_IS("symbol", SIZE(), 1);
@@ -107,6 +115,8 @@ void Environment::loadConvert()
// (keyword 123) -> :123
ADD_FUNCTION(
"keyword",
"",
"",
{
CHECK_ARG_COUNT_IS("keyword", SIZE(), 1);
+4 -4
View File
@@ -34,8 +34,8 @@ void Environment::loadFormat()
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) \
{ \
@@ -52,8 +52,8 @@ void Environment::loadFormat()
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
+4
View File
@@ -17,6 +17,8 @@ void Environment::loadMeta()
// (meta [1 2 3])
ADD_FUNCTION(
"meta",
"",
"",
{
CHECK_ARG_COUNT_IS("meta", SIZE(), 1);
@@ -36,6 +38,8 @@ void Environment::loadMeta()
// (with-meta [1 2 3] "some metadata")
ADD_FUNCTION(
"with-meta",
"",
"",
{
CHECK_ARG_COUNT_IS("with-meta", SIZE(), 2);
+8
View File
@@ -20,6 +20,8 @@ void Environment::loadMutable()
// (atom 1)
ADD_FUNCTION(
"atom",
"",
"",
{
CHECK_ARG_COUNT_IS("atom", SIZE(), 1);
@@ -29,6 +31,8 @@ void Environment::loadMutable()
// (deref myatom)
ADD_FUNCTION(
"deref",
"",
"",
{
CHECK_ARG_COUNT_IS("deref", SIZE(), 1);
@@ -40,6 +44,8 @@ void Environment::loadMutable()
// (reset! myatom 2)
ADD_FUNCTION(
"reset!",
"",
"",
{
CHECK_ARG_COUNT_IS("reset!", SIZE(), 2);
@@ -54,6 +60,8 @@ void Environment::loadMutable()
// (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);
+13
View File
@@ -16,6 +16,8 @@ void Environment::loadOperators()
{
ADD_FUNCTION(
"+",
"number...",
"Return the sum of any amount of arguments, where NUMBER is of type number.",
{
int64_t result = 0;
@@ -29,6 +31,11 @@ void Environment::loadOperators()
ADD_FUNCTION(
"-",
"[number] subtract...",
R"(Negate NUMBER or SUBTRACT numbers and return the result.
With one arg, negates it. With more than one arg,
subtracts all but the first from the first.)",
{
size_t length = SIZE();
if (length == 0) {
@@ -54,6 +61,8 @@ void Environment::loadOperators()
ADD_FUNCTION(
"*",
"",
"",
{
int64_t result = 1;
@@ -67,6 +76,8 @@ void Environment::loadOperators()
ADD_FUNCTION(
"/",
"",
"",
{
CHECK_ARG_COUNT_AT_LEAST("/", SIZE(), 1);
@@ -86,6 +97,8 @@ void Environment::loadOperators()
// (% 5 2) -> 1
ADD_FUNCTION(
"%",
"",
"",
{
CHECK_ARG_COUNT_IS("/", SIZE(), 2);
+4 -2
View File
@@ -5,9 +5,7 @@
*/
#include <chrono> // std::chrono::sytem_clock
#include <cstddef> // size_t
#include <cstdint> // int64_t
#include <memory> // std::static_pointer_cast
#include "ast.h"
#include "env/macro.h"
@@ -22,6 +20,8 @@ void Environment::loadOther()
// (throw x)
ADD_FUNCTION(
"throw",
"",
"",
{
CHECK_ARG_COUNT_IS("throw", SIZE(), 1);
@@ -35,6 +35,8 @@ void Environment::loadOther()
// (time-ms)
ADD_FUNCTION(
"time-ms",
"",
"",
{
CHECK_ARG_COUNT_IS("time-ms", SIZE(), 0);
+20 -12
View File
@@ -22,9 +22,9 @@ void Environment::loadPredicate()
}
// (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));
ADD_FUNCTION("nil?", "", "", IS_CONSTANT("nil?", Constant::Nil));
ADD_FUNCTION("true?", "", "", IS_CONSTANT("true?", Constant::True));
ADD_FUNCTION("false?", "", "", IS_CONSTANT("false?", Constant::False));
// -----------------------------------------
@@ -47,18 +47,20 @@ void Environment::loadPredicate()
}
// (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("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;
@@ -78,6 +80,8 @@ void Environment::loadPredicate()
ADD_FUNCTION(
"macro?",
"",
"",
{
bool result = true;
@@ -101,6 +105,8 @@ void Environment::loadPredicate()
// (contains? {"bar" 5} "foo") -> false
ADD_FUNCTION(
"contains?",
"",
"",
{
CHECK_ARG_COUNT_IS("contains?", SIZE(), 2);
@@ -117,6 +123,8 @@ void Environment::loadPredicate()
// (empty? [] [1 2 3] []) -> false
ADD_FUNCTION(
"empty?",
"",
"",
{
bool result = true;
+8
View File
@@ -19,6 +19,8 @@ void Environment::loadRepl()
// REPL reader
ADD_FUNCTION(
"read-string",
"",
"",
{
CHECK_ARG_COUNT_IS("read-string", SIZE(), 1);
@@ -31,6 +33,8 @@ void Environment::loadRepl()
// Read file contents
ADD_FUNCTION(
"slurp",
"",
"",
{
CHECK_ARG_COUNT_IS("slurp", SIZE(), 1);
@@ -45,6 +49,8 @@ void Environment::loadRepl()
// Prompt readline
ADD_FUNCTION(
"readline",
"",
"",
{
CHECK_ARG_COUNT_IS("readline", SIZE(), 1);
@@ -56,6 +62,8 @@ void Environment::loadRepl()
// REPL eval
ADD_FUNCTION(
"eval",
"",
"",
{
CHECK_ARG_COUNT_IS("eval", SIZE(), 1);