diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 3d43a428..40428c03 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -29,18 +29,28 @@ extern "C" { #endif #include -#include #include #include #include -#include -#include -#include -#include + +#if __STDC_VERSION__ >= 199901L +# include +#else +# define bool char +# define true 1 +# define false 0 +#endif + +#if __STDC_VERSION__ >= 199901L +# include +#elif ! defined(offsetof) +# define offsetof(s,m) ((size_t)&(((s *)NULL)->m)) +#endif #include "picrin/config.h" #include "picrin/util.h" +#include "picrin/compat.h" #if PIC_ENABLE_FLOAT # include diff --git a/extlib/benz/include/picrin/compat.h b/extlib/benz/include/picrin/compat.h new file mode 100644 index 00000000..cca83d95 --- /dev/null +++ b/extlib/benz/include/picrin/compat.h @@ -0,0 +1,141 @@ +/** + * See Copyright Notice in picrin.h + */ + +#ifndef PICRIN_COMPAT_H +#define PICRIN_COMPAT_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#if PIC_ENABLE_LIBC + +#include +#include +#include +#include + +#else + +# define assert(v) 0 + +PIC_INLINE int +isspace(int c) +{ + return c == ' ' || c == '\t' || c == '\r' || c == '\v' || c == '\f' || c == '\n'; +} + +PIC_INLINE int +tolower(int c) +{ + return ('A' <= c && c <= 'Z') ? c - 'A' + 'a' : c; +} + +PIC_INLINE int +isdigit(int c) +{ + return '0' <= c && c <= '9'; +} + +PIC_INLINE char * +strchr(const char *s, int c) +{ + do { + if (*s == c) + return (char *)s; + } while (*s++ != '\0'); + return NULL; +} + +PIC_INLINE size_t +strlen(const char *s) +{ + size_t l = 0; + + while (*s++) { + l++; + } + return l; +} + +PIC_INLINE int +strcmp(const char *s1, const char *s2) +{ + while (*s1 && *s1 == *s2) { + s1++; + s2++; + } + return (unsigned)*s1 - (unsigned)*s2; +} + +PIC_INLINE long +strtol(const char *nptr, char **endptr, int base) +{ + long l = 0; + char c; + int n; + + while (1) { + c = *nptr; + if ('0' <= c && c <= '9') + n = c - '0'; + else if ('a' <= c && c <= 'z') + n = c - 'a' + 10; + else if ('A' <= c && c <= 'Z') + n = c - 'A' + 10; + else + goto exit; + + if (base <= n) + goto exit; + + l = l * base + n; + nptr++; + } + exit: + if (endptr) + *endptr = (char *)nptr; + return l; +} + +PIC_INLINE void * +memset(void *s, int n, size_t c) +{ + char *p = s; + + while (c-- > 0) { + *p++ = n; + } + return s; +} + +PIC_INLINE void * +memcpy(void *dst, const void *src, size_t n) +{ + const char *s = src; + char *d = dst; + + while (n-- > 0) { + *d++ = *s++; + } + return d; +} + +PIC_INLINE char * +strcpy(char *dst, const char *src) +{ + char *d = dst; + + while ((*dst++ = *src++) != 0); + + return d; +} + +#endif + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/extlib/benz/include/picrin/config.h b/extlib/benz/include/picrin/config.h index edf07594..58f5d54b 100644 --- a/extlib/benz/include/picrin/config.h +++ b/extlib/benz/include/picrin/config.h @@ -14,6 +14,9 @@ /** enable floating point number support */ /* #define PIC_ENABLE_FLOAT 1 */ +/** no dependency on libc */ +/* #define PIC_ENABLE_LIBC 1 */ + /** treat false value as none */ /* #define PIC_NONE_IS_FALSE 1 */ @@ -82,6 +85,10 @@ # endif #endif +#ifndef PIC_ENABLE_LIBC +# define PIC_ENABLE_LIBC 1 +#endif + #if PIC_NAN_BOXING && defined(PIC_ENABLE_FLOAT) && ! PIC_ENABLE_FLOAT # error cannot disable float support when nan boxing is on #endif diff --git a/extlib/benz/vm.c b/extlib/benz/vm.c index 130084cd..fa07c6d2 100644 --- a/extlib/benz/vm.c +++ b/extlib/benz/vm.c @@ -255,10 +255,12 @@ pic_get_args(pic_state *pic, const char *format, ...) cstr = va_arg(ap, const char **); v = GET_OPERAND(pic,i); - if (! pic_str_p(v)) { + if (pic_str_p(v)) { + *cstr = pic_str_cstr(pic, pic_str_ptr(v)); + } + else { pic_errorf(pic, "pic_get_args: expected string, but got ~s", v); } - *cstr = pic_str_cstr(pic, pic_str_ptr(v)); break; } case 'm': {