diff --git a/src/core/strtod.c b/src/core/strtod.c index cc2c21d6..ee8b4b64 100644 --- a/src/core/strtod.c +++ b/src/core/strtod.c @@ -228,7 +228,7 @@ static double convert( bignat_lshift_n(mant, shamt); exponent2 -= shamt * BIGNAT_NBIT; for (;exponent < -3; exponent += 4) bignat_div(mant, base * base * base * base); - for (;exponent < -2; exponent += 2) bignat_div(mant, base * base); + for (;exponent < -1; exponent += 2) bignat_div(mant, base * base); for (;exponent < 0; exponent += 1) bignat_div(mant, base); } diff --git a/tools/amalg.janet b/tools/amalg.janet index 1a6270f9..7dcf3f5b 100644 --- a/tools/amalg.janet +++ b/tools/amalg.janet @@ -65,8 +65,8 @@ # as the version being generated (print "#define JANET_BUILD \"" janet/build "\"") -(print ```#define JANET_AMALG -#include "janet.h"```) +(print ```#define JANET_AMALG```) +(print ```#include "janet.h"```) (each h headers (dofile h)) (each s sources (dofile s)) diff --git a/tools/bars.janet b/tools/bars.janet new file mode 100644 index 00000000..c14eb40b --- /dev/null +++ b/tools/bars.janet @@ -0,0 +1,55 @@ +# A flexible templater for janet. Compiles +# templates to janet functions that produce buffers. + +(defn template + "Compile a template string into a function" + [source] + + # State for compilation machine + (def p (parser/new)) + (def forms @[]) + + (defn parse-chunk + "Parse a string and push produced values to forms." + [chunk] + (parser/consume p chunk) + (while (parser/has-more p) + (array/push forms (parser/produce p))) + (if (= :error (parser/status p)) + (error (parser/error p)))) + + (defn code-chunk + "Parse all the forms in str and return them + in a tuple prefixed with 'do." + [str] + (parse-chunk str) + true) + + (defn string-chunk + "Insert string chunk into parser" + [str] + (parser/insert p str) + (parse-chunk "") + true) + + # Run peg + (def grammar + ~{:code-chunk (* "{%" (drop (cmt '(any (if-not "%}" 1)) ,code-chunk)) "%}") + :main-chunk (drop (cmt '(any (if-not "{%" 1)) ,string-chunk)) + :main (any (+ :code-chunk :main-chunk (error "")))}) + (def parts (peg/match grammar source)) + + # Check errors in template and parser + (unless parts (error "invalid template syntax")) + (parse-chunk "\n") + (case (parser/status p) + :pending (error (string "unfinished parser state " (parser/state p))) + :error (error (parser/error p))) + + # Make ast from forms + (def ast ~(fn [params &] (default params @{}) (,buffer ;forms))) + + (def ctor (compile ast *env* source)) + (if-not (function? ctor) + (error (string "could not compile template"))) + (ctor))