1use crate::{2 binding, bool_val, context_creator, function_default, function_rhs, future_wrapper,3 lazy_binding, lazy_val, Context, ContextCreator, FuncDesc, LazyBinding, ObjMember, ObjValue,4 Val,5};6use closure::closure;7use jsonnet_parser::{8 ArgsDesc, BinaryOpType, BindSpec, Expr, FieldMember, LiteralType, Member, ObjBody, ParamsDesc,9 UnaryOpType, Visibility,10};11use std::{12 collections::{BTreeMap, HashMap},13 rc::Rc,14};1516pub fn evaluate_binding(b: &BindSpec, context_creator: ContextCreator) -> (String, LazyBinding) {17 let b = b.clone();18 if let Some(args) = &b.params {19 let args = args.clone();20 (21 b.name.clone(),22 lazy_binding!(move |this, super_obj| lazy_val!(23 closure!(clone b, clone args, clone context_creator, || evaluate_method(24 context_creator.0(this.clone(), super_obj.clone()),25 &b.value,26 args.clone()27 ))28 )),29 )30 } else {31 (32 b.name.clone(),33 lazy_binding!(move |this, super_obj| {34 lazy_val!(closure!(clone context_creator, clone b, || evaluate(35 context_creator.0(this.clone(), super_obj.clone()),36 &b.value37 )))38 }),39 )40 }41}4243pub fn evaluate_method(ctx: Context, expr: &Expr, arg_spec: ParamsDesc) -> Val {44 Val::Func(FuncDesc {45 ctx,46 params: arg_spec,47 eval_rhs: function_rhs!(closure!(clone expr, |ctx| evaluate(ctx, &expr))),48 eval_default: function_default!(|ctx, default| evaluate(ctx, &default)),49 })50}5152pub fn evaluate_field_name(context: Context, field_name: &jsonnet_parser::FieldName) -> String {53 match field_name {54 jsonnet_parser::FieldName::Fixed(n) => n.clone(),55 jsonnet_parser::FieldName::Dyn(expr) => {56 let name = evaluate(context, expr).unwrap_if_lazy();57 match name {58 Val::Str(n) => n,59 _ => panic!(60 "dynamic field name can be only evaluated to 'string', got: {:?}",61 name62 ),63 }64 }65 }66}6768pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Val {69 match (op, b) {70 (o, Val::Lazy(l)) => evaluate_unary_op(o, &l.0()),71 (UnaryOpType::Not, Val::Literal(LiteralType::True)) => Val::Literal(LiteralType::False),72 (UnaryOpType::Not, Val::Literal(LiteralType::False)) => Val::Literal(LiteralType::True),73 (op, o) => panic!("unary op not implemented: {:?} {:?}", op, o),74 }75}7677pub fn evaluate_binary_op(a: &Val, op: BinaryOpType, b: &Val) -> Val {78 match (a, op, b) {79 (Val::Lazy(a), o, b) => evaluate_binary_op(&a.0(), o, b),80 (a, o, Val::Lazy(b)) => evaluate_binary_op(a, o, &b.0()),8182 (Val::Str(v1), BinaryOpType::Add, Val::Str(v2)) => Val::Str(v1.to_owned() + &v2),83 (Val::Str(v1), BinaryOpType::Eq, Val::Str(v2)) => bool_val(v1 == v2),84 (Val::Str(v1), BinaryOpType::Ne, Val::Str(v2)) => bool_val(v1 != v2),8586 (Val::Str(v1), BinaryOpType::Add, Val::Num(v2)) => Val::Str(format!("{}{}", v1, v2)),87 (Val::Str(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Str(v1.repeat(*v2 as usize)),8889 (Val::Literal(LiteralType::False), BinaryOpType::And, Val::Literal(LiteralType::False)) => {90 bool_val(false)91 }92 (Val::Literal(LiteralType::False), BinaryOpType::And, Val::Literal(LiteralType::True)) => {93 bool_val(false)94 }95 (Val::Literal(LiteralType::True), BinaryOpType::And, Val::Literal(LiteralType::False)) => {96 bool_val(false)97 }98 (Val::Literal(LiteralType::True), BinaryOpType::And, Val::Literal(LiteralType::True)) => {99 bool_val(true)100 }101102 (Val::Obj(v1), BinaryOpType::Add, Val::Obj(v2)) => Val::Obj(v2.with_super(v1.clone())),103104 (Val::Arr(a), BinaryOpType::Add, Val::Arr(b)) => Val::Arr([&a[..], &b[..]].concat()),105106 (Val::Num(v1), BinaryOpType::Mul, Val::Num(v2)) => Val::Num(v1 * v2),107 (Val::Num(v1), BinaryOpType::Div, Val::Num(v2)) => Val::Num(v1 / v2),108 (Val::Num(v1), BinaryOpType::Mod, Val::Num(v2)) => Val::Num(v1 % v2),109110 (Val::Num(v1), BinaryOpType::Add, Val::Num(v2)) => Val::Num(v1 + v2),111 (Val::Num(v1), BinaryOpType::Sub, Val::Num(v2)) => Val::Num(v1 - v2),112113 (Val::Num(v1), BinaryOpType::Lhs, Val::Num(v2)) => {114 Val::Num(((*v1 as i32) << (*v2 as i32)) as f64)115 }116 (Val::Num(v1), BinaryOpType::Rhs, Val::Num(v2)) => {117 Val::Num(((*v1 as i32) >> (*v2 as i32)) as f64)118 }119120 (Val::Num(v1), BinaryOpType::Lt, Val::Num(v2)) => bool_val(v1 < v2),121 (Val::Num(v1), BinaryOpType::Gt, Val::Num(v2)) => bool_val(v1 > v2),122 (Val::Num(v1), BinaryOpType::Lte, Val::Num(v2)) => bool_val(v1 <= v2),123 (Val::Num(v1), BinaryOpType::Gte, Val::Num(v2)) => bool_val(v1 >= v2),124125 (Val::Num(v1), BinaryOpType::Eq, Val::Num(v2)) => bool_val((v1 - v2).abs() < f64::EPSILON),126 (Val::Num(v1), BinaryOpType::Ne, Val::Num(v2)) => bool_val((v1 - v2).abs() > f64::EPSILON),127128 (Val::Num(v1), BinaryOpType::BitAnd, Val::Num(v2)) => {129 Val::Num(((*v1 as i32) & (*v2 as i32)) as f64)130 }131 (Val::Num(v1), BinaryOpType::BitOr, Val::Num(v2)) => {132 Val::Num(((*v1 as i32) | (*v2 as i32)) as f64)133 }134 (Val::Num(v1), BinaryOpType::BitXor, Val::Num(v2)) => {135 Val::Num(((*v1 as i32) ^ (*v2 as i32)) as f64)136 }137 _ => panic!("no rules for binary operation: {:?} {:?} {:?}", a, op, b),138 }139}140141future_wrapper!(HashMap<String, LazyBinding>, FutureNewBindings);142future_wrapper!(ObjValue, FutureObjValue);143144145pub fn evaluate_object(context: Context, object: ObjBody) -> ObjValue {146 match object {147 ObjBody::MemberList(members) => {148 let new_bindings = FutureNewBindings::new();149 let future_this = FutureObjValue::new();150 let context_creator = context_creator!(151 closure!(clone context, clone new_bindings, clone future_this, |this: Option<ObjValue>, super_obj: Option<ObjValue>| {152 context.clone().extend(153 new_bindings.clone().unwrap(),154 context.clone().dollar().clone().or_else(||this.clone()),155 Some(this.clone().unwrap()),156 super_obj157 )158 })159 );160 {161 let mut bindings: HashMap<String, LazyBinding> = HashMap::new();162 for (n, b) in members163 .iter()164 .filter_map(|m| match m {165 Member::BindStmt(b) => Some(b.clone()),166 _ => None,167 })168 .map(|b| evaluate_binding(&b, context_creator.clone()))169 {170 bindings.insert(n, b);171 }172 new_bindings.fill(bindings);173 }174175 let mut new_members = BTreeMap::new();176 for member in members.into_iter() {177 match member {178 Member::Field(FieldMember {179 name,180 plus,181 params: None,182 visibility,183 value,184 }) => {185 let name = evaluate_field_name(context.clone(), &name);186 new_members.insert(187 name,188 ObjMember {189 add: plus,190 visibility: visibility.clone(),191 invoke: binding!(192 closure!(clone value, clone context_creator, |this, super_obj| {193 let context = context_creator.0(this, super_obj);194 195 evaluate(196 context,197 &value,198 ).unwrap_if_lazy()199 })200 ),201 },202 );203 }204 Member::Field(FieldMember {205 name,206 params: Some(params),207 value,208 ..209 }) => {210 let name = evaluate_field_name(context.clone(), &name);211 new_members.insert(212 name,213 ObjMember {214 add: false,215 visibility: Visibility::Hidden,216 invoke: binding!(217 closure!(clone value, clone context_creator, |this, super_obj| {218 219 evaluate_method(220 context_creator.0(this, super_obj),221 &value.clone(),222 params.clone(),223 )224 })225 ),226 },227 );228 }229 Member::BindStmt(_) => {}230 Member::AssertStmt(_) => {}231 }232 }233 future_this.fill(ObjValue::new(None, Rc::new(new_members)))234 }235 _ => todo!(),236 }237}238239pub fn evaluate(context: Context, expr: &Expr) -> Val {240 use Expr::*;241 match &*expr {242 Literal(LiteralType::This) => Val::Obj(243 context244 .this()245 .clone()246 .unwrap_or_else(|| panic!("this not found")),247 ),248 Literal(LiteralType::Super) => Val::Obj(249 context250 .super_obj()251 .clone()252 .unwrap_or_else(|| panic!("super not found")),253 ),254 Literal(t) => Val::Literal(t.clone()),255 Parened(e) => evaluate(context, e),256 Str(v) => Val::Str(v.clone()),257 Num(v) => Val::Num(*v),258 BinaryOp(v1, o, v2) => {259 evaluate_binary_op(&evaluate(context.clone(), v1), *o, &evaluate(context, v2))260 }261 UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(context, v)),262 Var(name) => Val::Lazy(context.binding(&name)).unwrap_if_lazy(),263 Index(box value, box index) => {264 match (265 evaluate(context.clone(), value).unwrap_if_lazy(),266 evaluate(context.clone(), index),267 ) {268 (Val::Obj(v), Val::Str(s)) => v269 .get(&s)270 .unwrap_or_else(closure!(clone context, || {271 if let Some(n) = v.get("__intristic_namespace__") {272 if let Val::Str(n) = n.unwrap_if_lazy() {273 Val::Intristic(n, s)274 } else {275 panic!("__intristic_namespace__ should be string");276 }277 } else {278 panic!("{} not found in {:?}", s, v)279 }280 }))281 .unwrap_if_lazy(),282 (Val::Arr(v), Val::Num(n)) => v283 .get(n as usize)284 .unwrap_or_else(|| panic!("out of bounds"))285 .clone(),286 (Val::Str(s), Val::Num(n)) => {287 Val::Str(s.chars().skip(n as usize - 1).take(1).collect())288 }289 (v, i) => todo!("not implemented: {:?}[{:?}]", v, i.unwrap_if_lazy()),290 }291 }292 LocalExpr(bindings, returned) => {293 let mut new_bindings: HashMap<String, LazyBinding> = HashMap::new();294 let future_context = Context::new_future();295296 let context_creator = context_creator!(297 closure!(clone future_context, |_, _| future_context.clone().unwrap())298 );299300 for (k, v) in bindings301 .iter()302 .map(move |b| evaluate_binding(b, context_creator.clone()))303 {304 new_bindings.insert(k, v);305 }306307 let context = context308 .extend(new_bindings, None, None, None)309 .into_future(future_context);310 evaluate(context, &*returned.clone())311 }312 Obj(body) => Val::Obj(evaluate_object(context, body.clone())),313 Apply(box value, ArgsDesc(args)) => {314 let value = evaluate(context.clone(), value).unwrap_if_lazy();315 match value {316 317 Val::Intristic(ns, name) => match (&ns as &str, &name as &str) {318 ("std", "length") => {319 assert_eq!(args.len(), 1);320 let expr = &args.get(0).unwrap().1;321 match evaluate(context, expr) {322 Val::Str(n) => Val::Num(n.chars().count() as f64),323 Val::Arr(i) => Val::Num(i.len() as f64),324 v => panic!("can't get length of {:?}", v),325 }326 }327 ("std", "type") => {328 assert_eq!(args.len(), 1);329 let expr = &args.get(0).unwrap().1;330 Val::Str(evaluate(context, expr).type_of().to_owned())331 }332 ("std", "makeArray") => {333 assert_eq!(args.len(), 2);334 if let (Val::Num(v), Val::Func(d)) = (335 evaluate(context.clone(), &args[0].1),336 evaluate(context, &args[1].1),337 ) {338 assert!(v > 0.0);339 let mut out = Vec::with_capacity(v as usize);340 for i in 0..v as usize {341 out.push(d.evaluate(vec![(None, Val::Num(i as f64))]))342 }343 Val::Arr(out)344 } else {345 panic!("bad makeArray call");346 }347 }348 ("std", "codepoint") => {349 assert_eq!(args.len(), 1);350 if let Val::Str(s) = evaluate(context.clone(), &args[0].1) {351 assert!(352 s.chars().count() == 1,353 "std.codepoint should receive single char string"354 );355 Val::Num(s.chars().take(1).next().unwrap() as u32 as f64)356 } else {357 panic!("bad codepoint call");358 }359 }360 (ns, name) => panic!("Intristic not found: {}.{}", ns, name),361 },362 Val::Func(f) => f.evaluate(363 args.clone()364 .into_iter()365 .map(|a| {366 (367 a.clone().0,368 Val::Lazy(lazy_val!(369 closure!(clone context, clone a, || evaluate(context.clone(), &a.clone().1))370 )),371 )372 })373 .collect(),374 ),375 _ => panic!("{:?} is not a function", value),376 }377 }378 Function(params, body) => evaluate_method(context, body, params.clone()),379 Error(e) => panic!("error: {}", evaluate(context, e)),380 IfElse {381 cond,382 cond_then,383 cond_else,384 } => match evaluate(context.clone(), &cond.0).unwrap_if_lazy() {385 Val::Literal(LiteralType::True) => evaluate(context, cond_then),386 Val::Literal(LiteralType::False) => match cond_else {387 Some(v) => evaluate(context, v),388 None => Val::Literal(LiteralType::False),389 },390 v => panic!("if condition evaluated to {:?} (boolean needed instead)", v),391 },392 _ => panic!("evaluation not implemented: {:?}", expr),393 }394}