Skip to content

Commit

Permalink
refactor(eval): move more eval states to State::Fn*
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosm committed Nov 6, 2024
1 parent 3aa08a9 commit 09bbc86
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 30 deletions.
9 changes: 6 additions & 3 deletions rsjsonnet-lang/src/program/eval/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,8 @@ impl<'p> Evaluator<'_, 'p> {
}
BuiltInFunc::AssertEqual => {
let [arg0, arg1] = check_num_args(args);
self.state_stack.push(State::StdAssertEqual);
self.state_stack
.push(State::FnInfallible(Self::do_std_assert_equal));
self.state_stack.push(State::DoThunk(arg1.view()));
self.state_stack.push(State::DoThunk(arg0.view()));
}
Expand Down Expand Up @@ -621,12 +622,14 @@ impl<'p> Evaluator<'_, 'p> {
self.state_stack.push(State::DoThunk(arg.view()));
}
BuiltInFunc::EncodeUtf8 => {
self.state_stack.push(State::StdEncodeUtf8);
self.state_stack
.push(State::FnFallible(Self::do_std_encode_utf8));
self.state_stack.push(State::DoThunk(args[0].view()));
}
BuiltInFunc::DecodeUtf8 => {
let [arg] = check_num_args(args);
self.state_stack.push(State::StdDecodeUtf8);
self.state_stack
.push(State::FnFallible(Self::do_std_decode_utf8));
self.state_stack.push(State::DoThunk(arg.view()));
}
BuiltInFunc::ManifestIni => {
Expand Down
8 changes: 0 additions & 8 deletions rsjsonnet-lang/src/program/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,6 @@ impl<'p, 'a> Evaluator<'a, 'p> {
State::StdPruneValue => self.do_std_prune_value(),
State::StdPruneArrayItem => self.do_std_prune_array_item(),
State::StdPruneObjectField { name } => self.do_std_prune_object_field(name),
State::StdAssertEqual => self.do_std_assert_equal(),
State::StdAssertEqualCheck => self.do_std_assert_equal_check(),
State::StdAssertEqualFail1 => self.do_std_assert_equal_fail_1(),
State::StdAssertEqualFail2 => self.do_std_assert_equal_fail_2()?,
State::StdFormat => self.do_std_format()?,
State::StdFormatCodesArray1 {
parts,
Expand Down Expand Up @@ -1347,10 +1343,6 @@ impl<'p, 'a> Evaluator<'a, 'p> {
fw,
prec,
} => self.do_std_format_code(&parts, part_i, fw, prec)?,
State::StdEncodeUtf8 => self.do_std_encode_utf8()?,
State::StdDecodeUtf8 => self.do_std_decode_utf8()?,
State::StdDecodeUtf8CheckItem => self.do_std_decode_utf8_check_item()?,
State::StdDecodeUtf8Finish => self.do_std_decode_utf8_finish(),
State::StdManifestIni => self.do_std_manifest_ini()?,
State::StdManifestIniSections => self.do_std_manifest_ini_sections()?,
State::StdManifestPython => self.do_std_manifest_python(),
Expand Down
8 changes: 0 additions & 8 deletions rsjsonnet-lang/src/program/eval/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ pub(super) enum State<'a, 'p> {
StdPruneObjectField {
name: InternedStr<'p>,
},
StdAssertEqual,
StdAssertEqualCheck,
StdAssertEqualFail1,
StdAssertEqualFail2,
StdFormat,
StdFormatCodesArray1 {
parts: Rc<Vec<FormatPart>>,
Expand Down Expand Up @@ -261,10 +257,6 @@ pub(super) enum State<'a, 'p> {
fw: u32,
prec: u32,
},
StdEncodeUtf8,
StdDecodeUtf8,
StdDecodeUtf8CheckItem,
StdDecodeUtf8Finish,
StdManifestIni,
StdManifestIniSections,
StdManifestPython,
Expand Down
27 changes: 16 additions & 11 deletions rsjsonnet-lang/src/program/eval/stdlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,12 @@ impl<'p> Evaluator<'_, 'p> {
self.value_stack.push(lhs);
self.value_stack.push(rhs);

self.state_stack.push(State::StdAssertEqualCheck);
self.state_stack
.push(State::FnInfallible(Self::do_std_assert_equal_check));
self.state_stack.push(State::EqualsValue);
}

pub(super) fn do_std_assert_equal_check(&mut self) {
fn do_std_assert_equal_check(&mut self) {
let equal = self.bool_stack.pop().unwrap();
if equal {
self.value_stack.pop().unwrap();
Expand All @@ -506,24 +507,26 @@ impl<'p> Evaluator<'_, 'p> {
self.value_stack
.swap(value_stack_len - 1, value_stack_len - 2);
self.string_stack.push(String::new());
self.state_stack.push(State::StdAssertEqualFail1);
self.state_stack
.push(State::FnInfallible(Self::do_std_assert_equal_fail_1));
self.state_stack.push(State::ManifestJson {
format: ManifestJsonFormat::default_to_string(),
depth: 0,
});
}
}

pub(super) fn do_std_assert_equal_fail_1(&mut self) {
fn do_std_assert_equal_fail_1(&mut self) {
self.string_stack.push(String::new());
self.state_stack.push(State::StdAssertEqualFail2);
self.state_stack
.push(State::FnFallible(Self::do_std_assert_equal_fail_2));
self.state_stack.push(State::ManifestJson {
format: ManifestJsonFormat::default_to_string(),
depth: 0,
});
}

pub(super) fn do_std_assert_equal_fail_2(&mut self) -> EvalResult<()> {
fn do_std_assert_equal_fail_2(&mut self) -> EvalResult<()> {
let rhs = self.string_stack.pop().unwrap();
let lhs = self.string_stack.pop().unwrap();
Err(self.report_error(EvalErrorKind::AssertEqualFailed { lhs, rhs }))
Expand Down Expand Up @@ -1060,16 +1063,18 @@ impl<'p> Evaluator<'_, 'p> {
let array = self.expect_std_func_arg_array(arg, "decodeUTF8", 0)?;

self.byte_array_stack.push(Vec::with_capacity(array.len()));
self.state_stack.push(State::StdDecodeUtf8Finish);
self.state_stack
.push(State::FnInfallible(Self::do_std_decode_utf8_finish));
for item in array.iter().rev() {
self.state_stack.push(State::StdDecodeUtf8CheckItem);
self.state_stack
.push(State::FnFallible(Self::do_std_decode_utf8_check_item));
self.state_stack.push(State::DoThunk(item.view()));
}

Ok(())
}

pub(super) fn do_std_decode_utf8_check_item(&mut self) -> EvalResult<()> {
fn do_std_decode_utf8_check_item(&mut self) -> EvalResult<()> {
let item = self.value_stack.pop().unwrap();
let ValueData::Number(value) = item else {
return Err(self.report_error(EvalErrorKind::Other {
Expand All @@ -1090,7 +1095,7 @@ impl<'p> Evaluator<'_, 'p> {
Ok(())
}

pub(super) fn do_std_decode_utf8_finish(&mut self) {
fn do_std_decode_utf8_finish(&mut self) {
let bytes = self.byte_array_stack.pop().unwrap();
let s = String::from_utf8_lossy(&bytes);
self.value_stack.push(ValueData::String(s.into()));
Expand Down Expand Up @@ -2143,7 +2148,7 @@ impl<'p> Evaluator<'_, 'p> {
Ok(())
}

pub(super) fn do_std_flatten_arrays_item(&mut self) -> EvalResult<()> {
fn do_std_flatten_arrays_item(&mut self) -> EvalResult<()> {
let item = self.value_stack.pop().unwrap();
let ValueData::Array(item) = item else {
return Err(self.report_error(EvalErrorKind::Other {
Expand Down

0 comments on commit 09bbc86

Please sign in to comment.