Thun/implementations/bigints.joy

96 lines
2.8 KiB
Plaintext

clear
[base 2147483648]
[ditch-empty-list [bool] [popd] [pop] ifte]
[bool-to-int [0] [1] branch]
[uncons-two [uncons] ii swapd]
[sandwich swap [cons] dip swoncat]
[build-list [i cons] genrec]
[digitalize [0 <=] [pop []] [base divmod swap] build-list]
[to-bigint [!-] [abs digitalize] cleave cons]
[from-bigint sign-int neg-if-necessary]
[sign-int [first] [prep from-bigint'] cleave]
[neg-if-necessary swap [neg] [] branch]
[prep rest 1 0 rolldown]
[from-bigint' [next-digit] step popd]
[next-digit [increase-power] [accumulate-digit] clop popdd]
[increase-power popop base *]
[accumulate-digit rolldown * +]
[neg-bigint [not] infra]
[add-with-carry _add-with-carry0 _add-with-carry1]
[_add-with-carry0 [bool-to-int] dipd + +]
[_add-with-carry1 base [mod] [>=] clop]
[add-carry-to-digits [pop not] [popd] [_actd_R0] build-list]
[_actd_R0 [bool] [_actd_R0.then] [_actd_R0.else] ifte]
[_actd_R0.else popd 1 false rolldown]
[_actd_R0.then 0 swap uncons [add-with-carry] dip]
[add-digits initial-carry add-digits']
[initial-carry false rollup]
[same-sign [first] ii xor not]
[extract-sign [uncons] dip rest]
[add-like-bigints extract-sign add-digits cons]
[add-bigints [same-sign] [add-like-bigints] [neg-bigint sub-like-bigints] ifte]
[build-two-list-combiner _btlc0 _btlc1 [[i cons] genrec] ccons cons]
[_btlc0.0 [[ditch-empty-list] swoncat] dip]
[_btlc0.1 [pop] swoncat]
[_btlc0.3 [_btlc0.0 _btlc0.1] dip]
[_btlc0.4 [uncons-two] [dipd] sandwich]
[_btlc0 _btlc0.3 _btlc0.4]
[_btlc1 [[ifte] ccons [P'] swons [P] swap] dip]
[P [bool] ii & not]
[P' [bool] ii |]
[carry [] [1 swons] branch]
[compare-pairs [bool not] [pop false] [_comp-pairs0] [_comp-pairs1] genrec]
[_comp-pairs0 [first [>=] infrst] [pop true]]
[_comp-pairs1 [rest] swoncat ifte]
[check-gt [gt-bigint] [swap [not] dipd] [] ifte]
[gt-bigint <<{} [_gtb_P] [_gtb_BASE] [_gtb_R1] tailrec]
[_gtb_R1 uncons-two [unit cons swons] dipd]
[_gtb_P [bool] ii & not]
[_gtb_BASE [bool] [popop pop true] [_gtb_BASE'] ifte]
[_gtb_BASE' [pop bool] [popop pop false] [popop compare-pairs] ifte]
[sub-carry-from-digits [pop not] [popd] [_scfd_R0] [i cons-but-not-leading-zeroes] genrec] inscribe
[_scfd_R0 uncons 0 swap [sub-with-carry] dip]
[cons-but-not-leading-zeroes [P'] [cons] [popd] ifte]
[sub-with-carry _sub-with-carry0 _sub-with-carry1]
[_sub-with-carry0 rolldown bool-to-int [-] ii]
[_sub-with-carry1 [base + base mod] [0 <] cleave]
[sub-like-bigints extract-sign check-gt sub-digits cons]
[sub-digits initial-carry sub-digits']
[sub-bigints [same-sign] [sub-like-bigints] [neg-bigint add-like-bigints] ifte]
enstacken [inscribe] step
[add-carry-to-digits]
[swap carry]
[add-with-carry]
build-two-list-combiner
[add-digits'] swoncat
inscribe
[sub-carry-from-digits]
[swap pop]
[sub-with-carry]
build-two-list-combiner
[sub-digits'] swoncat
inscribe
1234 to-bigint dup sub-bigints