Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add expr info to function (scope) ops #60

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions stack-core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,12 @@ impl Engine {
let scope = context.scope().clone();
let journal = context.journal_mut().as_mut().unwrap();
journal.commit();
journal.push_op(JournalOp::ScopedFnStart(scope.into()));
journal
.push_op(JournalOp::ScopedFnStart(expr.info.clone(), scope.into()));
} else {
let journal = context.journal_mut().as_mut().unwrap();
journal.commit();
journal.push_op(JournalOp::ScopelessFnStart);
journal.push_op(JournalOp::ScopelessFnStart(expr.info.clone()));
}
}

Expand All @@ -326,7 +327,7 @@ impl Engine {
let scope = context.scope().clone();
let journal = context.journal_mut().as_mut().unwrap();
journal.commit();
journal.push_op(JournalOp::FnEnd(scope.into()));
journal.push_op(JournalOp::FnEnd(expr.info.clone(), scope.into()));
}

if context.stack().last().map(|e| &e.kind)
Expand Down
12 changes: 8 additions & 4 deletions stack-core/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ impl Intrinsic {

if let Some(journal) = context.journal_mut() {
journal.commit();
journal.push_op(JournalOp::ScopelessFnStart);
journal.push_op(JournalOp::ScopelessFnStart(expr.info.clone()));
}

context.push_scope(scope);
Expand All @@ -857,7 +857,8 @@ impl Intrinsic {
let scope = context.scope().clone();
let journal = context.journal_mut().as_mut().unwrap();
journal.commit();
journal.push_op(JournalOp::FnEnd(scope.into()));
journal
.push_op(JournalOp::FnEnd(expr.info.clone(), scope.into()));
}

context.pop_scope();
Expand Down Expand Up @@ -995,7 +996,7 @@ impl Intrinsic {
// Imports should trigger a new commit
if let Some(journal) = context.journal_mut() {
journal.commit();
journal.push_op(JournalOp::ScopelessFnStart);
journal.push_op(JournalOp::ScopelessFnStart(expr.info.clone()));
}

match path.kind {
Expand All @@ -1011,7 +1012,10 @@ impl Intrinsic {
let scope = context.scope().clone();
let journal = context.journal_mut().as_mut().unwrap();
journal.commit();
journal.push_op(JournalOp::FnEnd(scope.into()));
journal.push_op(JournalOp::FnEnd(
expr.info.clone(),
scope.into(),
));
}
}

Expand Down
24 changes: 17 additions & 7 deletions stack-core/src/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::fmt;
use std::collections::HashMap;

use crate::{
expr::{Expr, ExprKind},
expr::{Expr, ExprInfo, ExprKind},
scope::Scope,
symbol::Symbol,
};
Expand Down Expand Up @@ -48,9 +48,9 @@ pub enum JournalOp {
Push(Expr),
Pop(Expr),

ScopedFnStart(JournalScope),
ScopelessFnStart,
FnEnd(JournalScope),
ScopedFnStart(Option<ExprInfo>, JournalScope),
ScopelessFnStart(Option<ExprInfo>),
FnEnd(Option<ExprInfo>, JournalScope),

ScopeDef(Symbol, Expr),
ScopeSet(Symbol, Expr, Expr),
Expand Down Expand Up @@ -96,6 +96,16 @@ impl JournalOp {
_ => None,
}
}

pub fn info(&self) -> Option<&ExprInfo> {
match self {
Self::ScopedFnStart(info, _) => info.as_ref(),
Self::ScopelessFnStart(info) => info.as_ref(),
Self::FnEnd(info, _) => info.as_ref(),

_ => self.expr().and_then(|expr| expr.info.as_ref()),
}
}
}

pub type JournalScope = HashMap<Symbol, Expr>;
Expand Down Expand Up @@ -233,7 +243,7 @@ impl Journal {
JournalOp::ScopedFnStart(..) => {
self.scope_levels.push(true);
}
JournalOp::ScopelessFnStart => {
JournalOp::ScopelessFnStart(..) => {
self.scope_levels.push(false);
}
JournalOp::FnEnd(..) => {
Expand Down Expand Up @@ -300,7 +310,7 @@ impl Journal {
) {
for op in entry.ops.iter() {
match op {
JournalOp::ScopedFnStart(scope) => {
JournalOp::ScopedFnStart(_, scope) => {
scopes.push(scope.clone());
}
JournalOp::FnEnd(..) => {
Expand Down Expand Up @@ -354,7 +364,7 @@ impl Journal {
JournalOp::ScopedFnStart(..) => {
scopes.pop();
}
JournalOp::FnEnd(scope) => {
JournalOp::FnEnd(_, scope) => {
scopes.push(scope.clone());
}
JournalOp::ScopeDef(key, ..) => {
Expand Down
2 changes: 1 addition & 1 deletion stack-core/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Lexer {
| 'a'..='z'
| 'A'..='Z' => state = State::Symbol,
// TODO: Square brackets should be checked in the parsing step.
' ' | '\n' | '\t' | '\r' | '[' | ']' => start = self.cursor + c_len,
' ' | '\n' | '\t' | '\r' => start = self.cursor + c_len,
_ => state = State::Invalid,
},
State::Invalid => match c {
Expand Down
8 changes: 4 additions & 4 deletions stack-debugger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ pub fn paint_op(op: &JournalOp, layout_job: &mut LayoutJob) {
layout_job,
),
JournalOp::SCall(expr) => append_to_job(
RichText::new(format!("{}", string_with_quotes(expr))).color(yellow),
RichText::new(string_with_quotes(expr)).color(yellow),
layout_job,
),
JournalOp::FnCall(expr) => append_to_job(
RichText::new(format!("{}", string_with_quotes(expr))).color(yellow),
RichText::new(string_with_quotes(expr)).color(yellow),
layout_job,
),
JournalOp::Push(expr) => append_to_job(
Expand All @@ -166,10 +166,10 @@ pub fn paint_op(op: &JournalOp, layout_job: &mut LayoutJob) {
RichText::new(format!("pop({})", string_with_quotes(expr))).color(red),
layout_job,
),
JournalOp::ScopedFnStart(_) => {
JournalOp::ScopedFnStart(..) => {
append_to_job(RichText::new("fn start"), layout_job);
}
JournalOp::ScopelessFnStart => {
JournalOp::ScopelessFnStart(..) => {
append_to_job(RichText::new("fn! start"), layout_job);
}
JournalOp::FnEnd(..) => {
Expand Down
121 changes: 58 additions & 63 deletions stack-debugger/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,12 @@ impl eframe::App for DebuggerApp {
);
if let Some(entry) = entry {
if let Some(first) = entry.ops.first() {
if let Some(expr) = first.expr() {
if let Some(info) = expr.info.clone() {
if let Some(location) = info.source.location(info.span.start) {
append_to_job(
RichText::new(format!("{}:{}", info.source.name(), location)),
&mut layout_job,
);
}
if let Some(info) = first.info() {
if let Some(location) = info.source.location(info.span.start) {
append_to_job(
RichText::new(format!("{}:{}", info.source.name(), location)),
&mut layout_job,
);
}
}
}
Expand All @@ -455,67 +453,64 @@ impl eframe::App for DebuggerApp {
let mut layout_job = LayoutJob::default();
if let Some(entry) = entry {
if let Some(first) = entry.ops.first() {
if let Some(expr) = first.expr() {
if let Some(info) = expr.info.clone() {
if let Some((start_loc, end_loc)) = info
.source
.location(info.span.start)
.zip(info.source.location(info.span.end))
{
const SURROUNDING_LINES: usize = 7;

let start =
start_loc.line.get().saturating_sub(SURROUNDING_LINES);
let end =
start_loc.line.get().saturating_add(SURROUNDING_LINES);

ui.add_space(5.0);
ui.label(RichText::new(info.source.name()).monospace());
ui.add_space(5.0);

for line in start..end {
if let Some(line_str) = NonZeroUsize::new(line)
.and_then(|line| info.source.line(line))
{
let mut text =
RichText::new(format!("{}: ", line)).monospace();

if line == start_loc.line.get() {
text = text.color(Color32::YELLOW);
}

append_to_job(text, &mut layout_job);
if let Some(info) = first.info() {
if let Some((start_loc, end_loc)) = info
.source
.location(info.span.start)
.zip(info.source.location(info.span.end))
{
const SURROUNDING_LINES: usize = 7;

let start =
start_loc.line.get().saturating_sub(SURROUNDING_LINES);
let end = start_loc.line.get().saturating_add(SURROUNDING_LINES);

ui.add_space(5.0);
ui.label(RichText::new(info.source.name()).monospace());
ui.add_space(5.0);

for line in start..end {
if let Some(line_str) = NonZeroUsize::new(line)
.and_then(|line| info.source.line(line))
{
let mut text =
RichText::new(format!("{}: ", line)).monospace();

if line == start_loc.line.get() {
text = text.color(Color32::YELLOW);
}

line_str.char_indices().for_each(|(i, c)| {
let mut text = RichText::new(c).monospace();
// TODO: properly support multiline exprs
//
// TODO: if the line span is greater than the surrounding lines,
// remove top surrounding lines until it fits
//
// TODO: lex and parse so we can use paint_expr
if line >= start_loc.line.into()
&& line <= end_loc.line.into()
append_to_job(text, &mut layout_job);

line_str.char_indices().for_each(|(i, c)| {
let mut text = RichText::new(c).monospace();
// TODO: properly support multiline exprs
//
// TODO: if the line span is greater than the surrounding lines,
// remove top surrounding lines until it fits
//
// TODO: lex and parse so we can use paint_expr
if line >= start_loc.line.into()
&& line <= end_loc.line.into()
{
if (i + 1) >= start_loc.column.into()
&& (i + 1) < end_loc.column.into()
{
if (i + 1) >= start_loc.column.into()
&& (i + 1) < end_loc.column.into()
{
text = text
.color(Color32::BLACK)
.background_color(Color32::YELLOW);
} else {
text = text.color(Color32::YELLOW);
}
text = text
.color(Color32::BLACK)
.background_color(Color32::YELLOW);
} else {
text = text.color(Color32::YELLOW);
}
}

append_to_job(text, &mut layout_job);
});
}
append_to_job(text, &mut layout_job);
});
}

ui.label(layout_job);
ui.add_space(5.0);
}

ui.label(layout_job);
ui.add_space(5.0);
}
}
}
Expand Down
Loading