Skip to content

Commit

Permalink
refactor(eval): implement identity function as a variant of FuncKind
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosm committed Nov 7, 2024
1 parent c2d435c commit 50eb060
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
11 changes: 10 additions & 1 deletion rsjsonnet-lang/src/program/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,13 @@ impl<'p> FuncData<'p> {
kind,
}
}

pub(super) fn new_identity_func(
func_name: Option<InternedStr<'p>>,
param: &'p [(InternedStr<'p>, Option<&'p ir::Expr<'p>>); 1],
) -> Self {
Self::new(param, FuncKind::Identity { name: func_name })
}
}

pub(super) struct FuncParams<'p> {
Expand All @@ -630,6 +637,9 @@ pub(super) struct FuncParams<'p> {
}

pub(super) enum FuncKind<'p> {
Identity {
name: Option<InternedStr<'p>>,
},
Normal {
name: Option<InternedStr<'p>>,
body: &'p ir::Expr<'p>,
Expand Down Expand Up @@ -658,7 +668,6 @@ impl GcTrace for FuncKind<'_> {

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub(super) enum BuiltInFunc {
Identity,
// External Variables
ExtVar,
// Types and Reflection
Expand Down
11 changes: 7 additions & 4 deletions rsjsonnet-lang/src/program/eval/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ impl<'p> Evaluator<'_, 'p> {
func: &FuncData<'p>,
) -> (Option<InternedStr<'p>>, Option<Gc<ThunkEnv<'p>>>) {
match func.kind {
FuncKind::Identity { name } => (name, None),
FuncKind::Normal { name, ref env, .. } => (name, Some(env.clone())),
FuncKind::BuiltIn { name, .. } => (Some(name), None),
FuncKind::Native { .. } => (None, None),
Expand Down Expand Up @@ -196,6 +197,12 @@ impl<'p> Evaluator<'_, 'p> {

pub(super) fn execute_call(&mut self, func: &FuncData<'p>, args: Box<[Gc<ThunkData<'p>>]>) {
match func.kind {
FuncKind::Identity { .. } => {
let [arg] = &*args else {
unreachable!();
};
self.state_stack.push(State::DoThunk(arg.view()));
}
FuncKind::Normal { body, ref env, .. } => {
self.execute_normal_call(&func.params, body, env.clone(), args);
}
Expand Down Expand Up @@ -236,10 +243,6 @@ impl<'p> Evaluator<'_, 'p> {
args.try_into().unwrap()
}
match kind {
BuiltInFunc::Identity => {
let [arg] = check_num_args(args);
self.state_stack.push(State::DoThunk(arg.view()));
}
BuiltInFunc::ExtVar => {
let [arg] = check_num_args(args);
self.state_stack
Expand Down
7 changes: 2 additions & 5 deletions rsjsonnet-lang/src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,9 @@ impl<'p> Program<'p> {
let stdlib_extra = Self::build_stdlib_extra(arena, &str_interner, &gc_ctx, &exprs);

let empty_array: GcView<ArrayData<'p>> = gc_ctx.alloc_view(Box::new([]));
let identity_func = gc_ctx.alloc_view(FuncData::new(
let identity_func = gc_ctx.alloc_view(FuncData::new_identity_func(
Some(str_interner.intern(arena, "id")),
arena.alloc([(str_interner.intern(arena, "x"), None)]),
FuncKind::BuiltIn {
name: str_interner.intern(arena, "id"),
kind: BuiltInFunc::Identity,
},
));

let mut this = Self {
Expand Down

0 comments on commit 50eb060

Please sign in to comment.