Skip to content

Commit 1bb3546

Browse files
committed
allow for private imports
1 parent 7fef6b3 commit 1bb3546

File tree

7 files changed

+34
-25
lines changed

7 files changed

+34
-25
lines changed

changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This version is not yet released. If you are reading this on the website, then t
1111
- **Breaking Change** - Numeric subscripted [`join ⊂`](https://uiua.org/docs/join) now joins that many arrays
1212
- **Breaking Change** - Remove combining `e` number literals
1313
- It was confusing when combined with `e` scientific number notation
14-
- Allow for private [modules](https://uiua.org/tutorial/modules#private-scoped-modules) and [data definitions](https://uiua.org/tutorial/datadefs#visibility)
14+
- Allow for private [modules](https://uiua.org/tutorial/modules#private-scoped-modules), [imports](/tutorial/modules#private-imports), and [data definitions](https://uiua.org/tutorial/datadefs#visibility)
1515
- Add array pack syntactic sugar. This lets you write code like `[⊃(+|×)]` as `⊃[+|×]`.
1616
- Subscripts can now be typed with `,` instead of `__`s
1717
- Add numeric subscripts for [`keep ▽`](https://uiua.org/docs/keep) to keep along a number of dimensions

parser/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ pub struct Import {
145145
pub name: Option<Sp<Ident>>,
146146
/// The span of the ~
147147
pub tilde_span: CodeSpan,
148+
/// Whether the import is public
149+
pub public: bool,
148150
/// The import path
149151
pub path: Sp<String>,
150152
/// The import lines

parser/src/parse.rs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -482,26 +482,6 @@ impl Parser<'_> {
482482
array_macro,
483483
})
484484
}
485-
fn import_init(&mut self) -> Option<(Option<Sp<Ident>>, CodeSpan, Sp<String>)> {
486-
let start = self.index;
487-
// Name
488-
let name = self.ident();
489-
self.spaces();
490-
// Tilde
491-
let Some(tilde_span) = self.exact(Tilde.into()) else {
492-
self.index = start;
493-
return None;
494-
};
495-
self.spaces();
496-
// Path
497-
let Some(path) = self.next_token_map(Token::as_string) else {
498-
self.index = start;
499-
return None;
500-
};
501-
let path = path.map(Into::into);
502-
self.spaces();
503-
Some((name, tilde_span, path))
504-
}
505485
fn binding(&mut self) -> Option<Binding> {
506486
let BindingInit {
507487
name,
@@ -785,7 +765,29 @@ impl Parser<'_> {
785765
}
786766
}
787767
fn import(&mut self) -> Option<Import> {
788-
let (name, tilde_span, path) = self.import_init()?;
768+
let start = self.index;
769+
// Name
770+
let name = self.ident();
771+
self.spaces();
772+
// Tilde
773+
let Some((tilde_span, public)) =
774+
(self.exact(Tilde.into()).map(|s| (s, true))).or_else(|| {
775+
self.exact(TildeStroke)
776+
.or_else(|| self.exact(DoubleTilde.into()))
777+
.map(|s| (s, false))
778+
})
779+
else {
780+
self.index = start;
781+
return None;
782+
};
783+
self.spaces();
784+
// Path
785+
let Some(path) = self.next_token_map(Token::as_string) else {
786+
self.index = start;
787+
return None;
788+
};
789+
let path = path.map(Into::into);
790+
self.spaces();
789791
// Items
790792
let mut lines: Vec<Option<ImportLine>> = Vec::new();
791793
let mut line: Option<ImportLine> = None;
@@ -842,6 +844,7 @@ impl Parser<'_> {
842844
Some(Import {
843845
name,
844846
tilde_span,
847+
public,
845848
path,
846849
lines,
847850
})

site/src/tutorial.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,6 +1591,9 @@ fn TutorialModules() -> impl IntoView {
15911591
<p>"To enter this arrow, you can put a "<code>"~"</code>" after a binding's normal "<code>"←"</code>" or "<code>"="</code>"."</p>
15921592
<p>"Try formatting the following example to see how this works."</p>
15931593
<Editor example="A = +1\nB ← +2\nC =~ +3\nD ←~ +4"/>
1594+
<Hd3 id="private-imports">"Private Imports"</Hd3>
1595+
<p>"Imports can be made private by using a "<code>"≁"</code>" instead of the first "<code>"~"</code>". "<code>"≁"</code>" formats from "<code>"~~"</code>"."</p>
1596+
<Editor example="# Try formatting!\n┌─╴M\n Ex ~~ \"example\"\n└─╴\nM~Ex~Foo"/> // Should fail
15941597
<Hd3 id="private-scoped-modules">"Private Scoped Modules"</Hd3>
15951598
<p>"Scoped modules can be made private with special delimiters "<code>"┌╶╶"</code>" and "<code>"└╶╶"</code>". These format from the normal delimiters or "<code>"---"</code>"s followed by a "<code>"~"</code>"."</p>
15961599
<Editor example="┌─╴A\n ┌╶╶B\n C ← 5\n └╶╶\n└─╴\nA~B~C"/> // Should fail

site/text/data_defs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Abilities!(°⊸Con 12 °⊸Dex 16 New)
118118

119119
### Visibility
120120

121-
Like bindings, data definitions can be made private with a special glyph. `` formats from `~~` and makes a data definition private.
121+
Like imports, data definitions can be made private by using `` instead of `~`. `` formats from `~~`.
122122

123123
```uiua should fail
124124
┌─╴M

src/compile/binding.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl Compiler {
543543
self.next_global += 1;
544544
let local = LocalName {
545545
index: global_index,
546-
public: true,
546+
public: import.public,
547547
};
548548
self.asm.add_binding_at(
549549
local,

src/format.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -873,7 +873,8 @@ impl Formatter<'_> {
873873
self.output.push(' ');
874874
self.prev_import_function = Some(name.value.clone());
875875
}
876-
self.output.push_str("~ ");
876+
self.output
877+
.push_str(if import.public { "~ " } else { "≁ " });
877878
self.push(&import.path.span, &format!("{:?}", import.path.value));
878879

879880
let mut import = import.clone();

0 commit comments

Comments
 (0)