Thun/implementations/bigints.joy

98 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]
[digitalize [0 <=] [pop []] [base divmod swap] [i cons] genrec]
[to-bigint [!-] [abs digitalize] cleave cons]
[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 * +]
[sign-int [first] [prep from-bigint'] cleave]
[neg-if-necessary swap [neg] [] branch]
[from-bigint sign-int neg-if-necessary]
[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] [i cons] genrec]
[_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]
[add-digits' [P] [THEN] [R0] [R1] genrec]
[P [bool] ii & not]
[THEN [P'] [THEN'] [ELSE] ifte]
[R0 uncons-two [add-with-carry] dipd]
[R1 i cons]
[P' [bool] ii |]
[THEN' ditch-empty-list add-carry-to-digits]
[ELSE pop swap [] [1 swons] branch]
[same-sign [first] ii xor not]
[add-like-bigints [uncons] dip rest add-digits cons]
[add-bigints [same-sign] [add-like-bigints] [1 0 /] ifte]
[build-two-list-combiner _btlc0 _btlc1 [i 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]
[carry [] [1 swons] branch]
[compare-pairs [bool not] [pop false] [[first [>=] infrst] [pop true]] [[rest] swoncat ifte] genrec]
[xR1 uncons-two [unit cons swons] dipd]
[xP [bool] ii & not]
[BASE [bool] [popop pop true] [[pop bool] [popop pop false] [popop compare-pairs] ifte] ifte]
[gt-bigint <<{} [xP] [BASE] [xR1] tailrec]
[check-gt [gt-bigint] [swap [not] dipd] [] ifte]
[sub-carry pop]
[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] inscribe
[cons-but-not-leading-zeroes [[bool] ii | not] [popd] [cons] 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 [uncons] dip rest check-gt sub-digits cons]
[sub-digits initial-carry sub-digits']
enstacken [inscribe] step
[add-carry-to-digits]
[swap carry]
[add-with-carry]
build-two-list-combiner
[genrec] ccons ccons
[add-digits'] swoncat
inscribe
[sub-carry-from-digits]
[swap sub-carry]
[sub-with-carry]
build-two-list-combiner
[genrec] ccons ccons
[sub-digits'] swoncat
inscribe
true [0 1] sub-carry-from-digits