From 9635163bdc7f1d060b93896a7268f9bf9b1f39d2 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Wed, 14 Oct 2020 12:00:19 -0700 Subject: 7029 --- html/apps/factorial.mu.html | 81 +++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 39 deletions(-) (limited to 'html/apps/factorial.mu.html') diff --git a/html/apps/factorial.mu.html b/html/apps/factorial.mu.html index 289330dd..c52b1198 100644 --- a/html/apps/factorial.mu.html +++ b/html/apps/factorial.mu.html @@ -74,45 +74,48 @@ if ('onhashchange' in window) { 15 compare n 1 16 { 17 break-if-> -18 result <- copy 1 -19 } -20 { -21 break-if-<= -22 var tmp/ecx: int <- copy n -23 tmp <- decrement -24 result <- factorial tmp -25 result <- multiply n -26 } -27 } -28 -29 fn test-factorial { -30 var result/eax: int <- factorial 5 -31 check-ints-equal result 0x78 "F - test-factorial" -32 } -33 -34 fn main args-on-stack: (addr array addr array byte) -> exit-status/ebx: int { -35 var args/eax: (addr array addr array byte) <- copy args-on-stack -36 var tmp/ecx: int <- length args -37 $main-body: { -38 # if (len(args) <= 1) factorial(5) -39 compare tmp, 1 -40 { -41 break-if-> -42 var tmp/eax: int <- factorial 5 -43 exit-status <- copy tmp -44 break $main-body -45 } -46 # if (args[1] == "test") run-tests() -47 var tmp2/ecx: (addr addr array byte) <- index args, 1 -48 var tmp3/eax: boolean <- string-equal? *tmp2, "test" -49 compare tmp3, 0 -50 { -51 break-if-= -52 run-tests -53 exit-status <- copy 0 # TODO: get at Num-test-failures somehow -54 } -55 } -56 } +18 # n <= 1; return 1 +19 result <- copy 1 +20 } +21 { +22 break-if-<= +23 # n > 1; return n * factorial(n-1) +24 var tmp/ecx: int <- copy n +25 tmp <- decrement +26 result <- factorial tmp +27 result <- multiply n +28 } +29 } +30 +31 fn test-factorial { +32 var result/eax: int <- factorial 5 +33 check-ints-equal result 0x78 "F - test-factorial" +34 } +35 +36 fn main args-on-stack: (addr array (addr array byte)) -> exit-status/ebx: int { +37 var args/eax: (addr array addr array byte) <- copy args-on-stack +38 # len = length(args) +39 var len/ecx: int <- length args +40 $main-body: { +41 # if (len <= 1) factorial(5) +42 compare len, 1 +43 { +44 break-if-> +45 var tmp/eax: int <- factorial 5 +46 exit-status <- copy tmp +47 break $main-body +48 } +49 # if (args[1] == "test") run-tests() +50 var tmp2/ecx: (addr addr array byte) <- index args, 1 +51 var tmp3/eax: boolean <- string-equal? *tmp2, "test" +52 compare tmp3, 0 +53 { +54 break-if-= +55 run-tests +56 exit-status <- copy 0 # TODO: get at Num-test-failures somehow +57 } +58 } +59 } -- cgit 1.4.1-2-gfad0