Skip to content

Commit

Permalink
Merge pull request #49 from metaborg/better-visualization
Browse files Browse the repository at this point in the history
redo of sg visualization code
  • Loading branch information
jdonszelmann authored Jun 4, 2024
2 parents b7c235a + 6b21754 commit 768aa42
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 129 deletions.
3 changes: 1 addition & 2 deletions scopegraphs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ rust-version = "1.75"
futures = { version = "0.3.30", default-features = false, features = ["std"] }

bumpalo = "3.14.0"
dot = { version = "0.1.4", optional = true }
scopegraphs-prust-lib = { version = "0.1.0" }
log = "0.4.20"

Expand All @@ -34,7 +33,7 @@ winnow = "0.6.8"

[features]
default = ["dot", "dynamic-regex"]
dot = ["scopegraphs-regular-expressions/dot", "scopegraphs-macros/dot", "dep:dot"]
dot = ["scopegraphs-regular-expressions/dot", "scopegraphs-macros/dot"]
dynamic-regex = ["scopegraphs-regular-expressions/dynamic"]
documentation = []

Expand Down
63 changes: 55 additions & 8 deletions scopegraphs/examples/records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ use crate::resolve::{resolve_lexical_ref, resolve_member_ref, resolve_record_ref
use async_recursion::async_recursion;
use futures::future::{join, join_all};
use scopegraphs::completeness::FutureCompleteness;
use scopegraphs::RenderScopeData;
use scopegraphs::render::{EdgeStyle, EdgeTo, RenderScopeData, RenderScopeLabel, RenderSettings};
use scopegraphs::{Scope, ScopeGraph, Storage};
use scopegraphs_macros::Label;
use smol::channel::{bounded, Sender};
use smol::LocalExecutor;
use std::cell::RefCell;
use std::error::Error;
use std::fmt::{Debug, Formatter};
use std::fs::File;
use std::fmt::{Debug, Display, Formatter};
use std::future::Future;
use std::rc::Rc;

Expand All @@ -22,6 +21,17 @@ enum SgLabel {
Lexical,
}

impl RenderScopeLabel for SgLabel {
fn render(&self) -> String {
match self {
SgLabel::TypeDefinition => "typ",
SgLabel::Definition => "def",
SgLabel::Lexical => "lex",
}
.to_string()
}
}

#[derive(Debug, Default, Hash, Eq, PartialEq, Clone)]
enum SgData {
VarDecl {
Expand All @@ -38,13 +48,38 @@ enum SgData {
}

impl RenderScopeData for SgData {
fn render(&self) -> Option<String> {
fn render_node(&self) -> Option<String> {
match self {
SgData::VarDecl { name, .. } | SgData::TypeDecl { name, .. } => Some(name.to_string()),
SgData::Nothing => None,
}
}

fn render_node_label(&self) -> Option<String> {
match self {
SgData::VarDecl { name, ty } => Some(format!("var {name}: {ty:?}")),
SgData::TypeDecl { name, scope } => Some(format!("record {name} -> {scope:?}")),
SgData::VarDecl { ty, .. } => {
if matches!(ty, PartialType::Variable(_)) {
None
} else {
Some(ty.to_string())
}
}
SgData::TypeDecl { .. } => None,
SgData::Nothing => None,
}
}

fn extra_edges(&self) -> Vec<EdgeTo> {
if let SgData::TypeDecl { scope, .. } = self {
vec![EdgeTo {
to: *scope,
edge_style: EdgeStyle {},
label_text: "fields".to_string(),
}]
} else {
Vec::new()
}
}
}

impl SgData {
Expand Down Expand Up @@ -80,6 +115,16 @@ enum PartialType {
Int,
}

impl Display for PartialType {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
PartialType::Variable(v) => write!(f, "#{}", v.0),
PartialType::Record { name, .. } => write!(f, "record {name}"),
PartialType::Int => write!(f, "int"),
}
}
}

#[derive(Default)]
pub struct UnionFind {
/// Records the parent of each type variable.
Expand Down Expand Up @@ -601,7 +646,10 @@ fn typecheck(ast: &Program) -> Option<Type> {
});

tc.sg
.render(&mut File::create("sg.dot").unwrap(), "sg")
.render_to(
"sg.dot",
RenderSettings::default().with_name("example with records"),
)
.unwrap();
println!("{:?}", tc.uf.borrow());

Expand Down Expand Up @@ -808,7 +856,6 @@ main = letrec
a = new A {x: 4, b: b};
b = new B {x: 3, a: a};
in a.b.a.x;
",
)
.map_err(|i| i.to_string())?;
Expand Down
4 changes: 1 addition & 3 deletions scopegraphs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ mod label;
pub use label::Label;

#[cfg(feature = "dot")]
mod render;
#[cfg(feature = "dot")]
pub use render::RenderScopeData;
pub mod render;

pub use scopegraphs_regular_expressions::*;

Expand Down
116 changes: 0 additions & 116 deletions scopegraphs/src/render.rs

This file was deleted.

Loading

0 comments on commit 768aa42

Please sign in to comment.