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
before · crates/jrsonnet-parser/src/lib.rs
1#![allow(clippy::redundant_closure_call)]23use peg::parser;4use std::{5	path::{Path, PathBuf},6	rc::Rc,7};8mod expr;9pub use expr::*;10pub use peg;1112pub struct ParserSettings {13	pub loc_data: bool,14	pub file_name: Rc<Path>,15}1617macro_rules! expr_bin {18	($a:ident $op:ident $b:ident) => {19		loc_expr_todo!(Expr::BinaryOp($a, $op, $b))20	};21}22macro_rules! expr_un {23	($op:ident $a:ident) => {24		loc_expr_todo!(Expr::UnaryOp($op, $a))25	};26}2728parser! {29	grammar jsonnet_parser() for str {30		use peg::ParseLiteral;3132		/// Standard C-like comments33		rule comment()34			= "//" (!['\n'][_])* "\n"35			/ "/*" ("\\*/" / "\\\\" / (!("*/")[_]))* "*/"36			/ "#" (!['\n'][_])* "\n"3738		rule single_whitespace() = quiet!{([' ' | '\r' | '\n' | '\t'] / comment())} / expected!("<whitespace>")39		rule _() = single_whitespace()*4041		/// For comma-delimited elements42		rule comma() = quiet!{_ "," _} / expected!("<comma>")43		rule alpha() -> char = c:$(['_' | 'a'..='z' | 'A'..='Z']) {c.chars().next().unwrap()}44		rule digit() -> char = d:$(['0'..='9']) {d.chars().next().unwrap()}45		rule end_of_ident() = !['0'..='9' | '_' | 'a'..='z' | 'A'..='Z']46		/// Sequence of digits47		rule uint() -> u64 = a:$(digit()+) { a.parse().unwrap() }48		/// Number in scientific notation format49		rule number() -> f64 = quiet!{a:$(uint() ("." uint())? (['e'|'E'] (s:['+'|'-'])? uint())?) { a.parse().unwrap() }} / expected!("<number>")5051		/// Reserved word followed by any non-alphanumberic52		rule reserved() = ("assert" / "else" / "error" / "false" / "for" / "function" / "if" / "import" / "importstr" / "in" / "local" / "null" / "tailstrict" / "then" / "self" / "super" / "true") end_of_ident()53		rule id() = quiet!{ !reserved() alpha() (alpha() / digit())*} / expected!("<identifier>")5455		rule keyword(id: &'static str) -> ()56			= ##parse_string_literal(id) end_of_ident()57		// Adds location data information to existing expression58		rule l(s: &ParserSettings, x: rule<Expr>) -> LocExpr59			= start:position!() v:x() end:position!() {loc_expr!(v, s.loc_data, (s.file_name.clone(), start, end))}6061		pub rule param(s: &ParserSettings) -> expr::Param = name:$(id()) expr:(_ "=" _ expr:expr(s){expr})? { expr::Param(name.into(), expr) }62		pub rule params(s: &ParserSettings) -> expr::ParamsDesc63			= params:param(s) ** comma() comma()? {64				let mut defaults_started = false;65				for param in &params {66					defaults_started = defaults_started || param.1.is_some();67					assert_eq!(defaults_started, param.1.is_some(), "defauld parameters should be used after all positionals");68				}69				expr::ParamsDesc(Rc::new(params))70			}71			/ { expr::ParamsDesc(Rc::new(Vec::new())) }7273		pub rule arg(s: &ParserSettings) -> expr::Arg74			= name:$(id()) _ "=" _ expr:expr(s) {expr::Arg(Some(name.into()), expr)}75			/ expr:expr(s) {expr::Arg(None, expr)}76		pub rule args(s: &ParserSettings) -> expr::ArgsDesc77			= args:arg(s) ** comma() comma()? {78				let mut named_started = false;79				for arg in &args {80					named_started = named_started || arg.0.is_some();81					assert_eq!(named_started, arg.0.is_some(), "named args should be used after all positionals");82				}83				expr::ArgsDesc(args)84			}85			/ { expr::ArgsDesc(Vec::new()) }8687		pub rule bind(s: &ParserSettings) -> expr::BindSpec88			= name:$(id()) _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: None, value: expr}}89			/ name:$(id()) _ "(" _ params:params(s) _ ")" _ "=" _ expr:expr(s) {expr::BindSpec{name:name.into(), params: Some(params), value: expr}}90		pub rule assertion(s: &ParserSettings) -> expr::AssertStmt91			= keyword("assert") _ cond:expr(s) msg:(_ ":" _ e:expr(s) {e})? { expr::AssertStmt(cond, msg) }9293		pub rule whole_line() -> &'input str94			= str:$((!['\n'][_])* "\n") {str}95		pub rule string_block() -> String96			= "|||" (!['\n']single_whitespace())* "\n"97			  empty_lines:$(['\n']*)98			  prefix:[' ' | '\t']+ first_line:whole_line()99			  lines:("\n" {"\n"} / [' ' | '\t']*<{prefix.len()}> s:whole_line() {s})*100			  [' ' | '\t']*<, {prefix.len() - 1}> "|||"101			  {let mut l = empty_lines.to_owned(); l.push_str(first_line); l.extend(lines); l}102		pub rule string() -> String103			= quiet!{ "\"" str:$(("\\\"" / "\\\\" / (!['"'][_]))*) "\"" {unescape::unescape(str).unwrap()}104			/ "'" str:$(("\\'" / "\\\\" / (!['\''][_]))*) "'" {unescape::unescape(str).unwrap()}105			/ "@'" str:$(("''" / (!['\''][_]))*) "'" {str.replace("''", "'")}106			/ "@\"" str:$(("\"\"" / (!['"'][_]))*) "\"" {str.replace("\"\"", "\"")}107			/ string_block() } / expected!("<string>")108109		pub rule field_name(s: &ParserSettings) -> expr::FieldName110			= name:$(id()) {expr::FieldName::Fixed(name.into())}111			/ name:string() {expr::FieldName::Fixed(name.into())}112			/ "[" _ expr:expr(s) _ "]" {expr::FieldName::Dyn(expr)}113		pub rule visibility() -> expr::Visibility114			= ":::" {expr::Visibility::Unhide}115			/ "::" {expr::Visibility::Hidden}116			/ ":" {expr::Visibility::Normal}117		pub rule field(s: &ParserSettings) -> expr::FieldMember118			= name:field_name(s) _ plus:"+"? _ visibility:visibility() _ value:expr(s) {expr::FieldMember{119				name,120				plus: plus.is_some(),121				params: None,122				visibility,123				value,124			}}125			/ name:field_name(s) _ "(" _ params:params(s) _ ")" _ visibility:visibility() _ value:expr(s) {expr::FieldMember{126				name,127				plus: false,128				params: Some(params),129				visibility,130				value,131			}}132		pub rule obj_local(s: &ParserSettings) -> BindSpec133			= keyword("local") _ bind:bind(s) {bind}134		pub rule member(s: &ParserSettings) -> expr::Member135			= bind:obj_local(s) {expr::Member::BindStmt(bind)}136			/ assertion:assertion(s) {expr::Member::AssertStmt(assertion)}137			/ field:field(s) {expr::Member::Field(field)}138		pub rule objinside(s: &ParserSettings) -> expr::ObjBody139			= pre_locals:(b: obj_local(s) comma() {b})* "[" _ key:expr(s) _ "]" _ ":" _ value:expr(s) post_locals:(comma() b:obj_local(s) {b})* _ forspec:forspec(s) others:(_ rest:compspec(s) {rest})? {140				let mut compspecs = vec![CompSpec::ForSpec(forspec)];141				compspecs.extend(others.unwrap_or_default());142				expr::ObjBody::ObjComp(expr::ObjComp{143					pre_locals,144					key,145					value,146					post_locals,147					compspecs,148				})149			}150			/ members:(member(s) ** comma()) comma()? {expr::ObjBody::MemberList(members)}151		pub rule ifspec(s: &ParserSettings) -> IfSpecData152			= keyword("if") _ expr:expr(s) {IfSpecData(expr)}153		pub rule forspec(s: &ParserSettings) -> ForSpecData154			= keyword("for") _ id:$(id()) _ keyword("in") _ cond:expr(s) {ForSpecData(id.into(), cond)}155		pub rule compspec(s: &ParserSettings) -> Vec<expr::CompSpec>156			= s:(i:ifspec(s) { expr::CompSpec::IfSpec(i) } / f:forspec(s) {expr::CompSpec::ForSpec(f)} ) ** _ {s}157		pub rule local_expr(s: &ParserSettings) -> LocExpr158			= l(s,<keyword("local") _ binds:bind(s) ** comma() _ ";" _ expr:expr(s) { Expr::LocalExpr(binds, expr) }>)159		pub rule string_expr(s: &ParserSettings) -> LocExpr160			= l(s, <s:string() {Expr::Str(s.into())}>)161		pub rule obj_expr(s: &ParserSettings) -> LocExpr162			= l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)163		pub rule array_expr(s: &ParserSettings) -> LocExpr164			= l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)165		pub rule array_comp_expr(s: &ParserSettings) -> LocExpr166			= l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {167				let mut specs = vec![CompSpec::ForSpec(forspec)];168				specs.extend(others.unwrap_or_default());169				Expr::ArrComp(expr, specs)170			}>)171		pub rule number_expr(s: &ParserSettings) -> LocExpr172			= l(s,<n:number() { expr::Expr::Num(n) }>)173		pub rule var_expr(s: &ParserSettings) -> LocExpr174			= l(s,<n:$(id()) { expr::Expr::Var(n.into()) }>)175		pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr176			= l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{177				cond,178				cond_then,179				cond_else,180			}}>)181182		pub rule literal(s: &ParserSettings) -> LocExpr183			= l(s,<v:(184				keyword("null") {LiteralType::Null}185				/ keyword("true") {LiteralType::True}186				/ keyword("false") {LiteralType::False}187				/ keyword("self") {LiteralType::This}188				/ keyword("$") {LiteralType::Dollar}189				/ keyword("super") {LiteralType::Super}190			) {Expr::Literal(v)}>)191192		pub rule expr_basic(s: &ParserSettings) -> LocExpr193			= literal(s)194195			/ string_expr(s) / number_expr(s)196			/ array_expr(s)197			/ obj_expr(s)198			/ array_expr(s)199			/ array_comp_expr(s)200201			/ l(s,<keyword("importstr") _ path:string() {Expr::ImportStr(PathBuf::from(path))}>)202			/ l(s,<keyword("import") _ path:string() {Expr::Import(PathBuf::from(path))}>)203204			/ var_expr(s)205			/ local_expr(s)206			/ if_then_else_expr(s)207208			/ l(s,<keyword("function") _ "(" _ params:params(s) _ ")" _ expr:expr(s) {Expr::Function(params, expr)}>)209			/ l(s,<assertion:assertion(s) _ ";" _ expr:expr(s) { Expr::AssertExpr(assertion, expr) }>)210211			/ l(s,<keyword("error") _ expr:expr(s) { Expr::ErrorStmt(expr) }>)212213		rule slice_part(s: &ParserSettings) -> Option<LocExpr>214			= e:(_ e:expr(s) _{e})? {e}215		pub rule slice_desc(s: &ParserSettings) -> SliceDesc216			= start:slice_part(s) ":" pair:(end:slice_part(s) step:(":" e:slice_part(s){e})? {(end, step.flatten())})? {217				let (end, step) = if let Some((end, step)) = pair {218					(end, step)219				}else{220					(None, None)221				};222223				SliceDesc { start, end, step }224			}225226		rule binop(x: rule<()>) -> ()227			= quiet!{ x() } / expected!("<binary op>")228		rule unaryop(x: rule<()>) -> ()229			= quiet!{ x() } / expected!("<unary op>")230231232		use BinaryOpType::*;233		use UnaryOpType::*;234		rule expr(s: &ParserSettings) -> LocExpr235			= start:position!() a:precedence! {236				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}237				--238				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}239				--240				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}241				--242				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}243				--244				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}245				--246				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}247				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}248				--249				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}250				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}251				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}252				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}253				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}254				--255				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}256				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}257				--258				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}259				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}260				--261				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}262				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}263				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}264				--265						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}266						unaryop(<"!">) _ b:@ {expr_un!(Not b)}267						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}268				--269				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}270				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}271				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}272				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}273				a:(@) _ "{" _ body:objinside(s) _ "}" {loc_expr_todo!(Expr::ObjExtend(a, body))}274				--275				e:expr_basic(s) {e}276				"(" _ e:expr(s) _ ")" {loc_expr_todo!(Expr::Parened(e))}277			} end:position!() {278				let LocExpr(e, _) = a;279				LocExpr(e, if s.loc_data {280					Some(ExprLocation(s.file_name.clone(), start, end))281				} else {282					None283				})284			}285			/ e:expr_basic(s) {e}286287		pub rule jsonnet(s: &ParserSettings) -> LocExpr = _ e:expr(s) _ {e}288	}289}290291pub type ParseError = peg::error::ParseError<peg::str::LineCol>;292pub fn parse(str: &str, settings: &ParserSettings) -> Result<LocExpr, ParseError> {293	jsonnet_parser::jsonnet(str, settings)294}295296#[macro_export]297macro_rules! el {298	($expr:expr) => {299		LocExpr(std::rc::Rc::new($expr), None)300	};301}302303#[cfg(test)]304pub mod tests {305	use super::{expr::*, parse};306	use crate::ParserSettings;307	use std::path::PathBuf;308	use BinaryOpType::*;309310	macro_rules! parse {311		($s:expr) => {312			parse(313				$s,314				&ParserSettings {315					loc_data: false,316					file_name: PathBuf::from("/test.jsonnet").into(),317				},318			)319			.unwrap()320		};321	}322323	mod expressions {324		use super::*;325326		pub fn basic_math() -> LocExpr {327			el!(Expr::BinaryOp(328				el!(Expr::Num(2.0)),329				Add,330				el!(Expr::BinaryOp(331					el!(Expr::Num(2.0)),332					Mul,333					el!(Expr::Num(2.0)),334				)),335			))336		}337	}338339	#[test]340	fn multiline_string() {341		assert_eq!(342			parse!("|||\n    Hello world!\n     a\n|||"),343			el!(Expr::Str("Hello world!\n a\n".into())),344		);345		assert_eq!(346			parse!("|||\n  Hello world!\n   a\n|||"),347			el!(Expr::Str("Hello world!\n a\n".into())),348		);349		assert_eq!(350			parse!("|||\n\t\tHello world!\n\t\t\ta\n|||"),351			el!(Expr::Str("Hello world!\n\ta\n".into())),352		);353		assert_eq!(354			parse!("|||\n   Hello world!\n    a\n |||"),355			el!(Expr::Str("Hello world!\n a\n".into())),356		);357	}358359	#[test]360	fn slice() {361		parse!("a[1:]");362		parse!("a[1::]");363		parse!("a[:1:]");364		parse!("a[::1]");365		parse!("str[:len - 1]");366	}367368	#[test]369	fn string_escaping() {370		assert_eq!(371			parse!(r#""Hello, \"world\"!""#),372			el!(Expr::Str(r#"Hello, "world"!"#.into())),373		);374		assert_eq!(375			parse!(r#"'Hello \'world\'!'"#),376			el!(Expr::Str("Hello 'world'!".into())),377		);378		assert_eq!(parse!(r#"'\\\\'"#), el!(Expr::Str("\\\\".into())),);379	}380381	#[test]382	fn string_unescaping() {383		assert_eq!(384			parse!(r#""Hello\nWorld""#),385			el!(Expr::Str("Hello\nWorld".into())),386		);387	}388389	#[test]390	fn string_verbantim() {391		assert_eq!(392			parse!(r#"@"Hello\n""World""""#),393			el!(Expr::Str("Hello\\n\"World\"".into())),394		);395	}396397	#[test]398	fn imports() {399		assert_eq!(400			parse!("import \"hello\""),401			el!(Expr::Import(PathBuf::from("hello"))),402		);403		assert_eq!(404			parse!("importstr \"garnish.txt\""),405			el!(Expr::ImportStr(PathBuf::from("garnish.txt")))406		);407	}408409	#[test]410	fn empty_object() {411		assert_eq!(parse!("{}"), el!(Expr::Obj(ObjBody::MemberList(vec![]))));412	}413414	#[test]415	fn basic_math() {416		assert_eq!(417			parse!("2+2*2"),418			el!(Expr::BinaryOp(419				el!(Expr::Num(2.0)),420				Add,421				el!(Expr::BinaryOp(422					el!(Expr::Num(2.0)),423					Mul,424					el!(Expr::Num(2.0))425				))426			))427		);428	}429430	#[test]431	fn basic_math_with_indents() {432		assert_eq!(parse!("2	+ 	  2	  *	2   	"), expressions::basic_math());433	}434435	#[test]436	fn basic_math_parened() {437		assert_eq!(438			parse!("2+(2+2*2)"),439			el!(Expr::BinaryOp(440				el!(Expr::Num(2.0)),441				Add,442				el!(Expr::Parened(expressions::basic_math())),443			))444		);445	}446447	/// Comments should not affect parsing448	#[test]449	fn comments() {450		assert_eq!(451			parse!("2//comment\n+//comment\n3/*test*/*/*test*/4"),452			el!(Expr::BinaryOp(453				el!(Expr::Num(2.0)),454				Add,455				el!(Expr::BinaryOp(456					el!(Expr::Num(3.0)),457					Mul,458					el!(Expr::Num(4.0))459				))460			))461		);462	}463464	/// Comments should be able to be escaped465	#[test]466	fn comment_escaping() {467		assert_eq!(468			parse!("2/*\\*/+*/ - 22"),469			el!(Expr::BinaryOp(470				el!(Expr::Num(2.0)),471				Sub,472				el!(Expr::Num(22.0))473			))474		);475	}476477	#[test]478	fn suffix() {479		// assert_eq!(parse!("std.test"), el!(Expr::Num(2.2)));480		// assert_eq!(parse!("std(2)"), el!(Expr::Num(2.2)));481		// assert_eq!(parse!("std.test(2)"), el!(Expr::Num(2.2)));482		// assert_eq!(parse!("a[b]"), el!(Expr::Num(2.2)))483	}484485	#[test]486	fn array_comp() {487		use Expr::*;488		assert_eq!(489			parse!("[std.deepJoin(x) for x in arr]"),490			el!(ArrComp(491				el!(Apply(492					el!(Index(el!(Var("std".into())), el!(Str("deepJoin".into())))),493					ArgsDesc(vec![Arg(None, el!(Var("x".into())))]),494					false,495				)),496				vec![CompSpec::ForSpec(ForSpecData(497					"x".into(),498					el!(Var("arr".into()))499				))]500			)),501		)502	}503504	#[test]505	fn reserved() {506		use Expr::*;507		assert_eq!(parse!("null"), el!(Literal(LiteralType::Null)));508		assert_eq!(parse!("nulla"), el!(Var("nulla".into())));509	}510511	#[test]512	fn multiple_args_buf() {513		parse!("a(b, null_fields)");514	}515516	#[test]517	fn infix_precedence() {518		use Expr::*;519		assert_eq!(520			parse!("!a && !b"),521			el!(BinaryOp(522				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),523				And,524				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))525			))526		);527	}528529	#[test]530	fn infix_precedence_division() {531		use Expr::*;532		assert_eq!(533			parse!("!a / !b"),534			el!(BinaryOp(535				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into())))),536				Div,537				el!(UnaryOp(UnaryOpType::Not, el!(Var("b".into()))))538			))539		);540	}541542	#[test]543	fn double_negation() {544		use Expr::*;545		assert_eq!(546			parse!("!!a"),547			el!(UnaryOp(548				UnaryOpType::Not,549				el!(UnaryOp(UnaryOpType::Not, el!(Var("a".into()))))550			))551		)552	}553554	#[test]555	fn array_test_error() {556		parse!("[a for a in b if c for e in f]");557		//                    ^^^^ failed code558	}559560	#[test]561	fn can_parse_stdlib() {562		parse!(jrsonnet_stdlib::STDLIB_STR);563	}564565	// From source code566	/*567	#[bench]568	fn bench_parse_peg(b: &mut Bencher) {569		b.iter(|| parse!(jrsonnet_stdlib::STDLIB_STR))570	}571	*/572}
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -1,9 +1,29 @@
 {
-  __intrinsic_namespace__:: 'std',
-
   local std = self,
   local id = std.id,
 
+  # Those functions aren't normally located in stdlib
+  length:: $intrinsic(length),
+  type:: $intrinsic(type),
+  makeArray:: $intrinsic(makeArray),
+  codepoint:: $intrinsic(codepoint),
+  objectFieldsEx:: $intrinsic(objectFieldsEx),
+  objectHasEx:: $intrinsic(objectHasEx),
+  primitiveEquals:: $intrinsic(primitiveEquals),
+  modulo:: $intrinsic(modulo),
+  floor:: $intrinsic(floor),
+  log:: $intrinsic(log),
+  pow:: $intrinsic(pow),
+  extVar:: $intrinsic(extVar),
+  native:: $intrinsic(native),
+  filter:: $intrinsic(filter),
+  char:: $intrinsic(char),
+  encodeUTF8:: $intrinsic(encodeUTF8),
+  md5:: $intrinsic(md5),
+  trace:: $intrinsic(trace),
+  id:: $intrinsic(id),
+  parseJson:: $intrinsic(parseJson),
+
   isString(v):: std.type(v) == 'string',
   isNumber(v):: std.type(v) == 'number',
   isBoolean(v):: std.type(v) == 'boolean',
@@ -109,37 +129,8 @@
       else
         aux(str, delim, i2, arr, v + c) tailstrict;
     aux(str, c, 0, [], ''),
-
-  strReplace(str, from, to)::
-    assert std.isString(str);
-    assert std.isString(from);
-    assert std.isString(to);
-    assert from != '' : "'from' string must not be zero length.";
 
-    // Cache for performance.
-    local str_len = std.length(str);
-    local from_len = std.length(from);
-
-    // True if from is at str[i].
-    local found_at(i) = str[i:i + from_len] == from;
-
-    // Return the remainder of 'str' starting with 'start_index' where
-    // all occurrences of 'from' after 'curr_index' are replaced with 'to'.
-    local replace_after(start_index, curr_index, acc) =
-      if curr_index > str_len then
-        acc + str[start_index:curr_index]
-      else if found_at(curr_index) then
-        local new_index = curr_index + std.length(from);
-        replace_after(new_index, new_index, acc + str[start_index:curr_index] + to) tailstrict
-      else
-        replace_after(start_index, curr_index + 1, acc) tailstrict;
-
-    // if from_len==1, then we replace by splitting and rejoining the
-    // string which is much faster than recursing on replace_after
-    if from_len == 1 then
-      std.join(to, std.split(str, from))
-    else
-      replace_after(0, 0, ''),
+  strReplace:: $intrinsic(strReplace),
 
   asciiUpper(str)::
     local cp = std.codepoint;
@@ -157,8 +148,7 @@
       c;
     std.join('', std.map(down_letter, std.stringChars(str))),
 
-  range(from, to)::
-    std.makeArray(to - from + 1, function(i) i + from),
+  range:: $intrinsic(range),
 
   repeat(what, count)::
     local joiner =
@@ -167,38 +157,7 @@
       else error 'std.repeat first argument must be an array or a string';
     std.join(joiner, std.makeArray(count, function(i) what)),
 
-  slice(indexable, index, end, step)::
-    local invar =
-      // loop invariant with defaults applied
-      {
-        indexable: indexable,
-        index:
-          if index == null then 0
-          else index,
-        end:
-          if end == null then std.length(indexable)
-          else end,
-        step:
-          if step == null then 1
-          else step,
-        length: std.length(indexable),
-        type: std.type(indexable),
-      };
-    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];
-    assert step != 0 : 'got %s but step must be greater than 0' % step;
-    assert std.isString(indexable) || std.isArray(indexable) : 'std.slice accepts a string or an array, but got: %s' % std.type(indexable);
-    local build(slice, cur) =
-      if cur >= invar.end || cur >= invar.length then
-        slice
-      else
-        build(
-          if invar.type == 'string' then
-            slice + invar.indexable[cur]
-          else
-            slice + [invar.indexable[cur]],
-          cur + invar.step
-        ) tailstrict;
-    build(if invar.type == 'string' then '' else [], invar.index),
+  slice:: $intrinsic(slice),
 
   member(arr, x)::
     if std.isArray(arr) then
@@ -209,21 +168,9 @@
 
   count(arr, x):: std.length(std.filter(function(v) v == x, arr)),
 
-  mod(a, b)::
-    if std.isNumber(a) && std.isNumber(b) then
-      std.modulo(a, b)
-    else if std.isString(a) then
-      std.format(a, b)
-    else
-      error 'Operator % cannot be used on types ' + std.type(a) + ' and ' + std.type(b) + '.',
+  mod:: $intrinsic(mod),
 
-  map(func, arr)::
-    if !std.isFunction(func) then
-      error ('std.map first param must be function, got ' + std.type(func))
-    else if !std.isArray(arr) && !std.isString(arr) then
-      error ('std.map second param must be array / string, got ' + std.type(arr))
-    else
-      std.makeArray(std.length(arr), function(i) func(arr[i])),
+  map:: $intrinsic(map),
 
   mapWithIndex(func, arr)::
     if !std.isFunction(func) then
@@ -250,26 +197,7 @@
       std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))
     else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),
 
-  join(sep, arr)::
-    local aux(arr, i, first, running) =
-      if i >= std.length(arr) then
-        running
-      else if arr[i] == null then
-        aux(arr, i + 1, first, running) tailstrict
-      else if std.type(arr[i]) != std.type(sep) then
-        error 'expected %s but arr[%d] was %s ' % [std.type(sep), i, std.type(arr[i])]
-      else if first then
-        aux(arr, i + 1, false, running + arr[i]) tailstrict
-      else
-        aux(arr, i + 1, false, running + sep + arr[i]) tailstrict;
-    if !std.isArray(arr) then
-      error 'join second parameter should be array, got ' + std.type(arr)
-    else if std.isString(sep) then
-      aux(arr, 0, true, '')
-    else if std.isArray(sep) then
-      aux(arr, 0, true, [])
-    else
-      error 'join first parameter should be string or array, got ' + std.type(sep),
+  join:: $intrinsic(join),
 
   lines(arr)::
     std.join('\n', arr + ['']),
@@ -281,479 +209,14 @@
       std.join('', [std.deepJoin(x) for x in arr])
     else
       error 'Expected string or array, got %s' % std.type(arr),
-
-
-  format(str, vals)::
-
-    /////////////////////////////
-    // Parse the mini-language //
-    /////////////////////////////
-
-    local try_parse_mapping_key(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local c = str[i];
-      if c == '(' then
-        local consume(str, j, v) =
-          if j >= std.length(str) then
-            error 'Truncated format code.'
-          else
-            local c = str[j];
-            if c != ')' then
-              consume(str, j + 1, v + c)
-            else
-              { i: j + 1, v: v };
-        consume(str, i + 1, '')
-      else
-        { i: i, v: null };
 
-    local try_parse_cflags(str, i) =
-      local consume(str, j, v) =
-        assert j < std.length(str) : 'Truncated format code.';
-        local c = str[j];
-        if c == '#' then
-          consume(str, j + 1, v { alt: true })
-        else if c == '0' then
-          consume(str, j + 1, v { zero: true })
-        else if c == '-' then
-          consume(str, j + 1, v { left: true })
-        else if c == ' ' then
-          consume(str, j + 1, v { blank: true })
-        else if c == '+' then
-          consume(str, j + 1, v { sign: true })
-        else
-          { i: j, v: v };
-      consume(str, i, { alt: false, zero: false, left: false, blank: false, sign: false });
 
-    local try_parse_field_width(str, i) =
-      if i < std.length(str) && str[i] == '*' then
-        { i: i + 1, v: '*' }
-      else
-        local consume(str, j, v) =
-          assert j < std.length(str) : 'Truncated format code.';
-          local c = str[j];
-          if c == '0' then
-            consume(str, j + 1, v * 10 + 0)
-          else if c == '1' then
-            consume(str, j + 1, v * 10 + 1)
-          else if c == '2' then
-            consume(str, j + 1, v * 10 + 2)
-          else if c == '3' then
-            consume(str, j + 1, v * 10 + 3)
-          else if c == '4' then
-            consume(str, j + 1, v * 10 + 4)
-          else if c == '5' then
-            consume(str, j + 1, v * 10 + 5)
-          else if c == '6' then
-            consume(str, j + 1, v * 10 + 6)
-          else if c == '7' then
-            consume(str, j + 1, v * 10 + 7)
-          else if c == '8' then
-            consume(str, j + 1, v * 10 + 8)
-          else if c == '9' then
-            consume(str, j + 1, v * 10 + 9)
-          else
-            { i: j, v: v };
-        consume(str, i, 0);
-
-    local try_parse_precision(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local c = str[i];
-      if c == '.' then
-        try_parse_field_width(str, i + 1)
-      else
-        { i: i, v: null };
-
-    // Ignored, if it exists.
-    local try_parse_length_modifier(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local c = str[i];
-      if c == 'h' || c == 'l' || c == 'L' then
-        i + 1
-      else
-        i;
-
-    local parse_conv_type(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local c = str[i];
-      if c == 'd' || c == 'i' || c == 'u' then
-        { i: i + 1, v: 'd', caps: false }
-      else if c == 'o' then
-        { i: i + 1, v: 'o', caps: false }
-      else if c == 'x' then
-        { i: i + 1, v: 'x', caps: false }
-      else if c == 'X' then
-        { i: i + 1, v: 'x', caps: true }
-      else if c == 'e' then
-        { i: i + 1, v: 'e', caps: false }
-      else if c == 'E' then
-        { i: i + 1, v: 'e', caps: true }
-      else if c == 'f' then
-        { i: i + 1, v: 'f', caps: false }
-      else if c == 'F' then
-        { i: i + 1, v: 'f', caps: true }
-      else if c == 'g' then
-        { i: i + 1, v: 'g', caps: false }
-      else if c == 'G' then
-        { i: i + 1, v: 'g', caps: true }
-      else if c == 'c' then
-        { i: i + 1, v: 'c', caps: false }
-      else if c == 's' then
-        { i: i + 1, v: 's', caps: false }
-      else if c == '%' then
-        { i: i + 1, v: '%', caps: false }
-      else
-        error 'Unrecognised conversion type: ' + c;
-
-
-    // Parsed initial %, now the rest.
-    local parse_code(str, i) =
-      assert i < std.length(str) : 'Truncated format code.';
-      local mkey = try_parse_mapping_key(str, i);
-      local cflags = try_parse_cflags(str, mkey.i);
-      local fw = try_parse_field_width(str, cflags.i);
-      local prec = try_parse_precision(str, fw.i);
-      local len_mod = try_parse_length_modifier(str, prec.i);
-      local ctype = parse_conv_type(str, len_mod);
-      {
-        i: ctype.i,
-        code: {
-          mkey: mkey.v,
-          cflags: cflags.v,
-          fw: fw.v,
-          prec: prec.v,
-          ctype: ctype.v,
-          caps: ctype.caps,
-        },
-      };
-
-    // Parse a format string (containing none or more % format tags).
-    local parse_codes(str, i, out, cur) =
-      if i >= std.length(str) then
-        out + [cur]
-      else
-        local c = str[i];
-        if c == '%' then
-          local r = parse_code(str, i + 1);
-          parse_codes(str, r.i, out + [cur, r.code], '') tailstrict
-        else
-          parse_codes(str, i + 1, out, cur + c) tailstrict;
-
-    local codes = parse_codes(str, 0, [], '');
-
-
-    ///////////////////////
-    // Format the values //
-    ///////////////////////
-
-    // Useful utilities
-    local padding(w, s) =
-      local aux(w, v) =
-        if w <= 0 then
-          v
-        else
-          aux(w - 1, v + s);
-      aux(w, '');
-
-    // Add s to the left of str so that its length is at least w.
-    local pad_left(str, w, s) =
-      padding(w - std.length(str), s) + str;
-
-    // Add s to the right of str so that its length is at least w.
-    local pad_right(str, w, s) =
-      str + padding(w - std.length(str), s);
-
-    // Render an integer (e.g., decimal or octal).
-    local render_int(n__, min_chars, min_digits, blank, sign, radix, zero_prefix) =
-      local n_ = std.abs(n__);
-      local aux(n) =
-        if n == 0 then
-          zero_prefix
-        else
-          aux(std.floor(n / radix)) + (n % radix);
-      local dec = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
-      local neg = n__ < 0;
-      local zp = min_chars - (if neg || blank || sign then 1 else 0);
-      local zp2 = std.max(zp, min_digits);
-      local dec2 = pad_left(dec, zp2, '0');
-      (if neg then '-' else if sign then '+' else if blank then ' ' else '') + dec2;
-
-    // Render an integer in hexadecimal.
-    local render_hex(n__, min_chars, min_digits, blank, sign, add_zerox, capitals) =
-      local numerals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-                       + if capitals then ['A', 'B', 'C', 'D', 'E', 'F']
-                       else ['a', 'b', 'c', 'd', 'e', 'f'];
-      local n_ = std.abs(n__);
-      local aux(n) =
-        if n == 0 then
-          ''
-        else
-          aux(std.floor(n / 16)) + numerals[n % 16];
-      local hex = if std.floor(n_) == 0 then '0' else aux(std.floor(n_));
-      local neg = n__ < 0;
-      local zp = min_chars - (if neg || blank || sign then 1 else 0)
-                 - (if add_zerox then 2 else 0);
-      local zp2 = std.max(zp, min_digits);
-      local hex2 = (if add_zerox then (if capitals then '0X' else '0x') else '')
-                   + pad_left(hex, zp2, '0');
-      (if neg then '-' else if sign then '+' else if blank then ' ' else '') + hex2;
-
-    local strip_trailing_zero(str) =
-      local aux(str, i) =
-        if i < 0 then
-          ''
-        else
-          if str[i] == '0' then
-            aux(str, i - 1)
-          else
-            std.substr(str, 0, i + 1);
-      aux(str, std.length(str) - 1);
-
-    // Render floating point in decimal form
-    local render_float_dec(n__, zero_pad, blank, sign, ensure_pt, trailing, prec) =
-      local n_ = std.abs(n__);
-      local whole = std.floor(n_);
-      local dot_size = if prec == 0 && !ensure_pt then 0 else 1;
-      local zp = zero_pad - prec - dot_size;
-      local str = render_int(std.sign(n__) * whole, zp, 0, blank, sign, 10, '');
-      if prec == 0 then
-        str + if ensure_pt then '.' else ''
-      else
-        local frac = std.floor((n_ - whole) * std.pow(10, prec) + 0.5);
-        if trailing || frac > 0 then
-          local frac_str = render_int(frac, prec, 0, false, false, 10, '');
-          str + '.' + if !trailing then strip_trailing_zero(frac_str) else frac_str
-        else
-          str;
-
-    // Render floating point in scientific form
-    local render_float_sci(n__, zero_pad, blank, sign, ensure_pt, trailing, caps, prec) =
-      local exponent = if n__ == 0 then 0 else std.floor(std.log(std.abs(n__)) / std.log(10));
-      local suff = (if caps then 'E' else 'e')
-                   + render_int(exponent, 3, 0, false, true, 10, '');
-      local mantissa = if exponent == -324 then
-        // Avoid a rounding error where std.pow(10, -324) is 0
-        // -324 is the smallest exponent possible.
-        n__ * 10 / std.pow(10, exponent + 1)
-      else
-        n__ / std.pow(10, exponent);
-      local zp2 = zero_pad - std.length(suff);
-      render_float_dec(mantissa, zp2, blank, sign, ensure_pt, trailing, prec) + suff;
-
-    // Render a value with an arbitrary format code.
-    local format_code(val, code, fw, prec_or_null, i) =
-      local cflags = code.cflags;
-      local fpprec = if prec_or_null != null then prec_or_null else 6;
-      local iprec = if prec_or_null != null then prec_or_null else 0;
-      local zp = if cflags.zero && !cflags.left then fw else 0;
-      if code.ctype == 's' then
-        std.toString(val)
-      else if code.ctype == 'd' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          render_int(val, zp, iprec, cflags.blank, cflags.sign, 10, '')
-      else if code.ctype == 'o' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          local zero_prefix = if cflags.alt then '0' else '';
-          render_int(val, zp, iprec, cflags.blank, cflags.sign, 8, zero_prefix)
-      else if code.ctype == 'x' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          render_hex(val,
-                     zp,
-                     iprec,
-                     cflags.blank,
-                     cflags.sign,
-                     cflags.alt,
-                     code.caps)
-      else if code.ctype == 'f' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          render_float_dec(val,
-                           zp,
-                           cflags.blank,
-                           cflags.sign,
-                           cflags.alt,
-                           true,
-                           fpprec)
-      else if code.ctype == 'e' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          render_float_sci(val,
-                           zp,
-                           cflags.blank,
-                           cflags.sign,
-                           cflags.alt,
-                           true,
-                           code.caps,
-                           fpprec)
-      else if code.ctype == 'g' then
-        if std.type(val) != 'number' then
-          error 'Format required number at '
-                + i + ', got ' + std.type(val)
-        else
-          local exponent = std.floor(std.log(std.abs(val)) / std.log(10));
-          if exponent < -4 || exponent >= fpprec then
-            render_float_sci(val,
-                             zp,
-                             cflags.blank,
-                             cflags.sign,
-                             cflags.alt,
-                             cflags.alt,
-                             code.caps,
-                             fpprec - 1)
-          else
-            local digits_before_pt = std.max(1, exponent + 1);
-            render_float_dec(val,
-                             zp,
-                             cflags.blank,
-                             cflags.sign,
-                             cflags.alt,
-                             cflags.alt,
-                             fpprec - digits_before_pt)
-      else if code.ctype == 'c' then
-        if std.type(val) == 'number' then
-          std.char(val)
-        else if std.type(val) == 'string' then
-          if std.length(val) == 1 then
-            val
-          else
-            error '%c expected 1-sized string got: ' + std.length(val)
-        else
-          error '%c expected number / string, got: ' + std.type(val)
-      else
-        error 'Unknown code: ' + code.ctype;
+  format:: $intrinsic(format),
 
-    // Render a parsed format string with an array of values.
-    local format_codes_arr(codes, arr, i, j, v) =
-      if i >= std.length(codes) then
-        if j < std.length(arr) then
-          error ('Too many values to format: ' + std.length(arr) + ', expected ' + j)
-        else
-          v
-      else
-        local code = codes[i];
-        if std.type(code) == 'string' then
-          format_codes_arr(codes, arr, i + 1, j, v + code) tailstrict
-        else
-          local tmp = if code.fw == '*' then {
-            j: j + 1,
-            fw: if j >= std.length(arr) then
-              error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + j)
-            else
-              arr[j],
-          } else {
-            j: j,
-            fw: code.fw,
-          };
-          local tmp2 = if code.prec == '*' then {
-            j: tmp.j + 1,
-            prec: if tmp.j >= std.length(arr) then
-              error ('Not enough values to format: ' + std.length(arr) + ', expected at least ' + tmp.j)
-            else
-              arr[tmp.j],
-          } else {
-            j: tmp.j,
-            prec: code.prec,
-          };
-          local j2 = tmp2.j;
-          local val =
-            if j2 < std.length(arr) then
-              arr[j2]
-            else
-              error ('Not enough values to format: ' + std.length(arr) + ', expected more than ' + j2);
-          local s =
-            if code.ctype == '%' then
-              '%'
-            else
-              format_code(val, code, tmp.fw, tmp2.prec, j2);
-          local s_padded =
-            if code.cflags.left then
-              pad_right(s, tmp.fw, ' ')
-            else
-              pad_left(s, tmp.fw, ' ');
-          local j3 =
-            if code.ctype == '%' then
-              j2
-            else
-              j2 + 1;
-          format_codes_arr(codes, arr, i + 1, j3, v + s_padded) tailstrict;
+  foldr:: $intrinsic(foldr),
 
-    // Render a parsed format string with an object of values.
-    local format_codes_obj(codes, obj, i, v) =
-      if i >= std.length(codes) then
-        v
-      else
-        local code = codes[i];
-        if std.type(code) == 'string' then
-          format_codes_obj(codes, obj, i + 1, v + code) tailstrict
-        else
-          local f =
-            if code.mkey == null then
-              error 'Mapping keys required.'
-            else
-              code.mkey;
-          local fw =
-            if code.fw == '*' then
-              error 'Cannot use * field width with object.'
-            else
-              code.fw;
-          local prec =
-            if code.prec == '*' then
-              error 'Cannot use * precision with object.'
-            else
-              code.prec;
-          local val =
-            if std.objectHasAll(obj, f) then
-              obj[f]
-            else
-              error 'No such field: ' + f;
-          local s =
-            if code.ctype == '%' then
-              '%'
-            else
-              format_code(val, code, fw, prec, f);
-          local s_padded =
-            if code.cflags.left then
-              pad_right(s, fw, ' ')
-            else
-              pad_left(s, fw, ' ');
-          format_codes_obj(codes, obj, i + 1, v + s_padded) tailstrict;
-
-    if std.isArray(vals) then
-      format_codes_arr(codes, vals, 0, 0, '')
-    else if std.isObject(vals) then
-      format_codes_obj(codes, vals, 0, '')
-    else
-      format_codes_arr(codes, [vals], 0, 0, ''),
-
-  foldr(func, arr, init)::
-    local aux(func, arr, running, idx) =
-      if idx < 0 then
-        running
-      else
-        aux(func, arr, func(arr[idx], running), idx - 1) tailstrict;
-    aux(func, arr, init, std.length(arr) - 1),
+  foldl:: $intrinsic(foldl),
 
-  foldl(func, arr, init)::
-    local aux(func, arr, running, idx) =
-      if idx >= std.length(arr) then
-        running
-      else
-        aux(func, arr, func(running, arr[idx]), idx + 1) tailstrict;
-    aux(func, arr, init, 0),
-
-
   filterMap(filter_func, map_func, arr)::
     if !std.isFunction(filter_func) then
       error ('std.filterMap first param must be function, got ' + std.type(filter_func))
@@ -912,30 +375,7 @@
     else
       error 'TOML body must be an object. Got ' + std.type(value),
 
-  escapeStringJson(str_)::
-    local str = std.toString(str_);
-    local trans(ch) =
-      if ch == '"' then
-        '\\"'
-      else if ch == '\\' then
-        '\\\\'
-      else if ch == '\b' then
-        '\\b'
-      else if ch == '\f' then
-        '\\f'
-      else if ch == '\n' then
-        '\\n'
-      else if ch == '\r' then
-        '\\r'
-      else if ch == '\t' then
-        '\\t'
-      else
-        local cp = std.codepoint(ch);
-        if cp < 32 || (cp >= 127 && cp <= 159) then
-          '\\u%04x' % [cp]
-        else
-          ch;
-    '"%s"' % std.join('', [trans(ch) for ch in std.stringChars(str)]),
+  escapeStringJson:: $intrinsic(escapeStringJson),
 
   escapeStringPython(str)::
     std.escapeStringJson(str),
@@ -960,42 +400,7 @@
 
   manifestJson(value):: std.manifestJsonEx(value, '    '),
 
-  manifestJsonEx(value, indent)::
-    local aux(v, path, cindent) =
-      if v == true then
-        'true'
-      else if v == false then
-        'false'
-      else if v == null then
-        'null'
-      else if std.isNumber(v) then
-        '' + v
-      else if std.isString(v) then
-        std.escapeStringJson(v)
-      else if std.isFunction(v) then
-        error 'Tried to manifest function at ' + path
-      else if std.isArray(v) then
-        local range = std.range(0, std.length(v) - 1);
-        local new_indent = cindent + indent;
-        local lines = ['[\n']
-                      + std.join([',\n'],
-                                 [
-                                   [new_indent + aux(v[i], path + [i], new_indent)]
-                                   for i in range
-                                 ])
-                      + ['\n' + cindent + ']'];
-        std.join('', lines)
-      else if std.isObject(v) then
-        local lines = ['{\n']
-                      + std.join([',\n'],
-                                 [
-                                   [cindent + indent + std.escapeStringJson(k) + ': '
-                                    + aux(v[k], path + [k], cindent + indent)]
-                                   for k in std.objectFields(v)
-                                 ])
-                      + ['\n' + cindent + '}'];
-        std.join('', lines);
-    aux(value, [], ''),
+  manifestJsonEx:: $intrinsic(manifestJsonEx),
 
   manifestYamlDoc(value, indent_array_in_object=false)::
     local aux(v, path, cindent) =
@@ -1136,52 +541,7 @@
   local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
   local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },
 
-  base64(input)::
-    local bytes =
-      if std.isString(input) then
-        std.map(function(c) std.codepoint(c), input)
-      else
-        input;
-
-    local aux(arr, i, r) =
-      if i >= std.length(arr) then
-        r
-      else if i + 1 >= std.length(arr) then
-        local str =
-          // 6 MSB of i
-          base64_table[(arr[i] & 252) >> 2] +
-          // 2 LSB of i
-          base64_table[(arr[i] & 3) << 4] +
-          '==';
-        aux(arr, i + 3, r + str) tailstrict
-      else if i + 2 >= std.length(arr) then
-        local str =
-          // 6 MSB of i
-          base64_table[(arr[i] & 252) >> 2] +
-          // 2 LSB of i, 4 MSB of i+1
-          base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
-          // 4 LSB of i+1
-          base64_table[(arr[i + 1] & 15) << 2] +
-          '=';
-        aux(arr, i + 3, r + str) tailstrict
-      else
-        local str =
-          // 6 MSB of i
-          base64_table[(arr[i] & 252) >> 2] +
-          // 2 LSB of i, 4 MSB of i+1
-          base64_table[(arr[i] & 3) << 4 | (arr[i + 1] & 240) >> 4] +
-          // 4 LSB of i+1, 2 MSB of i+2
-          base64_table[(arr[i + 1] & 15) << 2 | (arr[i + 2] & 192) >> 6] +
-          // 6 LSB of i+2
-          base64_table[(arr[i + 2] & 63)];
-        aux(arr, i + 3, r + str) tailstrict;
-
-    local sanity = std.foldl(function(r, a) r && (a < 256), bytes, true);
-    if !sanity then
-      error 'Can only base64 encode strings / arrays of single bytes.'
-    else
-      aux(bytes, 0, ''),
-
+  base64:: $intrinsic(base64),
 
   base64DecodeBytes(str)::
     if std.length(str) % 4 != 0 then
@@ -1207,47 +567,11 @@
   base64Decode(str)::
     local bytes = std.base64DecodeBytes(str);
     std.join('', std.map(function(b) std.char(b), bytes)),
-
-  reverse(arr)::
-    local l = std.length(arr);
-    std.makeArray(l, function(i) arr[l - i - 1]),
 
-  // Merge-sort for long arrays and naive quicksort for shorter ones
-  sortImpl(arr, keyF)::
-    local quickSort(arr, keyF=id) =
-      local l = std.length(arr);
-      if std.length(arr) <= 1 then
-        arr
-      else
-        local pos = 0;
-        local pivot = keyF(arr[pos]);
-        local rest = std.makeArray(l - 1, function(i) if i < pos then arr[i] else arr[i + 1]);
-        local left = std.filter(function(x) keyF(x) < pivot, rest);
-        local right = std.filter(function(x) keyF(x) >= pivot, rest);
-        quickSort(left, keyF) + [arr[pos]] + quickSort(right, keyF);
+  reverse:: $intrinsic(reverse),
 
-    local merge(a, b) =
-      local la = std.length(a), lb = std.length(b);
-      local aux(i, j, prefix) =
-        if i == la then
-          prefix + b[j:]
-        else if j == lb then
-          prefix + a[i:]
-        else
-          if keyF(a[i]) <= keyF(b[j]) then
-            aux(i + 1, j, prefix + [a[i]]) tailstrict
-          else
-            aux(i, j + 1, prefix + [b[j]]) tailstrict;
-      aux(0, 0, []);
+  sortImpl:: $intrinsic(sortImpl),
 
-    local l = std.length(arr);
-    if std.length(arr) <= 30 then
-      quickSort(arr, keyF=keyF)
-    else
-      local mid = std.floor(l / 2);
-      local left = arr[:mid], right = arr[mid:];
-      merge(std.sort(left, keyF=keyF), std.sort(right, keyF=keyF)),
-
   sort(arr, keyF=id)::
     std.sortImpl(arr, keyF),
 
@@ -1356,42 +680,7 @@
   objectValuesAll(o)::
     [o[k] for k in std.objectFieldsAll(o)],
 
-  equals(a, b)::
-    local ta = std.type(a);
-    local tb = std.type(b);
-    if !std.primitiveEquals(ta, tb) then
-      false
-    else
-      if std.primitiveEquals(ta, 'array') then
-        local la = std.length(a);
-        if !std.primitiveEquals(la, std.length(b)) then
-          false
-        else
-          local aux(a, b, i) =
-            if i >= la then
-              true
-            else if a[i] != b[i] then
-              false
-            else
-              aux(a, b, i + 1) tailstrict;
-          aux(a, b, 0)
-      else if std.primitiveEquals(ta, 'object') then
-        local fields = std.objectFields(a);
-        local lfields = std.length(fields);
-        if fields != std.objectFields(b) then
-          false
-        else
-          local aux(a, b, i) =
-            if i >= lfields then
-              true
-            else if local f = fields[i]; a[f] != b[f] then
-              false
-            else
-              aux(a, b, i + 1) tailstrict;
-          aux(a, b, 0)
-      else
-        std.primitiveEquals(a, b),
-
+  equals:: $intrinsic(equals),
 
   resolvePath(f, r)::
     local arr = std.split(f, '/');