Skip to content

Commit

Permalink
vine: module system enhancements (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi authored Jan 4, 2025
1 parent 0b8d300 commit 8944fbb
Show file tree
Hide file tree
Showing 48 changed files with 147 additions and 103 deletions.
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_02.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}};
use std::{option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let input = io.full_input();
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_03.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}};
use std::{option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let input = io.full_input();
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
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}};
use std::{option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let lines = [];
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_05.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}};
use std::{option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let masks = List::new(100, ((0, 0), (0, 0)));
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_06.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}, array::Array};
use std::{array::Array, option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let input = io.full_input();
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_07.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}, n64::N64};
use std::{n64::N64, option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let part1 = N64::from_n32(0);
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_08.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::Some, map::{Map, Cmp, Ord}, tuple::Pair};
use std::{map::{Cmp, Map, Ord}, option::Option::Some, tuple::Pair};

pub fn main(&io: &IO) {
let input = io.full_input();
Expand Down
4 changes: 2 additions & 2 deletions tests/programs/aoc_2024/day_09.vi
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

use std::{
option::Option::{Option, Some, None},
result::Result::{Result, Ok, Err},
n64::N64,
option::Option::{Option, None, Some},
result::Result::{Result, Err, Ok},
tuple::Pair,
};

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
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}};
use std::{option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let input = io.full_input();
Expand Down
4 changes: 2 additions & 2 deletions tests/programs/aoc_2024/day_11.vi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

use std::{
option::Option::{Option, Some, None},
result::Result::{Result, Ok, Err},
map::Map,
n64::N64,
option::Option::{Option, None, Some},
result::Result::{Result, Err, Ok},
};

const max: N32 = 75;
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_12.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}, array::Array};
use std::{array::Array, option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let regions = Regions(Array::empty);
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_13.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}, n64::N64};
use std::{n64::N64, option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let input = io.full_input();
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_14.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}, array::Array};
use std::{array::Array, option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

const width: N32 = 11;
const height: N32 = 7;
Expand Down
4 changes: 2 additions & 2 deletions tests/programs/aoc_2024/day_15.vi
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

use std::{
option::Option::{Option, Some, None},
result::Result::{Result, Ok, Err},
array::Array,
option::Option::{Option, None, Some},
result::Result::{Result, Err, Ok},
tuple::Pair,
};

Expand Down
4 changes: 2 additions & 2 deletions tests/programs/aoc_2024/day_16.vi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

use std::{
option::Option::{Option, Some, None},
result::Result::{Result, Ok, Err},
array::Array,
map::Map,
option::Option::{Option, None, Some},
result::Result::{Result, Err, Ok},
};

pub fn main(&io: &IO) {
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
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}, array::Array};
use std::{array::Array, option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let input = io.full_input();
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_18.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, array::Array};
use std::{array::Array, option::Option::{Option, None, Some}};

const config: (N32, N32) = (7, 12);
// const config: (N32, N32) = (71, 1024);
Expand Down
4 changes: 2 additions & 2 deletions tests/programs/aoc_2024/day_19.vi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

use std::{
option::Option::{Option, Some, None},
result::Result::{Result, Ok, Err},
array::Array,
n64::N64,
option::Option::{Option, None, Some},
result::Result::{Result, Err, Ok},
};

pub fn main(&io: &IO) {
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_20.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}, array::Array};
use std::{array::Array, option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

const cutoff: N32 = 50;

Expand Down
4 changes: 2 additions & 2 deletions tests/programs/aoc_2024/day_21.vi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

use std::{
option::Option::{Option, Some, None},
result::Result::{Result, Ok, Err},
array::Array,
n64::N64,
option::Option::{Option, None, Some},
result::Result::{Result, Err, Ok},
};

pub fn main(&io: &IO) {
Expand Down
6 changes: 3 additions & 3 deletions tests/programs/aoc_2024/day_22.vi
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

use std::{
option::Option::{Option, Some, None},
result::Result::{Result, Ok, Err},
n64::N64,
map::Map,
n64::N64,
option::Option::{Option, None, Some},
result::Result::{Result, Err, Ok},
tuple::Pair,
};

Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_23.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}, map::Map};
use std::{map::Map, option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let graph = Map::new(N32::cmp);
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_24.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, array::Array, map::Map, n64::N64};
use std::{array::Array, map::Map, n64::N64, option::Option::{Option, None, Some}};

pub fn main(&io: &IO) {
let map = Map::new(String::cmp);
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/aoc_2024/day_25.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{option::Option::{Option, Some, None}, result::Result::{Result, Ok, Err}};
use std::{option::Option::{Option, None, Some}, result::Result::{Result, Err, Ok}};

pub fn main(&io: &IO) {
let locks = [];
Expand Down
1 change: 1 addition & 0 deletions tests/programs/fmt/uses.vi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use a::{b::c as c, b::d as D, a, f::{f}, g::{g as G}, e::e},x::y,a::h,p::{q::{}, {}}
2 changes: 1 addition & 1 deletion tests/programs/option_party.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::option::Option::{Option, Some, None};
use std::option::Option::{Option, None, Some};

pub fn main(&io: &IO) {
dyn fn print_option_n32(option: Option[N32]) {
Expand Down
2 changes: 1 addition & 1 deletion tests/programs/verbose_add.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::option::Option::{Option, Some, None};
use std::option::Option::{Option, None, Some};

pub fn main(&io: &IO) {
io.println(verbose_add(Some(1), Some(2)));
Expand Down
6 changes: 3 additions & 3 deletions tests/snaps/vine/fail/hallo_world.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error tests/programs/fail/hallo_world.vi:4:3 - duplicate definition of `println`
error tests/programs/fail/hallo_world.vi:5:3 - cannot find `print_ln` in `::std::io`
error tests/programs/fail/hallo_world.vi:3:3 - cannot find `println` in `::std::io`
error tests/programs/fail/hallo_world.vi:2:1 - duplicate definition of `println`
error tests/programs/fail/hallo_world.vi:2:1 - cannot find `print_ln` in `::std::io`
error tests/programs/fail/hallo_world.vi:2:1 - cannot find `println` in `::std::io`
error tests/programs/fail/hallo_world.vi:8:13 - invalid pattern; this path is not a struct or enum variant
error tests/programs/fail/hallo_world.vi:9:3 - cannot find `io` in `::hallo_world::main`
error tests/programs/fail/hallo_world.vi:9:14 - cannot find `hallo` in `::hallo_world::main`
Expand Down
6 changes: 3 additions & 3 deletions tests/snaps/vine/fail/visibility.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error tests/programs/fail/visibility.vi:22:3 - subitems must be private
error tests/programs/fail/visibility.vi:26:7 - invalid visibility; expected the name of an ancestor module
error tests/programs/fail/visibility.vi:27:5 - `::visibility::lib::a::b` is only visible within `::visibility::lib::a`
error tests/programs/fail/visibility.vi:28:5 - `::visibility::lib::c` is only visible within `::visibility::lib`
error tests/programs/fail/visibility.vi:29:5 - `::visibility::lib::d::e` is only visible within `::visibility::lib`
error tests/programs/fail/visibility.vi:26:3 - `::visibility::lib::a::b` is only visible within `::visibility::lib::a`
error tests/programs/fail/visibility.vi:26:3 - `::visibility::lib::c` is only visible within `::visibility::lib`
error tests/programs/fail/visibility.vi:26:3 - `::visibility::lib::d::e` is only visible within `::visibility::lib`
error - `::visibility::main` is only visible within `::visibility`
error tests/programs/fail/visibility.vi:21:24 - `::visibility::lib::y` is only visible within `::visibility::lib`
error tests/programs/fail/visibility.vi:21:7 - `::visibility::lib::y` is only visible within `::visibility::lib`
Expand Down
2 changes: 2 additions & 0 deletions tests/snaps/vine/fmt/uses.fmt.vi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

use {a::{a, b::{c, d as D}, e::e, f, g as G, h}, x::y};
1 change: 1 addition & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ fn tests(t: &mut DynTester) {

t.group("fmt", |t| {
test_vi_fmt(t, "tests/programs/fmt/objects.vi");
test_vi_fmt(t, "tests/programs/fmt/uses.vi");
});

t.group("fail", |t| {
Expand Down
2 changes: 1 addition & 1 deletion vine/examples/fib_repl.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::option::Option::{Some, None};
use std::option::Option::{None, Some};

pub fn main(&io: &IO) {
while io.prompt("> ") is Some(line) {
Expand Down
2 changes: 1 addition & 1 deletion vine/examples/guessing_game.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::{rng::Rng, option::Option::Some};
use std::{option::Option::Some, rng::Rng};

pub fn main(&io: &IO) {
let seed = io.prompt("Enter a seed: ").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion vine/examples/primeness.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::result::Result::{Result, Ok, Err};
use std::result::Result::{Result, Err, Ok};

const end: N32 = 46;

Expand Down
2 changes: 1 addition & 1 deletion vine/examples/sub_min.vi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

use std::option::Option::{Some, None};
use std::option::Option::{None, Some};

pub fn main(&io: &IO) {
let list = [4, 3, 7, 9];
Expand Down
15 changes: 11 additions & 4 deletions vine/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
collections::BTreeMap,
fmt::{self, Debug, Display, Write},
mem::take,
path::PathBuf,
Expand Down Expand Up @@ -99,11 +100,17 @@ pub struct UseItem<'core> {
pub tree: UseTree<'core>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct UseTree<'core> {
pub span: Span,
pub path: Path<'core>,
pub children: Option<Vec<UseTree<'core>>>,
pub aliases: Vec<Ident<'core>>,
pub children: BTreeMap<Ident<'core>, UseTree<'core>>,
}

impl<'core> UseTree<'core> {
pub fn prune(&mut self) -> bool {
self.children.retain(|_, tree| tree.prune());
!self.aliases.is_empty() || !self.children.is_empty()
}
}

#[derive(Debug, Clone)]
Expand Down
30 changes: 21 additions & 9 deletions vine/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<'core: 'src, 'src> Formatter<'src> {
]),
ItemKind::Use(u) => Doc::concat([
Doc(if u.absolute { "use ::" } else { "use " }),
self.fmt_use_tree(&u.tree),
Self::fmt_use_tree(None, &u.tree),
Doc(";"),
]),
ItemKind::Ivy(_) => return self.fmt_verbatim(item.span),
Expand Down Expand Up @@ -248,15 +248,27 @@ impl<'core: 'src, 'src> Formatter<'src> {
}
}

fn fmt_use_tree(&self, use_tree: &UseTree<'core>) -> Doc<'src> {
if let Some(children) = &use_tree.children {
Doc::concat([
self.fmt_path(&use_tree.path),
Doc("::"),
Doc::brace_comma(children.iter().map(|x| self.fmt_use_tree(x))),
])
fn fmt_use_tree(name: Option<Ident<'core>>, tree: &UseTree<'core>) -> Doc<'src> {
let prefix = name.iter().map(|&name| Doc::concat([Doc(name), Doc("::")]));
let aliases = tree.aliases.iter().map(|&alias| {
if Some(alias) == name {
Doc(alias)
} else {
Doc::concat([Doc(name.unwrap()), Doc(" as "), Doc(alias)])
}
});
let children = tree.children.iter().map(|(&name, child)| Self::fmt_use_tree(Some(name), child));
let len = aliases.len() + children.len();
if len == 1 {
if aliases.len() == 1 {
Doc::concat(aliases)
} else {
Doc::concat(prefix.chain(children))
}
} else {
self.fmt_path(&use_tree.path)
Doc::concat(
prefix.chain([Doc::brace_comma(aliases.chain(children).collect::<Vec<_>>().into_iter())]),
)
}
}

Expand Down
2 changes: 2 additions & 0 deletions vine/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ pub enum Token {
Mod,
#[token("use")]
Use,
#[token("as")]
As,
#[token("fn")]
Fn,
#[token("pattern")]
Expand Down
Loading

0 comments on commit 8944fbb

Please sign in to comment.