/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

  1. class 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. create {ANY}
  10. make
  11. feature {ANY}
  12. make
  13. local
  14. x: INTEGER; format: STRING
  15. do
  16. -- How to call printf (example #1):
  17. x := 2
  18. format := "[
  19. x = %d
  20. ]"
  21. printf_integer(format.to_external, x)
  22. -- How to call printf (example #2):
  23. format := "[
  24. x = %.2f
  25. ]"
  26. printf_real(format.to_external, 3.14)
  27. -- How to call printf (example #3):
  28. inline_printf_integer(2)
  29. -- How to add to variables:
  30. x := sum(x, x)
  31. io.put_string("x = " + x.to_string + "%N")
  32. end
  33. feature {}
  34. printf_integer (format: POINTER; i: INTEGER)
  35. -- Call the C predefined `printf' function using `format' and `i'.
  36. -- (Also note here the signature example.)
  37. external "C macro signature (char*,int)"
  38. alias "printf"
  39. end
  40. printf_real (format: POINTER; d: REAL)
  41. -- Call the C predefined `printf' function using `format' and `i'.
  42. -- (Also note here the signature example.)
  43. external "C macro signature (char*,double)"
  44. alias "printf"
  45. end
  46. inline_printf_integer (i: INTEGER)
  47. -- Using the inline C definition. (Inside the alias string,
  48. -- the $i refer the `i' argument.)
  49. external "C inline"
  50. alias "[
  51. printf("i = %d\n",$i);
  52. ]"
  53. end
  54. sum (a, b: INTEGER): INTEGER
  55. -- Using the inline C definition. (Inside the alias string,
  56. -- the $a refer the `a' argument.)
  57. external "C inline"
  58. alias "[
  59. ($a + $b)
  60. ]"
  61. end
  62. end -- class EXAMPLE5