diff ppc/test/fac.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ppc/test/fac.p	Thu Oct 05 08:04:15 2017 +0100
@@ -0,0 +1,72 @@
+(* fac.p *)
+
+proc fac(n: integer): integer;
+begin
+  if n = 0 then
+    return 1
+  else
+    return n * fac(n-1)
+  end
+end;
+
+var f: integer;
+
+begin
+  f := fac(10);
+  print_num(f);
+  newline()
+end.
+        
+(*<<
+3628800
+>>*)
+
+(*[[
+MODULE Main 0 0
+IMPORT Lib 0
+ENDHDR
+
+PROC _fac 0 0 0
+!   if n = 0 then
+LDLW 16
+JEQZ L1
+JUMP L2
+LABEL L1
+!     return 1
+CONST 1
+RETURNW
+LABEL L2
+!     return n * fac(n-1)
+LDLW 16
+LDLW 16
+CONST 1
+MINUS
+CONST 0
+GLOBAL _fac
+PCALLW 1
+TIMES
+RETURNW
+END
+
+PROC MAIN 0 0 0
+!   f := fac(10);
+CONST 10
+CONST 0
+GLOBAL _fac
+PCALLW 1
+STGW _f
+!   print_num(f);
+LDGW _f
+CONST 0
+GLOBAL lib.print_num
+PCALL 1
+!   newline()
+CONST 0
+GLOBAL lib.newline
+PCALL 0
+RETURN
+END
+
+GLOVAR _f 4
+! End
+]]*)