Skip to content

Commit

Permalink
More pattern matching
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 12, 2024
1 parent bfde8e3 commit 3ba50e6
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 11 deletions.
30 changes: 30 additions & 0 deletions tremor-script/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ impl<'run, 'event> Scope<'run, 'event> {
.as_object()
.map_or(true, halfbrown::SizedHashMap::is_empty);
}

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L326-L350

Added lines #L326 - L350 were not covered by tests
// Inspect
Op::InspectLen => {
let v = last(&mut stack, *pc, *cc)?;

Check failure on line 353 in tremor-script/src/vm.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] tremor-script/src/vm.rs#L353

error: the function `last` doesn't need a mutable reference --> tremor-script/src/vm.rs:353:34 | 353 | let v = last(&mut stack, *pc, *cc)?; | ^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed note: the lint level is defined here --> tremor-script/src/lib.rs:20:5 | 20 | clippy::all, | ^^^^^^^^^^^ = note: `#[deny(clippy::unnecessary_mut_passed)]` implied by `#[deny(clippy::all)]`
Raw output
tremor-script/src/vm.rs:353:34:e:error: the function `last` doesn't need a mutable reference
   --> tremor-script/src/vm.rs:353:34
    |
353 |                     let v = last(&mut stack, *pc, *cc)?;
    |                                  ^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
note: the lint level is defined here
   --> tremor-script/src/lib.rs:20:5
    |
20  |     clippy::all,
    |     ^^^^^^^^^^^
    = note: `#[deny(clippy::unnecessary_mut_passed)]` implied by `#[deny(clippy::all)]`


__END__
let len = if let Some(v) = v.as_array() {
v.len()
} else if let Some(v) = v.as_object() {
v.len()

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L353-L357

Added lines #L353 - L357 were not covered by tests
} else {
return Err("Not an array or object".into());

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L359

Added line #L359 was not covered by tests
};
stack.push(Cow::Owned(Value::from(len)));

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L361

Added line #L361 was not covered by tests
}

// Records
Op::RecordSet => {
Expand Down Expand Up @@ -455,6 +467,24 @@ impl<'run, 'event> Scope<'run, 'event> {
.to_vec();
stack.push(Cow::Owned(v.into()));

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L459-L468

Added lines #L459 - L468 were not covered by tests
}

// Array
// FIXME: this is kind akeward, we use the stack here instead of the register
Op::ArrayPop => {
let arr = last_mut(&mut stack, *pc, *cc)?
.to_mut()
.as_array_mut()
.ok_or("needs array")?;
let v = arr
.pop()
.ok_or_else(|| error_generic(mid, mid, &"Empty array"))?;
stack.push(Cow::Owned(v));

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L474-L481

Added lines #L474 - L481 were not covered by tests
}
Op::ArrayReverse => {
let v = last_mut(&mut stack, *pc, *cc)?;
let arr = v.to_mut().as_array_mut().ok_or("needs array")?;
arr.reverse();

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L484-L486

Added lines #L484 - L486 were not covered by tests
}
}
*pc += 1;

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm.rs#L489

Added line #L489 was not covered by tests
}
Expand Down
64 changes: 54 additions & 10 deletions tremor-script/src/vm/compiler/impls/imut_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use tremor_value::Value;
use crate::{
ast::{
raw::{BytesDataType, Endian},
BaseExpr, BinOpKind, BooleanBinExpr, BooleanBinOpKind, BytesPart, ClausePreCondition,
Field, ImutExpr, Invoke, List, Merge, Patch, PatchOperation, Pattern, Record, Segment,
StrLitElement, StringLit,
ArrayPredicatePattern, BaseExpr, BinOpKind, BooleanBinExpr, BooleanBinOpKind, BytesPart,
ClausePreCondition, Field, ImutExpr, Invoke, List, Merge, Patch, PatchOperation, Pattern,
PredicatePattern, Record, Segment, StrLitElement, StringLit,
},
errors::Result,
vm::{
Expand Down Expand Up @@ -367,6 +367,17 @@ impl<'script> Compilable<'script> for BytesPart<'script> {
}

Check warning on line 367 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L359-L367

Added lines #L359 - L367 were not covered by tests
}

impl<'script> Compilable<'script> for PredicatePattern<'script> {
fn compile(self, _compiler: &mut Compiler<'script>) -> Result<()> {
todo!()

Check warning on line 372 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L371-L372

Added lines #L371 - L372 were not covered by tests
}
}
impl<'script> Compilable<'script> for ArrayPredicatePattern<'script> {
fn compile(self, _compiler: &mut Compiler<'script>) -> Result<()> {
todo!()

Check warning on line 377 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L376-L377

Added lines #L376 - L377 were not covered by tests
}
}

impl<'script> Compilable<'script> for Pattern<'script> {
fn compile(self, compiler: &mut Compiler<'script>) -> Result<()> {
self.compile_to_b(compiler)
Expand All @@ -378,20 +389,21 @@ impl<'script> Compilable<'script> for Pattern<'script> {
compiler.emit(Op::TestIsRecord, &r.mid);
let dst = compiler.new_jump_point();
compiler.emit(Op::JumpFalse { dst }, &r.mid);

Check warning on line 391 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L386-L391

Added lines #L386 - L391 were not covered by tests
if r.fields.is_empty() {
} else {
todo!()

for f in r.fields {
f.compile(compiler)?;

Check warning on line 394 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L393-L394

Added lines #L393 - L394 were not covered by tests
}

compiler.set_jump_target(dst);

Check warning on line 397 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L397

Added line #L397 was not covered by tests
}
Pattern::Array(a) => {
let mid = *a.mid;
compiler.emit(Op::TestIsArray, &mid);
let dst = compiler.new_jump_point();
compiler.emit(Op::JumpFalse { dst }, &mid);
if a.exprs.is_empty() {
} else {
todo!()
for e in a.exprs {
e.compile(compiler)?;
todo!("we need to look at all the array elements :sob:")

Check warning on line 406 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L399-L406

Added lines #L399 - L406 were not covered by tests
}
compiler.set_jump_target(dst);

Check warning on line 408 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L408

Added line #L408 was not covered by tests
}
Expand All @@ -403,7 +415,39 @@ impl<'script> Compilable<'script> for Pattern<'script> {
}

Pattern::Assign(_) => todo!(),
Pattern::Tuple(_) => todo!(),
Pattern::Tuple(t) => {
let mid = *t.mid;
compiler.emit(Op::TestIsArray, &mid);
let dst = compiler.new_jump_point();
compiler.emit(Op::JumpFalse { dst }, &mid);
// copy the item to the stack so we can itterate
compiler.emit(Op::CopyV1, &mid);
compiler.emit(Op::InspectLen, &mid);
compiler.emit_const(t.exprs.len(), &mid);

if t.open {
compiler.emit(Op::Binary { op: BinOpKind::Gte }, &mid);
} else {
compiler.emit(Op::Binary { op: BinOpKind::Eq }, &mid);
}
let end_and_pop = compiler.new_jump_point();

compiler.emit(Op::JumpFalse { dst: end_and_pop }, &mid);

// reverse the array so we can pop in the right order
compiler.emit(Op::ArrayReverse, &mid);
for e in t.exprs {
compiler.emit(Op::ArrayPop, &mid);
e.compile(compiler)?;
compiler.emit(Op::JumpFalse { dst: end_and_pop }, &mid);
todo!("we need to look at all the array elements :sob:")

Check warning on line 443 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L417-L443

Added lines #L417 - L443 were not covered by tests
}
compiler.emit(Op::LoadRB, &mid);
compiler.set_jump_target(end_and_pop);
// remove the array from the stack
compiler.emit(Op::Pop, &mid);
compiler.set_jump_target(dst);

Check warning on line 449 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls/imut_expr.rs#L445-L449

Added lines #L445 - L449 were not covered by tests
}
Pattern::Extract(_) => todo!(),
Pattern::DoNotCare => {
compiler.emit(Op::True, &NodeMeta::dummy());
Expand Down
13 changes: 12 additions & 1 deletion tremor-script/src/vm/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub(crate) enum Op {
end: u16,
},

// Tests
// Tests - does not pop the stack result is stored in the b register
TestRecortPresent,
TestIsU64,
TestIsI64,
Expand All @@ -117,6 +117,10 @@ pub(crate) enum Op {
TestArrayIsEmpty,
TestRecordIsEmpty,

// Inspect - does not pop the stack result is stored on the stack
//// returns the lenght of an array, object or 1 for scalar values
InspectLen,

// Patch
RecordSet,
RecordRemove,
Expand All @@ -127,6 +131,8 @@ pub(crate) enum Op {
TestIsArray,
RecordMergeKey,
RecordPop,
ArrayPop,
ArrayReverse,
}

impl Display for Op {
Expand Down Expand Up @@ -185,12 +191,17 @@ impl Display for Op {
Op::TestArrayIsEmpty => write!(f, "test_array_is_empty"),
Op::TestRecordIsEmpty => write!(f, "test_record_is_empty"),

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/op.rs#L185-L192

Added lines #L185 - L192 were not covered by tests

Op::InspectLen => write!(f, "inspect_len"),

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/op.rs#L194

Added line #L194 was not covered by tests

Op::RecordSet => write!(f, "record_set"),
Op::RecordRemove => write!(f, "record_remove"),
Op::RecordGet => write!(f, "record_get"),
Op::RecordMergeKey => write!(f, "record_merge_key"),
Op::RecordMerge => write!(f, "record_merge"),

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/op.rs#L196-L200

Added lines #L196 - L200 were not covered by tests

Op::RecordPop => write!(f, "record_pop"),
Op::ArrayPop => write!(f, "array_pop"),
Op::ArrayReverse => write!(f, "array_reverse"),

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/op.rs#L202-L204

Added lines #L202 - L204 were not covered by tests
}
}

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/op.rs#L206

Added line #L206 was not covered by tests
}

0 comments on commit 3ba50e6

Please sign in to comment.