Skip to content

Commit

Permalink
optimized scoped fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
LighghtEeloo committed May 30, 2024
1 parent 5ddcd9c commit 85970f2
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 162 deletions.
14 changes: 14 additions & 0 deletions zydeco-lang/lib/std/lib.zy
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ end



pub extern write_str : Thunk(String -> Thunk(OS) -> OS) end
pub extern read_line : Thunk(Thunk(String -> OS) -> OS) end
pub extern read_line_as_int : Thunk(Thunk(Option Int -> OS) -> OS) end
pub extern read_till_eof : Thunk(Thunk(String -> OS) -> OS) end
// pub extern arg_list : Thunk(Thunk(List String -> OS) -> OS) end
pub extern random_int : Thunk(Thunk(Int -> OS) -> OS) end
pub extern exit : Thunk(Int -> OS) end


main
! exit 0
end



codata IO (A: VType) where
| .run : Thunk (A -> OS) -> OS
Expand Down
60 changes: 30 additions & 30 deletions zydeco-lang/surface/src/bitter/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,37 +471,37 @@ impl Ugly for Literal {
// }
// }

impl Ugly for UsePath {
fn ugly(&self, f: &Formatter) -> String {
let mut s = String::new();
let UsePath(u) = self;
s += &u.ugly(f);
s
}
}
// impl Ugly for UsePath {
// fn ugly(&self, f: &Formatter) -> String {
// let mut s = String::new();
// let UsePath(u) = self;
// s += &u.ugly(f);
// s
// }
// }

impl Ugly for UseEnum {
fn ugly(&self, f: &Formatter) -> String {
let mut s = String::new();
match self {
| UseEnum::Name(n) => s += &n.ugly(f),
| UseEnum::Alias(UseAlias(binder, origin)) => {
s += &binder.ugly(f);
s += " = ";
s += &origin.ugly(f);
}
| UseEnum::All(UseAll) => {
s += "..";
}
| UseEnum::Cluster(Uses(u)) => {
s += "( ";
s += &u.iter().map(|u| u.ugly(f)).collect::<Vec<_>>().join(", ");
s += " )";
}
}
s
}
}
// impl Ugly for UseEnum {
// fn ugly(&self, f: &Formatter) -> String {
// let mut s = String::new();
// match self {
// | UseEnum::Name(n) => s += &n.ugly(f),
// | UseEnum::Alias(UseAlias(binder, origin)) => {
// s += &binder.ugly(f);
// s += " = ";
// s += &origin.ugly(f);
// }
// | UseEnum::All(UseAll) => {
// s += "..";
// }
// | UseEnum::Cluster(Uses(u)) => {
// s += "( ";
// s += &u.iter().map(|u| u.ugly(f)).collect::<Vec<_>>().join(", ");
// s += " )";
// }
// }
// s
// }
// }

// impl Ugly for UseDef {
// fn ugly(&self, f: &Formatter) -> String {
Expand Down
30 changes: 12 additions & 18 deletions zydeco-lang/surface/src/package/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,18 @@ impl Package {
.collect::<Vec<_>>();
println!("\tscc[{}]", cnt);
for victims in grouped_victims {
println!(
"\t\t{:?}",
victims
.iter()
.map(|decl| {
let decl = &pack.arena.decls[decl];
match decl {
| sc::Declaration::Alias(sc::Alias { binder, .. }) => {
binder.ugly(&Formatter::new(&pack.arena))
}
| sc::Declaration::Extern(sc::Extern { binder, .. }) => {
binder.ugly(&Formatter::new(&pack.arena))
}
| sc::Declaration::Main(_) => "$main".into(),
}
})
.collect::<Vec<_>>()
);
for victim in &victims {
println!("\t\t| {}", {
let mut s = victim.ugly(&Formatter::new(&pack.arena));
let budget = 80;
if s.len() > budget {
s.truncate(budget - 3);
s.push_str("...");
}
s
});
}
println!("\t\t+");
scc.release(victims);
}
cnt += 1;
Expand Down
114 changes: 4 additions & 110 deletions zydeco-lang/surface/src/scoped/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ impl Ugly for TopLevel {

impl Ugly for DefId {
fn ugly(&self, f: &Formatter) -> String {
let mut s = String::new();
let name = &f.arena.defs[self];
name.ugly(f)
s += &name.ugly(f);
s += &self.concise();
s
}
}

Expand Down Expand Up @@ -161,25 +164,6 @@ impl Ugly for VarName {
}
}

impl<T> Ugly for NameRef<T>
where
T: Ugly,
{
fn ugly(&self, f: &Formatter) -> String {
let mut s = String::new();
let NameRef(root, path, name) = self;
if *root {
s += "root/";
}
for p in path {
s += &p.ugly(f);
s += "/";
}
s += &name.ugly(f);
s
}
}

impl Ugly for CtorName {
fn ugly(&self, _f: &Formatter) -> String {
let CtorName(name) = self;
Expand Down Expand Up @@ -456,96 +440,6 @@ impl Ugly for Literal {
}
}

// impl Ugly for UseBind {
// fn ugly(&self, f: &Formatter) -> String {
// let mut s = String::new();
// let UseBind { uses, tail } = self;
// s += "use ";
// s += &uses.ugly(f);
// s += " in ";
// s += &tail.ugly(f);
// s
// }
// }

impl Ugly for UsePath {
fn ugly(&self, f: &Formatter) -> String {
let mut s = String::new();
let UsePath(u) = self;
s += &u.ugly(f);
s
}
}

impl Ugly for UseEnum {
fn ugly(&self, f: &Formatter) -> String {
let mut s = String::new();
match self {
| UseEnum::Name(n) => s += &n.ugly(f),
| UseEnum::Alias(UseAlias(binder, origin)) => {
s += &binder.ugly(f);
s += " = ";
s += &origin.ugly(f);
}
| UseEnum::All(UseAll) => {
s += "..";
}
| UseEnum::Cluster(Uses(u)) => {
s += "( ";
s += &u.iter().map(|u| u.ugly(f)).collect::<Vec<_>>().join(", ");
s += " )";
}
}
s
}
}

// impl Ugly for UseDef {
// fn ugly(&self, f: &Formatter) -> String {
// let mut s = String::new();
// let UseDef(u) = self;
// s += "use ";
// s += &u.ugly(f);
// s += " end";
// s
// }
// }

// impl Ugly for UseBlock {
// fn ugly(&self, f: &Formatter) -> String {
// let mut s = String::new();
// let UseBlock { uses, top } = self;
// s += "use ";
// s += &uses.ugly(f);
// s += " where\n";
// s += &top.ugly(f);
// s += "\nend";
// s
// }
// }

// impl Ugly for Layer {
// fn ugly(&self, f: &Formatter) -> String {
// let mut s = String::new();
// let Layer { name, uses, top } = self;
// if let Some(name) = name {
// s += "layer ";
// s += &name.ugly(f);
// }
// for Modifiers { public, inner } in uses {
// if *public {
// s += " pub";
// }
// s += " use ";
// s += &inner.ugly(f);
// }
// s += " where\n";
// s += &top.ugly(f);
// s += "\nend";
// s
// }
// }

impl Ugly for Alias {
fn ugly(&self, f: &Formatter) -> String {
let mut s = String::new();
Expand Down
9 changes: 5 additions & 4 deletions zydeco-lang/surface/src/scoped/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,11 @@ impl Resolve for TopLevel {
)?;
break 'out;
}
Err(ResolveError::UndefinedPrimitive({
let (name, def) = binders.iter().next().unwrap();
resolver.spans.defs[def].clone().make(name.clone())
}))?
// Note: the rest may be valid, but we don't know yet; no error is given here
// Err(ResolveError::UndefinedPrimitive({
// let (name, def) = binders.iter().next().unwrap();
// resolver.spans.defs[def].clone().make(name.clone())
// }))?
}
resolver.check_duplicate_and_update_global(id, binders, &mut global)?;
}
Expand Down
6 changes: 6 additions & 0 deletions zydeco-lang/utils/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,12 @@ macro_rules! new_key_type {
}
}

impl $name {
pub fn concise(&self) -> String {
format!("[{:?}#{:?}]", self.0, self.1)
}
}

$crate::new_key_type!($($rest)*);
};

Expand Down

0 comments on commit 85970f2

Please sign in to comment.