git.delta.rocks / jrsonnet / refs/commits / 1de48b14c4dd

difftreelog

refactor simplify intrinsic handling

Yaroslav Bolyukin2021-07-04parent: #0b0d703.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/Cargo.tomldiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/Cargo.toml
+++ b/crates/jrsonnet-evaluator/Cargo.toml
@@ -7,14 +7,11 @@
 edition = "2018"
 
 [features]
-default = ["serialized-stdlib", "faster", "explaining-traces", "serde-json"]
+default = ["serialized-stdlib", "explaining-traces", "serde-json"]
 # Serializes standard library AST instead of parsing them every run
 serialized-stdlib = ["serde", "bincode", "jrsonnet-parser/deserialize"]
 # Allow to convert Val into serde_json::Value and backwards
 serde-json = ["serde", "serde_json"]
-# Replace some standard library functions with faster implementations (I.e manifestJsonEx)
-# Library works fine without this feature, but requires more memory and time for std function calls
-faster = []
 # Rustc-like trace visualization
 explaining-traces = ["annotate-snippets"]
 # Allows library authors to throw custom errors
@@ -24,10 +21,10 @@
 unstable = []
 
 [dependencies]
-jrsonnet-interner = { path = "../jrsonnet-interner", version = "0.4.0" }
-jrsonnet-parser = { path = "../jrsonnet-parser", version = "0.4.0" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.0" }
-jrsonnet-types = { path = "../jrsonnet-types", version = "0.4.0" }
+jrsonnet-interner = { path="../jrsonnet-interner", version="0.4.0" }
+jrsonnet-parser = { path="../jrsonnet-parser", version="0.4.0" }
+jrsonnet-stdlib = { path="../jrsonnet-stdlib", version="0.4.0" }
+jrsonnet-types = { path="../jrsonnet-types", version="0.4.0" }
 pathdiff = "0.2.0"
 
 md5 = "0.7.0"
@@ -35,7 +32,7 @@
 rustc-hash = "1.1.0"
 
 thiserror = "1.0"
-jrsonnet-gc = { version = "0.4.2", features = ["derive"] }
+jrsonnet-gc = { version="0.4.2", features=["derive"] }
 
 [dependencies.anyhow]
 version = "1.0"
@@ -61,7 +58,7 @@
 optional = true
 
 [build-dependencies]
-jrsonnet-parser = { path = "../jrsonnet-parser", features = ["serialize", "deserialize"], version = "0.4.0" }
-jrsonnet-stdlib = { path = "../jrsonnet-stdlib", version = "0.4.0" }
+jrsonnet-parser = { path="../jrsonnet-parser", features=["serialize", "deserialize"], version="0.4.0" }
+jrsonnet-stdlib = { path="../jrsonnet-stdlib", version="0.4.0" }
 serde = "1.0"
 bincode = "1.3.1"
modifiedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/build.rs
+++ b/crates/jrsonnet-evaluator/build.rs
@@ -1,14 +1,11 @@
 use bincode::serialize;
-use jrsonnet_parser::{
-	parse, Expr, FieldMember, FieldName, LocExpr, Member, ObjBody, ParserSettings,
-};
+use jrsonnet_parser::{parse, ParserSettings};
 use jrsonnet_stdlib::STDLIB_STR;
 use std::{
 	env,
 	fs::File,
 	io::Write,
 	path::{Path, PathBuf},
-	rc::Rc,
 };
 
 fn main() {
@@ -21,37 +18,6 @@
 	)
 	.expect("parse");
 
-	let parsed = if cfg!(feature = "faster") {
-		let LocExpr(expr, location) = parsed;
-		LocExpr(
-			Rc::new(match Rc::try_unwrap(expr).unwrap() {
-				Expr::Obj(ObjBody::MemberList(members)) => Expr::Obj(ObjBody::MemberList(
-					members
-						.into_iter()
-						.filter(|p| {
-							!matches!(
-								p,
-								Member::Field(FieldMember {
-									name: FieldName::Fixed(name),
-									..
-								})
-								if name == "join" || name == "manifestJsonEx" ||
-								name == "escapeStringJson" || name == "equals" ||
-								name == "base64" || name == "foldl" || name == "foldr" ||
-								name == "sortImpl" || name == "format" || name == "range" ||
-								name == "reverse" || name == "slice" || name == "mod" ||
-								name == "strReplace" || name == "map"
-							)
-						})
-						.collect(),
-				)),
-				_ => panic!("std value should be object"),
-			}),
-			location,
-		)
-	} else {
-		parsed
-	};
 	{
 		let out_dir = env::var("OUT_DIR").unwrap();
 		let dest_path = Path::new(&out_dir).join("stdlib.bincode");
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -213,7 +213,6 @@
 	})
 }
 
-// faster
 fn builtin_slice(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "slice", args, 4, [
 		0, indexable: ty!((string | array));
@@ -230,7 +229,6 @@
 	})
 }
 
-// faster
 fn builtin_primitive_equals(
 	context: Context,
 	_loc: Option<&ExprLocation>,
@@ -244,7 +242,6 @@
 	})
 }
 
-// faster
 fn builtin_equals(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "equals", args, 2, [
 		0, a: ty!(any);
@@ -379,7 +376,6 @@
 	})
 }
 
-// faster
 fn builtin_format(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "format", args, 2, [
 		0, str: ty!(string) => Val::Str;
@@ -529,7 +525,6 @@
 	})
 }
 
-// faster
 fn builtin_escape_string_json(
 	context: Context,
 	_loc: Option<&ExprLocation>,
@@ -542,7 +537,6 @@
 	})
 }
 
-// faster
 fn builtin_manifest_json_ex(
 	context: Context,
 	_loc: Option<&ExprLocation>,
@@ -559,7 +553,6 @@
 	})
 }
 
-// faster
 fn builtin_reverse(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "reverse", args, 1, [
 		0, value: ty!(array) => Val::Arr;
@@ -576,7 +569,6 @@
 	})
 }
 
-// faster
 fn builtin_str_replace(
 	context: Context,
 	_loc: Option<&ExprLocation>,
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -546,8 +546,6 @@
 						|| {
 							if let Some(v) = v.get(s.clone())? {
 								Ok(v)
-							} else if v.get("__intrinsic_namespace__".into())?.is_some() {
-								Ok(Val::Func(Gc::new(FuncVal::Intrinsic(s))))
 							} else {
 								throw!(NoSuchField(s))
 							}
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -531,7 +531,6 @@
 	}
 
 	/// Calls `std.manifestJson`
-	#[cfg(feature = "faster")]
 	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {
 		manifest_json_ex(
 			self,
@@ -543,30 +542,6 @@
 		.map(|s| s.into())
 	}
 
-	/// Calls `std.manifestJson`
-	#[cfg(not(feature = "faster"))]
-	pub fn to_std_json(&self, padding: usize) -> Result<Rc<str>> {
-		with_state(|s| {
-			let ctx = s
-				.create_default_context()?
-				.with_var("__tmp__to_json__".into(), self.clone())?;
-			Ok(evaluate(
-				ctx,
-				&el!(Expr::Apply(
-					el!(Expr::Index(
-						el!(Expr::Var("std".into())),
-						el!(Expr::Str("manifestJsonEx".into()))
-					)),
-					ArgsDesc(vec![
-						Arg(None, el!(Expr::Var("__tmp__to_json__".into()))),
-						Arg(None, el!(Expr::Str(" ".repeat(padding).into())))
-					]),
-					false
-				)),
-			)?
-			.try_cast_str("to json")?)
-		})
-	}
 	pub fn to_yaml(&self, padding: usize) -> Result<IStr> {
 		with_state(|s| {
 			let ctx = s
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -192,6 +192,8 @@
 		pub rule expr_basic(s: &ParserSettings) -> LocExpr
 			= literal(s)
 
+			/ quiet!{l(s,<"$intrinsic(" name:$(id()) ")" {Expr::Intrinsic(name.into())}>)}
+
 			/ string_expr(s) / number_expr(s)
 			/ array_expr(s)
 			/ obj_expr(s)
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
1{1{
2 __intrinsic_namespace__:: 'std',
3
4 local std = self,2 local std = self,
5 local id = std.id,3 local id = std.id,
64
5 # Those functions aren't normally located in stdlib
6 length:: $intrinsic(length),
7 type:: $intrinsic(type),
8 makeArray:: $intrinsic(makeArray),
9 codepoint:: $intrinsic(codepoint),
10 objectFieldsEx:: $intrinsic(objectFieldsEx),
11 objectHasEx:: $intrinsic(objectHasEx),
12 primitiveEquals:: $intrinsic(primitiveEquals),
13 modulo:: $intrinsic(modulo),
14 floor:: $intrinsic(floor),
15 log:: $intrinsic(log),
16 pow:: $intrinsic(pow),
17 extVar:: $intrinsic(extVar),
18 native:: $intrinsic(native),
19 filter:: $intrinsic(filter),
20 char:: $intrinsic(char),
21 encodeUTF8:: $intrinsic(encodeUTF8),
22 md5:: $intrinsic(md5),
23 trace:: $intrinsic(trace),
24 id:: $intrinsic(id),
25 parseJson:: $intrinsic(parseJson),
26
7 isString(v):: std.type(v) == 'string',27 isString(v):: std.type(v) == 'string',
8 isNumber(v):: std.type(v) == 'number',28 isNumber(v):: std.type(v) == 'number',
9 isBoolean(v):: std.type(v) == 'boolean',29 isBoolean(v):: std.type(v) == 'boolean',
110 aux(str, delim, i2, arr, v + c) tailstrict;130 aux(str, delim, i2, arr, v + c) tailstrict;
111 aux(str, c, 0, [], ''),131 aux(str, c, 0, [], ''),
112132
113 strReplace(str, from, to)::133 strReplace:: $intrinsic(strReplace),
114 assert std.isString(str);
115 assert std.isString(from);
116 assert std.isString(to);
117 assert from != '' : "'from' string must not be zero length.";
118134
119 // Cache for performance.
120 local str_len = std.length(str);
121 local from_len = std.length(from);
122
123 // True if from is at str[i].
124 local found_at(i) = str[i:i + from_len] == from;
125
126 // Return the remainder of 'str' starting with 'start_index' where
127 // all occurrences of 'from' after 'curr_index' are replaced with 'to'.
128 local replace_after(start_index, curr_index, acc) =
129 if curr_index > str_len then
130 acc + str[start_index:curr_index]
131 else if found_at(curr_index) then
132 local new_index = curr_index + std.length(from);
133 replace_after(new_index, new_index, acc + str[start_index:curr_index] + to) tailstrict
134 else
135 replace_after(start_index, curr_index + 1, acc) tailstrict;
136
137 // if from_len==1, then we replace by splitting and rejoining the
138 // string which is much faster than recursing on replace_after
139 if from_len == 1 then
140 std.join(to, std.split(str, from))
141 else
142 replace_after(0, 0, ''),
143
144 asciiUpper(str)::135 asciiUpper(str)::
145 local cp = std.codepoint;136 local cp = std.codepoint;
146 local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then137 local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then
157 c;148 c;
158 std.join('', std.map(down_letter, std.stringChars(str))),149 std.join('', std.map(down_letter, std.stringChars(str))),
159150
160 range(from, to)::151 range:: $intrinsic(range),
161 std.makeArray(to - from + 1, function(i) i + from),
162152
163 repeat(what, count)::153 repeat(what, count)::
164 local joiner =154 local joiner =
167 else error 'std.repeat first argument must be an array or a string';157 else error 'std.repeat first argument must be an array or a string';
168 std.join(joiner, std.makeArray(count, function(i) what)),158 std.join(joiner, std.makeArray(count, function(i) what)),
169159
170 slice(indexable, index, end, step)::160 slice:: $intrinsic(slice),
171 local invar =
172 // loop invariant with defaults applied
173 {
174 indexable: indexable,
175 index:
176 if index == null then 0
177 else index,
178 end:
179 if end == null then std.length(indexable)
180 else end,
181 step:
182 if step == null then 1
183 else step,
184 length: std.length(indexable),
185 type: std.type(indexable),
186 };
187 assert invar.index >= 0 && invar.end >= 0 && invar.step >= 0 : 'got [%s:%s:%s] but negative index, end, and steps are not supported' % [invar.index, invar.end, invar.step];
188 assert step != 0 : 'got %s but step must be greater than 0' % step;
189 assert std.isString(indexable) || std.isArray(indexable) : 'std.slice accepts a string or an array, but got: %s' % std.type(indexable);
190 local build(slice, cur) =
191 if cur >= invar.end || cur >= invar.length then
192 slice
193 else
194 build(
195 if invar.type == 'string' then
196 slice + invar.indexable[cur]
197 else
198 slice + [invar.indexable[cur]],
199 cur + invar.step
200 ) tailstrict;
201 build(if invar.type == 'string' then '' else [], invar.index),
202161
203 member(arr, x)::162 member(arr, x)::
204 if std.isArray(arr) then163 if std.isArray(arr) then
209168
210 count(arr, x):: std.length(std.filter(function(v) v == x, arr)),169 count(arr, x):: std.length(std.filter(function(v) v == x, arr)),
211170
212 mod(a, b)::171 mod:: $intrinsic(mod),
213 if std.isNumber(a) && std.isNumber(b) then
214 std.modulo(a, b)
215 else if std.isString(a) then
216 std.format(a, b)
217 else
218 error 'Operator % cannot be used on types ' + std.type(a) + ' and ' + std.type(b) + '.',
219172
220 map(func, arr)::173 map:: $intrinsic(map),
221 if !std.isFunction(func) then
222 error ('std.map first param must be function, got ' + std.type(func))
223 else if !std.isArray(arr) && !std.isString(arr) then
224 error ('std.map second param must be array / string, got ' + std.type(arr))
225 else
226 std.makeArray(std.length(arr), function(i) func(arr[i])),
227174
228 mapWithIndex(func, arr)::175 mapWithIndex(func, arr)::
229 if !std.isFunction(func) then176 if !std.isFunction(func) then
250 std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))197 std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))
251 else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),198 else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),
252199
253 join(sep, arr)::200 join:: $intrinsic(join),
254 local aux(arr, i, first, running) =
255 if i >= std.length(arr) then
256 running
257 else if arr[i] == null then
258 aux(arr, i + 1, first, running) tailstrict
259 else if std.type(arr[i]) != std.type(sep) then
260 error 'expected %s but arr[%d] was %s ' % [std.type(sep), i, std.type(arr[i])]
261 else if first then
262 aux(arr, i + 1, false, running + arr[i]) tailstrict
263 else
264 aux(arr, i + 1, false, running + sep + arr[i]) tailstrict;
265 if !std.isArray(arr) then
266 error 'join second parameter should be array, got ' + std.type(arr)
267 else if std.isString(sep) then
268 aux(arr, 0, true, '')
269 else if std.isArray(sep) then
270 aux(arr, 0, true, [])
271 else
272 error 'join first parameter should be string or array, got ' + std.type(sep),
273201
274 lines(arr)::202 lines(arr)::
275 std.join('\n', arr + ['']),203 std.join('\n', arr + ['']),
283 error 'Expected string or array, got %s' % std.type(arr),211 error 'Expected string or array, got %s' % std.type(arr),
284212
285213
286 format(str, vals)::214 format:: $intrinsic(format),
287215
288 /////////////////////////////216 foldr:: $intrinsic(foldr),
289 // Parse the mini-language //
290 /////////////////////////////
291217
292 local try_parse_mapping_key(str, i) =218 foldl:: $intrinsic(foldl),
293 assert i < std.length(str) : 'Truncated format code.';
294 local c = str[i];
295 if c == '(' then
296 local consume(str, j, v) =
297 if j >= std.length(str) then
298 error 'Truncated format code.'
299 else
300 local c = str[j];
301 if c != ')' then
302 consume(str, j + 1, v + c)
303 else
304 { i: j + 1, v: v };
305 consume(str, i + 1, '')
306 else
307 { i: i, v: null };
308219
309 local try_parse_cflags(str, i) =
310 local consume(str, j, v) =
311 assert j < std.length(str) : 'Truncated format code.';
312 local c = str[j];
313 if c == '#' then
314 consume(str, j + 1, v { alt: true })
315 else if c == '0' then
316 consume(str, j + 1, v { zero: true })
317 else if c == '-' then
318 consume(str, j + 1, v { left: true })
319 else if c == ' ' then
320 consume(str, j + 1, v { blank: true })
321 else if c == '+' then
322 consume(str, j + 1, v { sign: true })
323 else
324 { i: j, v: v };
325 consume(str, i, { alt: false, zero: false, left: false, blank: false, sign: false });
326
327 local try_parse_field_width(str, i) =
328 if i < std.length(str) && str[i] == '*' then
329 { i: i + 1, v: '*' }
330 else
331 local consume(str, j, v) =
332 assert j < std.length(str) : 'Truncated format code.';
333 local c = str[j];
334 if c == '0' then
335 consume(str, j + 1, v * 10 + 0)
336 else if c == '1' then
337 consume(str, j + 1, v * 10 + 1)
338 else if c == '2' then
339 consume(str, j + 1, v * 10 + 2)
340 else if c == '3' then
341 consume(str, j + 1, v * 10 + 3)
342 else if c == '4' then
343 consume(str, j + 1, v * 10 + 4)
344 else if c == '5' then
345 consume(str, j + 1, v * 10 + 5)
346 else if c == '6' then
347 consume(str, j + 1, v * 10 + 6)
348 else if c == '7' then
349 consume(str, j + 1, v * 10 + 7)
350 else if c == '8' then
351 consume(str, j + 1, v * 10 + 8)
352 else if c == '9' then
353 consume(str, j + 1, v * 10 + 9)
354 else
355 { i: j, v: v };
356 consume(str, i, 0);
357
358 local try_parse_precision(str, i) =
359 assert i < std.length(str) : 'Truncated format code.';
360 local c = str[i];
361 if c == '.' then
362 try_parse_field_width(str, i + 1)
363 else
364 { i: i, v: null };
365
366 // Ignored, if it exists.
367 local try_parse_length_modifier(str, i) =
368 assert i < std.length(str) : 'Truncated format code.';
369 local c = str[i];
370 if c == 'h' || c == 'l' || c == 'L' then
371 i + 1
372 else
373 i;
374
375 local parse_conv_type(str, i) =
376 assert i < std.length(str) : 'Truncated format code.';
377 local c = str[i];
378 if c == 'd' || c == 'i' || c == 'u' then
379 { i: i + 1, v: 'd', caps: false }
380 else if c == 'o' then
381 { i: i + 1, v: 'o', caps: false }
382 else if c == 'x' then
383 { i: i + 1, v: 'x', caps: false }
384 else if c == 'X' then
385 { i: i + 1, v: 'x', caps: true }
386 else if c == 'e' then
387 { i: i + 1, v: 'e', caps: false }
388 else if c == 'E' then
389 { i: i + 1, v: 'e', caps: true }
390 else if c == 'f' then
391 { i: i + 1, v: 'f', caps: false }
392 else if c == 'F' then
393 { i: i + 1, v: 'f', caps: true }
394 else if c == 'g' then
395 { i: i + 1, v: 'g', caps: false }
396 else if c == 'G' then
397 { i: i + 1, v: 'g', caps: true }
398 else if c == 'c' then
399 { i: i + 1, v: 'c', caps: false }
400 else if c == 's' then
401 { i: i + 1, v: 's', caps: false }
402 else if c == '%' then
403 { i: i + 1, v: '%', caps: false }
404 else
405 error 'Unrecognised conversion type: ' + c;
406
407
408 // Parsed initial %, now the rest.
409 local parse_code(str, i) =
410 assert i < std.length(str) : 'Truncated format code.';
411 local mkey = try_parse_mapping_key(str, i);
412 local cflags = try_parse_cflags(str, mkey.i);
413 local fw = try_parse_field_width(str, cflags.i);
414 local prec = try_parse_precision(str, fw.i);
415 local len_mod = try_parse_length_modifier(str, prec.i);
416 local ctype = parse_conv_type(str, len_mod);
417 {
418 i: ctype.i,
419 code: {
420 mkey: mkey.v,
421 cflags: cflags.v,
422 fw: fw.v,
423 prec: prec.v,
424 ctype: ctype.v,
425 caps: ctype.caps,
426 },
427 };
428
429 // Parse a format string (containing none or more % format tags).
430 local parse_codes(str, i, out, cur) =
431 if i >= std.length(str) then
432 out + [cur]
433 else
434 local c = str[i];
435 if c == '%' then
436 local r = parse_code(str, i + 1);
437 parse_codes(str, r.i, out + [cur, r.code], '') tailstrict
438 else
439 parse_codes(str, i + 1, out, cur + c) tailstrict;
440
441 local codes = parse_codes(str, 0, [], '');
442
443
444 ///////////////////////
445 // Format the values //
446 ///////////////////////
447
448 // Useful utilities
449 local padding(w, s) =
450 local aux(w, v) =
451 if w <= 0 then
452 v
453 else
454 aux(w - 1, v + s);
455 aux(w, '');
456
457 // Add s to the left of str so that its length is at least w.
458 local pad_left(str, w, s) =
459 padding(w - std.length(str), s) + str;
460
461 // Add s to the right of str so that its length is at least w.
462 local pad_right(str, w, s) =
463 str + padding(w - std.length(str), s);
464
465 // Render an integer (e.g., decimal or octal).
466 local render_int(n__, min_chars, min_digits, blank, sign, radix, zero_prefix) =
467 local n_ = std.abs(n__);
468 local aux(n) =
469 if n == 0 then
470 zero_prefix
471 else
472 aux(std.floor(n / radix)) + (n % radix);
473 local dec = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
474 local neg = n__ < 0;
475 local zp = min_chars - (if neg || blank || sign then 1 else 0);
476 local zp2 = std.max(zp, min_digits);
477 local dec2 = pad_left(dec, zp2, '0');
478 (if neg then '-' else if sign then '+' else if blank then ' ' else '') + dec2;
479
480 // Render an integer in hexadecimal.
481 local render_hex(n__, min_chars, min_digits, blank, sign, add_zerox, capitals) =
482 local numerals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
483 + if capitals then ['A', 'B', 'C', 'D', 'E', 'F']
484 else ['a', 'b', 'c', 'd', 'e', 'f'];
485 local n_ = std.abs(n__);
486 local aux(n) =
487 if n == 0 then
488 ''
489 else
490 aux(std.floor(n / 16)) + numerals[n % 16];
491 local hex = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
492 local neg = n__ < 0;
493 local zp = min_chars - (if neg || blank || sign then 1 else 0)
494 - (if add_zerox then 2 else 0);
495 local zp2 = std.max(zp, min_digits);
496 local hex2 = (if add_zerox then (if capitals then '0X' else '0x') else '')
497 + pad_left(hex, zp2, '0');
498 (if neg then '-' else if sign then '+' else if blank then ' ' else '') + hex2;
499
500 local strip_trailing_zero(str) =
501 local aux(str, i) =
502 if i < 0 then
503 ''
504 else
505 if str[i] == '0' then
506 aux(str, i - 1)
507 else
508 std.substr(str, 0, i + 1);
509 aux(str, std.length(str) - 1);
510
511 // Render floating point in decimal form
512 local render_float_dec(n__, zero_pad, blank, sign, ensure_pt, trailing, prec) =
513 local n_ = std.abs(n__);
514 local whole = std.floor(n_);
515 local dot_size = if prec == 0 && !ensure_pt then 0 else 1;
516 local zp = zero_pad - prec - dot_size;
517 local str = render_int(std.sign(n__) * whole, zp, 0, blank, sign, 10, '');
518 if prec == 0 then
519 str + if ensure_pt then '.' else ''
520 else
521 local frac = std.floor((n_ - whole) * std.pow(10, prec) + 0.5);
522 if trailing || frac > 0 then
523 local frac_str = render_int(frac, prec, 0, false, false, 10, '');
524 str + '.' + if !trailing then strip_trailing_zero(frac_str) else frac_str
525 else
526 str;
527
528 // Render floating point in scientific form
529 local render_float_sci(n__, zero_pad, blank, sign, ensure_pt, trailing, caps, prec) =
530 local exponent = if n__ == 0 then 0 else std.floor(std.log(std.abs(n__)) / std.log(10));
531 local suff = (if caps then 'E' else 'e')
532 + render_int(exponent, 3, 0, false, true, 10, '');
533 local mantissa = if exponent == -324 then
534 // Avoid a rounding error where std.pow(10, -324) is 0
535 // -324 is the smallest exponent possible.
536 n__ * 10 / std.pow(10, exponent + 1)
537 else
538 n__ / std.pow(10, exponent);
539 local zp2 = zero_pad - std.length(suff);
540 render_float_dec(mantissa, zp2, blank, sign, ensure_pt, trailing, prec) + suff;
541
542 // Render a value with an arbitrary format code.
543 local format_code(val, code, fw, prec_or_null, i) =
544 local cflags = code.cflags;
545 local fpprec = if prec_or_null != null then prec_or_null else 6;
546 local iprec = if prec_or_null != null then prec_or_null else 0;
547 local zp = if cflags.zero && !cflags.left then fw else 0;
548 if code.ctype == 's' then
549 std.toString(val)
550 else if code.ctype == 'd' then
551 if std.type(val) != 'number' then
552 error 'Format required number at '
553 + i + ', got ' + std.type(val)
554 else
555 render_int(val, zp, iprec, cflags.blank, cflags.sign, 10, '')
556 else if code.ctype == 'o' then
557 if std.type(val) != 'number' then
558 error 'Format required number at '
559 + i + ', got ' + std.type(val)
560 else
561 local zero_prefix = if cflags.alt then '0' else '';
562 render_int(val, zp, iprec, cflags.blank, cflags.sign, 8, zero_prefix)
563 else if code.ctype == 'x' then
564 if std.type(val) != 'number' then
565 error 'Format required number at '
566 + i + ', got ' + std.type(val)
567 else
568 render_hex(val,
569 zp,
570 iprec,
571 cflags.blank,
572 cflags.sign,
573 cflags.alt,
574 code.caps)
575 else if code.ctype == 'f' then
576 if std.type(val) != 'number' then
577 error 'Format required number at '
578 + i + ', got ' + std.type(val)
579 else
580 render_float_dec(val,
581 zp,
582 cflags.blank,
583 cflags.sign,
584 cflags.alt,
585 true,
586 fpprec)
587 else if code.ctype == 'e' then
588 if std.type(val) != 'number' then
589 error 'Format required number at '
590 + i + ', got ' + std.type(val)
591 else
592 render_float_sci(val,
593 zp,
594 cflags.blank,
595 cflags.sign,
596 cflags.alt,
597 true,
598 code.caps,
599 fpprec)
600 else if code.ctype == 'g' then
601 if std.type(val) != 'number' then
602 error 'Format required number at '
603 + i + ', got ' + std.type(val)
604 else
605 local exponent = std.floor(std.log(std.abs(val)) / std.log(10));
606 if exponent < -4 || exponent >= fpprec then
607 render_float_sci(val,
608 zp,
609 cflags.blank,
610 cflags.sign,
611 cflags.alt,
612 cflags.alt,
613 code.caps,
614 fpprec - 1)
615 else
616 local digits_before_pt = std.max(1, exponent + 1);
617 render_float_dec(val,
618 zp,
619 cflags.blank,
620 cflags.sign,
621 cflags.alt,
622 cflags.alt,
623 fpprec - digits_before_pt)
624 else if code.ctype == 'c' then
625 if std.type(val) == 'number' then
626 std.char(val)
627 else if std.type(val) == 'string' then
628 if std.length(val) == 1 then
629 val
630 else
631 error '%c expected 1-sized string got: ' + std.length(val)
632 else
633 error '%c expected number / string, got: ' + std.type(val)
634 else
635 error 'Unknown code: ' + code.ctype;
636
637 // Render a parsed format string with an array of values.
638 local format_codes_arr(codes, arr, i, j, v) =
639 if i >= std.length(codes) then
640 if j < std.length(arr) then
641 error ('Too many values to format: ' + std.length(arr) + ', expected ' + j)
642 else
643 v
644 else
645 local code = codes[i];
646 if std.type(code) == 'string' then
647 format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
648 else
649 local tmp = if code.fw == '*' then {
650 j: j + 1,
651 fw: if j >= std.length(arr) then
652 error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + j)
653 else
654 arr[j],
655 } else {
656 j: j,
657 fw: code.fw,
658 };
659 local tmp2 = if code.prec == '*' then {
660 j: tmp.j + 1,
661 prec: if tmp.j >= std.length(arr) then
662 error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + tmp.j)
663 else
664 arr[tmp.j],
665 } else {
666 j: tmp.j,
667 prec: code.prec,
668 };
669 local j2 = tmp2.j;
670 local val =
671 if j2 < std.length(arr) then
672 arr[j2]
673 else
674 error ('Not enough values to format: ' + std.length(arr) + ', expected more than ' + j2);
675 local s =
676 if code.ctype == '%' then
677 '%'
678 else
679 format_code(val, code, tmp.fw, tmp2.prec, j2);
680 local s_padded =
681 if code.cflags.left then
682 pad_right(s, tmp.fw, ' ')
683 else
684 pad_left(s, tmp.fw, ' ');
685 local j3 =
686 if code.ctype == '%' then
687 j2
688 else
689 j2 + 1;
690 format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
691
692 // Render a parsed format string with an object of values.
693 local format_codes_obj(codes, obj, i, v) =
694 if i >= std.length(codes) then
695 v
696 else
697 local code = codes[i];
698 if std.type(code) == 'string' then
699 format_codes_obj(codes, obj, i + 1, v + code) tailstrict
700 else
701 local f =
702 if code.mkey == null then
703 error 'Mapping keys required.'
704 else
705 code.mkey;
706 local fw =
707 if code.fw == '*' then
708 error 'Cannot use * field width with object.'
709 else
710 code.fw;
711 local prec =
712 if code.prec == '*' then
713 error 'Cannot use * precision with object.'
714 else
715 code.prec;
716 local val =
717 if std.objectHasAll(obj, f) then
718 obj[f]
719 else
720 error 'No such field: ' + f;
721 local s =
722 if code.ctype == '%' then
723 '%'
724 else
725 format_code(val, code, fw, prec, f);
726 local s_padded =
727 if code.cflags.left then
728 pad_right(s, fw, ' ')
729 else
730 pad_left(s, fw, ' ');
731 format_codes_obj(codes, obj, i + 1, v + s_padded) tailstrict;
732
733 if std.isArray(vals) then
734 format_codes_arr(codes, vals, 0, 0, '')
735 else if std.isObject(vals) then
736 format_codes_obj(codes, vals, 0, '')
737 else
738 format_codes_arr(codes, [vals], 0, 0, ''),
739
740 foldr(func, arr, init)::
741 local aux(func, arr, running, idx) =
742 if idx < 0 then
743 running
744 else
745 aux(func, arr, func(arr[idx], running), idx - 1) tailstrict;
746 aux(func, arr, init, std.length(arr) - 1),
747
748 foldl(func, arr, init)::
749 local aux(func, arr, running, idx) =
750 if idx >= std.length(arr) then
751 running
752 else
753 aux(func, arr, func(running, arr[idx]), idx + 1) tailstrict;
754 aux(func, arr, init, 0),
755
756
757 filterMap(filter_func, map_func, arr)::220 filterMap(filter_func, map_func, arr)::
912 else375 else
913 error 'TOML body must be an object. Got ' + std.type(value),376 error 'TOML body must be an object. Got ' + std.type(value),
914377
915 escapeStringJson(str_)::378 escapeStringJson:: $intrinsic(escapeStringJson),
916 local str = std.toString(str_);
917 local trans(ch) =
918 if ch == '"' then
919 '\\"'
920 else if ch == '\\' then
921 '\\\\'
922 else if ch == '\b' then
923 '\\b'
924 else if ch == '\f' then
925 '\\f'
926 else if ch == '\n' then
927 '\\n'
928 else if ch == '\r' then
929 '\\r'
930 else if ch == '\t' then
931 '\\t'
932 else
933 local cp = std.codepoint(ch);
934 if cp < 32 || (cp >= 127 && cp <= 159) then
935 '\\u%04x' % [cp]
936 else
937 ch;
938 '"%s"' % std.join('', [trans(ch) for ch in std.stringChars(str)]),
939379
940 escapeStringPython(str)::380 escapeStringPython(str)::
941 std.escapeStringJson(str),381 std.escapeStringJson(str),
960400
961 manifestJson(value):: std.manifestJsonEx(value, ' '),401 manifestJson(value):: std.manifestJsonEx(value, ' '),
962402
963 manifestJsonEx(value, indent)::403 manifestJsonEx:: $intrinsic(manifestJsonEx),
964 local aux(v, path, cindent) =
965 if v == true then
966 'true'
967 else if v == false then
968 'false'
969 else if v == null then
970 'null'
971 else if std.isNumber(v) then
972 '' + v
973 else if std.isString(v) then
974 std.escapeStringJson(v)
975 else if std.isFunction(v) then
976 error 'Tried to manifest function at ' + path
977 else if std.isArray(v) then
978 local range = std.range(0, std.length(v) - 1);
979 local new_indent = cindent + indent;
980 local lines = ['[\n']
981 + std.join([',\n'],
982 [
983 [new_indent + aux(v[i], path + [i], new_indent)]
984 for i in range
985 ])
986 + ['\n' + cindent + ']'];
987 std.join('', lines)
988 else if std.isObject(v) then
989 local lines = ['{\n']
990 + std.join([',\n'],
991 [
992 [cindent + indent + std.escapeStringJson(k) + ': '
993 + aux(v[k], path + [k], cindent + indent)]
994 for k in std.objectFields(v)
995 ])
996 + ['\n' + cindent + '}'];
997 std.join('', lines);
998 aux(value, [], ''),
999404
1000 manifestYamlDoc(value, indent_array_in_object=false)::405 manifestYamlDoc(value, indent_array_in_object=false)::
1001 local aux(v, path, cindent) =406 local aux(v, path, cindent) =
1136 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',541 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
1137 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },542 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },
1138543
1139 base64(input)::544 base64:: $intrinsic(base64),
1140 local bytes =
1141 if std.isString(input) then
1142 std.map(function(c) std.codepoint(c), input)
1143 else
1144 input;
1145545
1146 local aux(arr, i, r) =
1147 if i >= std.length(arr) then
1148 r
1149 else if i + 1 >= std.length(arr) then
1150 local str =
1151 // 6 MSB of i
1152 base64_table[(arr[i] & 252) >> 2] +
1153 // 2 LSB of i
1154 base64_table[(arr[i] & 3) << 4] +
1155 '==';
1156 aux(arr, i + 3, r + str) tailstrict
1157 else if i + 2 >= std.length(arr) then
1158 local str =
1159 // 6 MSB of i
1160 base64_table[(arr[i] & 252) >> 2] +
1161 // 2 LSB of i, 4 MSB of i+1
1162 base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
1163 // 4 LSB of i+1
1164 base64_table[(arr[i + 1] & 15) << 2] +
1165 '=';
1166 aux(arr, i + 3, r + str) tailstrict
1167 else
1168 local str =
1169 // 6 MSB of i
1170 base64_table[(arr[i] & 252) >> 2] +
1171 // 2 LSB of i, 4 MSB of i+1
1172 base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
1173 // 4 LSB of i+1, 2 MSB of i+2
1174 base64_table[(arr[i + 1] & 15) << 2 | (arr[i + 2] & 192) >> 6] +
1175 // 6 LSB of i+2
1176 base64_table[(arr[i + 2] & 63)];
1177 aux(arr, i + 3, r + str) tailstrict;
1178
1179 local sanity = std.foldl(function(r, a) r && (a < 256), bytes, true);
1180 if !sanity then
1181 error 'Can only base64 encode strings / arrays of single bytes.'
1182 else
1183 aux(bytes, 0, ''),
1184
1185
1186 base64DecodeBytes(str)::546 base64DecodeBytes(str)::
1187 if std.length(str) % 4 != 0 then547 if std.length(str) % 4 != 0 then
1188 error 'Not a base64 encoded string "%s"' % str548 error 'Not a base64 encoded string "%s"' % str
1208 local bytes = std.base64DecodeBytes(str);568 local bytes = std.base64DecodeBytes(str);
1209 std.join('', std.map(function(b) std.char(b), bytes)),569 std.join('', std.map(function(b) std.char(b), bytes)),
1210570
1211 reverse(arr)::571 reverse:: $intrinsic(reverse),
1212 local l = std.length(arr);
1213 std.makeArray(l, function(i) arr[l - i - 1]),
1214572
1215 // Merge-sort for long arrays and naive quicksort for shorter ones573 sortImpl:: $intrinsic(sortImpl),
1216 sortImpl(arr, keyF)::
1217 local quickSort(arr, keyF=id) =
1218 local l = std.length(arr);
1219 if std.length(arr) <= 1 then
1220 arr
1221 else
1222 local pos = 0;
1223 local pivot = keyF(arr[pos]);
1224 local rest = std.makeArray(l - 1, function(i) if i < pos then arr[i] else arr[i + 1]);
1225 local left = std.filter(function(x) keyF(x) < pivot, rest);
1226 local right = std.filter(function(x) keyF(x) >= pivot, rest);
1227 quickSort(left, keyF) + [arr[pos]] + quickSort(right, keyF);
1228574
1229 local merge(a, b) =
1230 local la = std.length(a), lb = std.length(b);
1231 local aux(i, j, prefix) =
1232 if i == la then
1233 prefix + b[j:]
1234 else if j == lb then
1235 prefix + a[i:]
1236 else
1237 if keyF(a[i]) <= keyF(b[j]) then
1238 aux(i + 1, j, prefix + [a[i]]) tailstrict
1239 else
1240 aux(i, j + 1, prefix + [b[j]]) tailstrict;
1241 aux(0, 0, []);
1242
1243 local l = std.length(arr);
1244 if std.length(arr) <= 30 then
1245 quickSort(arr, keyF=keyF)
1246 else
1247 local mid = std.floor(l / 2);
1248 local left = arr[:mid], right = arr[mid:];
1249 merge(std.sort(left, keyF=keyF), std.sort(right, keyF=keyF)),
1250
1251 sort(arr, keyF=id)::575 sort(arr, keyF=id)::
1252 std.sortImpl(arr, keyF),576 std.sortImpl(arr, keyF),
1253577
1355679
1356 objectValuesAll(o)::680 objectValuesAll(o)::
1357 [o[k] for k in std.objectFieldsAll(o)],681 [o[k] for k in std.objectFieldsAll(o)],
1358
1359 equals(a, b)::
1360 local ta = std.type(a);
1361 local tb = std.type(b);
1362 if !std.primitiveEquals(ta, tb) then
1363 false
1364 else
1365 if std.primitiveEquals(ta, 'array') then
1366 local la = std.length(a);
1367 if !std.primitiveEquals(la, std.length(b)) then
1368 false
1369 else
1370 local aux(a, b, i) =
1371 if i >= la then
1372 true
1373 else if a[i] != b[i] then
1374 false
1375 else
1376 aux(a, b, i + 1) tailstrict;
1377 aux(a, b, 0)
1378 else if std.primitiveEquals(ta, 'object') then
1379 local fields = std.objectFields(a);
1380 local lfields = std.length(fields);
1381 if fields != std.objectFields(b) then
1382 false
1383 else
1384 local aux(a, b, i) =
1385 if i >= lfields then
1386 true
1387 else if local f = fields[i]; a[f] != b[f] then
1388 false
1389 else
1390 aux(a, b, i + 1) tailstrict;
1391 aux(a, b, 0)
1392 else
1393 std.primitiveEquals(a, b),
1394682
683 equals:: $intrinsic(equals),
1395684
1396 resolvePath(f, r)::685 resolvePath(f, r)::
1397 local arr = std.split(f, '/');686 local arr = std.split(f, '/');