1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use std::fmt::Debug;
pub use arglike::{ArgLike, ArgsLike, TlaArg};
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
pub use jrsonnet_macros::builtin;
use jrsonnet_parser::{Destruct, Expr, ExprLocation, LocExpr, ParamsDesc};
use self::{
arglike::OptionalContext,
builtin::{Builtin, StaticBuiltin},
native::NativeDesc,
parse::{parse_default_function_call, parse_function_call},
};
use crate::{evaluate, evaluate_trivial, gc::TraceBox, tb, Context, ContextBuilder, Result, Val};
pub mod arglike;
pub mod builtin;
pub mod native;
pub mod parse;
#[derive(Clone, Copy)]
pub struct CallLocation<'l>(pub Option<&'l ExprLocation>);
impl<'l> CallLocation<'l> {
pub const fn new(loc: &'l ExprLocation) -> Self {
Self(Some(loc))
}
}
impl CallLocation<'static> {
pub const fn native() -> Self {
Self(None)
}
}
#[derive(Debug, PartialEq, Trace)]
pub struct FuncDesc {
pub name: IStr,
pub ctx: Context,
pub params: ParamsDesc,
pub body: LocExpr,
}
impl FuncDesc {
pub fn default_body_context(&self) -> Result<Context> {
parse_default_function_call(self.ctx.clone(), &self.params)
}
pub fn call_body_context(
&self,
call_ctx: Context,
args: &dyn ArgsLike,
tailstrict: bool,
) -> Result<Context> {
parse_function_call(call_ctx, self.ctx.clone(), &self.params, args, tailstrict)
}
pub fn evaluate_trivial(&self) -> Option<Val> {
evaluate_trivial(&self.body)
}
}
#[allow(clippy::module_name_repetitions)]
#[derive(Trace, Clone)]
pub enum FuncVal {
Id,
Normal(Cc<FuncDesc>),
StaticBuiltin(#[trace(skip)] &'static dyn StaticBuiltin),
Builtin(Cc<TraceBox<dyn Builtin>>),
}
impl Debug for FuncVal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Id => f.debug_tuple("Id").finish(),
Self::Normal(arg0) => f.debug_tuple("Normal").field(arg0).finish(),
Self::StaticBuiltin(arg0) => {
f.debug_tuple("StaticBuiltin").field(&arg0.name()).finish()
}
Self::Builtin(arg0) => f.debug_tuple("Builtin").field(&arg0.name()).finish(),
}
}
}
impl FuncVal {
pub fn builtin(builtin: impl Builtin) -> Self {
Self::Builtin(Cc::new(tb!(builtin)))
}
pub fn params_len(&self) -> usize {
match self {
Self::Id => 1,
Self::Normal(n) => n.params.iter().filter(|p| p.1.is_none()).count(),
Self::StaticBuiltin(i) => i.params().iter().filter(|p| !p.has_default).count(),
Self::Builtin(i) => i.params().iter().filter(|p| !p.has_default).count(),
}
}
pub fn name(&self) -> IStr {
match self {
Self::Id => "id".into(),
Self::Normal(normal) => normal.name.clone(),
Self::StaticBuiltin(builtin) => builtin.name().into(),
Self::Builtin(builtin) => builtin.name().into(),
}
}
pub fn evaluate(
&self,
call_ctx: Context,
loc: CallLocation<'_>,
args: &dyn ArgsLike,
tailstrict: bool,
) -> Result<Val> {
match self {
Self::Id => {
#[allow(clippy::unnecessary_wraps)]
#[builtin]
const fn builtin_id(x: Val) -> Val {
x
}
static ID: &builtin_id = &builtin_id {};
ID.call(call_ctx, loc, args)
}
Self::Normal(func) => {
let body_ctx = func.call_body_context(call_ctx, args, tailstrict)?;
evaluate(body_ctx, &func.body)
}
Self::StaticBuiltin(b) => b.call(call_ctx, loc, args),
Self::Builtin(b) => b.call(call_ctx, loc, args),
}
}
pub fn evaluate_simple<A: ArgsLike + OptionalContext>(&self, args: &A) -> Result<Val> {
self.evaluate(
ContextBuilder::dangerous_empty_state().build(),
CallLocation::native(),
args,
true,
)
}
pub fn into_native<D: NativeDesc>(self) -> D::Value {
D::into_native(self)
}
pub fn is_identity(&self) -> bool {
match self {
Self::Id => true,
Self::Normal(desc) => {
if desc.params.len() != 1 {
return false;
}
let param = &desc.params[0];
if param.1.is_some() {
return false;
}
#[allow(clippy::infallible_destructuring_match)]
let id = match ¶m.0 {
Destruct::Full(id) => id,
#[cfg(feature = "exp-destruct")]
_ => return false,
};
&desc.body.0 as &Expr == &Expr::Var(id.clone())
}
_ => false,
}
}
pub const fn identity() -> Self {
Self::Id
}
pub fn evaluate_trivial(&self) -> Option<Val> {
match self {
FuncVal::Normal(n) => n.evaluate_trivial(),
_ => None,
}
}
}