Skip to content

Commit 15f27e1

Browse files
committed
add with modifier
1 parent 26d5299 commit 15f27e1

File tree

6 files changed

+68
-6
lines changed

6 files changed

+68
-6
lines changed

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This version is not yet released. If you are reading this on the website, then t
2020
- Add the experimental [`fft`](https://uiua.org/docs/fft) function, which performs the Fast Fourier transform
2121
- The inverse FFT is also supported via [`un °`](https://uiua.org/docs/un)
2222
- Add the experimental [`astar`](https://uiua.org/docs/astar) modifier, which performs the A* pathfinding algorithm
23+
- Add the experimental [`with ⫯`](https://uiua.org/docs/with) modifier, which is a complement to [`on ⟜`](https://uiua.org/docs/on) and [`by ⊸`](https://uiua.org/docs/by)
2324
- Adjacent [`trace ⸮`](https://uiua.org/docs/trace)s now function as a single [`trace ⸮`](https://uiua.org/docs/trace) of more values
2425
- N+1 adjacent [`stack ?`](https://uiua.org/docs/stack)s now format to N [`trace ⸮`](https://uiua.org/docs/trace)s
2526
- Add the [`&camcap`](https://uiua.org/docs/&camcap) system function, which captures a frame from a camera

site/primitives.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,6 +1248,14 @@
12481248
"class": "DyadicArray",
12491249
"description": "The n-wise windows of an array"
12501250
},
1251+
"with": {
1252+
"glyph": "",
1253+
"outputs": 1,
1254+
"modifier_args": 1,
1255+
"class": "Stack",
1256+
"description": "Call a function but keep its last argument on the top of the stack",
1257+
"experimental": true
1258+
},
12511259
"xlsx": {
12521260
"args": 1,
12531261
"outputs": 1,

site/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ input[type=number] {
740740

741741
.glyph-button {
742742
font-family: "Code Font", monospace;
743-
font-size: 0.90em;
743+
font-size: 0.89em;
744744
padding: 0.05em;
745745
margin: 0em;
746746
background-color: transparent;

src/compile/modifier.rs

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl Compiler {
447447
}};
448448
}
449449
match prim {
450-
Dip | Gap | On | By => {
450+
Dip | Gap | On | By | With => {
451451
// Compile operands
452452
let (mut instrs, sig) = self.compile_operand_word(modified.operands[0].clone())?;
453453
// Dip (|1 …) . diagnostic
@@ -488,7 +488,7 @@ impl Compiler {
488488
instrs.insert(0, Instr::Prim(Pop, span));
489489
Signature::new(sig.args + 1, sig.outputs)
490490
}
491-
On => {
491+
prim if prim == On || prim == With && sig.args <= 1 => {
492492
instrs.insert(
493493
0,
494494
if sig.args == 0 {
@@ -543,12 +543,53 @@ impl Compiler {
543543
}
544544
Signature::new(sig.args.max(1), sig.outputs + 1)
545545
}
546+
With => {
547+
let mut i = 0;
548+
if sig.args > 1 {
549+
instrs.insert(
550+
i,
551+
Instr::PushTemp {
552+
stack: TempStack::Inline,
553+
count: sig.args - 1,
554+
span,
555+
},
556+
);
557+
i += 1;
558+
}
559+
instrs.insert(i, Instr::Prim(Dup, span));
560+
i += 1;
561+
if sig.args > 1 {
562+
instrs.insert(
563+
i,
564+
Instr::PopTemp {
565+
stack: TempStack::Inline,
566+
count: sig.args - 1,
567+
span,
568+
},
569+
);
570+
}
571+
if sig.outputs > 2 {
572+
instrs.push(Instr::PushTemp {
573+
stack: TempStack::Inline,
574+
count: sig.outputs - 2,
575+
span,
576+
});
577+
for _ in 0..sig.outputs - 2 {
578+
instrs.push(Instr::Prim(Flip, span));
579+
instrs.push(Instr::PopTemp {
580+
stack: TempStack::Inline,
581+
count: 1,
582+
span,
583+
});
584+
}
585+
}
586+
instrs.push(Instr::Prim(Flip, span));
587+
Signature::new(sig.args.max(1), sig.outputs + 1)
588+
}
546589
_ => unreachable!(),
547590
};
548591
if call {
549-
// self.push_instr(Instr::PushSig(sig));
550592
self.push_all_instrs(instrs);
551-
// self.push_instr(Instr::PopSig);
552593
} else {
553594
let func =
554595
self.make_function(modified.modifier.span.clone().into(), sig, instrs);

src/primitive/defs.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,17 @@ primitive!(
16791679
/// : ⊜□⊸≠ @ "Hey there buddy"
16801680
/// : ⊕□⊸◿ 5 [2 9 5 21 10 17 3 35]
16811681
([1], By, Stack, ("by", '⊸')),
1682+
/// Call a function but keep its last argument on the top of the stack
1683+
///
1684+
/// ex: # Experimental!
1685+
/// : [⫯+ 2 5]
1686+
/// : [⫯- 2 5]
1687+
///
1688+
/// [with] can be used to copy a value from deep in the stack, or to move it.
1689+
/// ex: # Experimental!
1690+
/// : [⫯⊙⊙⊙∘ 1 2 3 4]
1691+
/// : [⫯⊙⊙⊙◌ 1 2 3 4]
1692+
([1], With, Stack, ("with", '⫯')),
16821693
/// Call a function on two sets of values
16831694
///
16841695
/// For monadic functions, [both] calls its function on each of the top 2 values on the stack.

src/primitive/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl Primitive {
386386
use SysOp::*;
387387
matches!(
388388
self,
389-
(Coordinate | Astar | Fft | Triangle | Case)
389+
With | (Coordinate | Astar | Fft | Triangle | Case)
390390
| Sys(Ffi | MemCopy | MemFree | TlsListen)
391391
| (Stringify | Quote | Sig)
392392
)
@@ -834,6 +834,7 @@ impl Primitive {
834834
| Primitive::Dip
835835
| Primitive::On
836836
| Primitive::By
837+
| Primitive::With
837838
| Primitive::Gap
838839
| Primitive::Un
839840
| Primitive::Under

0 commit comments

Comments
 (0)