1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
# 1 "higher_order_macros.cppo"
(* This macro application combinator provides call-by-value
semantics: the actual argument is evaluated up front and
its value is bound to a variable, which is passed as an
argument to the macro [F]. *)
# 7 "higher_order_macros.cppo"
(* Some trivial tests. *)
# 10 "higher_order_macros.cppo"
let forty_one =
# 10 "higher_order_macros.cppo"
(let __x = ( 41) in __x )
# 11 "higher_order_macros.cppo"
let forty_two =
# 11 "higher_order_macros.cppo"
(let __x = ( 42 ) in __x )
# 13 "higher_order_macros.cppo"
(* A [for]-loop macro. *)
# 20 "higher_order_macros.cppo"
(* A [for]-loop macro that performs unrolling. *)
# 35 "higher_order_macros.cppo"
(* In some of the examples that follow, #scope ... #endscope is used
to avoid the need to #undefine local macros such as BODY and F. *)
(* Iteration over an array, with a normal loop. *)
let iter f a =
# 42 "higher_order_macros.cppo"
(
for __index = 0 to Array.length a-1 do
(f a.(__index))
done
)
# 45 "higher_order_macros.cppo"
(* Iteration over an array, with an unrolled loop. *)
let unrolled_iter f a =
# 49 "higher_order_macros.cppo"
(
let __finish = ( Array.length a) in
let __index = ref (0) in
while !__index + 2 <= __finish do
(let __x = ( !__index) in (f a.(__x)) ) ;
(let __x = ( !__index + 1) in (f a.(__x)) ) ;
__index := !__index + 2
done;
while !__index < __finish do
(let __x = ( !__index) in (f a.(__x)) ) ;
__index := !__index + 1
done
)
# 52 "higher_order_macros.cppo"
(* Printing an array, with a normal loop. *)
let print_int_array a =
# 55 "higher_order_macros.cppo"
(
for __index = 0 to Array.length a-1 do
Printf.printf "%d" a.(__index)
done
)
# 57 "higher_order_macros.cppo"
(* A higher-order macro that produces a definition of [iter],
and accepts an arbitrary definition of the macro [LOOP]. *)
# 65 "higher_order_macros.cppo"
(* Some noise, which does not affect the above definitions. *)
# 68 "higher_order_macros.cppo"
let iter f a =
(
for __index = 0 to Array.length a-1 do
(f a.(__index))
done
)
# 69 "higher_order_macros.cppo"
let unrolled_iter f a =
(
let __finish = ( Array.length a) in
let __index = ref (0) in
while !__index + 2 <= __finish do
(let __x = ( !__index) in (f a.(__x)) ) ;
(let __x = ( !__index + 1) in (f a.(__x)) ) ;
__index := !__index + 2
done;
while !__index < __finish do
(let __x = ( !__index) in (f a.(__x)) ) ;
__index := !__index + 1
done
)
|