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]);
}
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) {
JanetArray *array;
int32_t newcount;
@ -240,15 +229,51 @@ static int cfun_concat(JanetArgs args) {
}
static const JanetReg cfuns[] = {
{"array.new", cfun_new},
{"array.pop", cfun_pop},
{"array.peek", cfun_peek},
{"array.push", cfun_push},
{"array.setcount", cfun_setcount},
{"array.ensure", cfun_ensure},
{"array.slice", cfun_slice},
{"array.concat", cfun_concat},
{NULL, NULL}
{"array.new", cfun_new,
"(array.new capacity)\n\n"
"Creates a new empty array with a preallocated capacity. The same as\n"
"(array) but can be more efficient if the maximum size of an array is known."
},
{"array.pop", cfun_pop,
"(array.pop arr)\n\n"
"Remove the last element of the array and return it. If the array is empty, will return nil. Modifies\n"
"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 */

View File

@ -924,9 +924,19 @@ static int cfun_disasm(JanetArgs args) {
}
static const JanetReg cfuns[] = {
{"asm", cfun_asm},
{"disasm", cfun_disasm},
{NULL, NULL}
{"asm", cfun_asm,
"(asm assembly)\n\n"
"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 */

View File

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

View File

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

View File

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

View File

@ -287,28 +287,28 @@ static int janet_core_hash(JanetArgs args) {
}
static const JanetReg cfuns[] = {
{"native", janet_core_native},
{"print", janet_core_print},
{"describe", janet_core_describe},
{"string", janet_core_string},
{"symbol", janet_core_symbol},
{"buffer", janet_core_buffer},
{"table", janet_core_table},
{"array", janet_core_array},
{"scan-number", janet_core_scannumber},
{"scan-integer", janet_core_scaninteger},
{"scan-real", janet_core_scanreal},
{"tuple", janet_core_tuple},
{"struct", janet_core_struct},
{"buffer", janet_core_buffer},
{"gensym", janet_core_gensym},
{"gccollect", janet_core_gccollect},
{"gcsetinterval", janet_core_gcsetinterval},
{"gcinterval", janet_core_gcinterval},
{"type", janet_core_type},
{"next", janet_core_next},
{"hash", janet_core_hash},
{NULL, NULL}
{"native", janet_core_native, NULL},
{"print", janet_core_print, NULL},
{"describe", janet_core_describe, NULL},
{"string", janet_core_string, NULL},
{"symbol", janet_core_symbol, NULL},
{"buffer", janet_core_buffer, NULL},
{"table", janet_core_table, NULL},
{"array", janet_core_array, NULL},
{"scan-number", janet_core_scannumber, NULL},
{"scan-integer", janet_core_scaninteger, NULL},
{"scan-real", janet_core_scanreal, NULL},
{"tuple", janet_core_tuple, NULL},
{"struct", janet_core_struct, NULL},
{"buffer", janet_core_buffer, NULL},
{"gensym", janet_core_gensym, NULL},
{"gccollect", janet_core_gccollect, NULL},
{"gcsetinterval", janet_core_gcsetinterval, NULL},
{"gcinterval", janet_core_gcinterval, NULL},
{"type", janet_core_type, NULL},
{"next", janet_core_next, NULL},
{"hash", janet_core_hash, NULL},
{NULL, NULL, NULL}
};
/* Utility for inline assembly */
@ -331,7 +331,7 @@ static void janet_quick_asm(
JANET_OUT_OF_MEMORY;
}
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 */
@ -562,7 +562,7 @@ JanetTable *janet_core_env(void) {
templatize_comparator(env, JANET_FUN_NEQ, "not==", 1, JOP_NUMERIC_EQUAL);
/* 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 */
janet_gcroot(janet_wrap_table(env));
@ -591,7 +591,7 @@ JanetTable *janet_core_env(void) {
}
/* Allow references to the environment */
janet_def(env, "_env", ret);
janet_def(env, "_env", ret, NULL);
/* Run bootstrap source */
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[] = {
{"fiber.new", cfun_new},
{"fiber.status", cfun_status},
{"fiber.stack", cfun_stack},
{"fiber.current", cfun_current},
{"fiber.lineage", cfun_lineage},
{"fiber.maxstack", cfun_maxstack},
{"fiber.setmaxstack", cfun_setmaxstack},
{NULL, NULL}
{"fiber.new", cfun_new, NULL},
{"fiber.status", cfun_status, NULL},
{"fiber.stack", cfun_stack, NULL},
{"fiber.current", cfun_current, NULL},
{"fiber.lineage", cfun_lineage, NULL},
{"fiber.maxstack", cfun_maxstack, NULL},
{"fiber.setmaxstack", cfun_setmaxstack, NULL},
{NULL, NULL, NULL}
};
/* Module entry point */

View File

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

View File

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

View File

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

View File

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

View File

@ -773,16 +773,16 @@ static int cfun_state(JanetArgs args) {
}
static const JanetReg cfuns[] = {
{"parser.new", cfun_parser},
{"parser.produce", cfun_produce},
{"parser.consume", cfun_consume},
{"parser.byte", cfun_byte},
{"parser.error", cfun_error},
{"parser.status", cfun_status},
{"parser.flush", cfun_flush},
{"parser.state", cfun_state},
{"parser.where", cfun_where},
{NULL, NULL}
{"parser.new", cfun_parser, NULL},
{"parser.produce", cfun_produce, NULL},
{"parser.consume", cfun_consume, NULL},
{"parser.byte", cfun_byte, NULL},
{"parser.error", cfun_error, NULL},
{"parser.status", cfun_status, NULL},
{"parser.flush", cfun_flush, NULL},
{"parser.state", cfun_state, NULL},
{"parser.where", cfun_where, NULL},
{NULL, NULL, NULL}
};
/* 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());
break;
case JANET_STRING:
janet_table_put(tab, janet_csymbolv("doc"), attr);
janet_table_put(tab, janet_csymbolv(":doc"), attr);
break;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -727,6 +727,7 @@ struct JanetAbstractHeader {
struct JanetReg {
const char *name;
JanetCFunction cfun;
const char *documentation;
};
/***** END SECTION TYPES *****/
@ -1082,8 +1083,8 @@ typedef enum {
JANET_BINDING_VAR,
JANET_BINDING_MACRO
} JanetBindingType;
JANET_API void janet_def(JanetTable *env, const char *name, Janet val);
JANET_API void janet_var(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, const char *documentation);
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 JanetTable *janet_env(JanetArgs args);

View File

@ -38,10 +38,10 @@ int main(int argc, char **argv) {
args = janet_array(argc);
for (i = 0; i < argc; 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 */
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_line_init();

View File

@ -73,11 +73,11 @@ void repl_init(void) {
env = janet_core_env();
/* 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 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);
/* Run startup script */