Skip to content

Commit

Permalink
vine: object structs (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi authored Jan 4, 2025
1 parent c09be75 commit 0b8d300
Show file tree
Hide file tree
Showing 134 changed files with 2,889 additions and 2,353 deletions.
2 changes: 1 addition & 1 deletion dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"exts": ["vi"]
}]
},
"excludes": ["tests/programs/fail", "tests/programs/repl", "tests/snaps"],
"excludes": ["tests/programs/fail", "tests/programs/repl", "tests/programs/fmt", "tests/snaps"],
"plugins": [
"https://plugins.dprint.dev/g-plane/pretty_yaml-v0.5.0.wasm",
"https://plugins.dprint.dev/json-0.19.3.wasm",
Expand Down
19 changes: 14 additions & 5 deletions ivy/src/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,21 @@ pub struct Optimizer {
impl Optimizer {
pub fn optimize(&mut self, nets: &mut Nets) {
prune(nets);
for (_, net) in nets.iter_mut() {
self.inline_vars.apply(net);
net.eta_reduce();
self.inline_vars.apply(net);
loop {
for (_, net) in nets.iter_mut() {
loop {
self.inline_vars.apply(net);
let reduced = net.eta_reduce();
if !reduced {
break;
}
}
}
let inlined = inline_globals(nets);
if !inlined {
break;
}
}
inline_globals(nets);
prune(nets);
}
}
7 changes: 5 additions & 2 deletions ivy/src/optimize/eta_reduce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ use crate::ast::{Net, Tree};
impl Net {
/// Apply eta-reduction rules to every net; replacing `(_ _)` with `_` and
/// `(a b) ... (a b)` with `x ... x`.
pub fn eta_reduce(&mut self) {
pub fn eta_reduce(&mut self) -> bool {
let mut walker = WalkTrees::default();
for tree in self.trees() {
walker.walk_tree(tree);
}
let nodes = { walker }.nodes;
let mut reducer = ReduceTrees { nodes: &nodes, index: 0 };
let mut reducer = ReduceTrees { nodes: &nodes, index: 0, reduced: false };
for tree in self.trees_mut() {
reducer.reduce_tree(tree);
}
reducer.reduced
}
}

Expand Down Expand Up @@ -70,6 +71,7 @@ impl<'a> WalkTrees<'a> {
struct ReduceTrees<'a> {
nodes: &'a [NodeKind],
index: usize,
reduced: bool,
}

impl<'a> ReduceTrees<'a> {
Expand All @@ -87,6 +89,7 @@ impl<'a> ReduceTrees<'a> {
_ => false,
};
if reducible {
self.reduced = true;
*tree = take(a);
return ak;
}
Expand Down
7 changes: 5 additions & 2 deletions ivy/src/optimize/inline_globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ use std::collections::HashMap;
use crate::ast::{Net, Nets, Tree};

/// Inline any globals defined to be single-node nets.
pub fn inline_globals(nets: &mut Nets) {
pub fn inline_globals(nets: &mut Nets) -> bool {
let mut inliner = Inliner::default();
inliner.populate_candidates(nets);
for (_, net) in nets.iter_mut() {
for tree in net.trees_mut() {
inliner.process(tree);
}
}
inliner.inlined
}

#[derive(Debug, Default)]
struct Inliner {
candidates: HashMap<String, Tree>,
inlined: bool,
}

impl Inliner {
Expand All @@ -35,10 +37,11 @@ impl Inliner {
}
}

fn process(&self, tree: &mut Tree) {
fn process(&mut self, tree: &mut Tree) {
if let Tree::Global(name) = tree {
if let Some(inlined) = self.candidates.get(name) {
*tree = inlined.clone();
self.inlined = true;
}
} else {
for t in tree.children_mut() {
Expand Down
4 changes: 2 additions & 2 deletions tests/programs/aoc_2024/day_01.vi
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub fn main(&io: &IO) {
(left, right)
});

let left = lines.map(fn((left, _)) left);
let right = lines.map(fn((_, right)) right);
let left = lines.map(fn((left, _)) { left });
let right = lines.map(fn((_, right)) { right });

left.sort_by(N32::ascending);
right.sort_by(N32::ascending);
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_02.vi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn main(&io: &IO) {

let iter = lines.into_iter();
while iter.next() is Some(line) {
let report = line.split(" ").map(fn(x) N32::parse(x).unwrap());
let report = line.split(" ").map(fn(x) { N32::parse(x).unwrap() });
if is_safe(report) {
safe_count += 1;
dampened_safe_count += 1;
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_04.vi
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn diamond[T, M, X](grid: List[List[T]], f: fn(T, Neighbors[M], &X), d: M, state
let north_east = List::new(width, neglect_channel(d));
while grid.pop_front() is Some(row) {
let west = neglect_channel(d);
{
do {
let north = north.iter();
let north_west = north_west.iter();
let ~north_east = north_east.iter();
Expand Down
10 changes: 5 additions & 5 deletions tests/programs/aoc_2024/day_07.vi
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ pub fn main(&io: &IO) {
while nums.pop_front() is Some(num) {
let len = num.len();
let num = N64::from_n32(N32::parse(num).unwrap());
let add = results.map(fn(x: N64) x.add(num));
let mul = results.map(fn(x: N64) x.mul(num));
let add = results.map(fn(x: N64) { x.add(num) });
let mul = results.map(fn(x: N64) { x.mul(num) });
let pow = 1;
while len > 0 {
pow *= 10;
len -= 1;
}
let add2 = results2.map(fn(x: N64) x.add(num));
let mul2 = results2.map(fn(x: N64) x.mul(num));
let concat = (results ++ results2).map(fn(x: N64) x.mul(N64::from_n32(pow)).add(num));
let add2 = results2.map(fn(x: N64) { x.add(num) });
let mul2 = results2.map(fn(x: N64) { x.mul(num) });
let concat = (results ++ results2).map(fn(x: N64) { x.mul(N64::from_n32(pow)).add(num) });
results = add ++ mul;
results2 = add2 ++ mul2 ++ concat;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_10.vi
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ fn cross[T, M, X](grid: List[List[T]], f: fn(T, Neighbors[M], &X), d: M, &state:
let north = List::new(width, neglect_channel(d));
while grid.pop_front() is Some(row) {
let west = neglect_channel(d);
{
do {
let north = north.iter();
while row.pop_front() is Some(cell) {
let (w, e) = foo_channel(&west);
Expand Down
20 changes: 10 additions & 10 deletions tests/programs/aoc_2024/day_12.vi
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ pub fn main(&io: &IO) {
let &Regions(array) = &regions;
while i < array.len() {
match *array.get(i) {
Region::Root(a, p) => {
Region::Root(a, p) {
price += a * (a * 4 - p * 2);
},
_ => {},
}
_ {}
}
i += 1;
}
Expand Down Expand Up @@ -89,8 +89,8 @@ enum Region {
mod Region {
pub fn to_string(self: Region) -> String {
match self {
Region::Root(a, p) => "Root(" ++ a.to_string() ++ ", " ++ p.to_string() ++ ")",
Region::Child(n) => "Child(" ++ n.to_string() ++ ")",
Region::Root(a, p) { "Root(" ++ a.to_string() ++ ", " ++ p.to_string() ++ ")" }
Region::Child(n) { "Child(" ++ n.to_string() ++ ")" }
}
}
}
Expand Down Expand Up @@ -120,23 +120,23 @@ mod Regions {
let i = self.find(i);
let &Regions(array) = &self;
match *array.get(i) {
Region::Root(a, _) => a,
Region::Root(a, _) { a }
}
}

pub fn union_found(&Regions(array), i: N32, j: N32) -> N32 {
if i == j {
match array.get(i) {
&Region::Root(_, p) => {
&Region::Root(_, p) {
p += 1;
},
}
}
i
} else {
let &r = array.get(i);
let &s = array.get(j);
match (r, s) {
(Region::Root(ra, rp), Region::Root(sa, sp)) => {
(Region::Root(ra, rp), Region::Root(sa, sp)) {
let a = ra + sa;
let p = rp + sp + 1;
if ra < sa {
Expand All @@ -148,7 +148,7 @@ mod Regions {
s = Region::Child(i);
i
}
},
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_17.vi
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn main(&io: &IO) {
let b = N32::parse(read_value()).unwrap();
let c = N32::parse(read_value()).unwrap();

let program = read_value().split(",").map(fn(x) N32::parse(x).unwrap());
let program = read_value().split(",").map(fn(x) { N32::parse(x).unwrap() });
let program = Array::from_list(program);

let output = [];
Expand Down
4 changes: 2 additions & 2 deletions tests/programs/aoc_2024/day_18.vi
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ mod DisjointSet {
let &a_node = array.get(a);
let &b_node = array.get(b);
match (&a_node, &b_node) {
(&Node::Root(a_size), &Node::Root(b_size)) => {
(&Node::Root(a_size), &Node::Root(b_size)) {
if a_size < b_size {
a_node = Node::Child(b);
b_size += a_size;
Expand All @@ -137,7 +137,7 @@ mod DisjointSet {
a_size += b_size;
a
}
},
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/programs/aoc_2024/day_23.vi
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn main(&io: &IO) {
(*graph.get_or_insert(a, Map::new(N32::cmp))).insert(b, ());
}

{
do {
let graph = graph;
let triangles = 0;
let t_triangles = 0;
Expand Down Expand Up @@ -65,8 +65,8 @@ pub fn main(&io: &IO) {
let best = [];

while graph.remove_min() is Some((a, tips)) {
extend_clique(&graph, &best, [a], tips.to_list().map(fn((x, _)) x));
extend_clique(&graph, &best, [a], tips.to_list().map(fn((x, _)) { x }));
}

io.println("Part 2: " ++ best.map(fn(x: N32) ['a' + x / 26, 'a' + x % 26]).join(","));
io.println("Part 2: " ++ best.map(fn(x: N32) { ['a' + x / 26, 'a' + x % 26] }).join(","));
}
2 changes: 1 addition & 1 deletion tests/programs/cyclist.vi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::option::Option::Some;
pub fn main(&io: &IO) {
let list = [0];
while list.len() < 32 {
list ++= list.map(fn(x: N32) list.len() + x);
list ++= list.map(fn(x: N32) { list.len() + x });
}

while list.pop_front() is Some(val) {
Expand Down
11 changes: 11 additions & 0 deletions tests/programs/fmt/objects.vi
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

struct Foo{a:N32,b:String,c:F32,d:{p:List[N32],q:&Char}}

fn foo(Foo({a:a:N32,b:x:String,c::F32,d}))
{ Foo({ a:a,
b:x,
c,
d: {p:d.p,q:d.q}
}) }


2 changes: 1 addition & 1 deletion tests/programs/inverse.vi
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod refs {
}

fn fns(&io: &IO) {
let f = {
let f = do {
let n: N32;
(move ~n, n * n)
};
Expand Down
16 changes: 8 additions & 8 deletions tests/programs/option_party.vi
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::option::Option::{Option, Some, None};
pub fn main(&io: &IO) {
dyn fn print_option_n32(option: Option[N32]) {
io.println(" " ++ match option {
Some(val) => "Some(" ++ val.to_string() ++ ")",
None => "None",
Some(val) { "Some(" ++ val.to_string() ++ ")" }
None { "None" }
})
}

Expand All @@ -22,9 +22,9 @@ pub fn main(&io: &IO) {
}

io.println("map:");
print_option_n32(None.map(fn(x: N32) x + 1));
print_option_n32(Some(0).map(fn(x: N32) x + 1));
print_option_n32(Some(100).map(fn(x: N32) x + 1));
print_option_n32(None.map(fn(x: N32) { x + 1 }));
print_option_n32(Some(0).map(fn(x: N32) { x + 1 }));
print_option_n32(Some(100).map(fn(x: N32) { x + 1 }));

io.println("increment_option:");
let option = None;
Expand Down Expand Up @@ -83,11 +83,11 @@ pub fn main(&io: &IO) {

fn increment_option(&option: &Option[N32]) {
match option.as_ref() {
Some(&x) => x += 1,
None => {},
Some(&x) { x += 1 }
None {}
}
}

fn add_options(a: Option[N32], b: Option[N32]) -> Option[N32] {
a.and_then(fn(a: N32) b.map(fn(b: N32) a + b))
a.and_then(fn(a: N32) { b.map(fn(b: N32) { a + b }) })
}
4 changes: 2 additions & 2 deletions tests/programs/repl/misc.vi
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ fn _foo() {}
[1,2,3,4].reversed()
let a = Array::from_fn(5, &(0; _), fn(&x: &N32) { let n = x; x += 1; n });
a.for_each(&io, fn(&io: &IO, v: N32) { io.println(v.to_string() )})
-a.fold_front(0, fn(a: N32, b: N32) a - b)
-a.fold_back(5, fn(a: N32, b: N32) a - b)
-a.fold_front(0, fn(a: N32, b: N32) { a - b })
-a.fold_back(5, fn(a: N32, b: N32) { a - b })
move a
Array::from_list([1,2,3,4,5,6,7,8,9]).reversed().to_list()
List::new(100, "")
Expand Down
14 changes: 14 additions & 0 deletions tests/programs/repl/objects.vi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let x = { a: 1, c: 3, b: 2 }
(x.a, x.b, x.c)
(x.c, x.b, x.a)
let y = { x }
let z = { y }
z.y.x.a
z.y.x.a += y.x.b
let { y: { x: o } } = z
let { c, b, a } = o
a + b + c
x.p
do { let { p } = x }
z = { Y: y }
z = { y: 1 }
Loading

0 comments on commit 0b8d300

Please sign in to comment.