difftreelog
feat(evaluator) ArrComp support
in: master
7 files changed
crates/jsonnet-evaluator/src/ctx.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/ctx.rs
+++ b/crates/jsonnet-evaluator/src/ctx.rs
@@ -1,4 +1,7 @@
-use crate::{future_wrapper, rc_fn_helper, LazyBinding, LazyVal, ObjValue};
+use crate::{
+ future_wrapper, lazy_binding, lazy_val, rc_fn_helper, LazyBinding, LazyVal, ObjValue, Val,
+};
+use closure::closure;
use std::{cell::RefCell, collections::HashMap, fmt::Debug, rc::Rc};
rc_fn_helper!(
@@ -62,6 +65,17 @@
ctx.unwrap()
}
+ pub fn with_var(&self, name: String, value: Val) -> Context {
+ let mut new_bindings: HashMap<_, LazyBinding> = HashMap::new();
+ new_bindings.insert(
+ name,
+ lazy_binding!(
+ closure!(clone value, |_t, _s|lazy_val!(closure!(clone value, ||value.clone())))
+ ),
+ );
+ self.extend(new_bindings, None, None, None)
+ }
+
pub fn extend(
&self,
new_bindings: HashMap<String, LazyBinding>,
crates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth1use crate::{2 binding, bool_val, context_creator, function_default, function_rhs, future_wrapper,3 lazy_binding, lazy_val, Context, ContextCreator, EvaluationState, FuncDesc, LazyBinding,4 ObjMember, ObjValue, Val,5};6use closure::closure;7use jsonnet_parser::{8 ArgsDesc, BinaryOpType, BindSpec, Expr, FieldMember, LiteralType, LocExpr, Member, ObjBody,9 ParamsDesc, UnaryOpType, Visibility,10};11use std::{12 collections::{BTreeMap, HashMap},13 rc::Rc,14};1516pub fn evaluate_binding(17 eval_state: EvaluationState,18 b: &BindSpec,19 context_creator: ContextCreator,20) -> (String, LazyBinding) {21 let b = b.clone();22 if let Some(args) = &b.params {23 let args = args.clone();24 (25 b.name.clone(),26 lazy_binding!(move |this, super_obj| lazy_val!(27 closure!(clone b, clone args, clone context_creator, clone eval_state, || evaluate_method(28 context_creator.0(this.clone(), super_obj.clone()),29 eval_state.clone(),30 &b.value,31 args.clone()32 ))33 )),34 )35 } else {36 (37 b.name.clone(),38 lazy_binding!(move |this, super_obj| {39 lazy_val!(40 closure!(clone context_creator, clone b, clone eval_state, || evaluate(41 context_creator.0(this.clone(), super_obj.clone()),42 eval_state.clone(),43 &b.value44 ))45 )46 }),47 )48 }49}5051pub fn evaluate_method(52 ctx: Context,53 eval_state: EvaluationState,54 expr: &LocExpr,55 arg_spec: ParamsDesc,56) -> Val {57 Val::Func(FuncDesc {58 ctx,59 params: arg_spec,60 eval_rhs: function_rhs!(61 closure!(clone expr, clone eval_state, |ctx| evaluate(ctx, eval_state.clone(), &expr))62 ),63 eval_default: function_default!(64 closure!(clone eval_state, |ctx, default| evaluate(ctx, eval_state.clone(), &default))65 ),66 })67}6869pub fn evaluate_field_name(70 context: Context,71 eval_state: EvaluationState,72 field_name: &jsonnet_parser::FieldName,73) -> String {74 match field_name {75 jsonnet_parser::FieldName::Fixed(n) => n.clone(),76 jsonnet_parser::FieldName::Dyn(expr) => {77 let name = evaluate(context, eval_state, expr).unwrap_if_lazy();78 match name {79 Val::Str(n) => n,80 _ => panic!(81 "dynamic field name can be only evaluated to 'string', got: {:?}",82 name83 ),84 }85 }86 }87}8889pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Val {90 match (op, b) {91 (o, Val::Lazy(l)) => evaluate_unary_op(o, &l.evaluate()),92 (UnaryOpType::Not, Val::Bool(v)) => Val::Bool(!v),93 (op, o) => panic!("unary op not implemented: {:?} {:?}", op, o),94 }95}9697pub fn evaluate_binary_op(a: &Val, op: BinaryOpType, b: &Val) -> Val {98 match (a, op, b) {99 (Val::Lazy(a), o, b) => evaluate_binary_op(&a.evaluate(), o, b),100 (a, o, Val::Lazy(b)) => evaluate_binary_op(a, o, &b.evaluate()),101102 (Val::Str(v1), BinaryOpType::Add, Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),103 (Val::Str(v1), BinaryOpType::Ne, Val::Str(v2)) => bool_val(v1 != v2),104105 (Val::Str(v1), BinaryOpType::Add, Val::Num(v2)) => Val::Str(format!("{}{}", v1, v2)),106 (Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize)),107108 (Val::Bool(a), BinaryOpType::And, Val::Bool(b)) => Val::Bool(*a && *b),109 (Val::Bool(a), BinaryOpType::Or, Val::Bool(b)) => Val::Bool(*a || *b),110111 (Val::Obj(v1), BinaryOpType::Add, Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),112113 (Val::Arr(a), BinaryOpType::Add, Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),114115 (Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Num(v1 * v2),116 (Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => Val::Num(v1 / v2),117 (Val::Num(v1), BinaryOpType::Mod, Val::Num(v2)) => Val::Num(v1 % v2),118119 (Val::Num(v1), BinaryOpType::Add, Val::Num(v2)) => Val::Num(v1 + v2),120 (Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::Num(v1 - v2),121122 (Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {123 Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)124 }125 (Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {126 Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)127 }128129 (Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => bool_val(v1 < v2),130 (Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => bool_val(v1 > v2),131 (Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => bool_val(v1 <= v2),132 (Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => bool_val(v1 >= v2),133134 (Val::Num(v1), BinaryOpType::Eq, Val::Num(v2)) => bool_val((v1 - v2).abs() < f64::EPSILON),135 (Val::Num(v1), BinaryOpType::Ne, Val::Num(v2)) => bool_val((v1 - v2).abs() > f64::EPSILON),136137 (Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {138 Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)139 }140 (Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {141 Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)142 }143 (Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {144 Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)145 }146 (a, BinaryOpType::Eq, b) => bool_val(a == b),147 (a, BinaryOpType::Ne, b) => bool_val(a != b),148 _ => panic!("no rules for binary operation: {:?} {:?} {:?}", a, op, b),149 }150}151152future_wrapper!(HashMap<String, LazyBinding>, FutureNewBindings);153future_wrapper!(ObjValue, FutureObjValue);154155// TODO: Asserts156pub fn evaluate_object(context: Context, eval_state: EvaluationState, object: ObjBody) -> ObjValue {157 match object {158 ObjBody::MemberList(members) => {159 let new_bindings = FutureNewBindings::new();160 let future_this = FutureObjValue::new();161 let context_creator = context_creator!(162 closure!(clone context, clone new_bindings, clone future_this, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {163 context.clone().extend(164 new_bindings.clone().unwrap(),165 context.clone().dollar().clone().or_else(||this.clone()),166 Some(this.unwrap()),167 super_obj168 )169 })170 );171 {172 let mut bindings: HashMap<String, LazyBinding> = HashMap::new();173 for (n, b) in members174 .iter()175 .filter_map(|m| match m {176 Member::BindStmt(b) => Some(b.clone()),177 _ => None,178 })179 .map(|b| evaluate_binding(eval_state.clone(), &b, context_creator.clone()))180 {181 bindings.insert(n, b);182 }183 new_bindings.fill(bindings);184 }185186 let mut new_members = BTreeMap::new();187 for member in members.into_iter() {188 match member {189 Member::Field(FieldMember {190 name,191 plus,192 params: None,193 visibility,194 value,195 }) => {196 let name = evaluate_field_name(context.clone(), eval_state.clone(), &name);197 new_members.insert(198 name,199 ObjMember {200 add: plus,201 visibility: visibility.clone(),202 invoke: binding!(203 closure!(clone value, clone context_creator, clone eval_state, |this, super_obj| {204 let context = context_creator.0(this, super_obj);205 // TODO: Assert206 evaluate(207 context,208 eval_state.clone(),209 &value,210 ).unwrap_if_lazy()211 })212 ),213 },214 );215 }216 Member::Field(FieldMember {217 name,218 params: Some(params),219 value,220 ..221 }) => {222 let name = evaluate_field_name(context.clone(), eval_state.clone(), &name);223 new_members.insert(224 name,225 ObjMember {226 add: false,227 visibility: Visibility::Hidden,228 invoke: binding!(229 closure!(clone value, clone context_creator, clone eval_state, |this, super_obj| {230 // TODO: Assert231 evaluate_method(232 context_creator.0(this, super_obj),233 eval_state.clone(),234 &value.clone(),235 params.clone(),236 )237 })238 ),239 },240 );241 }242 Member::BindStmt(_) => {}243 Member::AssertStmt(_) => {}244 }245 }246 future_this.fill(ObjValue::new(None, Rc::new(new_members)))247 }248 _ => todo!(),249 }250}251252pub fn evaluate(context: Context, eval_state: EvaluationState, expr: &LocExpr) -> Val {253 use Expr::*;254 eval_state.clone().push(expr.clone(), "expr".to_owned(), || {255 let LocExpr(expr, loc) = expr;256 match &**expr {257 Literal(LiteralType::This) => Val::Obj(258 context259 .this()260 .clone()261 .unwrap_or_else(|| panic!("this not found")),262 ),263 Literal(LiteralType::Super) => Val::Obj(264 context265 .super_obj()266 .clone()267 .unwrap_or_else(|| panic!("super not found")),268 ),269 Literal(LiteralType::True) => Val::Bool(true),270 Literal(LiteralType::False) => Val::Bool(false),271 Literal(LiteralType::Null) => Val::Null,272 Parened(e) => evaluate(context, eval_state.clone(), e),273 Str(v) => Val::Str(v.clone()),274 Num(v) => Val::Num(*v),275 BinaryOp(v1, o, v2) => evaluate_binary_op(276 &evaluate(context.clone(), eval_state.clone(), v1),277 *o,278 &evaluate(context, eval_state.clone(), v2),279 ),280 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, eval_state, v)),281 Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy(),282 Index(value, index) => {283 match (284 evaluate(context.clone(), eval_state.clone(), value).unwrap_if_lazy(),285 evaluate(context.clone(), eval_state.clone(), index),286 ) {287 (Val::Obj(v), Val::Str(s)) => v288 .get(&s)289 .unwrap_or_else(closure!(clone context, || {290 if let Some(n) = v.get("__intristic_namespace__") {291 if let Val::Str(n) = n.unwrap_if_lazy() {292 Val::Intristic(n, s)293 } else {294 panic!("__intristic_namespace__ should be string");295 }296 } else {297 panic!("{} not found in {:?}", s, v)298 }299 }))300 .unwrap_if_lazy(),301 (Val::Arr(v), Val::Num(n)) => v302 .get(n as usize)303 .unwrap_or_else(|| panic!("out of bounds"))304 .clone(),305 (Val::Str(s), Val::Num(n)) => {306 Val::Str(s.chars().skip(n as usize).take(1).collect())307 }308 (v, i) => todo!("not implemented: {:?}[{:?}]", v, i.unwrap_if_lazy()),309 }310 }311 LocalExpr(bindings, returned) => {312 let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();313 let future_context = Context::new_future();314315 let context_creator = context_creator!(316 closure!(clone future_context, |_, _| future_context.clone().unwrap())317 );318319 for (k, v) in bindings320 .iter()321 .map(|b| evaluate_binding(eval_state.clone(), b, context_creator.clone()))322 {323 new_bindings.insert(k, v);324 }325326 let context = context327 .extend(new_bindings, None, None, None)328 .into_future(future_context);329 evaluate(context, eval_state.clone(), &returned.clone())330 }331 Arr(items) => {332 let mut out = Vec::with_capacity(items.len());333 for item in items {334 out.push(evaluate(context.clone(), eval_state.clone(), item));335 }336 Val::Arr(out)337 }338 Obj(body) => Val::Obj(evaluate_object(context, eval_state, body.clone())),339 Apply(value, ArgsDesc(args)) => {340 let value = evaluate(context.clone(), eval_state.clone(), value).unwrap_if_lazy();341 match value {342 // TODO: Capture context of application343 Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {344 ("std", "length") => {345 assert_eq!(args.len(), 1);346 let expr = &args.get(0).unwrap().1;347 match evaluate(context, eval_state.clone(), expr) {348 Val::Str(n) => Val::Num(n.chars().count() as f64),349 Val::Arr(i) => Val::Num(i.len() as f64),350 v => panic!("can't get length of {:?}", v),351 }352 }353 ("std", "type") => {354 assert_eq!(args.len(), 1);355 let expr = &args.get(0).unwrap().1;356 Val::Str(evaluate(context, eval_state, expr).type_of().to_owned())357 }358 ("std", "makeArray") => {359 assert_eq!(args.len(), 2);360 if let (Val::Num(v), Val::Func(d)) = (361 evaluate(context.clone(), eval_state.clone(), &args[0].1),362 evaluate(context, eval_state, &args[1].1),363 ) {364 assert!(v > 0.0);365 let mut out = Vec::with_capacity(v as usize);366 for i in 0..v as usize {367 out.push(d.evaluate(vec![(None, Val::Num(i as f64))]))368 }369 Val::Arr(out)370 } else {371 panic!("bad makeArray call");372 }373 }374 ("std", "codepoint") => {375 assert_eq!(args.len(), 1);376 if let Val::Str(s) = evaluate(context, eval_state, &args[0].1) {377 assert!(378 s.chars().count() == 1,379 "std.codepoint should receive single char string"380 );381 Val::Num(s.chars().take(1).next().unwrap() as u32 as f64)382 } else {383 panic!("bad codepoint call");384 }385 }386 (ns, name) => panic!("Intristic not found: {}.{}", ns, name),387 },388 Val::Func(f) => f.evaluate(389 args.clone()390 .into_iter()391 .map(move |a| {392 (393 a.clone().0,394 Val::Lazy(lazy_val!(395 closure!(clone context, clone a, clone eval_state, || evaluate(context.clone(), eval_state.clone(), &a.clone().1))396 )),397 )398 })399 .collect(),400 ),401 _ => panic!("{:?} is not a function", value),402 }403 }404 Function(params, body) => evaluate_method(context, eval_state, body, params.clone()),405 Error(e) => panic!("error: {}", evaluate(context, eval_state, e)),406 IfElse {407 cond,408 cond_then,409 cond_else,410 } => match evaluate(context.clone(), eval_state.clone(), &cond.0).unwrap_if_lazy() {411 Val::Bool(true) => evaluate(context, eval_state.clone(), cond_then),412 Val::Bool(false) => match cond_else {413 Some(v) => evaluate(context, eval_state, v),414 None => Val::Bool(false),415 },416 v => panic!("if condition evaluated to {:?} (boolean needed instead)", v),417 },418 _ => panic!(419 "evaluation not implemented: {:?}",420 LocExpr(expr.clone(), loc.clone())421 ),422 }423 })424}crates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/lib.rs
+++ b/crates/jsonnet-evaluator/src/lib.rs
@@ -359,7 +359,12 @@
#[test]
fn json() {
- println!("{:?}", eval_stdlib!(r#"std.manifestJson({a:3, b:4, c:6})"#));
+ println!("{:?}", eval_stdlib!(r#"std.manifestJsonEx({a:3, b:4, c:6},"")"#));
+ }
+
+ #[test]
+ fn test() {
+ assert_json_stdlib!(r#"[[a, b] for a in [1,2,3] for b in [4,5,6]]"#, "");
}
#[test]
crates/jsonnet-evaluator/src/obj.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/obj.rs
+++ b/crates/jsonnet-evaluator/src/obj.rs
@@ -1,5 +1,5 @@
-use crate::{evaluate_binary_op, Binding, Val};
-use jsonnet_parser::{BinaryOpType, Visibility};
+use crate::{evaluate_add_op, Binding, Val};
+use jsonnet_parser::Visibility;
use std::{
cell::RefCell,
collections::{BTreeMap, BTreeSet, HashMap},
@@ -90,9 +90,8 @@
(Some(k), Some(s)) => {
let our = k.invoke.0(Some(real_this.clone()), self.0.super_obj.clone());
if k.add {
- s.get_raw(key, real_this).map_or(Some(our.clone()), |v| {
- Some(evaluate_binary_op(&v, BinaryOpType::Add, &our))
- })
+ s.get_raw(key, real_this)
+ .map_or(Some(our.clone()), |v| Some(evaluate_add_op(&v, &our)))
} else {
Some(our)
}
crates/jsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jsonnet-evaluator/src/val.rs
+++ b/crates/jsonnet-evaluator/src/val.rs
@@ -117,6 +117,18 @@
Intristic(String, String),
}
impl Val {
+ pub fn try_cast_bool(self) -> bool {
+ match self.unwrap_if_lazy() {
+ Val::Bool(v) => v,
+ v => panic!("expected bool, got {:?}", v),
+ }
+ }
+ pub fn try_cast_str(self) -> String {
+ match self.unwrap_if_lazy() {
+ Val::Str(v) => v,
+ v => panic!("expected bool, got {:?}", v),
+ }
+ }
pub fn unwrap_if_lazy(self) -> Self {
if let Val::Lazy(v) = self {
v.evaluate().unwrap_if_lazy()
crates/jsonnet-parser/src/expr.rsdiffbeforeafterboth--- a/crates/jsonnet-parser/src/expr.rs
+++ b/crates/jsonnet-parser/src/expr.rs
@@ -165,7 +165,7 @@
/// ]
/// ],
/// ```
- ArrComp(LocExpr, ForSpecData, Vec<CompSpec>),
+ ArrComp(LocExpr, Vec<CompSpec>),
/// Object: {a: 2}
Obj(ObjBody),
crates/jsonnet-parser/src/lib.rsdiffbeforeafterboth--- a/crates/jsonnet-parser/src/lib.rs
+++ b/crates/jsonnet-parser/src/lib.rs
@@ -125,7 +125,7 @@
pub rule string_expr(s: &ParserSettings) -> LocExpr = l(s, <s:string() {Expr::Str(s)}>)
pub rule obj_expr(s: &ParserSettings) -> LocExpr = l(s,<"{" _ body:objinside(s) _ "}" {Expr::Obj(body)}>)
pub rule array_expr(s: &ParserSettings) -> LocExpr = l(s,<"[" _ elems:(expr(s) ** comma()) _ comma()? "]" {Expr::Arr(elems)}>)
- pub rule array_comp_expr(s: &ParserSettings) -> LocExpr = l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {Expr::ArrComp(expr, forspec, others.unwrap_or_default())}>)
+ pub rule array_comp_expr(s: &ParserSettings) -> LocExpr = l(s,<"[" _ expr:expr(s) _ comma()? _ forspec:forspec(s) _ others:(others: compspec(s) _ {others})? "]" {Expr::ArrComp(expr, [vec![CompSpec::ForSpec(forspec)], others.unwrap_or_default()].concat())}>)
pub rule number_expr(s: &ParserSettings) -> LocExpr = l(s,<n:number() { expr::Expr::Num(n) }>)
pub rule var_expr(s: &ParserSettings) -> LocExpr = l(s,<n:id() { expr::Expr::Var(n) }>)
pub rule if_then_else_expr(s: &ParserSettings) -> LocExpr = l(s,<cond:ifspec(s) _ keyword("then") _ cond_then:expr(s) cond_else:(_ keyword("else") _ e:expr(s) {e})? {Expr::IfElse{
@@ -252,15 +252,18 @@
jsonnet_parser::jsonnet(str, settings)
}
+#[macro_export]
+macro_rules! el {
+ ($expr:expr) => {
+ LocExpr(std::rc::Rc::new($expr), None)
+ };
+}
+
#[cfg(test)]
pub mod tests {
use super::{expr::*, parse};
use crate::ParserSettings;
- macro_rules! el {
- ($expr:expr) => {
- LocExpr(std::rc::Rc::new($expr), None)
- };
- }
+
macro_rules! parse {
($s:expr) => {
parse(
@@ -390,8 +393,10 @@
)),
ArgsDesc(vec![Arg(None, el!(Var("x".to_owned())))])
)),
- ForSpecData("x".to_owned(), el!(Var("arr".to_owned()))),
- vec![]
+ vec![CompSpec::ForSpec(ForSpecData(
+ "x".to_owned(),
+ el!(Var("arr".to_owned()))
+ ))]
)),
)
}
@@ -403,24 +408,26 @@
parse!("[k for k in std.objectFields(patch) if patch[k] == null]"),
el!(ArrComp(
el!(Var("k".to_owned())),
- ForSpecData(
- "k".to_owned(),
- el!(Apply(
+ vec![
+ CompSpec::ForSpec(ForSpecData(
+ "k".to_owned(),
+ el!(Apply(
+ el!(Index(
+ el!(Var("std".to_owned())),
+ el!(Str("objectFields".to_owned()))
+ )),
+ ArgsDesc(vec![Arg(None, el!(Var("patch".to_owned())))])
+ ))
+ )),
+ CompSpec::IfSpec(IfSpecData(el!(BinaryOp(
el!(Index(
- el!(Var("std".to_owned())),
- el!(Str("objectFields".to_owned()))
+ el!(Var("patch".to_owned())),
+ el!(Var("k".to_owned()))
)),
- ArgsDesc(vec![Arg(None, el!(Var("patch".to_owned())))])
- ))
- ),
- vec![CompSpec::IfSpec(IfSpecData(el!(BinaryOp(
- el!(Index(
- el!(Var("patch".to_owned())),
- el!(Var("k".to_owned()))
- )),
- BinaryOpType::Eq,
- el!(Literal(LiteralType::Null))
- ))))]
+ BinaryOpType::Eq,
+ el!(Literal(LiteralType::Null))
+ ))))
+ ]
))
);
}