Skip to content

Instantly share code, notes, and snippets.

@durka
Created September 26, 2017 17:51
Show Gist options
  • Save durka/6dc45791e8c352ea47165188d1047554 to your computer and use it in GitHub Desktop.
Save durka/6dc45791e8c352ea47165188d1047554 to your computer and use it in GitHub Desktop.
// input helper subroutine for converting from decimal to unary
// step 1 "revconv": reverse the digits and convert them to zermelo
// step 2 "unary": iterate through the digits and construct a unary number
(@in @revconv
[] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @unary
[] [] [()] [$($digit)*]
$stuff
)
};
(@in @revconv
[0 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [() $($digit)*]
$stuff
)
};
(@in @revconv
[1 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [(()) $($digit)*]
$stuff
)
};
(@in @revconv
[2 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [((())) $($digit)*]
$stuff
)
};
(@in @revconv
[3 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [(((()))) $($digit)*]
$stuff
)
};
(@in @revconv
[4 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [((((())))) $($digit)*]
$stuff
)
};
(@in @revconv
[5 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [(((((()))))) $($digit)*]
$stuff
)
};
(@in @revconv
[6 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [((((((())))))) $($digit)*]
$stuff
)
};
(@in @revconv
[7 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [(((((((()))))))) $($digit)*]
$stuff
)
};
(@in @revconv
[8 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [((((((((())))))))) $($digit)*]
$stuff
)
};
(@in @revconv
[9 $($tail:tt)*] [$($digit:tt)*]
$stuff:tt
) => {
bf!(@in @revconv
[$($tail)*] [(((((((((()))))))))) $($digit)*]
$stuff
)
};
// how step 2 works:
// - $fullacc is the number being built up, $digitacc is used within digits (both are unary numbers)
// - $pv is the place value counter, multiplied by 10 each iteration
// - within a digit, count down the zermelo layers and inc $digitacc by $pv
// - at the end of a digit, just add $digitacc to $fullacc (and multiply $pv by 10)
(@in @unary
$fullacc:tt $digitacc:tt $pv:tt []
[[$head:tt $left:tt $right:tt] [$($other_stuff:tt)*]]
) => {
bf!(@run $head
[$fullacc $left $right]
$($other_stuff)*
)
};
(@in @unary
[$($fullacc:tt)*] [$($digitacc:tt)*] [$($pv:tt)*] [() $($digits:tt)*]
$stuff:tt
) => {
bf!(@in @unary
[$($fullacc)* $($digitacc)*] [] [$($pv)* $($pv)* $($pv)* $($pv)* $($pv)*
$($pv)* $($pv)* $($pv)* $($pv)* $($pv)*] [$($digits)*]
$stuff
)
};
(@in @unary
$fullacc:tt [$($digitacc:tt)*] [$($pv:tt)*] [($digit:tt) $($digits:tt)*]
$stuff:tt
) => {
bf!(@in @unary
$fullacc [$($digitacc)* $($pv)*] [$($pv)*] [$digit $($digits)*]
$stuff
)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment