2 // This file contains a slimmed down version of read_real().
3 // It does not pull in all the floating-point, complex and transcendental
10 #include "cln/rational_io.h"
15 #include "cln/input.h"
17 #include "cl_spushstring.h"
21 // We read an entire token (or even more, if it begins with #C) into a
22 // buffer and then call read_rational() on the buffer.
24 class pushstring_hack : public cl_spushstring {
26 char* start_pointer (void) { return buffer; }
27 char* end_pointer (void) { return buffer+index; }
30 static cl_boolean number_char_p (char c)
32 if ((c >= '0') && (c <= '9'))
34 if (((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')))
37 case '+': case '-': case '.': case '_': case '/':
44 const cl_RA read_rational (cl_istream stream, const cl_read_flags& flags)
46 // One pre-allocated buffer. This reduces the allocation/free cost.
47 static pushstring_hack buffer;
50 // Skip whitespace at the beginning.
52 c = freadchar(stream);
53 if (c == cl_EOF) goto eof;
54 if ((c == ' ') || (c == '\t') || (c == '\n'))
59 // Found first non-whitespace character.
60 // Numbers cannot cross lines. We can treat EOF and '\n' the same way.
63 if (!(flags.lsyntax & lsyntax_commonlisp))
66 // Read some digits, then a letter, then a token.
68 c = freadchar(stream);
69 if (c == cl_EOF) goto eof;
71 if ((c >= '0') && (c <= '9'))
76 if (!(((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z'))))
79 c = freadchar(stream);
80 if (c == cl_EOF) goto eof;
82 // Read a number token.
83 if (!number_char_p(c))
87 c = freadchar(stream);
90 if (!number_char_p(c)) {
91 funreadchar(stream,c);
96 return read_rational(flags,
97 buffer.start_pointer(), buffer.end_pointer(),
101 // Handle syntax error.
102 syntax1: buffer.push(c);
103 read_number_bad_syntax(buffer.start_pointer(),buffer.end_pointer());
105 // Handle premature EOF.
106 eof: read_number_eof();