Skip to content

Commit

Permalink
Touple expressions and compiler comments
Browse files Browse the repository at this point in the history
Signed-off-by: Heinz N. Gies <heinz@licenser.net>
  • Loading branch information
Licenser committed Aug 13, 2024
1 parent 3ba50e6 commit f920690
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 36 deletions.
2 changes: 1 addition & 1 deletion tremor-script/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ impl<'run, 'event> Scope<'run, 'event> {
}

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)?;
let v = last(&stack, *pc, *cc)?;
let len = if let Some(v) = v.as_array() {
v.len()
} else if let Some(v) = v.as_object() {
Expand Down
29 changes: 28 additions & 1 deletion tremor-script/src/vm/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
mod impls;

use crate::{ast::Script, errors::Result, NodeMeta};
use std::{collections::HashMap, fmt::Display, mem};
use simd_json_derive::Serialize;
use std::{
collections::{BTreeMap, HashMap},
fmt::Display,
mem,
};
use tremor_value::Value;

use super::Op;
Expand All @@ -16,6 +21,7 @@ pub struct Compiler<'script> {
consts: Vec<Value<'script>>,
keys: Vec<tremor_value::KnownKey<'script>>,
max_locals: usize,
pub(crate) comments: BTreeMap<usize, String>,
}

trait Compilable<'script>: Sized {
Expand All @@ -38,6 +44,7 @@ impl<'script> Compiler<'script> {
let consts = mem::take(&mut self.consts);
let mut jump_table = HashMap::with_capacity(self.jump_table.len());
let keys = mem::take(&mut self.keys);
let comments = mem::take(&mut self.comments);

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler.rs#L40-L47

Added lines #L40 - L47 were not covered by tests

for op in &mut opcodes {
match op {
Expand All @@ -57,6 +64,7 @@ impl<'script> Compiler<'script> {
jump_table,
consts,
keys,
comments,
max_locals: self.max_locals,
})
}

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler.rs#L61-L70

Added lines #L61 - L70 were not covered by tests
Expand All @@ -72,6 +80,7 @@ impl<'script> Compiler<'script> {
consts: Vec::new(),
keys: Vec::new(),
max_locals: 0,
comments: BTreeMap::new(),
}
}

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler.rs#L74-L85

Added lines #L74 - L85 were not covered by tests

Expand All @@ -81,6 +90,13 @@ impl<'script> Compiler<'script> {
// self.opcodes.len() as u32 - 1
// }

pub(crate) fn comment<S>(&mut self, comment: S)
where
S: Into<String>,
{
self.comments.insert(self.opcodes.len(), comment.into());
}

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler.rs#L93-L98

Added lines #L93 - L98 were not covered by tests

pub(crate) fn emit(&mut self, op: Op, mid: &NodeMeta) {
self.opcodes.push(op);
self.meta.push(mid.clone());
Expand Down Expand Up @@ -161,11 +177,16 @@ pub struct Program<'script> {
pub(crate) consts: Vec<Value<'script>>,
pub(crate) keys: Vec<tremor_value::KnownKey<'script>>,
pub(crate) max_locals: usize,
pub(crate) comments: BTreeMap<usize, String>,
}

impl Display for Program<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "Program: ")?;
for (idx, op) in self.opcodes.iter().enumerate() {
if let Some(comment) = self.comments.get(&idx) {
writeln!(f, "\n # {comment}")?;
}
if let Some(dst) = self.jump_table.get(&idx).copied() {
writeln!(f, "JMP<{dst:03}>")?;
}
Expand Down Expand Up @@ -197,6 +218,12 @@ impl Display for Program<'_> {
.copied()
.unwrap_or_default()
)?,
Op::Const { idx: c } => writeln!(
f,
" {idx:04}: {:30} {c:<15} {}",
"const",
self.consts[*c as usize].json_string().unwrap_or_default(),
)?,
_ => writeln!(f, " {idx:04}: {op}")?,

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler.rs#L184-L227

Added lines #L184 - L227 were not covered by tests
}
}
Expand Down
15 changes: 14 additions & 1 deletion tremor-script/src/vm/compiler/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,23 @@ where
} = self;
let end_dst = compiler.end_dst()?;
let dst = compiler.new_jump_point();

compiler.comment("Predicate Clause");
compiler.emit(Op::CopyV1, &mid);
pattern.compile_to_b(compiler)?;
compiler.comment("Jump to the next pattern");
compiler.emit(Op::JumpFalse { dst }, &mid);
if let Some(guard) = guard {
compiler.comment("Predicate Clause Guard");
guard.compile_to_b(compiler)?;
compiler.emit(Op::JumpFalse { dst }, &mid);
}
compiler.comment("Predicate Clause Body");
for e in exprs {
e.compile(compiler)?;

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L29-L51

Added lines #L29 - L51 were not covered by tests
}
last_expr.compile(compiler)?;

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L53

Added line #L53 was not covered by tests
// we were successful so we jump to the end
compiler.comment("Jump to the end of the matching statement since we were successful");
compiler.emit(Op::Jump { dst: end_dst }, &mid);
compiler.set_jump_target(dst);
Ok(())
Expand Down Expand Up @@ -90,7 +94,9 @@ where
patterns,

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L93-L94

Added lines #L93 - L94 were not covered by tests
} => {
if let Some(precondition) = precondition {
compiler.comment("Match Group Preconditions");
precondition.compile_to_b(compiler)?;
compiler.comment("Jump to next case if precondition is false");
compiler.emit(Op::JumpFalse { dst: next }, &NodeMeta::dummy());

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L96-L100

Added lines #L96 - L100 were not covered by tests
// FIXME
}
Expand All @@ -104,7 +110,9 @@ where
rest,

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L108-L110

Added lines #L108 - L110 were not covered by tests
} => {
if let Some(precondition) = precondition {
compiler.comment("Match Tree Preconditions");
precondition.compile_to_b(compiler)?;
compiler.comment("Jump to next case if precondition is false");
compiler.emit(Op::JumpFalse { dst: next }, &NodeMeta::dummy());

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L112-L116

Added lines #L112 - L116 were not covered by tests
// FIXME
}
Expand All @@ -118,7 +126,9 @@ where
groups,

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L125-L126

Added lines #L125 - L126 were not covered by tests
} => {
if let Some(precondition) = precondition {
compiler.comment("Match Combined Preconditions");
precondition.compile_to_b(compiler)?;
compiler.comment("Jump to next case if precondition is false");
compiler.emit(Op::JumpFalse { dst: next }, &NodeMeta::dummy());

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L128-L132

Added lines #L128 - L132 were not covered by tests
// FIXME
}
Expand All @@ -131,7 +141,9 @@ where
pattern,

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L140-L141

Added lines #L140 - L141 were not covered by tests
} => {
if let Some(precondition) = precondition {
compiler.comment("Match Single Preconditions");
precondition.compile_to_b(compiler)?;
compiler.comment("Jump to next case if precondition is false");
compiler.emit(Op::JumpFalse { dst: next }, &NodeMeta::dummy());
}
pattern.compile(compiler)?;

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/compiler/impls.rs#L143-L149

Added lines #L143 - L149 were not covered by tests
Expand All @@ -153,6 +165,7 @@ where
patterns,
default,
} = self;
compiler.comment("Match statement");
compiler.new_end_target();
// Save r1 to ensure we can restore it after the patch operation
compiler.emit(Op::StoreV1, &mid);
Expand Down
38 changes: 26 additions & 12 deletions tremor-script/src/vm/compiler/impls/imut_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,19 @@ impl<'script> Compilable<'script> for PredicatePattern<'script> {
}
}
impl<'script> Compilable<'script> for ArrayPredicatePattern<'script> {
fn compile(self, _compiler: &mut Compiler<'script>) -> Result<()> {
todo!()
fn compile(self, compiler: &mut Compiler<'script>) -> Result<()> {
match self {
ArrayPredicatePattern::Expr(e) => {
let mid = e.meta().clone();
e.compile(compiler)?;
compiler.emit(Op::Binary { op: BinOpKind::Eq }, &mid);
compiler.emit(Op::LoadRB, &mid);

Check warning on line 382 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-L382

Added lines #L376 - L382 were not covered by tests
}
ArrayPredicatePattern::Tilde(_) => todo!(),
ArrayPredicatePattern::Record(_) => todo!(),
ArrayPredicatePattern::Ignore => todo!(),

Check warning on line 386 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#L384-L386

Added lines #L384 - L386 were not covered by tests
}
Ok(())
}

Check warning on line 389 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#L388-L389

Added lines #L388 - L389 were not covered by tests
}

Expand Down Expand Up @@ -416,12 +427,12 @@ impl<'script> Compilable<'script> for Pattern<'script> {

Pattern::Assign(_) => todo!(),
Pattern::Tuple(t) => {
compiler.comment("Tuple pattern");
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);
let dst_next = compiler.new_jump_point();
compiler.emit(Op::JumpFalse { dst: dst_next }, &mid);
compiler.comment("Check if the array is long enough");
compiler.emit(Op::InspectLen, &mid);
compiler.emit_const(t.exprs.len(), &mid);

Expand All @@ -430,23 +441,26 @@ impl<'script> Compilable<'script> for Pattern<'script> {
} else {
compiler.emit(Op::Binary { op: BinOpKind::Eq }, &mid);
}
compiler.emit(Op::LoadRB, &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.comment("Save array for itteration and reverse it");
compiler.emit(Op::CopyV1, &mid);
compiler.emit(Op::ArrayReverse, &mid);
for e in t.exprs {
for (i, e) in t.exprs.into_iter().enumerate() {
compiler.comment(&format!("Test tuple element {}", i));

Check failure on line 453 in tremor-script/src/vm/compiler/impls/imut_expr.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] tremor-script/src/vm/compiler/impls/imut_expr.rs#L453

error: variables can be used directly in the `format!` string --> tremor-script/src/vm/compiler/impls/imut_expr.rs:453:39 | 453 | compiler.comment(&format!("Test tuple element {}", i)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args note: the lint level is defined here --> tremor-script/src/lib.rs:23:5 | 23 | clippy::pedantic, | ^^^^^^^^^^^^^^^^ = note: `#[deny(clippy::uninlined_format_args)]` implied by `#[deny(clippy::pedantic)]` help: change this to | 453 - compiler.comment(&format!("Test tuple element {}", i)); 453 + compiler.comment(&format!("Test tuple element {i}")); |
Raw output
tremor-script/src/vm/compiler/impls/imut_expr.rs:453:39:e:error: variables can be used directly in the `format!` string
   --> tremor-script/src/vm/compiler/impls/imut_expr.rs:453:39
    |
453 |                     compiler.comment(&format!("Test tuple element {}", i));
    |                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#uninlined_format_args
note: the lint level is defined here
   --> tremor-script/src/lib.rs:23:5
    |
23  |     clippy::pedantic,
    |     ^^^^^^^^^^^^^^^^
    = note: `#[deny(clippy::uninlined_format_args)]` implied by `#[deny(clippy::pedantic)]`
help: change this to
    |
453 -                     compiler.comment(&format!("Test tuple element {}", i));
453 +                     compiler.comment(&format!("Test tuple element {i}"));
    |


__END__
compiler.emit(Op::ArrayPop, &mid);
e.compile(compiler)?;
compiler.comment("Jump on no match");
compiler.emit(Op::JumpFalse { dst: end_and_pop }, &mid);

Check warning on line 457 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#L428-L457

Added lines #L428 - L457 were not covered by tests
todo!("we need to look at all the array elements :sob:")
}
compiler.emit(Op::LoadRB, &mid);
compiler.set_jump_target(end_and_pop);
// remove the array from the stack
compiler.comment("Remove the array from the stack");
compiler.set_jump_target(end_and_pop);
compiler.emit(Op::Pop, &mid);
compiler.set_jump_target(dst);
compiler.set_jump_target(dst_next);

Check warning on line 463 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#L460-L463

Added lines #L460 - L463 were not covered by tests
}
Pattern::Extract(_) => todo!(),
Pattern::DoNotCare => {
Expand Down
32 changes: 16 additions & 16 deletions tremor-script/src/vm/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,34 +153,34 @@ impl Display for Op {
Op::StoreRB => write!(f, "{:30} B1", "store_reg"),

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/op.rs#L152-L153

Added lines #L152 - L153 were not covered by tests

Op::LoadEvent => write!(f, "laod_event"),
Op::StoreEvent { elements } => write!(f, "{:30} {elements}", "store_event"),
Op::LoadLocal { idx } => write!(f, "{:30} {idx:10}", "load_local",),
Op::StoreEvent { elements } => write!(f, "{:30} {elements:<5}", "store_event"),
Op::LoadLocal { idx } => write!(f, "{:30} {idx:<5}", "load_local",),
Op::StoreLocal { elements, idx } => {
write!(f, "{:30} {idx:10} {elements}", "store_local")
write!(f, "{:30} {idx:<5} {elements}", "store_local")

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/op.rs#L155-L159

Added lines #L155 - L159 were not covered by tests
}

Op::Emit { dflt } => write!(f, "{:30} {dflt}", "emit"),
Op::Emit { dflt } => write!(f, "{:30} {dflt:<5}", "emit"),
Op::Drop => write!(f, "drop"),
Op::JumpTrue { dst } => write!(f, "{:30} {}", "jump_true", dst),
Op::JumpFalse { dst } => write!(f, "{:30} {}", "jump_false", dst),
Op::Jump { dst } => write!(f, "{:30} {}", "jump", dst),
Op::JumpTrue { dst } => write!(f, "{:30} {:<5}", "jump_true", dst),
Op::JumpFalse { dst } => write!(f, "{:30} {:<5}", "jump_false", dst),
Op::Jump { dst } => write!(f, "{:30} {:<5}", "jump", dst),
Op::True => write!(f, "true"),
Op::False => write!(f, "false"),
Op::Null => write!(f, "null"),
Op::Const { idx } => write!(f, "{:30} {}", "const", idx),
Op::Record { size } => write!(f, "{:30} {}", "record", size),
Op::Array { size } => write!(f, "{:30} {}", "array", size),
Op::String { size } => write!(f, "{:30} {}", "string", size),
Op::Bytes { size } => write!(f, "{:30} {}", "bytes", size),
Op::Const { idx } => write!(f, "{:30} {idx:<5}", "const"),
Op::Record { size } => write!(f, "{:30} {size:<5}", "record",),
Op::Array { size } => write!(f, "{:30} {size:<5}", "array",),
Op::String { size } => write!(f, "{:30} {size:<5}", "string",),
Op::Bytes { size } => write!(f, "{:30} {size:<5}", "bytes",),
Op::Xor => write!(f, "xor"),
Op::Binary { op } => write!(f, "{:30} {:?}", "binary", op),
Op::Unary { op } => write!(f, "{:30} {:?}", "unary", op),
Op::Binary { op } => write!(f, "{:30} {:<5?}", "binary", op),
Op::Unary { op } => write!(f, "{:30} {:<5?}", "unary", op),
Op::GetKey { key } => write!(f, "{:30} {}", "lookup_key", key),
Op::Get => write!(f, "lookup"),
Op::Index => write!(f, "idx"),
Op::IndexFast { idx } => write!(f, "{:30} {}", "idx_fast", idx),
Op::IndexFast { idx } => write!(f, "{:30} {:<5}", "idx_fast", idx),
Op::Range => write!(f, "range"),
Op::RangeFast { start, end } => write!(f, "{:30} {} {}", "range_fast", start, end),
Op::RangeFast { start, end } => write!(f, "{:30} {:<5} {}", "range_fast", start, end),

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

View check run for this annotation

Codecov / codecov/patch

tremor-script/src/vm/op.rs#L162-L183

Added lines #L162 - L183 were not covered by tests

Op::TestRecortPresent => write!(f, "test_record_present"),
Op::TestIsU64 => write!(f, "test_is_u64"),
Expand Down
Loading

0 comments on commit f920690

Please sign in to comment.