comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 (* twice.p *)
2
3 type int = integer;
4
5 proc square(x: int): int; begin return x * x end;
6
7 proc twice(proc f(y: int): int; x: int): int;
8 begin return f(f(x)) end;
9
10 proc ap_to_sq(proc ff(proc f(x: int): int; x: int): int; x: int): int;
11 begin return ff(square, x) end;
12
13 begin
14 print_num(ap_to_sq(twice, 3));
15 newline()
16 end.
17
18 (*<<
19 81
20 >>*)
21
22 (*[[
23 MODULE Main 0 0
24 IMPORT Lib 0
25 ENDHDR
26
27 PROC _square 0 0 0
28 ! proc square(x: int): int; begin return x * x end;
29 LDLW 16
30 LDLW 16
31 TIMES
32 RETURNW
33 END
34
35 PROC _twice 0 0 0
36 ! begin return f(f(x)) end;
37 LDLW 24
38 LDLW 20
39 LDLW 16
40 PCALLW 1
41 LDLW 20
42 LDLW 16
43 PCALLW 1
44 RETURNW
45 END
46
47 PROC _ap_to_sq 0 0 0
48 ! begin return ff(square, x) end;
49 LDLW 24
50 CONST 0
51 GLOBAL _square
52 LDLW 20
53 LDLW 16
54 PCALLW 3
55 RETURNW
56 END
57
58 PROC MAIN 0 0 0
59 ! print_num(ap_to_sq(twice, 3));
60 CONST 3
61 CONST 0
62 GLOBAL _twice
63 CONST 0
64 GLOBAL _ap_to_sq
65 PCALLW 3
66 CONST 0
67 GLOBAL lib.print_num
68 PCALL 1
69 ! newline()
70 CONST 0
71 GLOBAL lib.newline
72 PCALL 0
73 RETURN
74 END
75
76 ! End
77 ]]*)