shithub: femtolisp

Download patch

ref: 01890ff233ede5b4429f84c6e469d51a69c1cc68
parent: fe8b7a702954ae878054df064f9e007037ef8b59
author: Lassi Kortela <lassi@lassi.io>
date: Wed Feb 26 06:16:19 EST 2020

Use NUM_FORMAT in all tiny interpreters

Avoids compiler warnings about printf()

--- a/tiny/lisp-nontail.c
+++ b/tiny/lisp-nontail.c
@@ -21,6 +21,7 @@
 #include <sys/types.h>
 
 #include <ctype.h>
+#include <inttypes.h>
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -28,9 +29,11 @@
 #include <string.h>
 
 #ifdef __LP64__
+#define NUM_FORMAT "%" PRId64
 typedef u_int64_t value_t;
 typedef int64_t number_t;
 #else
+#define NUM_FORMAT "%" PRId32
 typedef u_int32_t value_t;
 typedef int32_t number_t;
 #endif
@@ -504,7 +507,7 @@
     value_t cd;
 
     switch (tag(v)) {
-    case TAG_NUM: fprintf(f, "%d", numval(v)); break;
+    case TAG_NUM: fprintf(f, NUM_FORMAT, numval(v)); break;
     case TAG_SYM: fprintf(f, "%s", ((symbol_t*)ptr(v))->name); break;
     case TAG_BUILTIN: fprintf(f, "#<builtin %s>",
                               builtin_names[intval(v)]); break;
--- a/tiny/lisp.c
+++ b/tiny/lisp.c
@@ -21,6 +21,7 @@
 #include <sys/types.h>
 
 #include <ctype.h>
+#include <inttypes.h>
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -28,9 +29,11 @@
 #include <string.h>
 
 #ifdef __LP64__
+#define NUM_FORMAT "%" PRId64
 typedef u_int64_t value_t;
 typedef int64_t number_t;
 #else
+#define NUM_FORMAT "%" PRId32
 typedef u_int32_t value_t;
 typedef int32_t number_t;
 #endif
@@ -502,7 +505,7 @@
     value_t cd;
 
     switch (tag(v)) {
-    case TAG_NUM: fprintf(f, "%ld", numval(v)); break;
+    case TAG_NUM: fprintf(f, NUM_FORMAT, numval(v)); break;
     case TAG_SYM: fprintf(f, "%s", ((symbol_t*)ptr(v))->name); break;
     case TAG_BUILTIN: fprintf(f, "#<builtin %s>",
                               builtin_names[intval(v)]); break;
--- a/tiny/lisp2.c
+++ b/tiny/lisp2.c
@@ -42,6 +42,7 @@
 #include <sys/types.h>
 
 #include <ctype.h>
+#include <inttypes.h>
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -49,9 +50,11 @@
 #include <string.h>
 
 #ifdef __LP64__
+#define NUM_FORMAT "%" PRId64
 typedef u_int64_t value_t;
 typedef int64_t number_t;
 #else
+#define NUM_FORMAT "%" PRId32
 typedef u_int32_t value_t;
 typedef int32_t number_t;
 #endif
@@ -765,7 +768,7 @@
     char *name;
 
     switch (tag(v)) {
-    case TAG_NUM: fprintf(f, "%d", numval(v)); break;
+    case TAG_NUM: fprintf(f, NUM_FORMAT, numval(v)); break;
     case TAG_SYM:
         name = ((symbol_t*)ptr(v))->name;
         if (princ)
--- a/tiny/lispf.c
+++ b/tiny/lispf.c
@@ -27,6 +27,7 @@
 #include <sys/types.h>
 
 #include <ctype.h>
+#include <inttypes.h>
 #include <setjmp.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -40,11 +41,14 @@
 #endif
 
 #ifdef FLOAT
+#define NUM_FORMAT "%f"
 typedef float number_t;
 #else
 #ifdef __LP64__
+#define NUM_FORMAT "%" PRId64
 typedef int64_t number_t;
 #else
+#define NUM_FORMAT "%" PRId32
 typedef int32_t number_t;
 #endif
 #endif
@@ -73,13 +77,11 @@
 #ifdef FLOAT
 #define number(x) ((*(value_t*)&(x))&~0x3)
 #define numval(x)  (*(number_t*)&(x))
-#define NUM_FORMAT "%f"
 extern float strtof(const char *nptr, char **endptr);
 #define strtonum(s, e) strtof(s, e)
 #else
 #define number(x) ((value_t)((x)<<2))
 #define numval(x)  (((number_t)(x))>>2)
-#define NUM_FORMAT "%d"
 #define strtonum(s, e) strtol(s, e, 10)
 #endif
 #define intval(x)  (((int)(x))>>2)