Skip to content

Commit

Permalink
op-based scope generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vandesm14 committed Jun 25, 2024
1 parent 881a609 commit b33b658
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 56 deletions.
99 changes: 70 additions & 29 deletions stack-core/src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ pub enum JournalOp {
Push(Expr),
Pop(Expr),

ScopedFnStart(JournalScope, JournalScope),
ScopedFnStart(JournalScope),
ScopelessFnStart,
FnEnd(JournalScope, JournalScope),
FnEnd(JournalScope),

ScopeDef(Symbol, Expr),
ScopeSet(Symbol, Expr, Expr),
Expand Down Expand Up @@ -114,7 +114,6 @@ pub struct Journal {
ops: Vec<JournalOp>,

entries: Vec<JournalEntry>,
scope: JournalScope,
scope_levels: Vec<bool>,

size: Option<usize>,
Expand Down Expand Up @@ -196,7 +195,6 @@ impl Journal {
ops: Vec::new(),

entries: Vec::new(),
scope: JournalScope::new(),
scope_levels: vec![false],

size: None,
Expand All @@ -214,31 +212,21 @@ impl Journal {
}

pub fn push_op(&mut self, op: JournalOp) {
match op {
JournalOp::ScopedFnStart(scope) => {
self.scope = scope;
match &op {
JournalOp::ScopedFnStart(..) => {
self.scope_levels.push(true);
}
JournalOp::ScopelessFnStart => {
self.scope_levels.push(false);
}
JournalOp::FnEnd(scope) => {
self.scope = scope;
JournalOp::FnEnd(..) => {
self.scope_levels.pop();
}
JournalOp::ScopeDef(key, value) => {
self.scope.insert(key, value);
}
JournalOp::ScopeSet(key, _, value) => {
self.scope.insert(key, value);
}

op => self.ops.push(op.clone()),
_ => {}
}
}

pub fn scope(&self) -> &JournalScope {
&self.scope
self.ops.push(op)
}

pub fn commit(&mut self) {
Expand All @@ -263,9 +251,32 @@ impl Journal {
self.len() == 0
}

fn construct_entry(&self, entry: &JournalEntry, stack: &mut Vec<Expr>) {
fn construct_entry(
&self,
entry: &JournalEntry,
stack: &mut Vec<Expr>,
scopes: &mut Vec<JournalScope>,
) {
for op in entry.ops.iter() {
match op {
JournalOp::ScopedFnStart(scope) => {
scopes.push(scope.clone());
}
JournalOp::FnEnd(..) => {
scopes.pop();
}
JournalOp::ScopeDef(key, value) => {
let scope = scopes.last_mut();
if let Some(scope) = scope {
scope.insert(*key, value.clone());
}
}
JournalOp::ScopeSet(key, _, value) => {
let scope = scopes.last_mut();
if let Some(scope) = scope {
scope.insert(*key, value.clone());
}
}
JournalOp::Push(expr) => stack.push(expr.clone()),
JournalOp::Pop(_) => {
stack.pop();
Expand All @@ -291,9 +302,32 @@ impl Journal {
}
}

fn unconstruct_entry(&self, entry: &JournalEntry, stack: &mut Vec<Expr>) {
fn unconstruct_entry(
&self,
entry: &JournalEntry,
stack: &mut Vec<Expr>,
scopes: &mut Vec<JournalScope>,
) {
for op in entry.ops.iter().rev() {
match op {
JournalOp::ScopedFnStart(..) => {
scopes.pop();
}
JournalOp::FnEnd(scope) => {
scopes.push(scope.clone());
}
JournalOp::ScopeDef(key, ..) => {
let scope = scopes.last_mut();
if let Some(scope) = scope {
scope.remove(key);
}
}
JournalOp::ScopeSet(key, old_value, _) => {
let scope = scopes.last_mut();
if let Some(scope) = scope {
scope.insert(*key, old_value.clone());
}
}
JournalOp::Push(_) => {
stack.pop();
}
Expand Down Expand Up @@ -321,43 +355,50 @@ impl Journal {
pub fn construct_to_from(
&self,
stack: &mut Vec<Expr>,
scopes: &mut Vec<JournalScope>,
to: usize,
from: usize,
) {
let skip = (self.entries.len() - 1) - from;
let take = from - to;
for entry in self.entries.iter().rev().skip(skip).take(take) {
self.unconstruct_entry(entry, stack);
self.unconstruct_entry(entry, stack, scopes);
}
}

/// Constructing from a lower to a higher index.
pub fn construct_from_to(
&self,
stack: &mut Vec<Expr>,
scopes: &mut Vec<JournalScope>,
from: usize,
to: usize,
) {
let skip = from + 1;
let take = to - from;

for entry in self.entries.iter().skip(skip).take(take) {
self.construct_entry(entry, stack);
self.construct_entry(entry, stack, scopes);
}
}

pub fn construct_at_zero(&self, stack: &mut Vec<Expr>) {
pub fn construct_at_zero(
&self,
stack: &mut Vec<Expr>,
scopes: &mut Vec<JournalScope>,
) {
if let Some(entry) = self.entries.first() {
self.construct_entry(entry, stack);
self.construct_entry(entry, stack, scopes);
}
}

pub fn construct_to(&self, index: usize) -> Vec<Expr> {
pub fn construct_to(&self, index: usize) -> (Vec<Expr>, Vec<JournalScope>) {
let mut stack = Vec::new();
self.construct_at_zero(&mut stack);
self.construct_from_to(&mut stack, 0, index);
let mut scopes = vec![JournalScope::new()];
self.construct_at_zero(&mut stack, &mut scopes);
self.construct_from_to(&mut stack, &mut scopes, 0, index);

stack
(stack, scopes)
}

// pub fn trim_to(mut self, index: usize) -> Self {
Expand Down
1 change: 0 additions & 1 deletion stack-debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use itertools::Itertools;
use stack_core::{
journal::{Journal, JournalOp, JournalScope},
prelude::*,
scope::Scope,
};

pub enum IOHookEvent {
Expand Down
60 changes: 34 additions & 26 deletions stack-debugger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use eframe::egui::{self, text::LayoutJob, Color32, RichText};
use notify::{
Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher,
};
use stack_core::prelude::*;
use stack_core::{journal::JournalScope, prelude::*};
use stack_debugger::*;

#[derive(Debug, Clone, PartialEq, Eq, Default, clap::Parser)]
Expand Down Expand Up @@ -83,11 +83,13 @@ pub fn main() {
engine,
input: cli.input.clone(),

stack: Vec::new(),
scopes: Vec::new(),

error: None,
prints: Vec::new(),
index: 0,
last_index: 0,
stack: Vec::new(),
journal_string: Vec::new(),
};

Expand Down Expand Up @@ -142,11 +144,13 @@ pub struct DebuggerApp {
engine: Engine,
input: PathBuf,

stack: Vec<Expr>,
scopes: Vec<JournalScope>,

error: Option<String>,
prints: Vec<IOHookEvent>,
index: usize,
last_index: usize,
stack: Vec<Expr>,
journal_string: Vec<String>,
}

Expand Down Expand Up @@ -193,13 +197,12 @@ impl DebuggerApp {
evt
}));

self.stack = self
(self.stack, self.scopes) = self
.context
.journal()
.as_ref()
.unwrap()
.construct_to(self.index)
.to_vec();
.construct_to(self.index);

self.journal_string = self
.context
Expand Down Expand Up @@ -520,14 +523,15 @@ impl eframe::App for DebuggerApp {

let mut layout_job = LayoutJob::default();
if let Some(entry) = entry {
let scope = self.context.journal().as_ref().unwrap().scope();

append_to_job(
RichText::new(format!("Scope (level {}):\n", entry.scope_level))
.color(Color32::WHITE),
&mut layout_job,
);
paint_scope(scope, &mut layout_job);
let scope = self.scopes.last();
if let Some(scope) = scope {
append_to_job(
RichText::new(format!("Scope (level {}):\n", entry.scope_level))
.color(Color32::WHITE),
&mut layout_job,
);
paint_scope(scope, &mut layout_job);
}
}
ui.label(layout_job);

Expand All @@ -543,18 +547,22 @@ impl eframe::App for DebuggerApp {

// Update stack
match self.index.cmp(&self.last_index) {
Ordering::Greater => self
.context
.journal()
.as_ref()
.unwrap()
.construct_from_to(&mut self.stack, self.last_index, self.index),
Ordering::Less => self
.context
.journal()
.as_ref()
.unwrap()
.construct_to_from(&mut self.stack, self.index, self.last_index),
Ordering::Greater => {
self.context.journal().as_ref().unwrap().construct_from_to(
&mut self.stack,
&mut self.scopes,
self.last_index,
self.index,
)
}
Ordering::Less => {
self.context.journal().as_ref().unwrap().construct_to_from(
&mut self.stack,
&mut self.scopes,
self.index,
self.last_index,
)
}

_ => {}
}
Expand Down

0 comments on commit b33b658

Please sign in to comment.