git.delta.rocks / jrsonnet / refs/commits / 0b0d703c6d05

difftreelog

refactor do not desugar mod/slice

Yaroslav Bolyukin2021-07-04parent: #53ec857.patch.diff
in: master

7 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
1use crate::{1use crate::{
2 equals,2 equals,
3 error::{Error::*, Result},3 error::{Error::*, Result},
4 operator::evaluate_mod_op,
4 parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,5 parse_args, primitive_equals, push, throw, with_state, ArrValue, Context, EvaluationState,
5 FuncVal, LazyVal, Val,6 FuncVal, IndexableVal, LazyVal, Val,
6};7};
7use format::{format_arr, format_obj};8use format::{format_arr, format_obj};
8use jrsonnet_gc::Gc;9use jrsonnet_gc::Gc;
9use jrsonnet_interner::IStr;10use jrsonnet_interner::IStr;
10use jrsonnet_parser::{ArgsDesc, BinaryOpType, ExprLocation};11use jrsonnet_parser::{ArgsDesc, ExprLocation};
11use jrsonnet_types::ty;12use jrsonnet_types::ty;
12use std::{collections::HashMap, path::PathBuf, rc::Rc};13use std::{collections::HashMap, path::PathBuf, rc::Rc};
1314
20pub mod manifest;21pub mod manifest;
21pub mod sort;22pub mod sort;
2223
23fn std_format(str: IStr, vals: Val) -> Result<Val> {24pub fn std_format(str: IStr, vals: Val) -> Result<Val> {
24 push(25 push(
25 Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),26 Some(&ExprLocation(Rc::from(PathBuf::from("std.jsonnet")), 0, 0)),
26 || format!("std.format of {}", str),27 || format!("std.format of {}", str),
34 )35 )
35}36}
37
38pub fn std_slice(
39 indexable: IndexableVal,
40 index: Option<usize>,
41 end: Option<usize>,
42 step: Option<usize>,
43) -> Result<Val> {
44 let index = index.unwrap_or(0);
45 let end = end.unwrap_or_else(|| match &indexable {
46 IndexableVal::Str(_) => usize::MAX,
47 IndexableVal::Arr(v) => v.len(),
48 });
49 let step = step.unwrap_or(1);
50 match &indexable {
51 IndexableVal::Str(s) => Ok(Val::Str(
52 (s.chars()
53 .skip(index)
54 .take(end - index)
55 .step_by(step)
56 .collect::<String>())
57 .into(),
58 )),
59 IndexableVal::Arr(arr) => Ok(Val::Arr(
60 (arr.iter()
61 .skip(index)
62 .take(end - index)
63 .step_by(step)
64 .collect::<Result<Vec<Val>>>()?)
65 .into(),
66 )),
67 }
68}
3669
37type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;70type Builtin = fn(context: Context, loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val>;
3871
188 2, end: ty!((number | null));221 2, end: ty!((number | null));
189 3, step: ty!((number | null));222 3, step: ty!((number | null));
190 ], {223 ], {
191 let index = match index {224 std_slice(
192 Val::Num(v) => v as usize,225 indexable.to_indexable()?,
193 Val::Null => 0,
194 _ => unreachable!(),226 index.try_cast_nullable_num("index")?.map(|v| v as usize),
195 };
196 let end = match end {
197 Val::Num(v) => v as usize,
198 Val::Null => match &indexable {
199 Val::Str(s) => s.chars().count(),227 end.try_cast_nullable_num("end")?.map(|v| v as usize),
200 Val::Arr(v) => v.len(),
201 _ => unreachable!()
202 },
203 _ => unreachable!()
204 };
205 let step = match step {
206 Val::Num(v) => v as usize,
207 Val::Null => 1,
208 _ => unreachable!()
209 };
210 match &indexable {
211 Val::Str(s) => {
212 Ok(Val::Str((s.chars().skip(index).take(end-index).step_by(step).collect::<String>()).into()))228 step.try_cast_nullable_num("step")?.map(|v| v as usize),
213 }229 )
214 Val::Arr(arr) => {
215 Ok(Val::Arr((arr.iter().skip(index).take(end-index).step_by(step).collect::<Result<Vec<Val>>>()?).into()))
216 }
217 _ => unreachable!()
218 }
219 })230 })
220}231}
221232
257 0, a: ty!((number | string));268 0, a: ty!((number | string));
258 1, b: ty!(any);269 1, b: ty!(any);
259 ], {270 ], {
260 match (a, b) {271 evaluate_mod_op(&a, &b)
261 (Val::Num(a), Val::Num(b)) => Ok(Val::Num(a % b)),
262 (Val::Str(str), vals) => std_format(str, vals),
263 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(BinaryOpType::Mod, a.value_type(), b.value_type()))
264 }
265 })272 })
266}273}
267274
modifiedcrates/jrsonnet-evaluator/src/error.rsdiffbeforeafterboth
73 ValueIndexMustBeTypeGot(ValType, ValType, ValType),73 ValueIndexMustBeTypeGot(ValType, ValType, ValType),
74 #[error("cant index into {0}")]74 #[error("cant index into {0}")]
75 CantIndexInto(ValType),75 CantIndexInto(ValType),
76 #[error("{0} is not indexable")]
77 ValueIsNotIndexable(ValType),
7678
77 #[error("super can't be used standalone")]79 #[error("super can't be used standalone")]
78 StandaloneSuper,80 StandaloneSuper,
modifiedcrates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth
1use crate::{1use crate::{
2 builtin::std_slice,
2 error::Error::*,3 error::Error::*,
3 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},4 evaluate::operator::{evaluate_add_op, evaluate_binary_op_special, evaluate_unary_op},
4 push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,5 push, throw, with_state, ArrValue, Bindable, Context, ContextCreator, FuncDesc, FuncVal,
679 }680 }
680 }681 }
681 }682 }
683 Slice(value, desc) => {
684 let indexable = evaluate(context.clone(), value)?;
685
686 fn parse_num(
687 context: &Context,
688 expr: Option<&LocExpr>,
689 desc: &'static str,
690 ) -> Result<Option<usize>> {
691 Ok(match expr {
692 Some(s) => evaluate(context.clone(), &s)?
693 .try_cast_nullable_num(desc)?
694 .map(|v| v as usize),
695 None => None,
696 })
697 }
698
699 let start = parse_num(&context, desc.start.as_ref(), "start")?;
700 let end = parse_num(&context, desc.end.as_ref(), "end")?;
701 let step = parse_num(&context, desc.step.as_ref(), "step")?;
702
703 std_slice(indexable.to_indexable()?, start, end, step)?
704 }
682 Import(path) => {705 Import(path) => {
683 let tmp = loc706 let tmp = loc
684 .clone()707 .clone()
modifiedcrates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth
1use crate::builtin::std_format;
1use crate::{equals, evaluate, Context, Val};2use crate::{equals, evaluate, Context, Val};
2use crate::{error::Error::*, throw, Result};3use crate::{error::Error::*, throw, Result};
3use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};4use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
41 })42 })
42}43}
44
45pub fn evaluate_mod_op(a: &Val, b: &Val) -> Result<Val> {
46 use Val::*;
47 match (a, b) {
48 (Num(a), Num(b)) => Ok(Num(a % b)),
49 (Str(str), vals) => std_format(str.clone(), vals.clone()),
50 (a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(
51 BinaryOpType::Mod,
52 a.value_type(),
53 b.value_type()
54 )),
55 }
56}
4357
44pub fn evaluate_binary_op_special(58pub fn evaluate_binary_op_special(
45 context: Context,59 context: Context,
60 use BinaryOpType::*;74 use BinaryOpType::*;
61 use Val::*;75 use Val::*;
62 Ok(match (a, op, b) {76 Ok(match (a, op, b) {
63 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
64
65 (a, Add, b) => evaluate_add_op(a, b)?,77 (a, Add, b) => evaluate_add_op(a, b)?,
6678
67 (a, Eq, b) => Bool(equals(a, b)?),79 (a, Eq, b) => Bool(equals(a, b)?),
68 (a, Neq, b) => Bool(!equals(a, b)?),80 (a, Neq, b) => Bool(!equals(a, b)?),
81
82 (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
83 (a, Mod, b) => evaluate_mod_op(a, b)?,
6984
70 (Str(v1), Mul, Num(v2)) => Str(v1.repeat(*v2 as usize).into()),85 (Str(v1), Mul, Num(v2)) => Str(v1.repeat(*v2 as usize).into()),
7186
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
345 }345 }
346}346}
347
348pub enum IndexableVal {
349 Str(IStr),
350 Arr(ArrValue),
351}
347352
348#[derive(Debug, Clone, Trace)]353#[derive(Debug, Clone, Trace)]
349#[trivially_drop]354#[trivially_drop]
402 self.assert_type(context, ValType::Num)?;407 self.assert_type(context, ValType::Num)?;
403 self.unwrap_num()408 self.unwrap_num()
404 }409 }
410 pub fn try_cast_nullable_num(self, context: &'static str) -> Result<Option<f64>> {
411 Ok(match self {
412 Val::Null => None,
413 Val::Num(num) => Some(num),
414 _ => throw!(TypeMismatch(
415 context,
416 vec![ValType::Null, ValType::Num],
417 self.value_type()
418 )),
419 })
420 }
405 pub const fn value_type(&self) -> ValType {421 pub const fn value_type(&self) -> ValType {
406 match self {422 match self {
407 Self::Str(..) => ValType::Str,423 Self::Str(..) => ValType::Str,
580 .try_cast_str("to json")596 .try_cast_str("to json")
581 })597 })
582 }598 }
599 pub fn to_indexable(self) -> Result<IndexableVal> {
600 Ok(match self {
601 Val::Str(s) => IndexableVal::Str(s),
602 Val::Arr(arr) => IndexableVal::Arr(arr),
603 _ => throw!(ValueIsNotIndexable(self.value_type())),
604 })
605 }
583}606}
584607
585const fn is_function_like(val: &Val) -> bool {608const fn is_function_like(val: &Val) -> bool {
modifiedcrates/jrsonnet-parser/src/expr.rsdiffbeforeafterboth
274 False,274 False,
275}275}
276276
277#[cfg_attr(feature = "serialize", derive(Serialize))]
278#[cfg_attr(feature = "deserialize", derive(Deserialize))]
277#[derive(Debug, PartialEq, Trace)]279#[derive(Debug, PartialEq, Trace)]
278#[trivially_drop]280#[trivially_drop]
279pub struct SliceDesc {281pub struct SliceDesc {
349 cond_then: LocExpr,351 cond_then: LocExpr,
350 cond_else: Option<LocExpr>,352 cond_else: Option<LocExpr>,
351 },353 },
354 Slice(LocExpr, SliceDesc),
352}355}
353356
354/// file, begin offset, end offset357/// file, begin offset, end offset
modifiedcrates/jrsonnet-parser/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-parser/src/lib.rs
+++ b/crates/jrsonnet-parser/src/lib.rs
@@ -14,6 +14,17 @@
 	pub file_name: Rc<Path>,
 }
 
+macro_rules! expr_bin {
+	($a:ident $op:ident $b:ident) => {
+		loc_expr_todo!(Expr::BinaryOp($a, $op, $b))
+	};
+}
+macro_rules! expr_un {
+	($op:ident $a:ident) => {
+		loc_expr_todo!(Expr::UnaryOp($op, $a))
+	};
+}
+
 parser! {
 	grammar jsonnet_parser() for str {
 		use peg::ParseLiteral;
@@ -219,54 +230,43 @@
 
 
 		use BinaryOpType::*;
+		use UnaryOpType::*;
 		rule expr(s: &ParserSettings) -> LocExpr
 			= start:position!() a:precedence! {
-				a:(@) _ binop(<"||">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Or, b))}
+				a:(@) _ binop(<"||">) _ b:@ {expr_bin!(a Or b)}
 				--
-				a:(@) _ binop(<"&&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, And, b))}
+				a:(@) _ binop(<"&&">) _ b:@ {expr_bin!(a And b)}
 				--
-				a:(@) _ binop(<"|">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BitOr, b))}
+				a:(@) _ binop(<"|">) _ b:@ {expr_bin!(a BitOr b)}
 				--
-				a:@ _ binop(<"^">) _ b:(@) {loc_expr_todo!(Expr::BinaryOp(a, BitXor, b))}
+				a:@ _ binop(<"^">) _ b:(@) {expr_bin!(a BitXor b)}
 				--
-				a:(@) _ binop(<"&">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, BitAnd, b))}
+				a:(@) _ binop(<"&">) _ b:@ {expr_bin!(a BitAnd b)}
 				--
-				a:(@) _ binop(<"==">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Eq, b))}
-				a:(@) _ binop(<"!=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Neq, b))}
+				a:(@) _ binop(<"==">) _ b:@ {expr_bin!(a Eq b)}
+				a:(@) _ binop(<"!=">) _ b:@ {expr_bin!(a Neq b)}
 				--
-				a:(@) _ binop(<"<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lt, b))}
-				a:(@) _ binop(<">">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Gt, b))}
-				a:(@) _ binop(<"<=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lte, b))}
-				a:(@) _ binop(<">=">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Gte, b))}
-				a:(@) _ binop(<keyword("in")>) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, In, b))}
+				a:(@) _ binop(<"<">) _ b:@ {expr_bin!(a Lt b)}
+				a:(@) _ binop(<">">) _ b:@ {expr_bin!(a Gt b)}
+				a:(@) _ binop(<"<=">) _ b:@ {expr_bin!(a Lte b)}
+				a:(@) _ binop(<">=">) _ b:@ {expr_bin!(a Gte b)}
+				a:(@) _ binop(<keyword("in")>) _ b:@ {expr_bin!(a In b)}
 				--
-				a:(@) _ binop(<"<<">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Lhs, b))}
-				a:(@) _ binop(<">>">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Rhs, b))}
+				a:(@) _ binop(<"<<">) _ b:@ {expr_bin!(a Lhs b)}
+				a:(@) _ binop(<">>">) _ b:@ {expr_bin!(a Rhs b)}
 				--
-				a:(@) _ binop(<"+">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Add, b))}
-				a:(@) _ binop(<"-">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Sub, b))}
+				a:(@) _ binop(<"+">) _ b:@ {expr_bin!(a Add b)}
+				a:(@) _ binop(<"-">) _ b:@ {expr_bin!(a Sub b)}
 				--
-				a:(@) _ binop(<"*">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Mul, b))}
-				a:(@) _ binop(<"/">) _ b:@ {loc_expr_todo!(Expr::BinaryOp(a, Div, b))}
-				a:(@) _ binop(<"%">) _ b:@ {loc_expr_todo!(Expr::Apply(
-					el!(Expr::Intrinsic("mod".into())), ArgsDesc(vec![Arg(None, a), Arg(None, b)]),
-					false
-				))}
+				a:(@) _ binop(<"*">) _ b:@ {expr_bin!(a Mul b)}
+				a:(@) _ binop(<"/">) _ b:@ {expr_bin!(a Div b)}
+				a:(@) _ binop(<"%">) _ b:@ {expr_bin!(a Mod b)}
 				--
-						unaryop(<"-">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Minus, b))}
-						unaryop(<"!">) _ b:@ {loc_expr_todo!(Expr::UnaryOp(UnaryOpType::Not, b))}
-						unaryop(<"~">) _ b:@ { loc_expr_todo!(Expr::UnaryOp(UnaryOpType::BitNot, b)) }
+						unaryop(<"-">) _ b:@ {expr_un!(Minus b)}
+						unaryop(<"!">) _ b:@ {expr_un!(Not b)}
+						unaryop(<"~">) _ b:@ {expr_un!(BitNot b)}
 				--
-				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Apply(
-					el!(Expr::Intrinsic("slice".into())),
-					ArgsDesc(vec![
-						Arg(None, a),
-						Arg(None, s.start.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),
-						Arg(None, s.end.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),
-						Arg(None, s.step.unwrap_or_else(||el!(Expr::Literal(LiteralType::Null)))),
-					]),
-					true,
-				))}
+				a:(@) _ "[" _ s:slice_desc(s) _ "]" {loc_expr_todo!(Expr::Slice(a, s))}
 				a:(@) _ "." _ s:$(id()) {loc_expr_todo!(Expr::Index(a, el!(Expr::Str(s.into()))))}
 				a:(@) _ "[" _ s:expr(s) _ "]" {loc_expr_todo!(Expr::Index(a, s))}
 				a:(@) _ "(" _ args:args(s) _ ")" ts:(_ keyword("tailstrict"))? {loc_expr_todo!(Expr::Apply(a, args, ts.is_some()))}