git.delta.rocks / jrsonnet / refs/commits / 8d448f754f46

difftreelog

feat(evaluator) propogate EvaluationState

Лач2020-06-01parent: #1450eba.patch.diff
in: master

4 files changed

addedcrates/jsonnet-evaluator/src/error.rsdiffbeforeafterboth

no changes

modifiedcrates/jsonnet-evaluator/src/evaluate.rsdiffbeforeafterboth
1use crate::{1use crate::{
2 binding, bool_val, context_creator, function_default, function_rhs, future_wrapper,2 binding, bool_val, context_creator, function_default, function_rhs, future_wrapper,
3 lazy_binding, lazy_val, Context, ContextCreator, FuncDesc, LazyBinding, ObjMember, ObjValue,3 lazy_binding, lazy_val, Context, ContextCreator, EvaluationState, FuncDesc, LazyBinding,
4 Val,4 ObjMember, ObjValue, Val,
5};5};
6use closure::closure;6use closure::closure;
14};14};
1515
16pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (String, LazyBinding) {16pub fn evaluate_binding(
17 eval_state: EvaluationState,
18 b: &BindSpec,
19 context_creator: ContextCreator,
20) -> (String, LazyBinding) {
17 let b = b.clone();21 let b = b.clone();
18 if let Some(args) = &b.params {22 if let Some(args) = &b.params {
19 let args = args.clone();23 let args = args.clone();
20 (24 (
21 b.name.clone(),25 b.name.clone(),
22 lazy_binding!(move |this, super_obj| lazy_val!(26 lazy_binding!(move |this, super_obj| lazy_val!(
23 closure!(clone b, clone args, clone context_creator, || evaluate_method(27 closure!(clone b, clone args, clone context_creator, clone eval_state, || evaluate_method(
24 context_creator.0(this.clone(), super_obj.clone()),28 context_creator.0(this.clone(), super_obj.clone()),
29 eval_state.clone(),
25 &b.value,30 &b.value,
26 args.clone()31 args.clone()
27 ))32 ))
32 b.name.clone(),37 b.name.clone(),
33 lazy_binding!(move |this, super_obj| {38 lazy_binding!(move |this, super_obj| {
34 lazy_val!(closure!(clone context_creator, clone b, || evaluate(39 lazy_val!(
40 closure!(clone context_creator, clone b, clone eval_state, || evaluate(
35 context_creator.0(this.clone(), super_obj.clone()),41 context_creator.0(this.clone(), super_obj.clone()),
42 eval_state.clone(),
36 &b.value43 &b.value
37 )))44 ))
45 )
4250
43pub fn evaluate_method(ctx: Context, expr: &LocExpr, arg_spec: ParamsDesc) -> Val {51pub fn evaluate_method(
52 ctx: Context,
53 eval_state: EvaluationState,
54 expr: &LocExpr,
55 arg_spec: ParamsDesc,
56) -> Val {
44 Val::Func(FuncDesc {57 Val::Func(FuncDesc {
45 ctx,58 ctx,
46 params: arg_spec,59 params: arg_spec,
47 eval_rhs: function_rhs!(closure!(clone expr, |ctx| evaluate(ctx, &expr))),60 eval_rhs: function_rhs!(
61 closure!(clone expr, clone eval_state, |ctx| evaluate(ctx, eval_state.clone(), &expr))
62 ),
48 eval_default: function_default!(|ctx, default| evaluate(ctx, &default)),63 eval_default: function_default!(
64 closure!(clone eval_state, |ctx, default| evaluate(ctx, eval_state.clone(), &default))
65 ),
49 })66 })
50}67}
5168
52pub fn evaluate_field_name(context: Context, field_name: &jsonnet_parser::FieldName) -> String {69pub fn evaluate_field_name(
70 context: Context,
71 eval_state: EvaluationState,
72 field_name: &jsonnet_parser::FieldName,
73) -> String {
53 match field_name {74 match field_name {
54 jsonnet_parser::FieldName::Fixed(n) => n.clone(),75 jsonnet_parser::FieldName::Fixed(n) => n.clone(),
55 jsonnet_parser::FieldName::Dyn(expr) => {76 jsonnet_parser::FieldName::Dyn(expr) => {
56 let name = evaluate(context, expr).unwrap_if_lazy();77 let name = evaluate(context, eval_state, expr).unwrap_if_lazy();
57 match name {78 match name {
58 Val::Str(n) => n,79 Val::Str(n) => n,
59 _ => panic!(80 _ => panic!(
132future_wrapper!(ObjValue, FutureObjValue);153future_wrapper!(ObjValue, FutureObjValue);
133154
134// TODO: Asserts155// TODO: Asserts
135pub fn evaluate_object(context: Context, object: ObjBody) -> ObjValue {156pub fn evaluate_object(context: Context, eval_state: EvaluationState, object: ObjBody) -> ObjValue {
136 match object {157 match object {
137 ObjBody::MemberList(members) => {158 ObjBody::MemberList(members) => {
138 let new_bindings = FutureNewBindings::new();159 let new_bindings = FutureNewBindings::new();
155 Member::BindStmt(b) => Some(b.clone()),176 Member::BindStmt(b) => Some(b.clone()),
156 _ => None,177 _ => None,
157 })178 })
158 .map(|b| evaluate_binding(&b, context_creator.clone()))179 .map(|b| evaluate_binding(eval_state.clone(), &b, context_creator.clone()))
159 {180 {
160 bindings.insert(n, b);181 bindings.insert(n, b);
161 }182 }
172 visibility,193 visibility,
173 value,194 value,
174 }) => {195 }) => {
175 let name = evaluate_field_name(context.clone(), &name);196 let name = evaluate_field_name(context.clone(), eval_state.clone(), &name);
176 new_members.insert(197 new_members.insert(
177 name,198 name,
178 ObjMember {199 ObjMember {
179 add: plus,200 add: plus,
180 visibility: visibility.clone(),201 visibility: visibility.clone(),
181 invoke: binding!(202 invoke: binding!(
182 closure!(clone value, clone context_creator, |this, super_obj| {203 closure!(clone value, clone context_creator, clone eval_state, |this, super_obj| {
183 let context = context_creator.0(this, super_obj);204 let context = context_creator.0(this, super_obj);
184 // TODO: Assert205 // TODO: Assert
185 evaluate(206 evaluate(
186 context,207 context,
208 eval_state.clone(),
187 &value,209 &value,
188 ).unwrap_if_lazy()210 ).unwrap_if_lazy()
189 })211 })
197 value,219 value,
198 ..220 ..
199 }) => {221 }) => {
200 let name = evaluate_field_name(context.clone(), &name);222 let name = evaluate_field_name(context.clone(), eval_state.clone(), &name);
201 new_members.insert(223 new_members.insert(
202 name,224 name,
203 ObjMember {225 ObjMember {
204 add: false,226 add: false,
205 visibility: Visibility::Hidden,227 visibility: Visibility::Hidden,
206 invoke: binding!(228 invoke: binding!(
207 closure!(clone value, clone context_creator, |this, super_obj| {229 closure!(clone value, clone context_creator, clone eval_state, |this, super_obj| {
208 // TODO: Assert230 // TODO: Assert
209 evaluate_method(231 evaluate_method(
210 context_creator.0(this, super_obj),232 context_creator.0(this, super_obj),
233 eval_state.clone(),
211 &value.clone(),234 &value.clone(),
212 params.clone(),235 params.clone(),
213 )236 )
226 }249 }
227}250}
228251
229pub fn evaluate(context: Context, expr: &LocExpr) -> Val {252pub fn evaluate(context: Context, eval_state: EvaluationState, expr: &LocExpr) -> Val {
253 println!("===");
254 eval_state.print_stack_trace();
230 use Expr::*;255 use Expr::*;
256 eval_state.clone().push(expr.clone(), "expr".to_owned(), || {
231 let LocExpr(expr, loc) = expr;257 let LocExpr(expr, loc) = expr;
232 match &**expr {258 match &**expr {
233 Literal(LiteralType::This) => Val::Obj(259 Literal(LiteralType::This) => Val::Obj(
245 Literal(LiteralType::True) => Val::Bool(true),271 Literal(LiteralType::True) => Val::Bool(true),
246 Literal(LiteralType::False) => Val::Bool(false),272 Literal(LiteralType::False) => Val::Bool(false),
247 Literal(LiteralType::Null) => Val::Null,273 Literal(LiteralType::Null) => Val::Null,
248 Parened(e) => evaluate(context, e),274 Parened(e) => evaluate(context, eval_state.clone(), e),
249 Str(v) => Val::Str(v.clone()),275 Str(v) => Val::Str(v.clone()),
250 Num(v) => Val::Num(*v),276 Num(v) => Val::Num(*v),
251 BinaryOp(v1, o, v2) => {277 BinaryOp(v1, o, v2) => evaluate_binary_op(
252 evaluate_binary_op(&evaluate(context.clone(), v1), *o, &evaluate(context, v2))278 &evaluate(context.clone(), eval_state.clone(), v1),
253 }279 *o,
280 &evaluate(context, eval_state.clone(), v2),
281 ),
254 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)),282 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, eval_state, v)),
255 Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy(),283 Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy(),
256 Index(value, index) => {284 Index(value, index) => {
257 match (285 match (
258 evaluate(context.clone(), value).unwrap_if_lazy(),286 evaluate(context.clone(), eval_state.clone(), value).unwrap_if_lazy(),
259 evaluate(context.clone(), index),287 evaluate(context.clone(), eval_state.clone(), index),
260 ) {288 ) {
261 (Val::Obj(v), Val::Str(s)) => v289 (Val::Obj(v), Val::Str(s)) => v
262 .get(&s)290 .get(&s)
292320
293 for (k, v) in bindings321 for (k, v) in bindings
294 .iter()322 .iter()
295 .map(move |b| evaluate_binding(b, context_creator.clone()))323 .map(|b| evaluate_binding(eval_state.clone(), b, context_creator.clone()))
296 {324 {
297 new_bindings.insert(k, v);325 new_bindings.insert(k, v);
298 }326 }
299327
300 let context = context328 let context = context
301 .extend(new_bindings, None, None, None)329 .extend(new_bindings, None, None, None)
302 .into_future(future_context);330 .into_future(future_context);
303 evaluate(context, &returned.clone())331 evaluate(context, eval_state.clone(), &returned.clone())
304 }332 }
305 Obj(body) => Val::Obj(evaluate_object(context, body.clone())),333 Obj(body) => Val::Obj(evaluate_object(context, eval_state, body.clone())),
306 Apply(value, ArgsDesc(args)) => {334 Apply(value, ArgsDesc(args)) => {
307 let value = evaluate(context.clone(), value).unwrap_if_lazy();335 let value = evaluate(context.clone(), eval_state.clone(), value).unwrap_if_lazy();
308 match value {336 match value {
309 // TODO: Capture context of application337 // TODO: Capture context of application
310 Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {338 Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {
311 ("std", "length") => {339 ("std", "length") => {
312 assert_eq!(args.len(), 1);340 assert_eq!(args.len(), 1);
313 let expr = &args.get(0).unwrap().1;341 let expr = &args.get(0).unwrap().1;
314 match evaluate(context, expr) {342 match evaluate(context, eval_state.clone(), expr) {
315 Val::Str(n) => Val::Num(n.chars().count() as f64),343 Val::Str(n) => Val::Num(n.chars().count() as f64),
316 Val::Arr(i) => Val::Num(i.len() as f64),344 Val::Arr(i) => Val::Num(i.len() as f64),
317 v => panic!("can't get length of {:?}", v),345 v => panic!("can't get length of {:?}", v),
320 ("std", "type") => {348 ("std", "type") => {
321 assert_eq!(args.len(), 1);349 assert_eq!(args.len(), 1);
322 let expr = &args.get(0).unwrap().1;350 let expr = &args.get(0).unwrap().1;
323 Val::Str(evaluate(context, expr).type_of().to_owned())351 Val::Str(evaluate(context, eval_state, expr).type_of().to_owned())
324 }352 }
325 ("std", "makeArray") => {353 ("std", "makeArray") => {
326 assert_eq!(args.len(), 2);354 assert_eq!(args.len(), 2);
327 if let (Val::Num(v), Val::Func(d)) = (355 if let (Val::Num(v), Val::Func(d)) = (
328 evaluate(context.clone(), &args[0].1),356 evaluate(context.clone(), eval_state.clone(), &args[0].1),
329 evaluate(context, &args[1].1),357 evaluate(context, eval_state, &args[1].1),
330 ) {358 ) {
331 assert!(v > 0.0);359 assert!(v > 0.0);
332 let mut out = Vec::with_capacity(v as usize);360 let mut out = Vec::with_capacity(v as usize);
340 }368 }
341 ("std", "codepoint") => {369 ("std", "codepoint") => {
342 assert_eq!(args.len(), 1);370 assert_eq!(args.len(), 1);
343 if let Val::Str(s) = evaluate(context, &args[0].1) {371 if let Val::Str(s) = evaluate(context, eval_state, &args[0].1) {
344 assert!(372 assert!(
345 s.chars().count() == 1,373 s.chars().count() == 1,
346 "std.codepoint should receive single char string"374 "std.codepoint should receive single char string"
355 Val::Func(f) => f.evaluate(383 Val::Func(f) => f.evaluate(
356 args.clone()384 args.clone()
357 .into_iter()385 .into_iter()
358 .map(|a| {386 .map(move |a| {
359 (387 (
360 a.clone().0,388 a.clone().0,
361 Val::Lazy(lazy_val!(389 Val::Lazy(lazy_val!(
362 closure!(clone context, clone a, || evaluate(context.clone(), &a.clone().1))390 closure!(clone context, clone a, clone eval_state, || evaluate(context.clone(), eval_state.clone(), &a.clone().1))
363 )),391 )),
364 )392 )
365 })393 })
368 _ => panic!("{:?} is not a function", value),396 _ => panic!("{:?} is not a function", value),
369 }397 }
370 }398 }
371 Function(params, body) => evaluate_method(context, body, params.clone()),399 Function(params, body) => evaluate_method(context, eval_state, body, params.clone()),
372 Error(e) => panic!("error: {}", evaluate(context, e)),400 Error(e) => panic!("error: {}", evaluate(context, eval_state, e)),
373 IfElse {401 IfElse {
374 cond,402 cond,
375 cond_then,403 cond_then,
376 cond_else,404 cond_else,
377 } => match evaluate(context.clone(), &cond.0).unwrap_if_lazy() {405 } => match evaluate(context.clone(), eval_state.clone(), &cond.0).unwrap_if_lazy() {
378 Val::Bool(true) => evaluate(context, cond_then),406 Val::Bool(true) => evaluate(context, eval_state.clone(), cond_then),
379 Val::Bool(false) => match cond_else {407 Val::Bool(false) => match cond_else {
380 Some(v) => evaluate(context, v),408 Some(v) => evaluate(context, eval_state, v),
381 None => Val::Bool(false),409 None => Val::Bool(false),
382 },410 },
383 v => panic!("if condition evaluated to {:?} (boolean needed instead)", v),411 v => panic!("if condition evaluated to {:?} (boolean needed instead)", v),
387 LocExpr(expr.clone(), loc.clone())415 LocExpr(expr.clone(), loc.clone())
388 ),416 ),
389 }417 }
418 })
390}419}
391420
modifiedcrates/jsonnet-evaluator/src/lib.rsdiffbeforeafterboth
4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]4#![allow(macro_expanded_macro_exports_accessed_by_absolute_paths)]
5mod ctx;5mod ctx;
6mod dynamic;6mod dynamic;
7mod error;
7mod evaluate;8mod evaluate;
8mod obj;9mod obj;
9mod val;10mod val;
1011
11pub use ctx::*;12pub use ctx::*;
12pub use dynamic::*;13pub use dynamic::*;
14pub use error::*;
13pub use evaluate::*;15pub use evaluate::*;
14use jsonnet_parser::*;16use jsonnet_parser::*;
15pub use obj::*;17pub use obj::*;
33 dyn Fn(Context, LocExpr) -> Val35 dyn Fn(Context, LocExpr) -> Val
34);36);
3537
36pub struct ExitGuard<'s>(&'s EvaluationState);38#[derive(Default, Clone)]
37impl<'s> Drop for ExitGuard<'s> {
38 fn drop(&mut self) {
39 self.0.stack.borrow_mut().pop();
40 }
41}
42
43pub struct EvaluationState {39pub struct EvaluationState {
40 /// Used for stack-overflows and stacktraces
44 pub stack: Rc<RefCell<Vec<LocExpr>>>,41 pub stack: Rc<RefCell<Vec<(LocExpr, String)>>>,
42 /// Contains file source codes and evaluated results for imports and pretty printing stacktraces
45 pub files: Rc<RefCell<HashMap<String, String>>>,43 pub files: Rc<RefCell<HashMap<String, (String, Option<Val>)>>>,
46}44}
47impl EvaluationState {45impl EvaluationState {
48 #[must_use = "should keep exit guard before exit from function"]
49 pub fn push(&self, e: LocExpr) -> ExitGuard {46 pub fn push<T>(&self, e: LocExpr, comment: String, f: impl FnOnce() -> T) -> T {
50 self.stack.borrow_mut().push(e);47 self.stack.borrow_mut().push((e, comment));
48 let result = f();
51 ExitGuard(self)49 self.stack.borrow_mut().pop();
50 result
52 }51 }
53 pub fn print_stack_trace(&self) {52 pub fn print_stack_trace(&self) {
54 for e in self53 for e in self
55 .stack54 .stack
56 .borrow()55 .borrow()
57 .iter()56 .iter()
58 .rev()57 .rev()
59 .map(|e| e.1.clone())58 .map(|(loc, comment)| loc.1.clone().map(|v| (v, comment.clone())))
60 .flatten()59 .flatten()
61 {60 {
62 println!("{:?}", e)61 println!("{:?} - {:?}", e.0, e.1)
63 }62 }
64 }63 }
64 pub fn stack_trace(&self) -> Vec<(LocExpr, String)> {
65 self.stack
66 .borrow()
67 .iter()
68 .rev()
69 .map(|e| e.clone())
70 .collect()
71 }
65}72}
66impl Default for EvaluationState {
67 fn default() -> Self {
68 EvaluationState {
69 stack: Rc::new(RefCell::new(Vec::new())),
70 files: Rc::new(RefCell::new(HashMap::new())),
71 }
72 }
73}
7473
75#[cfg(test)]74#[cfg(test)]
76pub mod tests {75pub mod tests {
81 #[test]80 #[test]
82 fn eval_state_stacktrace() {81 fn eval_state_stacktrace() {
83 let state = EvaluationState::default();82 let state = EvaluationState::default();
84 let _v = state.push(loc_expr!(83 state.push(
85 Expr::Num(0.0),84 loc_expr!(Expr::Num(0.0), true, ("test1.jsonnet".to_owned(), 10, 20)),
86 true,85 "outer".to_owned(),
87 ("test.jsonnet".to_owned(), 10, 20)86 || {
87 state.push(
88 loc_expr!(Expr::Num(0.0), true, ("test2.jsonnet".to_owned(), 30, 40)),
89 "inner".to_owned(),
90 || state.print_stack_trace(),
91 );
92 },
88 ));93 );
89
90 state.print_stack_trace()
91 }94 }
9295
93 macro_rules! eval {96 macro_rules! eval {
94 ($str: expr) => {97 ($str: expr) => {
95 evaluate(98 evaluate(
96 Context::new(),99 Context::new(),
100 EvaluationState::default(),
97 &parse(101 &parse(
98 $str,102 $str,
99 &ParserSettings {103 &ParserSettings {
111 let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";115 let std = "local std = ".to_owned() + jsonnet_stdlib::STDLIB_STR + ";";
112 evaluate(116 evaluate(
113 Context::new(),117 Context::new(),
118 EvaluationState::default(),
114 &parse(119 &parse(
115 &(std + $str),120 &(std + $str),
116 &ParserSettings {121 &ParserSettings {
128 assert_eq!(133 assert_eq!(
129 evaluate(134 evaluate(
130 Context::new(),135 Context::new(),
136 EvaluationState::default(),
131 &parse(137 &parse(
132 $str,138 $str,
133 &ParserSettings {139 &ParserSettings {
148 "{}",154 "{}",
149 evaluate(155 evaluate(
150 Context::new(),156 Context::new(),
157 EvaluationState::default(),
151 &parse(158 &parse(
152 $str,159 $str,
153 &ParserSettings {160 &ParserSettings {
172 assert_eq!(179 assert_eq!(
173 evaluate(180 evaluate(
174 Context::new(),181 Context::new(),
182 EvaluationState::default(),
175 &parse(183 &parse(
176 $str,184 $str,
177 &ParserSettings {185 &ParserSettings {
357 #[test]365 #[test]
358 fn sjsonnet() {366 fn sjsonnet() {
359 eval!(367 eval!(
360 r#"368 r#"
361 local x0 = {k: 1};369 local x0 = {k: 1};
362 local x1 = {k: x0.k + x0.k};370 local x1 = {k: x0.k + x0.k};
363 local x2 = {k: x1.k + x1.k};371 local x2 = {k: x1.k + x1.k};
364 local x3 = {k: x2.k + x2.k};372 local x3 = {k: x2.k + x2.k};
365 local x4 = {k: x3.k + x3.k};373 local x4 = {k: x3.k + x3.k};
366 local x5 = {k: x4.k + x4.k};374 local x5 = {k: x4.k + x4.k};
367 local x6 = {k: x5.k + x5.k};375 local x6 = {k: x5.k + x5.k};
368 local x7 = {k: x6.k + x6.k};376 local x7 = {k: x6.k + x6.k};
369 local x8 = {k: x7.k + x7.k};377 local x8 = {k: x7.k + x7.k};
370 local x9 = {k: x8.k + x8.k};378 local x9 = {k: x8.k + x8.k};
371 local x10 = {k: x9.k + x9.k};379 local x10 = {k: x9.k + x9.k};
372 local x11 = {k: x10.k + x10.k};380 local x11 = {k: x10.k + x10.k};
373 local x12 = {k: x11.k + x11.k};381 local x12 = {k: x11.k + x11.k};
374 local x13 = {k: x12.k + x12.k};382 local x13 = {k: x12.k + x12.k};
375 local x14 = {k: x13.k + x13.k};383 local x14 = {k: x13.k + x13.k};
376 local x15 = {k: x14.k + x14.k};384 local x15 = {k: x14.k + x14.k};
377 local x16 = {k: x15.k + x15.k};385 local x16 = {k: x15.k + x15.k};
378 local x17 = {k: x16.k + x16.k};386 local x17 = {k: x16.k + x16.k};
379 local x18 = {k: x17.k + x17.k};387 local x18 = {k: x17.k + x17.k};
380 local x19 = {k: x18.k + x18.k};388 local x19 = {k: x18.k + x18.k};
381 local x20 = {k: x19.k + x19.k};389 local x20 = {k: x19.k + x19.k};
382 local x21 = {k: x20.k + x20.k};390 local x21 = {k: x20.k + x20.k};
383 x21.k391 "#
384 "#
385 );392 );
386 }393 }
387}394}
modifiedcrates/jsonnet-parser/src/expr.rsdiffbeforeafterboth

no syntactic changes