Simplified the arithmetic operator code
[fridhskrift.git] / lexer / number.cpp
blob630a73f41979d05efa22402e3f48894212f2fb65
1 #include <fridh/lexer.hpp>
2 #include <ail/string.hpp>
4 namespace fridh
6 bool lexer::parse_number(line_of_code & output, bool & error_occured)
8 std::size_t start = i;
9 char byte = input[i];
10 error_occured = false;
12 if(ail::is_digit(byte))
14 i++;
15 if(byte == '0')
17 std::size_t remaining_bytes = end - i;
18 if(remaining_bytes > 1)
20 char next_byte = input[i + 1];
21 if(next_byte == 'x')
23 i++;
24 remaining_bytes = end - i;
25 if(remaining_bytes == 0)
27 error = number_parsing_error("Incomplete hex number at the end of the input", error_occured);
28 return false;
31 std::size_t hex_start = i;
33 for(; i < end && ail::is_hex_digit(input[i]); i++);
35 std::size_t hex_length = i - hex_start;
36 if(hex_length == 0)
38 error_occured = true;
39 error = lexer_error("Incomplete hex number");
40 return false;
43 std::string hex_string = input.substr(hex_start, i - end);
44 types::unsigned_integer value = ail::string_to_number<types::unsigned_integer>(hex_string, std::ios_base::hex);
45 output.lexemes.push_back(lexeme(value));
46 return true;
51 char const dot = '.';
53 bool got_dot = false;
54 char last_byte = byte;
55 for(; i < end; i++)
57 byte = input[i];
58 if(byte == dot)
60 if(got_dot)
62 error = number_parsing_error("Encountered a floating point value containing multiple dots", error_occured);
63 return false;
65 else
66 got_dot = true;
68 else if(!ail::is_digit(byte))
69 break;
71 last_byte = byte;
74 if(last_byte == dot)
76 error = number_parsing_error("Encountered a floating point value ending with a dot", error_occured);
77 return false;
80 std::string number_string = input.substr(start, i - start);
81 lexeme current_lexeme;
82 if(got_dot)
83 current_lexeme = lexeme(ail::string_to_number<types::floating_point_value>(number_string));
84 else
85 current_lexeme = lexeme(ail::string_to_number<types::signed_integer>(number_string));
86 output.lexemes.push_back(current_lexeme);
88 return true;
90 else
91 return false;