add libc fallback

This commit is contained in:
Yuichi Nishiwaki 2015-05-28 01:55:55 +09:00
parent 7dcd050e1a
commit 304664eaf0
4 changed files with 167 additions and 7 deletions

View File

@ -29,18 +29,28 @@ extern "C" {
#endif
#include <stddef.h>
#include <stdbool.h>
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#if __STDC_VERSION__ >= 199901L
# include <stdbool.h>
#else
# define bool char
# define true 1
# define false 0
#endif
#if __STDC_VERSION__ >= 199901L
# include <stddef.h>
#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 <math.h>

View File

@ -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 <string.h>
#include <ctype.h>
#include <assert.h>
#include <stdlib.h>
#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

View File

@ -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

View File

@ -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': {