PageRenderTime 9ms CodeModel.GetById 5ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/tutorial/external/C/example5.e

http://github.com/tybor/Liberty
Specman e | 75 lines | 43 code | 13 blank | 19 comment | 0 complexity | c6261dc94176bc5d3ea9c4b3737bb243 MD5 | raw file
 1class EXAMPLE5
 2   --
 3   -- How to use some other external "C" features (i.e. calling C from Eiffel).
 4   --
 5   -- To compile this file:
 6   --
 7   --       compile example5
 8   --
 9
10create {ANY}
11   make
12
13feature {ANY}
14   make
15      local
16         x: INTEGER; format: STRING
17      do
18         -- How to call printf (example #1):
19         x := 2
20         format := "[
21                      x = %d
22
23                    ]"
24
25         printf_integer(format.to_external, x)
26         -- How to call printf (example #2):
27         format := "[
28                      x = %.2f
29
30                    ]"
31
32         printf_real(format.to_external, 3.14)
33         -- How to call printf (example #3):
34         inline_printf_integer(2)
35         -- How to add to variables:
36         x := sum(x, x)
37         io.put_string("x = " + x.to_string + "%N")
38      end
39
40feature {}
41   printf_integer (format: POINTER; i: INTEGER)
42         -- Call the C predefined `printf' function using `format' and `i'.
43         -- (Also note here the signature example.)
44      external "C macro signature (char*,int)"
45      alias "printf"
46      end
47
48   printf_real (format: POINTER; d: REAL)
49         -- Call the C predefined `printf' function using `format' and `i'.
50         -- (Also note here the signature example.)
51      external "C macro signature (char*,double)"
52      alias "printf"
53      end
54
55   inline_printf_integer (i: INTEGER)
56         -- Using the inline C definition. (Inside the alias string,
57         -- the $i refer the `i' argument.)
58      external "C inline"
59      alias "[
60              printf("i = %d\n",$i);
61
62             ]"
63      end
64
65   sum (a, b: INTEGER): INTEGER
66         -- Using the inline C definition. (Inside the alias string,
67         -- the $a refer the `a' argument.)
68      external "C inline"
69      alias "[
70              ($a + $b)
71
72             ]"
73      end
74
75end -- class EXAMPLE5