Skip to content

Commit

Permalink
add experimental self modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jan 17, 2025
1 parent fa520d3 commit 6fea90a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This version is not yet released. If you are reading this on the website, then t
- Add the [`# External!`](https://www.uiua.org/tutorial/documentation#external) semantic comment to mark functions that are provided via Rust code
- These functions don't require a Uiua implementation and will show up in the LSP
- Calling an `# External!` function that hasn't been bound will throw an error
- Add experimental [`self ˙`](https://uiua.org/docs/self) modifier
- Add experimental subscripts to [`negate ¯`](https://uiua.org/docs/negate)
- This will [`multiply ×`](https://uiua.org/docs/multiply) a number by the Nth root of unity
### Interpreter
Expand Down
28 changes: 28 additions & 0 deletions src/compile/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,34 @@ impl Compiler {
let span = self.add_span(modified.modifier.span.clone());
Node::Mod(prim, eco_vec![sn], span)
}
Slf => {
let (SigNode { mut node, sig }, _) = self.monadic_modifier_op(modified)?;
match sig.args {
0 => {
let span = self.add_span(modified.modifier.span.clone());
node.push(Node::Prim(Dup, span));
node
}
1 => {
self.emit_diagnostic(
format!(
"Remove {} here, as it does nothing for monadic functions",
Slf.format(),
),
DiagnosticKind::Style,
modified.modifier.span.clone(),
);
node
}
n => {
let span = self.add_span(modified.modifier.span.clone());
for _ in 0..n - 1 {
node.prepend(Node::Prim(Dup, span))
}
node
}
}
}
Backward => {
let (SigNode { mut node, sig }, _) = self.monadic_modifier_op(modified)?;
match sig.args {
Expand Down
13 changes: 13 additions & 0 deletions src/primitive/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1938,6 +1938,19 @@ primitive!(
///
/// See also: [above]
([1], Below, Stack, ("below", '◡')),
/// Call a function with the same array as all arguments
///
/// ex: # Experimental!
/// : ˙+ 5
/// ex: # Experimental!
/// : ˙⊞+ 1_2_3
/// ex: # Experimental!
/// : ˙(⊂⊂) π
/// [self] on a noadic function duplicates the output. This makes it distinct from [both] on a monadic function.
/// ex: # Experimental!
/// : ⌊×10[˙⚂]
/// : ⌊×10[∩⚂]
([1], Slf, Stack, ("self", '˙')),
/// Call a function with its arguments reversed
///
/// This is a modifier version of [flip].
Expand Down
3 changes: 2 additions & 1 deletion src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ static ALIASES: Lazy<HashMap<Primitive, &[&str]>> = Lazy::new(|| {
(Primitive::Utf8, &["utf", "utf__8"]),
(Primitive::First, &["fst"]),
(Primitive::Last, &["lst"]),
(Primitive::Slf, &["slf"]),
(Primitive::ImageEncode, &["&ime", "imen"]),
(Primitive::GifEncode, &["&gife", "gifen"]),
(Primitive::AudioEncode, &["&ae", "auden"]),
Expand Down Expand Up @@ -540,7 +541,7 @@ impl Primitive {
use SysOp::*;
matches!(
self,
(Reach | Backward | Above | Around)
(Reach | Slf | Backward | Above | Around)
| (Or | Base | Fft | Layout | Binary)
| Astar
| (Derivative | Integral)
Expand Down

0 comments on commit 6fea90a

Please sign in to comment.