From cbcde1e0b122b0073b3114c5a5362e1016cf4d13 Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 9 Apr 2015 14:13:15 -0400 Subject: [PATCH] Allow (string->number) to parse floats and ints --- cyclone.h | 2 ++ cyclone.scm | 2 +- runtime.h | 19 +++++++++++++++---- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/cyclone.h b/cyclone.h index 597b3ef0..0636beaf 100644 --- a/cyclone.h +++ b/cyclone.h @@ -40,6 +40,7 @@ typedef long tag_type; #include #include #include +#include #ifndef CLOCKS_PER_SEC /* gcc doesn't define this, even though ANSI requires it in .. */ @@ -270,6 +271,7 @@ void do_dispatch(int argc, function_type func, object clo, object *buffer); /* All constant-size objects */ typedef union { + boolean_type boolean_t; cons_type cons_t; symbol_type symbol_t; primitive_type primitive_t; diff --git a/cyclone.scm b/cyclone.scm index ef9c989a..15d3441c 100644 --- a/cyclone.scm +++ b/cyclone.scm @@ -160,7 +160,7 @@ (if cc? (system ;; -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 (let ((args (command-line-arguments))) ;; TODO: port (command-line-arguments) to husk?? diff --git a/runtime.h b/runtime.h index f5c1f0b3..493ea779 100644 --- a/runtime.h +++ b/runtime.h @@ -662,13 +662,24 @@ static void __string2list(const char *str, cons_type *buf, int buflen){ static common_type Cyc_string2number(object str){ common_type result; - make_int(n, 0); + double n; if (type_of(str) == string_tag && ((string_type *) str)->str){ - // TODO: not good enough long-term since it doesn't parse floats - n.value = atoi(((string_type *) str)->str); + n = atof(((string_type *) str)->str); + + if (ceilf(n) == n) { + result.integer_t.tag = integer_tag; + result.integer_t.value = (int)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; } - result.integer_t = n; + return result; }