Skip to content

Commit

Permalink
add un both and un bracket
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Nov 25, 2024
1 parent ccd0736 commit ce6229e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ impl VirtualEnv {
}
self.node(&f.node)?;
}
ImplPrimitive::UnBracket => {
let [f, g] = get_args(args)?;
self.handle_args_outputs(f.args + g.args, f.outputs + g.outputs);
}
prim => {
let args = prim.args();
for _ in 0..args {
Expand Down
13 changes: 13 additions & 0 deletions src/compile/invert/un.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ pub static UN_PATTERNS: &[&dyn InvertPattern] = &[
&ArrayPat,
&UnpackPat,
&DipPat,
&BothPat,
&BracketPat,
&OnPat,
&ByPat,
&RowsPat,
Expand Down Expand Up @@ -331,6 +333,17 @@ inverse!(DipPat, input, asm, Dip, span, [f], {
Ok((input, Mod(Dip, eco_vec![inv], span)))
});

inverse!(BothPat, input, asm, Both, span, [f], {
let inv = f.un_inverse(asm)?;
Ok((input, ImplMod(UnBoth, eco_vec![inv], span)))
});

inverse!(BracketPat, input, asm, Bracket, span, [f, g], {
let f_inv = f.un_inverse(asm)?;
let g_inv = g.un_inverse(asm)?;
Ok((input, ImplMod(UnBracket, eco_vec![f_inv, g_inv], span)))
});

inverse!(OnPat, input, asm, On, span, [f], {
let inv = Mod(On, eco_vec![f.anti_inverse(asm)?], span);
Ok((input, inv))
Expand Down
1 change: 1 addition & 0 deletions src/primitive/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3668,6 +3668,7 @@ impl_primitive!(
(3, UndoRemove),
(1(0), TryClose),
(2[1], UnBoth),
(2[2], UnBracket),
// Optimizations
(1, FirstMinIndex),
(1, FirstMaxIndex),
Expand Down
8 changes: 8 additions & 0 deletions src/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ impl fmt::Display for ImplPrimitive {
UnFft => write!(f, "{Un}{Fft}"),
UnDatetime => write!(f, "{Un}{DateTime}"),
UnBoth => write!(f, "{Un}{Both}"),
UnBracket => write!(f, "{Un}{Bracket}"),
ImageDecode => write!(f, "{Un}{ImageEncode}"),
GifDecode => write!(f, "{Un}{GifEncode}"),
AudioDecode => write!(f, "{Un}{AudioEncode}"),
Expand Down Expand Up @@ -1710,6 +1711,13 @@ impl ImplPrimitive {
env.exec(f.node)?;
env.push_all(vals);
}
ImplPrimitive::UnBracket => {
let [f, g] = get_ops(ops, env)?;
env.exec(f.node)?;
let f_outputs = env.pop_n(f.sig.outputs)?;
env.exec(g.node)?;
env.push_all(f_outputs);
}
ImplPrimitive::EachSub(n)
| ImplPrimitive::RowsSub(n)
| ImplPrimitive::InventorySub(n) => {
Expand Down
22 changes: 15 additions & 7 deletions tests/units.ua
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,21 @@ F ← ⌅⍜⊢¯ ⊏⊙"nubtqphsoe" -@0 °⋕
⍤⤙≍ 5 °(++⊃(×.|ׯ2|¯5)) 10
⍤⤙≍ 10 °(+∩(×.)+1.) 221

# Un both
⍤⤙≍ [1 2 3 4] [°∩⊟] 1_2 3_4
⍤⤙≍ {1 [2] 3 [4]} {°∩⊂} 1_2 3_4

# Un bracket
⍤⤙≍ [1 2 3 4] [°⊓⊟⊟] 1_2 3_4
⍤⤙≍ {1 2 3 [4]} {°⊓⊟⊂} 1_2 3_4

# Subscript both
⍤⤙≍ [] [∩₀+]
⍤⤙≍ [3] [∩₁+ 1 2]
⍤⤙≍ [3 7] [∩₂+ 1 2 3 4]
⍤⤙≍ [3 7 11] [∩₃+ 1 2 3 4 5 6]
⍤⤙≍ [3 7 11 15] [∩₄+ 1 2 3 4 5 6 7 8]

# Experimental!

# Off
Expand All @@ -440,13 +455,6 @@ F ← ⌅⍜⊢¯ ⊏⊙"nubtqphsoe" -@0 °⋕
⍤⤙≍ [1 2 3 6] [◠(++)] 1 2 3
⍤⤙≍ [1 2 ¯1 2] [◠¯] 1 2

# Subscript both
⍤⤙≍ [] [∩₀+]
⍤⤙≍ [3] [∩₁+ 1 2]
⍤⤙≍ [3 7] [∩₂+ 1 2 3 4]
⍤⤙≍ [3 7 11] [∩₃+ 1 2 3 4 5 6]
⍤⤙≍ [3 7 11 15] [∩₄+ 1 2 3 4 5 6 7 8]

# Derivative
⍤⤙≍ 10 ∂(×.) 5
⍤⤙≍ 1.5 ∂√ 1/9
Expand Down

0 comments on commit ce6229e

Please sign in to comment.