/tutorial/number/example5.e

http://github.com/tybor/Liberty · Specman e · 96 lines · 86 code · 7 blank · 3 comment · 6 complexity · e3469be756ead91ad4a248879b3705c0 MD5 · raw file

  1. class EXAMPLE5
  2. --
  3. -- Using the NUMBER_TOOLS parser.
  4. --
  5. create {ANY}
  6. make
  7. feature {}
  8. make
  9. local
  10. stop: BOOLEAN; formula: STRING
  11. do
  12. from
  13. until
  14. stop
  15. loop
  16. io.put_string(once "[
  17. Type Q to quit or ...
  18. Enter your NUMBER formula on a single line:
  19. ]")
  20. io.read_line
  21. formula := io.last_string.twin
  22. if formula.is_empty then
  23. stop := True
  24. else
  25. if formula.count = 1 then
  26. inspect
  27. formula.first
  28. when 'Q', 'q' then
  29. stop := True
  30. else
  31. end
  32. end
  33. if not stop then
  34. parsing_of(formula)
  35. end
  36. end
  37. end
  38. io.put_string("Bye.%N")
  39. end
  40. parsing_of (formula: STRING)
  41. local
  42. number_tools: NUMBER_TOOLS; number: NUMBER; i: INTEGER
  43. do
  44. if number_tools.is_number(formula) then
  45. number := number_tools.from_string(formula)
  46. if number /= Void then
  47. io.put_string(once "=%N")
  48. io.put_number(number)
  49. if number.is_fraction_general_number and then number.fit_real then
  50. io.put_string(once " (")
  51. io.put_real(number.force_to_real_64)
  52. io.put_character(')')
  53. end
  54. io.put_new_line
  55. else
  56. io.put_string(once "Error: ")
  57. print_error(number_tools.parser_buffer.last_error)
  58. end
  59. else
  60. io.put_string(once "Syntax error: ")
  61. print_error(number_tools.parser_buffer.last_error)
  62. io.put_line(formula)
  63. from
  64. i := 1
  65. until
  66. i = number_tools.parser_buffer.current_index
  67. loop
  68. io.put_character(' ')
  69. i := i + 1
  70. end
  71. io.put_string(once "^%N")
  72. end
  73. end
  74. print_error (error: PARSE_ERROR)
  75. require
  76. error /= Void
  77. local
  78. err: PARSE_ERROR
  79. do
  80. from
  81. err := error
  82. until
  83. err = Void
  84. loop
  85. io.put_line(err.message)
  86. err := err.next
  87. end
  88. end
  89. end -- class EXAMPLE5