Read numbers

This commit is contained in:
Justin Ethier 2017-08-14 15:29:02 +00:00
parent afdecfbfe8
commit d4836eb36d

View file

@ -5741,23 +5741,9 @@ void _read_error(void *data, port_type *p, const char *msg)
Cyc_rt_raise_msg(data, buf);
}
void _read_return_atom(void *data, object cont, port_type *p)
int _read_is_numeric(const char *tok)
{
object sym;
// Back up a char, since we always get here after reaching a terminal char
// indicating we have the full atom
p->buf_idx--;
p->col_num--;
p->tok_buf[p->tok_end] = '\0'; // TODO: what if buffer is full?
p->tok_end = 0; // Reset for next atom
// printf("TODO: return atom from %s\n", p->tok_buf);
/*
TODO: not good enough, could be a number.
logic from existing read is:
/* Equivalent to:
(define (sign? c)
(or
(equal? c #\+)
@ -5773,6 +5759,29 @@ void _read_return_atom(void *data, object cont, port_type *p)
(or (char-numeric? (cadr a))
(eq? #\. (cadr a)))
(sign? (car a)))))
*/
int len = strlen(tok);
return (len &&
((isdigit(tok[0])) ||
((len > 1) && tok[0] == '.' && isdigit(tok[1])) ||
((len > 1) && (tok[1] == '.' || isdigit(tok[1])) && (tok[0] == '-' || tok[0] == '+'))));
}
void _read_return_atom(void *data, object cont, port_type *p)
{
object sym;
// Back up a char, since we always get here after reaching a terminal char
// indicating we have the full atom
p->buf_idx--;
p->col_num--;
p->tok_buf[p->tok_end] = '\0'; // TODO: what if buffer is full?
p->tok_end = 0; // Reset for next atom
/*
TODO: not good enough, could be a number.
logic from existing read is:
;; parse-atom -> [chars] -> literal
(define (parse-atom a)
@ -5786,9 +5795,22 @@ void _read_return_atom(void *data, object cont, port_type *p)
(/ 0.0 0.0)
(string->symbol (list->string a))))))
*/
if (_read_is_numeric(p->tok_buf)) {
make_string(str, p->tok_buf);
Cyc_string2number_(data, cont, &str);
} else if (strncmp("+inf.0", p->tok_buf, 6) == 0 ||
strncmp("-inf.0", p->tok_buf, 6) == 0) {
make_double(d, 2.0);
Cyc_expt(data, cont, &d, obj_int2obj(1000000));
} else if (strncmp("+nan.0", p->tok_buf, 6) == 0 ||
strncmp("-nan.0", p->tok_buf, 6) == 0) {
make_double(d, 0.0 / 0.0);
return_closcall1(data, cont, &d);
} else {
sym = find_or_add_symbol(p->tok_buf);
return_closcall1(data, cont, sym);
}
}
void Cyc_io_read_token(void *data, object cont, object port)
{