summaryrefslogtreecommitdiff
path: root/src/simple_ast.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/simple_ast.h')
-rw-r--r--src/simple_ast.h21
1 files changed, 15 insertions, 6 deletions
diff --git a/src/simple_ast.h b/src/simple_ast.h
index a04fbf712..de894b4bc 100644
--- a/src/simple_ast.h
+++ b/src/simple_ast.h
@@ -811,14 +811,14 @@ struct JSPrinter {
emit(node[1]->getCString());
}
- void printNum(Ref node) {
- double d = node[1]->getNumber();
+ static char* numToString(double d, bool finalize=true) {
bool neg = d < 0;
if (neg) d = -d;
// try to emit the fewest necessary characters
bool integer = fmod(d, 1) == 0;
#define BUFFERSIZE 1000
- static char storage_f[BUFFERSIZE], storage_e[BUFFERSIZE]; // f is normal, e is scientific for float, x for integer
+ static char full_storage_f[BUFFERSIZE], full_storage_e[BUFFERSIZE]; // f is normal, e is scientific for float, x for integer
+ static char *storage_f = full_storage_f + 1, *storage_e = full_storage_e + 1; // full has one more char, for a possible '-'
double err_f, err_e;
for (int e = 0; e <= 1; e++) {
char *buffer = e ? storage_e : storage_f;
@@ -919,12 +919,21 @@ struct JSPrinter {
//errv("..current attempt: %.18f => %s", d, buffer);
}
//fprintf(stderr, "options:\n%s\n%s\n (first? %d)\n", storage_e, storage_f, strlen(storage_e) < strlen(storage_f));
- if (neg) emit('-');
+ char *ret;
if (err_e == err_f) {
- emit(strlen(storage_e) < strlen(storage_f) ? storage_e : storage_f);
+ ret = strlen(storage_e) < strlen(storage_f) ? storage_e : storage_f;
} else {
- emit(err_e < err_f ? storage_e : storage_f);
+ ret = err_e < err_f ? storage_e : storage_f;
+ }
+ if (neg) {
+ ret--; // safe to go back one, there is one more char in full_*
+ *ret = '-';
}
+ return ret;
+ }
+
+ void printNum(Ref node) {
+ emit(numToString(node[1]->getNumber(), finalize));
}
void printString(Ref node) {