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