1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-25 06:33:16 +00:00

Update documentation to include source

location of bindings.
This commit is contained in:
Calvin Rose 2018-12-17 12:06:50 -05:00
parent 003472354d
commit 77ea11c603
6 changed files with 46 additions and 22 deletions

View File

@ -9,9 +9,16 @@
<title>Janet Language Documentation</title>
<meta name="description" content="API Documentation for the janet programming language.">
<style>
p {
.docstring {
font-family: monospace;
}
.binding-type {
color: blue;
}
.source-map {
color: steelblue;
font-size: 0.8em;
}
</style>
</head>
```)
@ -64,14 +71,19 @@ p {
(let [{:macro macro
:value val
:ref ref
:source-map sm
:doc docstring} env-entry
binding-type (cond
macro :macro
ref (string :var " (" (type ref.0) ")")
(type val))]
(string "<h2>" (html-escape key) "</h2>"
"<span style=\"color:blue;\">" binding-type "</span>"
"<p>" (trim-lead (html-escape docstring)) "</p>")))
(type val))
source-ref (if-let [[path start end] sm]
(string "<span class=\"source-map\">" path " (" start ":" end ")</span>")
"")]
(string "<h2 class=\"binding\">" (html-escape key) "</h2>\n"
"<span class=\"binding-type\">" binding-type "</span>\n"
"<p class=\"docstring\">" (trim-lead (html-escape docstring)) "</p>\n"
source-ref)))
# Generate parts and print them to stdout
(def parts (seq [[k entry]
@ -81,5 +93,5 @@ p {
(print
prelude
(make-title)
;(interpose "<hr>" parts)
;(interpose "<hr>\n" parts)
postlude)

View File

@ -575,13 +575,13 @@ JanetSlot janetc_value(JanetFopts opts, Janet x) {
if (c->result.status == JANET_COMPILE_ERROR)
return janetc_cslot(janet_wrap_nil());
c->current_mapping = last_mapping;
if (opts.flags & JANET_FOPTS_TAIL)
ret = janetc_return(opts.compiler, ret);
if (opts.flags & JANET_FOPTS_HINT) {
janetc_copy(opts.compiler, opts.hint, ret);
ret = opts.hint;
}
c->current_mapping = last_mapping;
opts.compiler->recursion_guard++;
return ret;
}

View File

@ -131,9 +131,9 @@
(defmacro with-idemp
"Return janet code body that has been prepended
with a binding of form to atom. If form is a non-
idempotent form (a function call, etc.), make sure the resulting
code will only call evaluate once, even if body contains multiple
with a binding of form to atom. If form is a non-idempotent
form (a function call, etc.), make sure the resulting
code will only evaluate once, even if body contains multiple
copies of binding. In body, use binding instead of form."
[binding form & body]
(def $result (gensym))
@ -152,7 +152,7 @@
(defmacro ++ "Increments the var x by 1." [x] ~(set ,x (,+ ,x ,1)))
(defmacro -- "Decrements the var x by 1." [x] ~(set ,x (,- ,x ,1)))
(defmacro += "Increments the var x by n." [x n] ~(set ,x (,+ ,x ,n)))
(defmacro -= "Decrements the vat x by n." [x n] ~(set ,x (,- ,x ,n)))
(defmacro -= "Decrements the var x by n." [x n] ~(set ,x (,- ,x ,n)))
(defmacro *= "Shorthand for (set x (* x n))." [x n] ~(set ,x (,* ,x ,n)))
(defmacro /= "Shorthand for (set x (/ x n))." [x n] ~(set ,x (,/ ,x ,n)))
(defmacro %= "Shorthand for (set x (% x n))." [x n] ~(set ,x (,% ,x ,n)))
@ -441,9 +441,9 @@
accum)
(defmacro if-let
"Takes the first one or two forms in a vector and if both are true binds
all the forms with let and evaluates the first expression else
evaluates the second"
"Make mutliple bindings, anf if all are truthy,
evaluate the tru form. If any are false or nil, evaluate
the fal form. Bindings have the same syntax as the let macro."
[bindings tru fal &]
(def len (length bindings))
(if (zero? len) (error "expected at least 1 binding"))
@ -472,8 +472,7 @@
(aux 0))
(defmacro when-let
"Takes the first one or two forms in vector and if true binds
all the forms with let and evaluates the body"
"Same as (if-let bindings (do ;body))."
[bindings & body]
~(if-let ,bindings (do ,;body)))
@ -803,8 +802,8 @@ value, one key will be ignored."
ret)
(defn zipcoll
"Creates an table or tuple from two arrays/tuples. If a third argument of
:struct is given result is struct else is table. Returns a new table."
"Creates an table or tuple from two arrays/tuples.
Returns a new table."
[keys vals]
(def res @{})
(def lk (length keys))
@ -1075,7 +1074,7 @@ value, one key will be ignored."
(print "symbol " sym " not found.")
(do
(def bind-type
(string " "
(string " "
(cond
x:ref (string :var " (" (type (get x:ref 0)) ")")
x:macro :macro

View File

@ -324,7 +324,7 @@ static const JanetReg cfuns[] = {
"(buffer & xs)\n\n"
"Creates a new buffer by concatenating values together. Values are "
"converted to bytes via describe if they are not byte sequences. Returns "
"the new symbol."
"the new buffer."
},
{"abstract?", janet_core_is_abstract,
"(abstract? x)\n\n"
@ -384,7 +384,7 @@ static const JanetReg cfuns[] = {
{"gcsetinterval", janet_core_gcsetinterval,
"(gcsetinterval interval)\n\n"
"Set an integer number of bytes to allocate before running garbage collection. "
"Low values interval will be slower but use less memory. "
"Low valuesi for interval will be slower but use less memory. "
"High values will be faster but use more memory."
},
{"gcinterval", janet_core_gcinterval,

View File

@ -520,11 +520,11 @@ static int root(JanetParser *p, JanetParseState *state, uint8_t c) {
int janet_parser_consume(JanetParser *parser, uint8_t c) {
int consumed = 0;
if (parser->error) return 0;
parser->offset++;
while (!consumed && !parser->error) {
JanetParseState *state = parser->states + parser->statecount - 1;
consumed = state->consumer(parser, state, c);
}
parser->offset++;
parser->lookback = c;
return 1;
}

View File

@ -172,6 +172,15 @@ static int destructure(JanetCompiler *c,
}
}
/* Create a source map for definitions. */
static const Janet *janetc_make_sourcemap(JanetCompiler *c) {
Janet *tup = janet_tuple_begin(3);
tup[0] = janet_wrap_string(c->source);
tup[1] = janet_wrap_integer(c->current_mapping.start);
tup[2] = janet_wrap_integer(c->current_mapping.end);
return janet_tuple_end(tup);
}
static JanetSlot janetc_varset(JanetFopts opts, int32_t argn, const Janet *argv) {
/*JanetFopts subopts = janetc_fopts_default(opts.compiler);*/
/*JanetSlot ret, dest;*/
@ -252,6 +261,8 @@ static int varleaf(
JanetArray *ref = janet_array(1);
janet_array_push(ref, janet_wrap_nil());
janet_table_put(reftab, janet_csymbolv(":ref"), janet_wrap_array(ref));
janet_table_put(reftab, janet_csymbolv(":source-map"),
janet_wrap_tuple(janetc_make_sourcemap(c)));
janet_table_put(c->env, janet_wrap_symbol(sym), janet_wrap_table(reftab));
refslot = janetc_cslot(janet_wrap_array(ref));
janetc_emit_ssu(c, JOP_PUT_INDEX, refslot, s, 0, 0);
@ -278,6 +289,8 @@ static int defleaf(
JanetTable *attr) {
if (c->scope->flags & JANET_SCOPE_TOP) {
JanetTable *tab = janet_table(2);
janet_table_put(tab, janet_csymbolv(":source-map"),
janet_wrap_tuple(janetc_make_sourcemap(c)));
tab->proto = attr;
JanetSlot valsym = janetc_cslot(janet_csymbolv(":value"));
JanetSlot tabslot = janetc_cslot(janet_wrap_table(tab));