Skip to content

Commit

Permalink
Add support for record patterns
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <[email protected]>
  • Loading branch information
Licenser committed Aug 16, 2024
1 parent 5e1dfff commit 1421979
Show file tree
Hide file tree
Showing 9 changed files with 1,321 additions and 849 deletions.
22 changes: 21 additions & 1 deletion tremor-script/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use op::Op;
#[allow(dead_code)]
pub struct Vm {}

#[derive(Debug)]
struct Registers<'run, 'event> {
/// Value register 1
v1: Cow<'run, Value<'event>>,
Expand Down Expand Up @@ -92,7 +93,13 @@ impl<'run, 'event> Scope<'run, 'event> {
*cc += 1;
let mid = &self.program.meta[*pc];
// ALLOW: we test that pc is always in bounds in the while loop above
match unsafe { *self.program.opcodes.get_unchecked(*pc) } {
let op = unsafe { *self.program.opcodes.get_unchecked(*pc) };
// if let Some(comment) = self.program.comments.get(pc) {
// println!("# {comment}");
// }
// dbg!(stack.last(), &self.reg);
// println!("{pc:3}: {op}");
match op {
Op::Nop => continue,

Check warning on line 103 in tremor-script/src/vm.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L92-L103

Added lines #L92 - L103 were not covered by tests
// Loads
Op::LoadV1 => self.reg.v1 = pop(&mut stack, *pc, *cc)?,
Expand All @@ -105,6 +112,9 @@ impl<'run, 'event> Scope<'run, 'event> {
self.reg.b1 = pop(&mut stack, *pc, *cc)?.try_as_bool()?;

Check warning on line 112 in tremor-script/src/vm.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L112

Added line #L112 was not covered by tests
}
Op::StoreRB => stack.push(Cow::Owned(Value::from(self.reg.b1))),
Op::NotRB => self.reg.b1 = !self.reg.b1,
Op::TrueRB => self.reg.b1 = true,
Op::FalseRB => self.reg.b1 = false,
Op::LoadEvent => stack.push(Cow::Owned(event.clone())),
Op::StoreEvent { elements } => unsafe {
let mut tmp = event as *mut Value;
Expand Down Expand Up @@ -384,6 +394,10 @@ impl<'run, 'event> Scope<'run, 'event> {
.as_object()
.map_or(true, halfbrown::SizedHashMap::is_empty);
}
Op::TestRecordContainsKey { key } => {
let key = &self.program.keys[key as usize];
self.reg.b1 = key.lookup(&self.reg.v1).is_some();
}

Check warning on line 400 in tremor-script/src/vm.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L390-L400

Added lines #L390 - L400 were not covered by tests
// Inspect
Op::InspectLen => {
let len = if let Some(v) = self.reg.v1.as_array() {
Expand Down Expand Up @@ -452,6 +466,12 @@ impl<'run, 'event> Scope<'run, 'event> {
let res = key.lookup(&v).ok_or("Missing Key FIXME")?.clone();
stack.push(Cow::Owned(res));

Check warning on line 467 in tremor-script/src/vm.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L466-L467

Added lines #L466 - L467 were not covered by tests
}
Op::GetKeyRegV1 { key } => {
let key = &self.program.keys[key as usize];

Check warning on line 470 in tremor-script/src/vm.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L469-L470

Added lines #L469 - L470 were not covered by tests
// FIXME: can we avoid this clone here
let res = key.lookup(&self.reg.v1).ok_or("Missing Key FIXME")?.clone();
stack.push(Cow::Owned(res));

Check warning on line 473 in tremor-script/src/vm.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L472-L473

Added lines #L472 - L473 were not covered by tests
}
Op::Get => {
let key = pop(&mut stack, *pc, *cc)?;
let v = pop(&mut stack, *pc, *cc)?;
Expand Down
13 changes: 13 additions & 0 deletions tremor-script/src/vm/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2020-2024, The Tremor Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
mod impls;

use crate::{ast::Script, errors::Result, NodeMeta};
Expand Down
13 changes: 13 additions & 0 deletions tremor-script/src/vm/compiler/impls.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
// Copyright 2020-2024, The Tremor Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
mod imut_expr;
mod mut_expr;

Expand Down
Loading

0 comments on commit 1421979

Please sign in to comment.