mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-15 08:47:35 +02:00
Allow (string->number) to parse floats and ints
This commit is contained in:
parent
ea8f56a4ee
commit
cbcde1e0b1
3 changed files with 18 additions and 5 deletions
|
@ -40,6 +40,7 @@ typedef long tag_type;
|
|||
#include <setjmp.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <math.h>
|
||||
|
||||
#ifndef CLOCKS_PER_SEC
|
||||
/* 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 */
|
||||
typedef union {
|
||||
boolean_type boolean_t;
|
||||
cons_type cons_t;
|
||||
symbol_type symbol_t;
|
||||
primitive_type primitive_t;
|
||||
|
|
|
@ -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??
|
||||
|
|
19
runtime.h
19
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue