cursed-ua
cursed.ua
uiua code macro library
C ~ "git: github.com/alex-s168/cursed-ua"
Code macros
InvTry! code macro
invertible try
the inverse of InvTry!(A|B|H) is:
try(unA|unB|unH)
Source code
InvTry! ←^ EmitInvTry
Match! code macro
example:
Match!(
1 + 2 => "a"
| 3 => "b"
| . => "y")
To put more complicated expression into the match arm cases, surround them with parenthesis:
Match!(
1 + (+1 1) => "a"
| 3 => "b"
| . => "y")
Source code
Match! ←^ EmitMatch
Enum! code macro
defines all the enum constants, EnumBin (un-able), and EnumStr (un-able)
example:
Enum!(
Player = 4 # start counting at 4
| Sheep # id 5
| Horse # id 6
| ..Mob # all other ids are Mobs
)
Currently the entry with the ".." in front of it assignes to all unassigned IDs, instead of just the following. this however might be changed in the future.
The names of enum entries should not start with 'Enum'
You can also use more complicated expressions in ID assignments by wrapping them in parenthesis, like Player = (+Abc 2)
If you don't assign the first element, it will start counting from zero.
You can skip IDs by assigning elements in the middle.
Source code
Enum! ←^ ( ≡⍚( SplitWithIndent‼!@(@)@← ⊂{⊙⊙} ▽≠@ . ⊃(⨬(:0)(:1↘2) =0⬚1⊢⊚⌕"..". °□⊢|↘1) ) # []box(box[str]|is_rem:box[bol]|overwr:opt[box[int]]) ⬚(□{0 0 "neg1"})\( ⨬(□⊂°□⊙(□$"+1(_)"°□◇⊣)|⊙◌)=3◇⧻.: )
⊃(≡⍚( °[⊙⊙⊙] ◇⊃( ⊃($"_ => _" ⊙◌ | $"_ => _" : ⊙◇⨬$"(_)"("."◌)) | ⊙◌ ◇$"\"_\"" :⊃( $"(_) => _" | $"(_) => _": ) ) ) ∩₄EmitMatch $"EnumBin = obv(_|_)" ⊙$"EnumStr = obv(_|_)" | ≡⍚($"_ = $_ _" . ⊙◌ °{⊙⊙⊙}) Join!$"_\n_" ) ⇌{⊙⊙⊙} Join!$"_\n_")
SelectBits! code macro
can also be un-ed
example:
{SelectBits!(3|3|2)} 231
## {[1 1 1] [0 0 1] [1 1]}
Source code
SelectBits! ←^ SelectBitsGen
NoInv! code macro
defines a fake un- inverse (that errors when it is called) for the given function
this can be used to get the no inverse found error at runtime instead of compile time
Source code
NoInv! ←^ $"obv(_|assert\"NoInv! inverse was called\"0 _)" ◇⊢: ⍥(⊂"0 "): ⍥(⊂"pop"):"" :°[⊙⊙] ⊢:
Index macros
Z! index macro
zero-initialize struct ^0
Source code
Z! ← °[°^0] ↯⊙0 Args!^0
Indent‼ index macro
Source code
Indent‼ ← ⬚0\(⍤"mismatched parenthesis"≥0. ⍣(+1°^0|-1°^1|◌):)
SplitWithIndent‼! index macro
macro args: UpIndent DownIndent SplitBy:ch|[]ch
arr[box[str]] ? str
example: SplitWithIndent‼! @[ @] @, "[1,2,3]"
Source code
SplitWithIndent‼! ← ( ⊜□¬×⊃(=0Indent‼^0^1|≡(/+=^2)|∘))
ParseMatchArms! index macro
default:arr[box[str]] cases:arr[box[T]] ? arr[box[str]]
example:
ParseMatchArms!$"_ => _"
Source code
ParseMatchArms! ← ( °□/⍚⊂ ≡⍚(≡{⊙⊙}⊙¤ ParseMatchArm) ⍤"only one default arm allowed"≤1⧻.⊃▽(▽¬) ≡◇≍0≡⊢. ⊙(≡(□^0∩°□°□°[⊙⊙])) ♭≡(⊡1))
Join! index macro
Join arr of boxed strings with ^0
example: Join!$"_, _"
Source code
Join! ← ⍣(""◌°0|°□⊢°1|°□/⍚^0 ◌) ⧻.
Monadic functions
ParseMatchArm function
example: "a + b => x" example: "1 + 2 + (3+4) => 3" example: ". => x" (returns 0 and " x")
Cases:or[0|arr[box[str]]] Value:str ? arm:str
Source code
ParseMatchArm ← ⍣( ⌕"=>". +↘¯ 1⊂0. # expand selection to => ⊜□¬ ⍤""=2⧻. ∩°□°[⊙⊙]
≍"."▽≠@ .. ⨬(SplitWithIndent‼!@(@)@+ ▽≡◇(¬/×=@ ). | 0)| ⍤"invalid pattern match arm synta"0)
EmitInvTry function
code mapper of InvTry!
Source code
EmitInvTry ← $"obv((_)|(_))":∩($"try(_)"⨬(°□◇⊢|°□/⍚$"_|_")≥2⧻.)⊸≡⍚($"un(_)")
EmitMatch function
code generator for Match!
Source code
EmitMatch ← $"try(_)" ⨬($"_|assert\"pattern match failure\"0"◌|$"_|(_) pop":°□⊢) ≠0⧻. ⊙Join!$"_|_" ParseMatchArms!($"case(_) un(_)":)
SelectBitsGen function
See SelectBits!
Source code
SelectBitsGen ← ( ⊃(⊂□"0"↘¯1 \⍚($"+__") .≡⍚$"(_)" $"desrow(revbit,8)" ⊙($"fork(_)" /$"_|_" ≡⍚˜$"tak_dro_") $"_ _": | $"unbitrevjoi,__"⧻: $"bra(_)" /$"_|_" ≡⍚$"revfil0tak_rev".) $"obv(_\n| _)")
Dyadic functions
RmTail function
Source code
RmTail ← ⍣(↘¯:⟜(=⊣):|◌)
RmBegin function
Source code
RmBegin ← ⍣(↘:⟜(=⊢):|◌)