annotate ppc/test/twice.p @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
mike@0 1 (* twice.p *)
mike@0 2
mike@0 3 type int = integer;
mike@0 4
mike@0 5 proc square(x: int): int; begin return x * x end;
mike@0 6
mike@0 7 proc twice(proc f(y: int): int; x: int): int;
mike@0 8 begin return f(f(x)) end;
mike@0 9
mike@0 10 proc ap_to_sq(proc ff(proc f(x: int): int; x: int): int; x: int): int;
mike@0 11 begin return ff(square, x) end;
mike@0 12
mike@0 13 begin
mike@0 14 print_num(ap_to_sq(twice, 3));
mike@0 15 newline()
mike@0 16 end.
mike@0 17
mike@0 18 (*<<
mike@0 19 81
mike@0 20 >>*)
mike@0 21
mike@0 22 (*[[
mike@0 23 MODULE Main 0 0
mike@0 24 IMPORT Lib 0
mike@0 25 ENDHDR
mike@0 26
mike@0 27 PROC _square 0 0 0
mike@0 28 ! proc square(x: int): int; begin return x * x end;
mike@0 29 LDLW 16
mike@0 30 LDLW 16
mike@0 31 TIMES
mike@0 32 RETURNW
mike@0 33 END
mike@0 34
mike@0 35 PROC _twice 0 0 0
mike@0 36 ! begin return f(f(x)) end;
mike@0 37 LDLW 24
mike@0 38 LDLW 20
mike@0 39 LDLW 16
mike@0 40 PCALLW 1
mike@0 41 LDLW 20
mike@0 42 LDLW 16
mike@0 43 PCALLW 1
mike@0 44 RETURNW
mike@0 45 END
mike@0 46
mike@0 47 PROC _ap_to_sq 0 0 0
mike@0 48 ! begin return ff(square, x) end;
mike@0 49 LDLW 24
mike@0 50 CONST 0
mike@0 51 GLOBAL _square
mike@0 52 LDLW 20
mike@0 53 LDLW 16
mike@0 54 PCALLW 3
mike@0 55 RETURNW
mike@0 56 END
mike@0 57
mike@0 58 PROC MAIN 0 0 0
mike@0 59 ! print_num(ap_to_sq(twice, 3));
mike@0 60 CONST 3
mike@0 61 CONST 0
mike@0 62 GLOBAL _twice
mike@0 63 CONST 0
mike@0 64 GLOBAL _ap_to_sq
mike@0 65 PCALLW 3
mike@0 66 CONST 0
mike@0 67 GLOBAL lib.print_num
mike@0 68 PCALL 1
mike@0 69 ! newline()
mike@0 70 CONST 0
mike@0 71 GLOBAL lib.newline
mike@0 72 PCALL 0
mike@0 73 RETURN
mike@0 74 END
mike@0 75
mike@0 76 ! End
mike@0 77 ]]*)