2014-12-22 12:35:05 +01:00
|
|
|
#include "c_stdlib.h"
|
|
|
|
#include "c_stdio.h"
|
|
|
|
#include "c_types.h"
|
|
|
|
#include "c_string.h"
|
|
|
|
#include "user_interface.h"
|
2015-02-11 16:07:08 +01:00
|
|
|
#include "user_config.h"
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
// const char *lua_init_value = "print(\"Hello world\")";
|
|
|
|
const char *lua_init_value = "@init.lua";
|
|
|
|
|
2015-01-05 03:09:51 +01:00
|
|
|
// int c_abs(int x){
|
2015-02-11 16:05:46 +01:00
|
|
|
// return x>0?x:0-x;
|
2014-12-22 12:35:05 +01:00
|
|
|
// }
|
2015-01-05 03:09:51 +01:00
|
|
|
// void c_exit(int e){
|
2014-12-22 12:35:05 +01:00
|
|
|
// }
|
2015-02-11 16:05:46 +01:00
|
|
|
const char *c_getenv(const char *__string)
|
|
|
|
{
|
|
|
|
if (c_strcmp(__string, "LUA_INIT") == 0)
|
|
|
|
{
|
|
|
|
return lua_init_value;
|
|
|
|
}
|
|
|
|
return NULL;
|
2014-12-22 12:35:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure there is enough memory before real malloc, otherwise malloc will panic and reset
|
2015-01-24 16:36:04 +01:00
|
|
|
// void *c_malloc(size_t __size){
|
2015-02-11 16:05:46 +01:00
|
|
|
// if(__size>system_get_free_heap_size()){
|
|
|
|
// NODE_ERR("malloc: not enough memory\n");
|
|
|
|
// return NULL;
|
|
|
|
// }
|
|
|
|
// return (void *)os_malloc(__size);
|
2015-01-24 16:36:04 +01:00
|
|
|
// }
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2015-01-24 16:36:04 +01:00
|
|
|
// void *c_zalloc(size_t __size){
|
2015-02-11 16:05:46 +01:00
|
|
|
// if(__size>system_get_free_heap_size()){
|
|
|
|
// NODE_ERR("zalloc: not enough memory\n");
|
|
|
|
// return NULL;
|
|
|
|
// }
|
|
|
|
// return (void *)os_zalloc(__size);
|
2015-01-24 16:36:04 +01:00
|
|
|
// }
|
2014-12-22 12:35:05 +01:00
|
|
|
|
2015-01-24 16:36:04 +01:00
|
|
|
// void c_free(void *p){
|
2015-02-11 16:05:46 +01:00
|
|
|
// // NODE_ERR("free1: %d\n", system_get_free_heap_size());
|
|
|
|
// os_free(p);
|
|
|
|
// // NODE_ERR("-free1: %d\n", system_get_free_heap_size());
|
2015-01-24 16:36:04 +01:00
|
|
|
// }
|
2014-12-22 12:35:05 +01:00
|
|
|
|
|
|
|
|
2015-02-11 16:05:46 +01:00
|
|
|
// int c_rand(void){
|
2014-12-22 12:35:05 +01:00
|
|
|
// }
|
2015-01-05 03:09:51 +01:00
|
|
|
// void c_srand(unsigned int __seed){
|
2014-12-22 12:35:05 +01:00
|
|
|
// }
|
|
|
|
|
2015-02-11 16:05:46 +01:00
|
|
|
// int c_atoi(const char *__nptr){
|
2014-12-22 12:35:05 +01:00
|
|
|
// }
|
2015-01-25 21:15:54 +01:00
|
|
|
#include <_ansi.h>
|
|
|
|
//#include <reent.h>
|
|
|
|
#include <string.h>
|
|
|
|
//#include "mprec.h"
|
|
|
|
|
2015-02-11 16:07:08 +01:00
|
|
|
double powersOf10[] ICACHE_STORE_ATTR ICACHE_RODATA_ATTR = /* Table giving binary powers of 10. Entry */
|
2015-01-25 21:15:54 +01:00
|
|
|
{
|
2015-02-11 16:05:46 +01:00
|
|
|
10., /* is 10^2^i. Used to convert decimal */
|
|
|
|
100., /* exponents into floating-point numbers. */
|
2015-01-25 21:15:54 +01:00
|
|
|
1.0e4,
|
|
|
|
1.0e8,
|
|
|
|
1.0e16,
|
|
|
|
1.0e32,
|
|
|
|
1.0e64,
|
|
|
|
1.0e128,
|
|
|
|
1.0e256
|
2015-02-11 16:05:46 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
double c_strtod(const char *string, char **endPtr)
|
|
|
|
{
|
|
|
|
int maxExponent = 511; /* Largest possible base 10 exponent. Any
|
|
|
|
* exponent larger than this will already
|
|
|
|
* produce underflow or overflow, so there's
|
|
|
|
* no need to worry about additional digits.
|
|
|
|
*/
|
|
|
|
|
2015-01-25 21:15:54 +01:00
|
|
|
int sign, expSign = FALSE;
|
|
|
|
double fraction, dblExp, *d;
|
|
|
|
register const char *p;
|
|
|
|
register int c;
|
2015-02-11 16:05:46 +01:00
|
|
|
int exp = 0; /* Exponent read from "EX" field. */
|
|
|
|
int fracExp = 0; /* Exponent that derives from the fractional
|
|
|
|
* part. Under normal circumstatnces, it is
|
|
|
|
* the negative of the number of digits in F.
|
|
|
|
* However, if I is very long, the last digits
|
|
|
|
* of I get dropped (otherwise a long I with a
|
|
|
|
* large negative exponent could cause an
|
|
|
|
* unnecessary overflow on I alone). In this
|
|
|
|
* case, fracExp is incremented one for each
|
|
|
|
* dropped digit. */
|
|
|
|
int mantSize; /* Number of digits in mantissa. */
|
|
|
|
int decPt; /* Number of mantissa digits BEFORE decimal
|
|
|
|
* point. */
|
|
|
|
const char *pExp; /* Temporarily holds location of exponent
|
|
|
|
* in string. */
|
2015-01-25 21:15:54 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Strip off leading blanks and check for a sign.
|
|
|
|
*/
|
|
|
|
|
|
|
|
p = string;
|
2015-02-11 16:05:46 +01:00
|
|
|
while (isspace((unsigned char)(*p)))
|
|
|
|
{
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
if (*p == '-')
|
|
|
|
{
|
|
|
|
sign = TRUE;
|
|
|
|
p += 1;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
2015-02-11 16:05:46 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*p == '+')
|
|
|
|
{
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
sign = FALSE;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Count the number of digits in the mantissa (including the decimal
|
|
|
|
* point), and also locate the decimal point.
|
|
|
|
*/
|
|
|
|
|
|
|
|
decPt = -1;
|
|
|
|
for (mantSize = 0; ; mantSize += 1)
|
|
|
|
{
|
2015-02-11 16:05:46 +01:00
|
|
|
c = *p;
|
|
|
|
if (!isdigit(c))
|
|
|
|
{
|
|
|
|
if ((c != '.') || (decPt >= 0))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
decPt = mantSize;
|
|
|
|
}
|
|
|
|
p += 1;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now suck up the digits in the mantissa. Use two integers to
|
|
|
|
* collect 9 digits each (this is faster than using floating-point).
|
|
|
|
* If the mantissa has more than 18 digits, ignore the extras, since
|
|
|
|
* they can't affect the value anyway.
|
|
|
|
*/
|
2015-02-11 16:05:46 +01:00
|
|
|
|
2015-01-25 21:15:54 +01:00
|
|
|
pExp = p;
|
|
|
|
p -= mantSize;
|
2015-02-11 16:05:46 +01:00
|
|
|
if (decPt < 0)
|
|
|
|
{
|
|
|
|
decPt = mantSize;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
2015-02-11 16:05:46 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
mantSize -= 1; /* One of the digits was the point. */
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
2015-02-11 16:05:46 +01:00
|
|
|
if (mantSize > 18)
|
|
|
|
{
|
|
|
|
fracExp = decPt - 18;
|
|
|
|
mantSize = 18;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fracExp = decPt - mantSize;
|
|
|
|
}
|
|
|
|
if (mantSize == 0)
|
|
|
|
{
|
|
|
|
fraction = 0.0;
|
|
|
|
p = string;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int frac1, frac2;
|
|
|
|
frac1 = 0;
|
|
|
|
for ( ; mantSize > 9; mantSize -= 1)
|
|
|
|
{
|
|
|
|
c = *p;
|
|
|
|
p += 1;
|
|
|
|
if (c == '.')
|
|
|
|
{
|
|
|
|
c = *p;
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
frac1 = 10 * frac1 + (c - '0');
|
|
|
|
}
|
|
|
|
frac2 = 0;
|
|
|
|
for (; mantSize > 0; mantSize -= 1)
|
|
|
|
{
|
|
|
|
c = *p;
|
|
|
|
p += 1;
|
|
|
|
if (c == '.')
|
|
|
|
{
|
|
|
|
c = *p;
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
frac2 = 10 * frac2 + (c - '0');
|
|
|
|
}
|
|
|
|
fraction = (1.0e9 * frac1) + frac2;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Skim off the exponent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
p = pExp;
|
2015-02-11 16:05:46 +01:00
|
|
|
if ((*p == 'E') || (*p == 'e'))
|
|
|
|
{
|
|
|
|
p += 1;
|
|
|
|
if (*p == '-')
|
|
|
|
{
|
|
|
|
expSign = TRUE;
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*p == '+')
|
|
|
|
{
|
|
|
|
p += 1;
|
|
|
|
}
|
|
|
|
expSign = FALSE;
|
|
|
|
}
|
|
|
|
if (!isdigit((unsigned char)(*p)))
|
|
|
|
{
|
|
|
|
p = pExp;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
while (isdigit((unsigned char)(*p)))
|
|
|
|
{
|
|
|
|
exp = exp * 10 + (*p - '0');
|
|
|
|
p += 1;
|
|
|
|
}
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
2015-02-11 16:05:46 +01:00
|
|
|
if (expSign)
|
|
|
|
{
|
|
|
|
exp = fracExp - exp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
exp = fracExp + exp;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate a floating-point number that represents the exponent.
|
|
|
|
* Do this by processing the exponent one bit at a time to combine
|
|
|
|
* many powers of 2 of 10. Then combine the exponent with the
|
|
|
|
* fraction.
|
|
|
|
*/
|
2015-02-11 16:05:46 +01:00
|
|
|
|
|
|
|
if (exp < 0)
|
|
|
|
{
|
|
|
|
expSign = TRUE;
|
|
|
|
exp = -exp;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
2015-02-11 16:05:46 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
expSign = FALSE;
|
|
|
|
}
|
|
|
|
if (exp > maxExponent)
|
|
|
|
{
|
|
|
|
exp = maxExponent;
|
|
|
|
// errno = ERANGE;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
|
|
|
dblExp = 1.0;
|
2015-02-11 16:05:46 +01:00
|
|
|
for (d = powersOf10; exp != 0; exp >>= 1, d += 1)
|
|
|
|
{
|
|
|
|
if (exp & 01)
|
|
|
|
{
|
|
|
|
dblExp *= *d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (expSign)
|
|
|
|
{
|
|
|
|
fraction /= dblExp;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
2015-02-11 16:05:46 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fraction *= dblExp;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2015-02-11 16:05:46 +01:00
|
|
|
if (endPtr != NULL)
|
|
|
|
{
|
|
|
|
*endPtr = (char *) p;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
|
|
|
|
2015-02-11 16:05:46 +01:00
|
|
|
if (sign)
|
|
|
|
{
|
|
|
|
return -fraction;
|
2015-01-25 21:15:54 +01:00
|
|
|
}
|
|
|
|
return fraction;
|
|
|
|
}
|
|
|
|
|
2015-02-11 16:05:46 +01:00
|
|
|
// long c_strtol(const char *__n, char **__end_PTR, int __base){
|
2014-12-22 12:35:05 +01:00
|
|
|
// }
|
2015-01-05 03:09:51 +01:00
|
|
|
// unsigned long c_strtoul(const char *__n, char **__end_PTR, int __base){
|
2014-12-22 12:35:05 +01:00
|
|
|
// }
|
2015-01-05 03:09:51 +01:00
|
|
|
// long long c_strtoll(const char *__n, char **__end_PTR, int __base){
|
2014-12-22 12:35:05 +01:00
|
|
|
// }
|