/usr/share/doc/ats-lang-anairiats-examples/examples/KernighanRitchie/Chapter03/reverse.dats is in ats-lang-anairiats-examples 0.2.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | //
// K&R, 2nd edition, page 62
//
// Translated to ATS by Hongwei Xi (hwxi AT cs DOT bu DOT edu)
extern fun strbuf_swap {m,n:nat | n < m}
(s: &strbuf (m, n), i: natLte n, j: natLte n):<> void
= "strbuf_swap"
extern fun reverse {m,n:nat | n < m} (s: &strbuf (m, n)): void
= "strbuf_reverse"
implement reverse {m,n} (s) =
loop {0,n} (s, 0, n - 1) where {
val n = strbuf_length s; val n = int1_of_size1 n
fun loop {i,j:nat | i+j==n} .<j>.
(s: &strbuf (m, n), i: int i, j1: int (j-1)): void =
if i < j1 then begin
strbuf_swap (s, i, j1); loop {i+1,j-1} (s, i+1, j1-1)
end // end of [if]
} // end of [reverse]
%{^
ats_void_type strbuf_swap
(ats_ref_type s0, ats_int_type i, ats_int_type j)
{
char *s = s0; int c ;
c = s[i]; s[i] = s[j]; s[j] = c ;
return ;
}
%}
(* ****** ****** *)
#define BUFLEN 1024
val alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
implement main () = let
var !p_buf with pf_buf = @[byte][BUFLEN]()
val () = strbuf_initialize_substring (pf_buf | p_buf, alphabet, 0, 26)
val () = print "reverse bef: "
val () = print_string (__cast p_buf) where {
extern castfn __cast (p: ptr): string
}
val () = print_newline ()
val () = print "reverse aft: "
val () = reverse (!p_buf)
val () = print_string (__cast p_buf) where {
extern castfn __cast (p: ptr): string
}
val () = print_newline ()
prval () = pf_buf := bytes_v_of_strbuf_v (pf_buf)
in
// empty
end // end of [main]
(* ****** ****** *)
(* end of [reverse.dats] *)
|