[Nickle] nickle: Branch 'master' - 3 commits
Keith Packard
keithp at keithp.com
Mon Mar 27 12:58:24 PDT 2017
doc/tutorial/Makefile.am | 11 ++++++-
json.5c | 70 ++++++++++++++++++++++++++++++++++++-----------
test/Makefile.am | 3 +-
test/jsontest.5c | 25 ++++++++++++++++
4 files changed, 91 insertions(+), 18 deletions(-)
New commits:
commit 0880d7445ba518ef17a8fb3febc4489734995418
Author: Keith Packard <keithp at keithp.com>
Date: Fri Mar 24 09:45:49 2017 +0100
Add JSON tests
Signed-off-by: Keith Packard <keithp at keithp.com>
diff --git a/test/Makefile.am b/test/Makefile.am
index 683e5a8..7794477 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -12,7 +12,8 @@ check_SCRIPTS=\
round.5c \
math.5c \
factorial.5c \
- is_type.5c
+ is_type.5c \
+ jsontest.5c
noinst_PROGRAMS=math-tables
diff --git a/test/jsontest.5c b/test/jsontest.5c
new file mode 100644
index 0000000..a7aa0fa
--- /dev/null
+++ b/test/jsontest.5c
@@ -0,0 +1,25 @@
+autoimport JSON;
+
+exception bad_json(poly val, string json, poly got);
+
+void json_test_val(poly val, string match) {
+ string json = to_json(val);
+ poly t = from_json(json);
+
+ /* Make sure the string representation is correct and parsing
+ * matches the input
+ */
+ if (json != match || val != t)
+ raise bad_json(val, json, t);
+}
+
+json_test_val(1, "1");
+json_test_val(1.1, "1.1000000000000");
+json_test_val("hello world", "\"hello world\"");
+json_test_val((poly[string]) {
+ "float" => 1.1,
+ "hash" => (poly[string]) { "hashval" => 12 },
+ "int" => 10,
+ "string" => "world"
+ },
+ "{\n\t\"float\": 1.1000000000000,\n\t\"hash\": {\n\t\t\"hashval\": 12\n\t},\n\t\"int\": 10,\n\t\"string\": \"world\"\n}");
commit 0240e97419536a7a5474afae077b42fd49725100
Author: Keith Packard <keithp at keithp.com>
Date: Fri Mar 24 09:45:12 2017 +0100
Add floats to JSON module.
Support floating point values in JSON input and output.
Signed-off-by: Keith Packard <keithp at keithp.com>
diff --git a/json.5c b/json.5c
index 60c71fb..3e9fe9d 100644
--- a/json.5c
+++ b/json.5c
@@ -81,6 +81,10 @@ namespace JSON {
return sprintf ("%d", arg);
}
+ string json_float(real arg) {
+ return sprintf("%.13g", arg);
+ }
+
json = string func(poly arg, int indent) {
if (is_hash(arg))
return json_hash(arg, indent);
@@ -90,6 +94,8 @@ namespace JSON {
return json_string(arg);
if (is_int(arg))
return json_int(arg);
+ if (is_number(arg))
+ return json_float(arg);
return "";
};
@@ -99,6 +105,7 @@ namespace JSON {
typedef enum {
_string,
_int,
+ _float,
_oc,
_cc,
_os,
@@ -111,8 +118,9 @@ namespace JSON {
typedef struct {
union {
- int ival;
+ int ival;
string sval;
+ real fval;
} val;
term_t token;
} token_t;
@@ -144,6 +152,26 @@ namespace JSON {
File::ungetc(c, f->f);
}
+ bool is_int_char(int c) {
+ if (Ctype::isdigit(c))
+ return true;
+ if (c == '-')
+ return true;
+ return false;
+ }
+
+ bool is_float_char(int c) {
+ if (Ctype::isdigit(c))
+ return true;
+ if (c == '-')
+ return true;
+ if (c == '.')
+ return true;
+ if (c == 'e' || c == 'E')
+ return true;
+ return false;
+ }
+
try {
f->token = "";
for (;;) {
@@ -168,13 +196,21 @@ namespace JSON {
return (token_t) { .token = term_t._colon };
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
- int ival = 0;
- while (Ctype::isdigit(c)) {
- ival = ival * 10 + c - '0';
+ case '-': case '.':
+ string nval = "";
+ bool has_float = false;
+ while (is_float_char(c)) {
+ nval = nval + String::new(c);
+ if (!is_int_char(c))
+ has_float = true;
+ if (File::end(f->f))
+ break;
c = ch();
}
unch(c);
- return (token_t) { .val = { .ival = ival }, .token = term_t._int };
+ if (has_float)
+ return (token_t) { .val = { .fval = atof(nval) }, .token = term_t._float };
+ return (token_t) { .val = { .ival = atoi(nval) }, .token = term_t._int };
case '"':
string sval = "";
for (;;) {
@@ -271,19 +307,23 @@ namespace JSON {
value = poly func() {
switch (token.token) {
case term_t._oc:
- next();
- return hash();
+ next();
+ return hash();
case term_t._os:
- next();
- return array();
+ next();
+ return array();
case term_t._int:
- int ival = token.val.ival;
- next();
- return ival;
+ int ival = token.val.ival;
+ next();
+ return ival;
+ case term_t._float:
+ real fval = token.val.fval;
+ next();
+ return fval;
case term_t._string:
- string sval = token.val.sval;
- next();
- return sval;
+ string sval = token.val.sval;
+ next();
+ return sval;
}
};
commit 8064554410bc05798fe7a8cafa80da0fdec16b87
Author: Keith Packard <keithp at keithp.com>
Date: Thu Mar 23 14:38:10 2017 +0100
Make nickle-tutorial.pdf build reproducibly
Set TeX dates to RELEASE_DATE.
Remove PDF /ID entry.
Signed-off-by: Keith Packard <keithp at keithp.com>
diff --git a/doc/tutorial/Makefile.am b/doc/tutorial/Makefile.am
index baf586b..e4a1bd0 100644
--- a/doc/tutorial/Makefile.am
+++ b/doc/tutorial/Makefile.am
@@ -2,8 +2,15 @@
SGMLFILES=nickle-tutorial.sgml
PDFFILES=$(SGMLFILES:.sgml=.pdf)
+PDF_DATE=$(shell date -ud '$(RELEASE_DATE)')
+
.sgml.pdf:
- docbook2pdf $<
+ docbook2tex $< && \
+ pdfjadetex '\pdfinfo{/CreationDate($(PDF_DATE))/ModDate($(PDF_DATE))}\input{$*.tex}' >/dev/null && \
+ pdfjadetex '\pdfinfo{/CreationDate($(PDF_DATE))/ModDate($(PDF_DATE))}\input{$*.tex}' >/dev/null && \
+ pdfjadetex '\pdfinfo{/CreationDate($(PDF_DATE))/ModDate($(PDF_DATE))}\input{$*.tex}' >/dev/null && \
+ sed -i '/^\/ID \[<[0-9A-F][0-9A-F]*> <[0-9A-F][0-9A-F]*>\]/d' $@ && \
+ rm $*.aux $*.log *$.out *$.tex
SGMLINC= \
tour/tour.sgml \
@@ -36,4 +43,4 @@ $(PDFFILES): $(SGMLINC)
doc_DATA = $(PDFFILES)
-endif
\ No newline at end of file
+endif
More information about the Nickle
mailing list