mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-15 16:57:35 +02:00
Read numbers
This commit is contained in:
parent
afdecfbfe8
commit
d4836eb36d
1 changed files with 41 additions and 19 deletions
60
runtime.c
60
runtime.c
|
@ -5741,30 +5741,16 @@ 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:
|
||||
|
||||
(define (sign? c)
|
||||
/* Equivalent to:
|
||||
(define (sign? c)
|
||||
(or
|
||||
(equal? c #\+)
|
||||
(equal? c #\-)))
|
||||
|
||||
;; token-numeric? -> [chars] -> boolean
|
||||
(define (token-numeric? a)
|
||||
;; token-numeric? -> [chars] -> boolean
|
||||
(define (token-numeric? a)
|
||||
(or (char-numeric? (car a))
|
||||
(and (> (length a) 1)
|
||||
(eq? #\. (car a))
|
||||
|
@ -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,8 +5795,21 @@ 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)
|
||||
|
|
Loading…
Add table
Reference in a new issue