Allow (string->number) to parse floats and ints

This commit is contained in:
Justin Ethier 2015-04-09 14:13:15 -04:00
parent ea8f56a4ee
commit cbcde1e0b1
3 changed files with 18 additions and 5 deletions

View file

@ -40,6 +40,7 @@ typedef long tag_type;
#include <setjmp.h> #include <setjmp.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <math.h>
#ifndef CLOCKS_PER_SEC #ifndef CLOCKS_PER_SEC
/* gcc doesn't define this, even though ANSI requires it in <time.h>.. */ /* gcc doesn't define this, even though ANSI requires it in <time.h>.. */
@ -270,6 +271,7 @@ void do_dispatch(int argc, function_type func, object clo, object *buffer);
/* All constant-size objects */ /* All constant-size objects */
typedef union { typedef union {
boolean_type boolean_t;
cons_type cons_t; cons_type cons_t;
symbol_type symbol_t; symbol_type symbol_t;
primitive_type primitive_t; primitive_type primitive_t;

View file

@ -160,7 +160,7 @@
(if cc? (if cc?
(system (system
;; -I is a hack, real answer is to use 'make install' to place .h file ;; -I is a hack, real answer is to use 'make install' to place .h file
(string-append "gcc " src-file " -L. -lcyclone -I. -g -o " exec-file))))) (string-append "gcc " src-file " -L. -lcyclone -lm -I. -g -o " exec-file)))))
;; Handle command line arguments ;; Handle command line arguments
(let ((args (command-line-arguments))) ;; TODO: port (command-line-arguments) to husk?? (let ((args (command-line-arguments))) ;; TODO: port (command-line-arguments) to husk??

View file

@ -662,13 +662,24 @@ static void __string2list(const char *str, cons_type *buf, int buflen){
static common_type Cyc_string2number(object str){ static common_type Cyc_string2number(object str){
common_type result; common_type result;
make_int(n, 0); double n;
if (type_of(str) == string_tag && if (type_of(str) == string_tag &&
((string_type *) str)->str){ ((string_type *) str)->str){
// TODO: not good enough long-term since it doesn't parse floats n = atof(((string_type *) str)->str);
n.value = atoi(((string_type *) str)->str);
if (ceilf(n) == n) {
result.integer_t.tag = integer_tag;
result.integer_t.value = (int)n;
} }
result.integer_t = n; else {
result.double_t.tag = double_tag;
result.double_t.value = n;
}
} else {
// TODO: not good enough because we do pointer comparisons to #f
//result.boolean_t = boolean_f;
}
return result; return result;
} }