Skip to content

Commit

Permalink
add with modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Jun 12, 2024
1 parent 26d5299 commit 15f27e1
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This version is not yet released. If you are reading this on the website, then t
- Add the experimental [`fft`](https://uiua.org/docs/fft) function, which performs the Fast Fourier transform
- The inverse FFT is also supported via [`un °`](https://uiua.org/docs/un)
- Add the experimental [`astar`](https://uiua.org/docs/astar) modifier, which performs the A* pathfinding algorithm
- 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)
- Adjacent [`trace ⸮`](https://uiua.org/docs/trace)s now function as a single [`trace ⸮`](https://uiua.org/docs/trace) of more values
- N+1 adjacent [`stack ?`](https://uiua.org/docs/stack)s now format to N [`trace ⸮`](https://uiua.org/docs/trace)s
- Add the [`&camcap`](https://uiua.org/docs/&camcap) system function, which captures a frame from a camera
Expand Down
8 changes: 8 additions & 0 deletions site/primitives.json
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,14 @@
"class": "DyadicArray",
"description": "The n-wise windows of an array"
},
"with": {
"glyph": "",
"outputs": 1,
"modifier_args": 1,
"class": "Stack",
"description": "Call a function but keep its last argument on the top of the stack",
"experimental": true
},
"xlsx": {
"args": 1,
"outputs": 1,
Expand Down
2 changes: 1 addition & 1 deletion site/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ input[type=number] {

.glyph-button {
font-family: "Code Font", monospace;
font-size: 0.90em;
font-size: 0.89em;
padding: 0.05em;
margin: 0em;
background-color: transparent;
Expand Down
49 changes: 45 additions & 4 deletions src/compile/modifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ impl Compiler {
}};
}
match prim {
Dip | Gap | On | By => {
Dip | Gap | On | By | With => {
// Compile operands
let (mut instrs, sig) = self.compile_operand_word(modified.operands[0].clone())?;
// Dip (|1 …) . diagnostic
Expand Down Expand Up @@ -488,7 +488,7 @@ impl Compiler {
instrs.insert(0, Instr::Prim(Pop, span));
Signature::new(sig.args + 1, sig.outputs)
}
On => {
prim if prim == On || prim == With && sig.args <= 1 => {
instrs.insert(
0,
if sig.args == 0 {
Expand Down Expand Up @@ -543,12 +543,53 @@ impl Compiler {
}
Signature::new(sig.args.max(1), sig.outputs + 1)
}
With => {
let mut i = 0;
if sig.args > 1 {
instrs.insert(
i,
Instr::PushTemp {
stack: TempStack::Inline,
count: sig.args - 1,
span,
},
);
i += 1;
}
instrs.insert(i, Instr::Prim(Dup, span));
i += 1;
if sig.args > 1 {
instrs.insert(
i,
Instr::PopTemp {
stack: TempStack::Inline,
count: sig.args - 1,
span,
},
);
}
if sig.outputs > 2 {
instrs.push(Instr::PushTemp {
stack: TempStack::Inline,
count: sig.outputs - 2,
span,
});
for _ in 0..sig.outputs - 2 {
instrs.push(Instr::Prim(Flip, span));
instrs.push(Instr::PopTemp {
stack: TempStack::Inline,
count: 1,
span,
});
}
}
instrs.push(Instr::Prim(Flip, span));
Signature::new(sig.args.max(1), sig.outputs + 1)
}
_ => unreachable!(),
};
if call {
// self.push_instr(Instr::PushSig(sig));
self.push_all_instrs(instrs);
// self.push_instr(Instr::PopSig);
} else {
let func =
self.make_function(modified.modifier.span.clone().into(), sig, instrs);
Expand Down
11 changes: 11 additions & 0 deletions src/primitive/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,17 @@ primitive!(
/// : ⊜□⊸≠ @ "Hey there buddy"
/// : ⊕□⊸◿ 5 [2 9 5 21 10 17 3 35]
([1], By, Stack, ("by", '⊸')),
/// Call a function but keep its last argument on the top of the stack
///
/// ex: # Experimental!
/// : [⫯+ 2 5]
/// : [⫯- 2 5]
///
/// [with] can be used to copy a value from deep in the stack, or to move it.
/// ex: # Experimental!
/// : [⫯⊙⊙⊙∘ 1 2 3 4]
/// : [⫯⊙⊙⊙◌ 1 2 3 4]
([1], With, Stack, ("with", '⫯')),
/// Call a function on two sets of values
///
/// For monadic functions, [both] calls its function on each of the top 2 values on the stack.
Expand Down
3 changes: 2 additions & 1 deletion src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl Primitive {
use SysOp::*;
matches!(
self,
(Coordinate | Astar | Fft | Triangle | Case)
With | (Coordinate | Astar | Fft | Triangle | Case)
| Sys(Ffi | MemCopy | MemFree | TlsListen)
| (Stringify | Quote | Sig)
)
Expand Down Expand Up @@ -834,6 +834,7 @@ impl Primitive {
| Primitive::Dip
| Primitive::On
| Primitive::By
| Primitive::With
| Primitive::Gap
| Primitive::Un
| Primitive::Under
Expand Down

0 comments on commit 15f27e1

Please sign in to comment.