1
0
mirror of https://github.com/janet-lang/janet synced 2025-07-05 03:22:54 +00:00

Begin adding more complete documentation.

This commit is contained in:
Calvin Rose 2018-11-15 15:45:41 -05:00
parent d603e0eb8d
commit 4d119e4e03
20 changed files with 283 additions and 222 deletions

View File

@ -153,17 +153,6 @@ static int cfun_push(JanetArgs args) {
JANET_RETURN(args, args.v[0]); JANET_RETURN(args, args.v[0]);
} }
static int cfun_setcount(JanetArgs args) {
JanetArray *array;
int32_t newcount;
JANET_FIXARITY(args, 2);
JANET_ARG_ARRAY(array, args, 0);
JANET_ARG_INTEGER(newcount, args, 1);
if (newcount < 0) JANET_THROW(args, "expected positive integer");
janet_array_setcount(array, newcount);
JANET_RETURN(args, args.v[0]);
}
static int cfun_ensure(JanetArgs args) { static int cfun_ensure(JanetArgs args) {
JanetArray *array; JanetArray *array;
int32_t newcount; int32_t newcount;
@ -240,15 +229,51 @@ static int cfun_concat(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"array.new", cfun_new}, {"array.new", cfun_new,
{"array.pop", cfun_pop}, "(array.new capacity)\n\n"
{"array.peek", cfun_peek}, "Creates a new empty array with a preallocated capacity. The same as\n"
{"array.push", cfun_push}, "(array) but can be more efficient if the maximum size of an array is known."
{"array.setcount", cfun_setcount}, },
{"array.ensure", cfun_ensure}, {"array.pop", cfun_pop,
{"array.slice", cfun_slice}, "(array.pop arr)\n\n"
{"array.concat", cfun_concat}, "Remove the last element of the array and return it. If the array is empty, will return nil. Modifies\n"
{NULL, NULL} "the input array."
},
{"array.peek", cfun_peek,
"(array.peel arr)\n\n"
"Returns the last element of the array. Does not modify the array."
},
{"array.push", cfun_push,
"(array.push arr x)\n\n"
"Insert an element in the end of an array. Modifies the input array and returns it."
},
{"array.ensure", cfun_ensure,
"(array.ensure arr capacity)\n\n"
"Ensures that the memory backing the array has enough memory for capacity\n"
"items. Capacity must be an integer. If the backing capacity is already enough,\n"
"then this function does nothing. Otherwise, the backing memory will be reallocated\n"
"so that there is enough space."
},
{"array.slice", cfun_slice,
"(array.slice arrtup)\n\n"
"Returns a copy of an array or tuple.\n\n"
"(array.slice arrtup start)\n\n"
"Takes a slice of an array or tuple from the index start to the last element. Indexes\n"
"are from 0, or can be negative to index from the end of the array, Where -1 is the last\n"
"element of the array. Returns a new array.\n\n"
"(array.slice arrtup start end)\n\n"
"Takes a slice of array or tuple from start to end. The range is half open,\n"
"[start, end). Indexes can also be negative, indicating indexing from the end of the\n"
"end of the array. Returns a new array."
},
{"array.concat", cfun_concat,
"(array.concat arr & parts)\n\n"
"Concatenates a variadic number of arrays (and tuples) into the first argument\n"
"which must an array. If any of the parts are arrays or tuples, their elements will\n"
"be inserted into the array. Otherwise, each part in parts will be appended to arr in order.\n"
"Return the modified array arr."
},
{NULL, NULL, NULL}
}; };
/* Load the array module */ /* Load the array module */

View File

@ -924,9 +924,19 @@ static int cfun_disasm(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"asm", cfun_asm}, {"asm", cfun_asm,
{"disasm", cfun_disasm}, "(asm assembly)\n\n"
{NULL, NULL} "Returns a new function that is the compiled result of the assembly.\n"
"The syntax for the assembly can be found on the janet wiki. Will throw an\n"
"error on invalid assembly."
},
{"disasm", cfun_disasm,
"(disasm func)\n\n"
"Returns assembly that could be used be compile the given function.\n"
"func must be a function, not a c function. Will throw on error on a badly\n"
"typed argument."
},
{NULL, NULL, NULL}
}; };
/* Load the library */ /* Load the library */

View File

@ -267,14 +267,14 @@ static int cfun_slice(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"buffer.new", cfun_new}, {"buffer.new", cfun_new, NULL},
{"buffer.push-byte", cfun_u8}, {"buffer.push-byte", cfun_u8, NULL},
{"buffer.push-integer", cfun_int}, {"buffer.push-integer", cfun_int, NULL},
{"buffer.push-string", cfun_chars}, {"buffer.push-string", cfun_chars, NULL},
{"buffer.popn", cfun_popn}, {"buffer.popn", cfun_popn, NULL},
{"buffer.clear", cfun_clear}, {"buffer.clear", cfun_clear, NULL},
{"buffer.slice", cfun_slice}, {"buffer.slice", cfun_slice, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
int janet_lib_buffer(JanetArgs args) { int janet_lib_buffer(JanetArgs args) {

View File

@ -706,8 +706,8 @@ static int cfun(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"compile", cfun}, {"compile", cfun, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
int janet_lib_compile(JanetArgs args) { int janet_lib_compile(JanetArgs args) {

View File

@ -12,26 +12,43 @@
_env) _env)
(def defn :macro (def defn :macro
"Define a function. Equivalent to (def name (fn name [args] ...))." "(def name & more)\n\nDefine a function. Equivalent to (def name (fn name [args] ...))."
(fn defn [name & more] (fn defn [name & more]
(def len (length more)) (def len (length more))
(def modifiers @[])
(var docstr "")
(def fstart (def fstart
(fn recur [i] (fn recur [i]
(def {i ith} more) (def {i ith} more)
(def t (type ith)) (def t (type ith))
(def tuple? (= t :tuple)) (def tuple? (= t :tuple))
(def array? (= t :array)) (def array? (= t :array))
(if (if tuple? tuple? array?) i (if (if tuple? tuple? array?)
(if (< i len) (recur (+ i 1)))))) i
(do
(if (= (type ith) :string)
(:= docstr ith)
(array.push modifiers ith))
(if (< i len) (recur (+ i 1)))))))
(def start (fstart 0)) (def start (fstart 0))
(def args (get more start))
# Add arguments to definition
(var index 0)
(def arglen (length args))
(def buf (buffer "(" name))
(while (< index arglen)
(buffer.push-string buf " " (get args index))
(:= index (+ index 1)))
(array.push modifiers (string buf ")\n\n" docstr))
# Build return value
(def fnbody (tuple.prepend (tuple.prepend (tuple.slice more start) name) 'fn)) (def fnbody (tuple.prepend (tuple.prepend (tuple.slice more start) name) 'fn))
(def formargs (array.concat @['def name] (array.slice more 0 start) @[fnbody])) (def formargs (array.concat @['def name] modifiers @[fnbody]))
(tuple.slice formargs 0))) (tuple.slice formargs 0)))
(def defmacro :macro (defn defmacro :macro
"Define a macro." "Define a macro."
(fn defmacro [name & more] [name & more]
(apply defn (array.concat @[name :macro] more)))) (apply defn (array.concat @[name :macro] more)))
(defmacro defmacro- (defmacro defmacro-
"Define a private macro that will not be exported." "Define a private macro that will not be exported."
@ -63,70 +80,75 @@
nil) nil)
# Basic predicates # Basic predicates
(defn even? [x] (== 0 (% x 2))) (defn even? "Check if x is even." [x] (== 0 (% x 2)))
(defn odd? [x] (not= 0 (% x 2))) (defn odd? "Check if x is odd." [x] (not= 0 (% x 2)))
(defn zero? [x] (== x 0)) (defn zero? "Check if x is zero." [x] (== x 0))
(defn pos? [x] (> x 0)) (defn pos? "Check if x is greater than 0." [x] (> x 0))
(defn neg? [x] (< x 0)) (defn neg? "Check if x is less than 0." [x] (< x 0))
(defn one? [x] (== x 1)) (defn one? "Check if x is equal to 1." [x] (== x 1))
(defn integer? [x] (= (type x) :integer)) (defn integer? "Check if x is an integer." [x] (= (type x) :integer))
(defn real? [x] (= (type x) :real)) (defn real? [x] "Check if x is a real number." (= (type x) :real))
(defn number? [x] (defn number? "Check if x is a number." [x]
(def t (type x)) (def t (type x))
(if (= t :integer) true (= t :real))) (if (= t :integer) true (= t :real)))
(defn fiber? [x] (= (type x) :fiber)) (defn fiber? "Check if x is a fiber." [x] (= (type x) :fiber))
(defn string? [x] (= (type x) :string)) (defn string? "Check if x is a string." [x] (= (type x) :string))
(defn symbol? [x] (= (type x) :symbol)) (defn symbol? "Check if x is a symbol." [x] (= (type x) :symbol))
(defn keyword? [x] (if (not= (type x) :symbol) nil (= 58 (get x 0)))) (defn keyword? "Check if x is a keyword style symbol."
(defn buffer? [x] (= (type x) :buffer)) [x]
(defn function? [x] (= (type x) :function)) (if (not= (type x) :symbol) nil (= 58 (get x 0))))
(defn cfunction? [x] (= (type x) :cfunction)) (defn buffer? "Check if x is a buffer." [x] (= (type x) :buffer))
(defn abstract? [x] (= (type x) :abstract)) (defn function? "Check if x is a function (not a cfunction)."
(defn table? [x] (= (type x) :table )) [x] (= (type x) :function))
(defn struct? [x] (= (type x) :struct)) (defn cfunction? "Check if x a cfunction." [x] (= (type x) :cfunction))
(defn array? [x] (= (type x) :array)) (defn abstract? "Check if x an abstract type." [x] (= (type x) :abstract))
(defn tuple? [x] (= (type x) :tuple)) (defn table? [x] "Check if x a table." (= (type x) :table ))
(defn boolean? [x] (= (type x) :boolean)) (defn struct? [x] "Check if x a struct." (= (type x) :struct))
(defn bytes? [x] (defn array? [x] "Check if x is an array." (= (type x) :array))
(defn tuple? [x] "Check if x is a tuple." (= (type x) :tuple))
(defn boolean? [x] "Check if x is a boolean." (= (type x) :boolean))
(defn bytes? "Check if x is a string, symbol, or buffer." [x]
(def t (type x)) (def t (type x))
(if (= t :string) true (if (= t :symbol) true (= t :buffer)))) (if (= t :string) true (if (= t :symbol) true (= t :buffer))))
(defn dictionary? [x] (defn dictionary? "Check if x a table or struct." [x]
(def t (type x)) (def t (type x))
(if (= t :table) true (= t :struct))) (if (= t :table) true (= t :struct)))
(defn indexed? [x] (defn indexed? "Check if x is an array or tuple." [x]
(def t (type x)) (def t (type x))
(if (= t :array) true (= t :tuple))) (if (= t :array) true (= t :tuple)))
(defn callable? [x] (defn callable? "Check if x is a function or cfunction." [x]
(def t (type x)) (def t (type x))
(if (= t :function) true (= t :cfunction))) (if (= t :function) true (= t :cfunction)))
(defn true? [x] (= x true)) (defn true? "Check if x is true." [x] (= x true))
(defn false? [x] (= x false)) (defn false? "Check if x is false." [x] (= x false))
(defn nil? [x] (= x nil)) (defn nil? "Check if x is nil." [x] (= x nil))
(def atomic? (def atomic?
"(atomic? x)\n\nCheck if x is a value that evaluates to itself when compiled."
(do (do
(def non-atomic-types (def non-atomic-types
{:array true {:array true
:tuple true :tuple true
:table true :table true
:buffer true
:struct true}) :struct true})
(fn [x] (not (get non-atomic-types (type x)))))) (fn atomic? [x] (not (get non-atomic-types (type x))))))
# C style macros and functions for imperative sugar # C style macros and functions for imperative sugar
(defn inc [x] (+ x 1)) (defn inc "Returns x + 1." [x] (+ x 1))
(defn dec [x] (- x 1)) (defn dec "Returns x - 1." [x] (- x 1))
(defmacro ++ [x] (tuple ':= x (tuple + x 1))) (defmacro ++ "Increments the var x by 1." [x] (tuple ':= x (tuple + x 1)))
(defmacro -- [x] (tuple ':= x (tuple - x 1))) (defmacro -- "Decrements the var x by 1." [x] (tuple ':= x (tuple - x 1)))
(defmacro += [x n] (tuple ':= x (tuple + x n))) (defmacro += "Increments the var x by n." [x n] (tuple ':= x (tuple + x n)))
(defmacro -= [x n] (tuple ':= x (tuple - x n))) (defmacro -= "Decrements the vat x by n." [x n] (tuple ':= x (tuple - x n)))
(defmacro *= [x n] (tuple ':= x (tuple * x n))) (defmacro *= "Shorthand for (:= x (* x n))." [x n] (tuple ':= x (tuple * x n)))
(defmacro /= [x n] (tuple ':= x (tuple / x n))) (defmacro /= "Shorthand for (:= x (/ x n))." [x n] (tuple ':= x (tuple / x n)))
(defmacro %= [x n] (tuple ':= x (tuple % x n))) (defmacro %= "Shorthand for (:= x (% x n))." [x n] (tuple ':= x (tuple % x n)))
(defmacro &= [x n] (tuple ':= x (tuple & x n))) (defmacro &= "Shorthand for (:= x (& x n))." [x n] (tuple ':= x (tuple & x n)))
(defmacro |= [x n] (tuple ':= x (tuple | x n))) (defmacro |= "Shorthand for (:= x (| x n))." [x n] (tuple ':= x (tuple | x n)))
(defmacro ^= [x n] (tuple ':= x (tuple ^ x n))) (defmacro ^= "Shorthand for (:= x (^ x n))." [x n] (tuple ':= x (tuple ^ x n)))
(defmacro >>= [x n] (tuple ':= x (tuple >> x n))) (defmacro >>= "Shorthand for (:= x (>> x n))." [x n] (tuple ':= x (tuple >> x n)))
(defmacro <<= [x n] (tuple ':= x (tuple << x n))) (defmacro <<= "Shorthand for (:= x (<< x n))." [x n] (tuple ':= x (tuple << x n)))
(defmacro >>>= [x n] (tuple ':= x (tuple >>> x n))) (defmacro >>>= "Shorthand for (:= x (>>> x n))." [x n] (tuple ':= x (tuple >>> x n)))
(defmacro default (defmacro default
"Define a default value for an optional argument. "Define a default value for an optional argument.
@ -169,12 +191,13 @@
(aux 0)) (aux 0))
(defn doc* (defn doc*
"Get the documentation for a symbol in a given environment."
[env sym] [env sym]
(def x (get env sym)) (def x (get env sym))
(if (not x) (if (not x)
(print "symbol " x " not found.") (print "symbol " sym " not found.")
(do (do
(def d (get x 'doc)) (def d (get x :doc))
(print "\n" (if d d "no documentation found.") "\n")))) (print "\n" (if d d "no documentation found.") "\n"))))
(defmacro doc (defmacro doc

View File

@ -287,28 +287,28 @@ static int janet_core_hash(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"native", janet_core_native}, {"native", janet_core_native, NULL},
{"print", janet_core_print}, {"print", janet_core_print, NULL},
{"describe", janet_core_describe}, {"describe", janet_core_describe, NULL},
{"string", janet_core_string}, {"string", janet_core_string, NULL},
{"symbol", janet_core_symbol}, {"symbol", janet_core_symbol, NULL},
{"buffer", janet_core_buffer}, {"buffer", janet_core_buffer, NULL},
{"table", janet_core_table}, {"table", janet_core_table, NULL},
{"array", janet_core_array}, {"array", janet_core_array, NULL},
{"scan-number", janet_core_scannumber}, {"scan-number", janet_core_scannumber, NULL},
{"scan-integer", janet_core_scaninteger}, {"scan-integer", janet_core_scaninteger, NULL},
{"scan-real", janet_core_scanreal}, {"scan-real", janet_core_scanreal, NULL},
{"tuple", janet_core_tuple}, {"tuple", janet_core_tuple, NULL},
{"struct", janet_core_struct}, {"struct", janet_core_struct, NULL},
{"buffer", janet_core_buffer}, {"buffer", janet_core_buffer, NULL},
{"gensym", janet_core_gensym}, {"gensym", janet_core_gensym, NULL},
{"gccollect", janet_core_gccollect}, {"gccollect", janet_core_gccollect, NULL},
{"gcsetinterval", janet_core_gcsetinterval}, {"gcsetinterval", janet_core_gcsetinterval, NULL},
{"gcinterval", janet_core_gcinterval}, {"gcinterval", janet_core_gcinterval, NULL},
{"type", janet_core_type}, {"type", janet_core_type, NULL},
{"next", janet_core_next}, {"next", janet_core_next, NULL},
{"hash", janet_core_hash}, {"hash", janet_core_hash, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Utility for inline assembly */ /* Utility for inline assembly */
@ -331,7 +331,7 @@ static void janet_quick_asm(
JANET_OUT_OF_MEMORY; JANET_OUT_OF_MEMORY;
} }
memcpy(def->bytecode, bytecode, bytecode_size); memcpy(def->bytecode, bytecode, bytecode_size);
janet_def(env, name, janet_wrap_function(janet_thunk(def))); janet_def(env, name, janet_wrap_function(janet_thunk(def)), NULL);
} }
/* Macros for easier inline janet assembly */ /* Macros for easier inline janet assembly */
@ -562,7 +562,7 @@ JanetTable *janet_core_env(void) {
templatize_comparator(env, JANET_FUN_NEQ, "not==", 1, JOP_NUMERIC_EQUAL); templatize_comparator(env, JANET_FUN_NEQ, "not==", 1, JOP_NUMERIC_EQUAL);
/* Platform detection */ /* Platform detection */
janet_def(env, "janet.version", janet_cstringv(JANET_VERSION)); janet_def(env, "janet.version", janet_cstringv(JANET_VERSION), NULL);
/* Set as gc root */ /* Set as gc root */
janet_gcroot(janet_wrap_table(env)); janet_gcroot(janet_wrap_table(env));
@ -591,7 +591,7 @@ JanetTable *janet_core_env(void) {
} }
/* Allow references to the environment */ /* Allow references to the environment */
janet_def(env, "_env", ret); janet_def(env, "_env", ret, NULL);
/* Run bootstrap source */ /* Run bootstrap source */
janet_dobytes(env, janet_gen_core, sizeof(janet_gen_core), "core.janet", NULL); janet_dobytes(env, janet_gen_core, sizeof(janet_gen_core), "core.janet", NULL);

View File

@ -455,14 +455,14 @@ static int cfun_setmaxstack(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"fiber.new", cfun_new}, {"fiber.new", cfun_new, NULL},
{"fiber.status", cfun_status}, {"fiber.status", cfun_status, NULL},
{"fiber.stack", cfun_stack}, {"fiber.stack", cfun_stack, NULL},
{"fiber.current", cfun_current}, {"fiber.current", cfun_current, NULL},
{"fiber.lineage", cfun_lineage}, {"fiber.lineage", cfun_lineage, NULL},
{"fiber.maxstack", cfun_maxstack}, {"fiber.maxstack", cfun_maxstack, NULL},
{"fiber.setmaxstack", cfun_setmaxstack}, {"fiber.setmaxstack", cfun_setmaxstack, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Module entry point */ /* Module entry point */

View File

@ -366,14 +366,14 @@ static int janet_io_fseek(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"file.open", janet_io_fopen}, {"file.open", janet_io_fopen, NULL},
{"file.close", janet_io_fclose}, {"file.close", janet_io_fclose, NULL},
{"file.read", janet_io_fread}, {"file.read", janet_io_fread, NULL},
{"file.write", janet_io_fwrite}, {"file.write", janet_io_fwrite, NULL},
{"file.flush", janet_io_fflush}, {"file.flush", janet_io_fflush, NULL},
{"file.seek", janet_io_fseek}, {"file.seek", janet_io_fseek, NULL},
{"file.popen", janet_io_popen}, {"file.popen", janet_io_popen, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Module entry point */ /* Module entry point */
@ -383,15 +383,15 @@ int janet_lib_io(JanetArgs args) {
/* stdout */ /* stdout */
janet_def(env, "stdout", janet_def(env, "stdout",
makef(stdout, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE)); makef(stdout, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE), NULL);
/* stderr */ /* stderr */
janet_def(env, "stderr", janet_def(env, "stderr",
makef(stderr, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE)); makef(stderr, IO_APPEND | IO_NOT_CLOSEABLE | IO_SERIALIZABLE), NULL);
/* stdin */ /* stdin */
janet_def(env, "stdin", janet_def(env, "stdin",
makef(stdin, IO_READ | IO_NOT_CLOSEABLE | IO_SERIALIZABLE)); makef(stdin, IO_READ | IO_NOT_CLOSEABLE | IO_SERIALIZABLE), NULL);
return 0; return 0;
} }

View File

@ -1144,10 +1144,10 @@ static int cfun_unmarshal(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"marshal", cfun_marshal}, {"marshal", cfun_marshal, NULL},
{"unmarshal", cfun_unmarshal}, {"unmarshal", cfun_unmarshal, NULL},
{"env-lookup", cfun_env_lookup}, {"env-lookup", cfun_env_lookup, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Module entry point */ /* Module entry point */

View File

@ -130,26 +130,26 @@ static int janet_not(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"%", janet_remainder}, {"%", janet_remainder, NULL},
{"not", janet_not}, {"not", janet_not, NULL},
{"int", janet_int}, {"int", janet_int, NULL},
{"real", janet_real}, {"real", janet_real, NULL},
{"math.random", janet_rand}, {"math.random", janet_rand, NULL},
{"math.seedrandom", janet_srand}, {"math.seedrandom", janet_srand, NULL},
{"math.cos", janet_cos}, {"math.cos", janet_cos, NULL},
{"math.sin", janet_sin}, {"math.sin", janet_sin, NULL},
{"math.tan", janet_tan}, {"math.tan", janet_tan, NULL},
{"math.acos", janet_acos}, {"math.acos", janet_acos, NULL},
{"math.asin", janet_asin}, {"math.asin", janet_asin, NULL},
{"math.atan", janet_atan}, {"math.atan", janet_atan, NULL},
{"math.exp", janet_exp}, {"math.exp", janet_exp, NULL},
{"math.log", janet_log}, {"math.log", janet_log, NULL},
{"math.log10", janet_log10}, {"math.log10", janet_log10, NULL},
{"math.sqrt", janet_sqrt}, {"math.sqrt", janet_sqrt, NULL},
{"math.floor", janet_floor}, {"math.floor", janet_floor, NULL},
{"math.ceil", janet_ceil}, {"math.ceil", janet_ceil, NULL},
{"math.pow", janet_pow}, {"math.pow", janet_pow, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Module entry point */ /* Module entry point */
@ -157,8 +157,8 @@ int janet_lib_math(JanetArgs args) {
JanetTable *env = janet_env(args); JanetTable *env = janet_env(args);
janet_cfuns(env, NULL, cfuns); janet_cfuns(env, NULL, cfuns);
janet_def(env, "math.pi", janet_wrap_real(3.1415926535897931)); janet_def(env, "math.pi", janet_wrap_real(3.1415926535897931), NULL);
janet_def(env, "math.e", janet_wrap_real(2.7182818284590451)); janet_def(env, "math.e", janet_wrap_real(2.7182818284590451), NULL);
janet_def(env, "math.inf", janet_wrap_real(INFINITY)); janet_def(env, "math.inf", janet_wrap_real(INFINITY), NULL);
return 0; return 0;
} }

View File

@ -294,17 +294,17 @@ static int os_cwd(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"os.which", os_which}, {"os.which", os_which, NULL},
{"os.execute", os_execute}, {"os.execute", os_execute, NULL},
{"os.shell", os_shell}, {"os.shell", os_shell, NULL},
{"os.exit", os_exit}, {"os.exit", os_exit, NULL},
{"os.getenv", os_getenv}, {"os.getenv", os_getenv, NULL},
{"os.setenv", os_setenv}, {"os.setenv", os_setenv, NULL},
{"os.time", os_time}, {"os.time", os_time, NULL},
{"os.clock", os_clock}, {"os.clock", os_clock, NULL},
{"os.sleep", os_sleep}, {"os.sleep", os_sleep, NULL},
{"os.cwd", os_cwd}, {"os.cwd", os_cwd, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Module entry point */ /* Module entry point */

View File

@ -773,16 +773,16 @@ static int cfun_state(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"parser.new", cfun_parser}, {"parser.new", cfun_parser, NULL},
{"parser.produce", cfun_produce}, {"parser.produce", cfun_produce, NULL},
{"parser.consume", cfun_consume}, {"parser.consume", cfun_consume, NULL},
{"parser.byte", cfun_byte}, {"parser.byte", cfun_byte, NULL},
{"parser.error", cfun_error}, {"parser.error", cfun_error, NULL},
{"parser.status", cfun_status}, {"parser.status", cfun_status, NULL},
{"parser.flush", cfun_flush}, {"parser.flush", cfun_flush, NULL},
{"parser.state", cfun_state}, {"parser.state", cfun_state, NULL},
{"parser.where", cfun_where}, {"parser.where", cfun_where, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Load the library */ /* Load the library */

View File

@ -130,7 +130,7 @@ static JanetTable *handleattr(JanetCompiler *c, int32_t argn, const Janet *argv)
janet_table_put(tab, attr, janet_wrap_true()); janet_table_put(tab, attr, janet_wrap_true());
break; break;
case JANET_STRING: case JANET_STRING:
janet_table_put(tab, janet_csymbolv("doc"), attr); janet_table_put(tab, janet_csymbolv(":doc"), attr);
break; break;
} }
} }

View File

@ -1040,22 +1040,22 @@ static int cfun_number(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"string.slice", cfun_slice}, {"string.slice", cfun_slice, NULL},
{"string.repeat", cfun_repeat}, {"string.repeat", cfun_repeat, NULL},
{"string.bytes", cfun_bytes}, {"string.bytes", cfun_bytes, NULL},
{"string.from-bytes", cfun_frombytes}, {"string.from-bytes", cfun_frombytes, NULL},
{"string.ascii-lower", cfun_asciilower}, {"string.ascii-lower", cfun_asciilower, NULL},
{"string.ascii-upper", cfun_asciiupper}, {"string.ascii-upper", cfun_asciiupper, NULL},
{"string.reverse", cfun_reverse}, {"string.reverse", cfun_reverse, NULL},
{"string.find", cfun_find}, {"string.find", cfun_find, NULL},
{"string.find-all", cfun_findall}, {"string.find-all", cfun_findall, NULL},
{"string.replace", cfun_replace}, {"string.replace", cfun_replace, NULL},
{"string.replace-all", cfun_replaceall}, {"string.replace-all", cfun_replaceall, NULL},
{"string.split", cfun_split}, {"string.split", cfun_split, NULL},
{"string.check-set", cfun_checkset}, {"string.check-set", cfun_checkset, NULL},
{"string.join", cfun_join}, {"string.join", cfun_join, NULL},
{"string.number", cfun_number}, {"string.number", cfun_number, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Module entry point */ /* Module entry point */

View File

@ -252,12 +252,12 @@ static int cfun_rawget(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"table.new", cfun_new}, {"table.new", cfun_new, NULL},
{"table.to-struct", cfun_tostruct}, {"table.to-struct", cfun_tostruct, NULL},
{"table.getproto", cfun_getproto}, {"table.getproto", cfun_getproto, NULL},
{"table.setproto", cfun_setproto}, {"table.setproto", cfun_setproto, NULL},
{"table.rawget", cfun_rawget}, {"table.rawget", cfun_rawget, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Load the table module */ /* Load the table module */

View File

@ -150,10 +150,10 @@ static int cfun_append(JanetArgs args) {
} }
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"tuple.slice", cfun_slice}, {"tuple.slice", cfun_slice, NULL},
{"tuple.append", cfun_append}, {"tuple.append", cfun_append, NULL},
{"tuple.prepend", cfun_prepend}, {"tuple.prepend", cfun_prepend, NULL},
{NULL, NULL} {NULL, NULL, NULL}
}; };
/* Load the tuple module */ /* Load the tuple module */

View File

@ -195,25 +195,28 @@ const void *janet_strbinsearch(
/* Register a value in the global registry */ /* Register a value in the global registry */
void janet_register(const char *name, JanetCFunction cfun) { void janet_register(const char *name, JanetCFunction cfun) {
Janet value = janet_wrap_cfunction(cfun); Janet key = janet_wrap_cfunction(cfun);
Janet regkey = janet_csymbolv(name); Janet value = janet_csymbolv(name);
janet_table_put(janet_vm_registry, regkey, value); janet_table_put(janet_vm_registry, key, value);
janet_table_put(janet_vm_registry, value, regkey);
} }
/* Add a def to an environment */ /* Add a def to an environment */
void janet_def(JanetTable *env, const char *name, Janet val) { void janet_def(JanetTable *env, const char *name, Janet val, const char *doc) {
JanetTable *subt = janet_table(1); JanetTable *subt = janet_table(2);
janet_table_put(subt, janet_csymbolv(":value"), val); janet_table_put(subt, janet_csymbolv(":value"), val);
if (doc)
janet_table_put(subt, janet_csymbolv(":doc"), janet_cstringv(doc));
janet_table_put(env, janet_csymbolv(name), janet_wrap_table(subt)); janet_table_put(env, janet_csymbolv(name), janet_wrap_table(subt));
} }
/* Add a var to the environment */ /* Add a var to the environment */
void janet_var(JanetTable *env, const char *name, Janet val) { void janet_var(JanetTable *env, const char *name, Janet val, const char *doc) {
JanetArray *array = janet_array(1); JanetArray *array = janet_array(1);
JanetTable *subt = janet_table(1); JanetTable *subt = janet_table(2);
janet_array_push(array, val); janet_array_push(array, val);
janet_table_put(subt, janet_csymbolv(":ref"), janet_wrap_array(array)); janet_table_put(subt, janet_csymbolv(":ref"), janet_wrap_array(array));
if (doc)
janet_table_put(subt, janet_csymbolv(":doc"), janet_cstringv(doc));
janet_table_put(env, janet_csymbolv(name), janet_wrap_table(subt)); janet_table_put(env, janet_csymbolv(name), janet_wrap_table(subt));
} }
@ -235,8 +238,7 @@ void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns)
longname = janet_wrap_symbol(janet_string_end(longname_buffer)); longname = janet_wrap_symbol(janet_string_end(longname_buffer));
} }
Janet fun = janet_wrap_cfunction(cfuns->cfun); Janet fun = janet_wrap_cfunction(cfuns->cfun);
janet_def(env, cfuns->name, fun); janet_def(env, cfuns->name, fun, cfuns->documentation);
janet_table_put(janet_vm_registry, longname, fun);
janet_table_put(janet_vm_registry, fun, longname); janet_table_put(janet_vm_registry, fun, longname);
cfuns++; cfuns++;
} }

View File

@ -727,6 +727,7 @@ struct JanetAbstractHeader {
struct JanetReg { struct JanetReg {
const char *name; const char *name;
JanetCFunction cfun; JanetCFunction cfun;
const char *documentation;
}; };
/***** END SECTION TYPES *****/ /***** END SECTION TYPES *****/
@ -1082,8 +1083,8 @@ typedef enum {
JANET_BINDING_VAR, JANET_BINDING_VAR,
JANET_BINDING_MACRO JANET_BINDING_MACRO
} JanetBindingType; } JanetBindingType;
JANET_API void janet_def(JanetTable *env, const char *name, Janet val); JANET_API void janet_def(JanetTable *env, const char *name, Janet val, const char *documentation);
JANET_API void janet_var(JanetTable *env, const char *name, Janet val); JANET_API void janet_var(JanetTable *env, const char *name, Janet val, const char *documentation);
JANET_API void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns); JANET_API void janet_cfuns(JanetTable *env, const char *regprefix, const JanetReg *cfuns);
JANET_API JanetBindingType janet_resolve(JanetTable *env, const uint8_t *sym, Janet *out); JANET_API JanetBindingType janet_resolve(JanetTable *env, const uint8_t *sym, Janet *out);
JANET_API JanetTable *janet_env(JanetArgs args); JANET_API JanetTable *janet_env(JanetArgs args);

View File

@ -38,10 +38,10 @@ int main(int argc, char **argv) {
args = janet_array(argc); args = janet_array(argc);
for (i = 0; i < argc; i++) for (i = 0; i < argc; i++)
janet_array_push(args, janet_cstringv(argv[i])); janet_array_push(args, janet_cstringv(argv[i]));
janet_def(env, "process.args", janet_wrap_array(args)); janet_def(env, "process.args", janet_wrap_array(args), "Command line arguments.");
/* Expose line getter */ /* Expose line getter */
janet_def(env, "getline", janet_wrap_cfunction(janet_line_getter)); janet_def(env, "getline", janet_wrap_cfunction(janet_line_getter), NULL);
janet_register("getline", janet_line_getter); janet_register("getline", janet_line_getter);
janet_line_init(); janet_line_init();

View File

@ -73,11 +73,11 @@ void repl_init(void) {
env = janet_core_env(); env = janet_core_env();
/* Janet line getter */ /* Janet line getter */
janet_def(env, "repl-yield", janet_wrap_cfunction(repl_yield)); janet_def(env, "repl-yield", janet_wrap_cfunction(repl_yield), NULL);
janet_register("repl-yield", repl_yield); janet_register("repl-yield", repl_yield);
/* Janet line getter */ /* Janet line getter */
janet_def(env, "js", janet_wrap_cfunction(cfun_js)); janet_def(env, "js", janet_wrap_cfunction(cfun_js), NULL);
janet_register("js", cfun_js); janet_register("js", cfun_js);
/* Run startup script */ /* Run startup script */