Skip to content

Commit 3c90c70

Browse files
committed
add ^ shorthand
1 parent 0373155 commit 3c90c70

File tree

9 files changed

+25
-19
lines changed

9 files changed

+25
-19
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This version is not yet released. If you are reading this on the website, then t
2727
- Add numeric subscripts for [`keep ▽`](https://uiua.org/docs/keep) to keep along a number of dimensions
2828
- Add [`under ⍜`](https://uiua.org/docs/under) capability to [`fork ⊃`](https://uiua.org/docs/fork)s of monadic functions
2929
- This allows using [`un °`](https://uiua.org/docs/un)[`by ⊸`](https://uiua.org/docs/by)[`fork ⊃`](https://uiua.org/docs/fork) to set multiple properties at once
30+
- Add `^` shorthand for `^0` in index macros
3031
- Stabilize [`self ˙`](https://uiua.org/docs/self)
3132
- Add experimental sided subscripts for [`self ˙`](https://uiua.org/docs/self)
3233
- Add experimental sided subscripts for [`backward ˜`](https://uiua.org/docs/backward)

parser/src/ast.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ pub enum Word {
348348
Pack(FunctionPack),
349349
Primitive(Primitive),
350350
Modified(Box<Modified>),
351-
Placeholder(usize),
351+
Placeholder(Option<usize>),
352352
Comment(String),
353353
Spaces,
354354
BreakLine,
@@ -469,7 +469,8 @@ impl fmt::Debug for Word {
469469
Word::Modified(modified) => modified.fmt(f),
470470
Word::Spaces => write!(f, "' '"),
471471
Word::Comment(comment) => write!(f, "# {comment}"),
472-
Word::Placeholder(op) => write!(f, "^{op}"),
472+
Word::Placeholder(Some(i)) => write!(f, "^{i}"),
473+
Word::Placeholder(None) => write!(f, "^"),
473474
Word::BreakLine => write!(f, "break_line"),
474475
Word::FlipLine => write!(f, "unbreak_line"),
475476
Word::SemanticComment(comment) => write!(f, "{comment}"),

parser/src/lex.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ pub enum Token {
609609
MultilineFormatStr(Vec<String>),
610610
Simple(AsciiToken),
611611
Glyph(Primitive),
612-
Placeholder(usize),
612+
Placeholder(Option<usize>),
613613
Subscr(Subscript),
614614
LeftArrow,
615615
LeftStrokeArrow,
@@ -674,7 +674,7 @@ impl Token {
674674
_ => None,
675675
}
676676
}
677-
pub(crate) fn as_placeholder(&self) -> Option<usize> {
677+
pub(crate) fn as_placeholder(&self) -> Option<Option<usize>> {
678678
match self {
679679
Token::Placeholder(i) => Some(*i),
680680
_ => None,
@@ -741,7 +741,8 @@ impl fmt::Display for Token {
741741
Token::OpenModule => write!(f, "┌─╴"),
742742
Token::OpenPrivateModule => write!(f, "┌╶╶"),
743743
Token::CloseModule => write!(f, "└─╴"),
744-
Token::Placeholder(i) => write!(f, "^{i}"),
744+
Token::Placeholder(Some(i)) => write!(f, "^{i}"),
745+
Token::Placeholder(None) => write!(f, "^"),
745746
}
746747
}
747748
}
@@ -763,7 +764,6 @@ pub enum AsciiToken {
763764
DoubleSemicolon,
764765
Star,
765766
Percent,
766-
Caret,
767767
Equal,
768768
EqualTilde,
769769
BangEqual,
@@ -791,7 +791,6 @@ impl fmt::Display for AsciiToken {
791791
AsciiToken::DoubleSemicolon => write!(f, ";;"),
792792
AsciiToken::Star => write!(f, "*"),
793793
AsciiToken::Percent => write!(f, "%"),
794-
AsciiToken::Caret => write!(f, "^"),
795794
AsciiToken::Equal => write!(f, "="),
796795
AsciiToken::BangEqual => write!(f, "!="),
797796
AsciiToken::EqualTilde => write!(f, "=~"),
@@ -1083,9 +1082,9 @@ impl<'a> Lexer<'a> {
10831082
"%" => self.end(Percent, start),
10841083
"^" => {
10851084
if let Some(x) = self.next_char_if(|c| c.chars().all(|c| c.is_ascii_digit())) {
1086-
self.end(Placeholder(x.parse().unwrap()), start)
1085+
self.end(Placeholder(Some(x.parse().unwrap())), start)
10871086
} else {
1088-
self.end(Caret, start)
1087+
self.end(Placeholder(None), start)
10891088
}
10901089
}
10911090
"=" if self.next_char_exact("~") => self.end(EqualTilde, start),

parser/src/parse.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ impl Parser<'_> {
464464
} else {
465465
glyph_span
466466
};
467-
let array_macro = if let Some(span) = self.exact(Caret.into()) {
467+
let array_macro = if let Some(span) = self.exact(Placeholder(None)) {
468468
arrow_span = arrow_span.merge(span);
469469
true
470470
} else {
@@ -1612,7 +1612,7 @@ impl Parser<'_> {
16121612
Some(if branches.is_empty() {
16131613
// Normal func
16141614
let reset = self.index;
1615-
let caret_span = self.exact(Caret.into());
1615+
let caret_span = self.exact(Placeholder(None));
16161616
if let Some(ident) = self
16171617
.ident()
16181618
.filter(|ident| !is_array && ident.value.chars().all(|c| "!‼".contains(c)))
@@ -2009,7 +2009,7 @@ pub fn max_placeholder(words: &[Sp<Word>]) -> Option<usize> {
20092009
};
20102010
for word in words {
20112011
match &word.value {
2012-
Word::Placeholder(i) => set(Some(*i)),
2012+
Word::Placeholder(i) => set(Some(i.unwrap_or(0))),
20132013
Word::Strand(items) => set(max_placeholder(items)),
20142014
Word::Array(arr) => {
20152015
for line in arr.word_lines() {

site/text/macros.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ F‼(×2|+1) 5
4747
F‼(+1|⊂0) 1
4848
```
4949

50+
Because macros using a only `^0` placeholders are very common, `^0` can be written as just `^`.
51+
52+
```uiua
53+
F! ← ^⇌^
54+
F!(⊂1) 5
55+
```
56+
5057
## Two Kinds of Macros
5158
The macros described so far are called *index macros*, because arguments are referenced directly by their position when the macro is called.
5259

src/compile/modifier.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,8 @@ impl Compiler {
18381838
let mut error = None;
18391839
recurse_words_mut(words, &mut |word| match &mut word.value {
18401840
Word::Placeholder(n) => {
1841-
if let Some(replacement) = initial.get(*n) {
1841+
let n = n.unwrap_or(0);
1842+
if let Some(replacement) = initial.get(n) {
18421843
*word = replacement.clone();
18431844
} else {
18441845
error = Some(self.error(

src/format.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,8 @@ impl Formatter<'_> {
11001100
self.format_modifier(&m.modifier, depth);
11011101
self.format_words(&m.operands, true, depth);
11021102
}
1103-
Word::Placeholder(i) => self.push(&word.span, &format!("^{i}")),
1103+
Word::Placeholder(Some(i)) => self.push(&word.span, &format!("^{i}")),
1104+
Word::Placeholder(None) => self.push(&word.span, "^"),
11041105
Word::Subscripted(sub) => match &sub.word.value {
11051106
Word::Modified(m) => {
11061107
if sub.script.value.num.is_some()

src/lsp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub enum SpanKind {
3434
Label,
3535
Signature,
3636
Whitespace,
37-
Placeholder(usize),
37+
Placeholder(Option<usize>),
3838
Delimiter,
3939
LexOrder,
4040
FuncDelim(Signature, SetInverses),

todo.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Uiua Todo
22

33
# 0.17
4-
- `recur` modifier
5-
- LSP optimizations
6-
- Fix rename
7-
- Fix goto references
84
- Sided `join`
95
- Sided `box`
106
- `table` subscripts for rank selection

0 commit comments

Comments
 (0)