2014-11-02 13:49:06 +01:00
|
|
|
/****************************************************************************
|
2014-11-11 19:34:00 +01:00
|
|
|
* apps/interpreters/bas/bas_value.c
|
2014-11-02 13:49:06 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 1999-2014 Michael Haardt
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
2023-02-22 06:39:24 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
2014-11-02 13:49:06 +01:00
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-03 16:11:25 +01:00
|
|
|
#include <nuttx/config.h>
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <float.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-11-11 19:34:00 +01:00
|
|
|
#include "bas_error.h"
|
|
|
|
#include "bas_value.h"
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-03 15:33:39 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define _(String) String
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const char *typestr[] =
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
(const char *)0,
|
|
|
|
(const char *)0,
|
2014-10-27 14:53:12 +01:00
|
|
|
"integer",
|
2014-11-02 02:01:20 +01:00
|
|
|
(const char *)0,
|
2014-10-27 14:53:12 +01:00
|
|
|
"real",
|
|
|
|
"string",
|
|
|
|
"void"
|
|
|
|
};
|
|
|
|
|
|
|
|
/* for xgettext */
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
const enum ValueType Value_commonType[V_VOID + 1][V_VOID + 1] =
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
{ 0, 0, 0, 0, 0, 0, 0 },
|
|
|
|
{ 0, V_ERROR, V_ERROR, V_ERROR, V_ERROR, V_ERROR, V_ERROR },
|
|
|
|
{ 0, V_ERROR, V_INTEGER, V_ERROR, V_REAL, V_ERROR, V_ERROR },
|
|
|
|
{ 0, V_ERROR, V_ERROR, V_ERROR, V_ERROR, V_ERROR, V_ERROR },
|
|
|
|
{ 0, V_ERROR, V_REAL, V_ERROR, V_REAL, V_ERROR, V_ERROR },
|
|
|
|
{ 0, V_ERROR, V_ERROR, V_ERROR, V_ERROR, V_STRING, V_ERROR },
|
|
|
|
{ 0, V_ERROR, V_ERROR, V_ERROR, V_ERROR, V_ERROR, V_ERROR }
|
|
|
|
};
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void format_double(struct String *buf, double value, int width,
|
|
|
|
int precision, int exponent)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
if (exponent)
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
size_t len;
|
|
|
|
char *e;
|
|
|
|
int en;
|
|
|
|
|
|
|
|
len = buf->length;
|
|
|
|
String_appendPrintf(buf, "%.*E", width - 1 - (precision >= 0), value);
|
|
|
|
if (buf->character[len + 1] == '.')
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_delete(buf, len + 1, 1);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
if (precision >= 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_insertChar(buf, len + width - precision - 1, '.');
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
for (e = buf->character + buf->length - 1;
|
2014-11-02 13:49:06 +01:00
|
|
|
e >= buf->character && *e != 'E';
|
|
|
|
--e);
|
2014-11-02 02:01:20 +01:00
|
|
|
++e;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
en = strtol(e, (char **)0, 10);
|
|
|
|
en = en + 2 - (width - precision);
|
|
|
|
len = e - buf->character;
|
|
|
|
String_delete(buf, len, buf->length - len);
|
|
|
|
String_appendPrintf(buf, "%+0*d", exponent - 1, en);
|
|
|
|
}
|
|
|
|
else if (precision > 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendPrintf(buf, "%.*f", precision, value);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else if (precision == 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendPrintf(buf, "%.f.", value);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else if (width)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendPrintf(buf, "%.f", value);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
double x = value;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (x < 0.0001 || x >= 10000000.0) /* print scientific notation */
|
|
|
|
{
|
|
|
|
String_appendPrintf(buf, "%.7g", value);
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
else /* print decimal numbers or integers, if
|
2014-11-02 02:01:20 +01:00
|
|
|
* possible */
|
|
|
|
{
|
2023-02-22 06:39:24 +01:00
|
|
|
int o;
|
|
|
|
int n;
|
|
|
|
int p = 6;
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
while (x >= 10.0 && p > 0)
|
|
|
|
{
|
|
|
|
x /= 10.0;
|
|
|
|
--p;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
o = buf->length;
|
|
|
|
String_appendPrintf(buf, "%.*f", p, value);
|
|
|
|
n = buf->length;
|
|
|
|
if (memchr(buf->character + o, '.', n - o))
|
|
|
|
{
|
|
|
|
while (buf->character[buf->length - 1] == '0')
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
--buf->length;
|
|
|
|
}
|
2023-02-22 06:39:24 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (buf->character[buf->length - 1] == '.')
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
--buf->length;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
double Value_trunc(double d)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
return (d < 0.0 ? ceil(d) : floor(d));
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
double Value_round(double d)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
return (d < 0.0 ? ceil(d - 0.5) : floor(d + 0.5));
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
long int Value_toi(double d, int *overflow)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
d = Value_round(d);
|
|
|
|
*overflow = (d < LONG_MIN || d > LONG_MAX);
|
2014-10-27 14:53:12 +01:00
|
|
|
return lrint(d);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
long int Value_vali(const char *s, char **end, int *overflow)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
long int n;
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
errno = 0;
|
|
|
|
if (*s == '&' && tolower(*(s + 1)) == 'h')
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
n = strtoul(s + 2, end, 16);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else if (*s == '&' && tolower(*(s + 1)) == 'o')
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
n = strtoul(s + 2, end, 8);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
n = strtol(s, end, 10);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
*overflow = (errno == ERANGE);
|
2014-10-27 14:53:12 +01:00
|
|
|
return n;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
double Value_vald(const char *s, char **end, int *overflow)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
double d;
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
errno = 0;
|
|
|
|
d = strtod(s, end);
|
|
|
|
*overflow = (errno == ERANGE);
|
2014-10-27 14:53:12 +01:00
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
struct Value *Value_new_NIL(struct Value *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
|
|
|
this->type = V_NIL;
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-02-22 06:39:24 +01:00
|
|
|
struct Value *Value_new_ERROR(struct Value *this, int code,
|
|
|
|
const char *error, ...)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[128];
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
|
|
|
va_start(ap, error);
|
|
|
|
vsprintf(buf, error, ap);
|
2014-10-27 14:53:12 +01:00
|
|
|
va_end(ap);
|
2014-11-02 02:01:20 +01:00
|
|
|
this->type = V_ERROR;
|
|
|
|
this->u.error.code = code;
|
2023-02-09 12:01:32 +01:00
|
|
|
this->u.error.msg = strdup(buf);
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_new_INTEGER(struct Value *this, int n)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
|
|
|
this->type = V_INTEGER;
|
|
|
|
this->u.integer = n;
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_new_REAL(struct Value *this, double n)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
|
|
|
this->type = V_REAL;
|
|
|
|
this->u.real = n;
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_new_STRING(struct Value *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
|
|
|
this->type = V_STRING;
|
2014-10-27 14:53:12 +01:00
|
|
|
String_new(&this->u.string);
|
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_new_VOID(struct Value *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
|
|
|
this->type = V_VOID;
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_new_null(struct Value *this, enum ValueType type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
2014-10-27 14:53:12 +01:00
|
|
|
switch (type)
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
this->type = V_INTEGER;
|
|
|
|
this->u.integer = 0;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
this->type = V_REAL;
|
|
|
|
this->u.real = 0.0;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
this->type = V_STRING;
|
|
|
|
String_new(&this->u.string);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_VOID:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
this->type = V_VOID;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
int Value_isNull(const struct Value *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (this->type)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
case V_INTEGER:
|
|
|
|
return (this->u.integer == 0);
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_REAL:
|
|
|
|
return (this->u.real == 0.0);
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_STRING:
|
|
|
|
return (this->u.string.length == 0);
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
void Value_destroy(struct Value *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
2014-10-27 14:53:12 +01:00
|
|
|
switch (this->type)
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_ERROR:
|
|
|
|
free(this->u.error.msg);
|
2014-10-27 14:53:12 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_INTEGER:
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_NIL:
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
String_destroy(&this->u.string);
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_VOID:
|
2014-10-27 14:53:12 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
this->type = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_clone(struct Value *this, const struct Value *original)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this != (struct Value *)0);
|
|
|
|
assert(original != (struct Value *)0);
|
2023-02-09 12:01:32 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (original->type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_ERROR:
|
|
|
|
{
|
2023-02-09 12:01:32 +01:00
|
|
|
this->u.error.msg = strdup(original->u.error.msg);
|
2014-11-02 02:01:20 +01:00
|
|
|
this->u.error.code = original->u.error.code;
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
this->u.integer = original->u.integer;
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_NIL:
|
2014-10-27 14:53:12 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
this->u.real = original->u.real;
|
2014-10-27 14:53:12 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
String_clone(&this->u.string, &original->u.string);
|
2014-10-27 14:53:12 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
this->type = original->type;
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_uplus(struct Value *this, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (this->type)
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDUOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_uneg(struct Value *this, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (this->type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = -this->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real = -this->u.real;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDUOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_unot(struct Value *this, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (this->type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = ~this->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_retype(this, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = ~this->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDUOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_add(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer += x->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real += x->u.real;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendString(&this->u.string, &x->u.string);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_sub(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer -= x->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real -= x->u.real;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_mult(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer *= x->u.integer;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real *= x->u.real;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_div(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.real == 0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, UNDEFINED, "Division by zero");
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real /= x->u.real;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.real == 0.0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, UNDEFINED, "Division by zero");
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real /= x->u.real;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_idiv(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.integer == 0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, UNDEFINED, "Division by zero");
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer /= x->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.real == 0.0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, UNDEFINED, "Division by zero");
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real = Value_trunc(this->u.real / x->u.real);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_mod(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_INTEGER:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.integer == 0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, UNDEFINED, "Modulo by zero");
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer %= x->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.real == 0.0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, UNDEFINED, "Modulo by zero");
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real = fmod(this->u.real, x->u.real);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_pow(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_INTEGER:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (this->u.integer == 0 && x->u.integer == 0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, UNDEFINED, "0^0");
|
|
|
|
}
|
|
|
|
else if (x->u.integer > 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = pow(this->u.integer, x->u.integer);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
long int thisi = this->u.integer;
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_REAL(this, pow(thisi, x->u.integer));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (this->u.real == 0.0 && x->u.real == 0.0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, UNDEFINED, "0^0");
|
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.real = pow(this->u.real, x->u.real);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_and(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer &= x->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_or(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer |= x->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_xor(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer ^= x->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_eqv(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = ~(this->u.integer ^ x->u.integer);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_STRING:
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Value *Value_imp(struct Value *this, struct Value *x, int calc)
|
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
|
|
|
case V_INTEGER:
|
|
|
|
case V_REAL:
|
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = (~this->u.integer) | x->u.integer;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, INVALIDOPERAND);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Value *Value_lt(struct Value *this, struct Value *x, int calc)
|
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = (this->u.integer < x->u.integer) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
|
|
|
int v;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (this->u.real < x->u.real) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_STRING:
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (String_cmp(&this->u.string, &x->u.string) < 0) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_le(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = (this->u.integer <= x->u.integer) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (this->u.real <= x->u.real) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (String_cmp(&this->u.string, &x->u.string) <= 0) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_eq(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = (this->u.integer == x->u.integer) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (this->u.real == x->u.real) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2023-02-22 06:39:24 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (String_cmp(&this->u.string, &x->u.string) == 0) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_ge(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = (this->u.integer >= x->u.integer) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (this->u.real >= x->u.real) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (String_cmp(&this->u.string, &x->u.string) >= 0) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_gt(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = (this->u.integer > x->u.integer) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (this->u.real > x->u.real) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (String_cmp(&this->u.string, &x->u.string) > 0) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_ne(struct Value *this, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (Value_commonType[this->type][x->type])
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
VALUE_RETYPE(this, V_INTEGER);
|
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
this->u.integer = (this->u.integer != x->u.integer) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
VALUE_RETYPE(this, V_REAL);
|
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = (this->u.real != x->u.real) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
int v;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = String_cmp(&this->u.string, &x->u.string) ? -1 : 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_INTEGER(this, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-02-22 06:39:24 +01:00
|
|
|
int Value_exitFor(struct Value *this,
|
|
|
|
struct Value *limit, struct Value *step)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (this->type)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
case V_INTEGER:
|
|
|
|
return
|
|
|
|
(step->u.integer < 0
|
|
|
|
? (this->u.integer < limit->u.integer)
|
|
|
|
: (this->u.integer > limit->u.integer));
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_REAL:
|
|
|
|
return
|
|
|
|
(step->u.real < 0.0
|
|
|
|
? (this->u.real < limit->u.real) : (this->u.real > limit->u.real));
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_STRING:
|
|
|
|
return (String_cmp(&this->u.string, &limit->u.string) > 0);
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
void Value_errorPrefix(struct Value *this, const char *prefix)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-02-22 06:39:24 +01:00
|
|
|
size_t prefixlen;
|
|
|
|
size_t msglen;
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
assert(this->type == V_ERROR);
|
|
|
|
prefixlen = strlen(prefix);
|
|
|
|
msglen = strlen(this->u.error.msg);
|
|
|
|
this->u.error.msg = realloc(this->u.error.msg, prefixlen + msglen + 1);
|
|
|
|
memmove(this->u.error.msg + prefixlen, this->u.error.msg, msglen);
|
|
|
|
memcpy(this->u.error.msg, prefix, prefixlen);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
void Value_errorSuffix(struct Value *this, const char *suffix)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-02-22 06:39:24 +01:00
|
|
|
size_t suffixlen;
|
|
|
|
size_t msglen;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
assert(this->type == V_ERROR);
|
|
|
|
suffixlen = strlen(suffix);
|
|
|
|
msglen = strlen(this->u.error.msg);
|
|
|
|
this->u.error.msg = realloc(this->u.error.msg, suffixlen + msglen + 1);
|
|
|
|
memcpy(this->u.error.msg + msglen, suffix, suffixlen + 1);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_new_typeError(struct Value *this, enum ValueType t1,
|
|
|
|
enum ValueType t2)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
assert(typestr[t1]);
|
|
|
|
assert(typestr[t2]);
|
2023-02-22 06:39:24 +01:00
|
|
|
return Value_new_ERROR(this, TYPEMISMATCH1,
|
|
|
|
_(typestr[t1]), _(typestr[t2]));
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
static void retypeError(struct Value *this, enum ValueType to)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
enum ValueType thisType = this->type;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
assert(typestr[thisType]);
|
|
|
|
assert(typestr[to]);
|
|
|
|
Value_destroy(this);
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_new_ERROR(this, TYPEMISMATCH1, _(typestr[thisType]), _(typestr[to]));
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_retype(struct Value *this, enum ValueType type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (this->type)
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case V_INTEGER:
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_REAL:
|
|
|
|
this->u.real = this->u.integer;
|
|
|
|
this->type = type;
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_VOID:
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_VOID(this);
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
retypeError(this, type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
int overflow;
|
|
|
|
|
|
|
|
switch (type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
|
|
|
this->u.integer = Value_toi(this->u.real, &overflow);
|
|
|
|
this->type = V_INTEGER;
|
|
|
|
if (overflow)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_ERROR(this, OUTOFRANGE, typestr[V_INTEGER]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_REAL:
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_VOID:
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_destroy(this);
|
2014-11-02 02:01:20 +01:00
|
|
|
Value_new_VOID(this);
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
retypeError(this, type);
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case V_STRING:
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_VOID:
|
|
|
|
Value_destroy(this);
|
|
|
|
Value_new_VOID(this);
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
retypeError(this, type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_VOID:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case V_VOID:
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
retypeError(this, type);
|
|
|
|
}
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_ERROR:
|
2014-10-27 14:53:12 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct String *Value_toString(struct Value *this, struct String *s, char pad,
|
|
|
|
int headingsign, size_t width, int commas,
|
|
|
|
int dollar, int dollarleft, int precision,
|
|
|
|
int exponent, int trailingsign)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
size_t oldlength = s->length;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
switch (this->type)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
case V_ERROR:
|
|
|
|
String_appendChars(s, this->u.error.msg);
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
int sign;
|
|
|
|
struct String buf;
|
|
|
|
size_t totalwidth = width;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
String_new(&buf);
|
|
|
|
if (this->type == V_INTEGER)
|
|
|
|
{
|
|
|
|
if (this->u.integer < 0)
|
|
|
|
{
|
|
|
|
sign = -1;
|
|
|
|
this->u.integer = -this->u.integer;
|
|
|
|
}
|
|
|
|
else if (this->u.integer == 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
sign = 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
sign = 1;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this->u.real < 0.0)
|
|
|
|
{
|
|
|
|
sign = -1;
|
|
|
|
this->u.real = -this->u.real;
|
|
|
|
}
|
|
|
|
else if (this->u.real == 0.0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
sign = 0;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
sign = 1;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (headingsign)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
{
|
|
|
|
++totalwidth;
|
2014-11-02 02:27:54 +01:00
|
|
|
String_appendChar(&buf, sign == -1 ? '-' : ' ');
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case 0:
|
|
|
|
{
|
|
|
|
if (sign == -1)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendChar(&buf, '-');
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
++totalwidth;
|
|
|
|
String_appendChar(&buf, sign == -1 ? '-' : '+');
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case 2:
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
totalwidth += exponent;
|
|
|
|
if (this->type == V_INTEGER)
|
|
|
|
{
|
|
|
|
if (precision > 0 || exponent)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-02-22 06:39:24 +01:00
|
|
|
format_double(&buf, (double)this->u.integer, width,
|
|
|
|
precision, exponent);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else if (precision == 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendPrintf(&buf, "%lu.", this->u.integer);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendPrintf(&buf, "%lu", this->u.integer);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
format_double(&buf, this->u.real, width, precision, exponent);
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (commas)
|
|
|
|
{
|
|
|
|
size_t digits;
|
|
|
|
int first;
|
|
|
|
|
|
|
|
first = (headingsign ? 1 : 0);
|
|
|
|
for (digits = first;
|
|
|
|
digits < buf.length && buf.character[digits] >= '0' &&
|
|
|
|
buf.character[digits] <= '9'; ++digits);
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
while (digits > first + 3)
|
|
|
|
{
|
|
|
|
digits -= 3;
|
|
|
|
String_insertChar(&buf, digits, ',');
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (dollar)
|
|
|
|
{
|
|
|
|
String_insertChar(&buf, 0, '$');
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (trailingsign == -1)
|
|
|
|
{
|
|
|
|
++totalwidth;
|
|
|
|
String_appendChar(&buf, sign == -1 ? '-' : ' ');
|
|
|
|
}
|
|
|
|
else if (trailingsign == 1)
|
|
|
|
{
|
|
|
|
++totalwidth;
|
|
|
|
String_appendChar(&buf, sign == -1 ? '-' : '+');
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
String_size(s,
|
|
|
|
oldlength + (totalwidth >
|
|
|
|
buf.length ? totalwidth : buf.length));
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (totalwidth > buf.length)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
memset(s->character + oldlength, pad,
|
|
|
|
totalwidth - buf.length + dollarleft);
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
memcpy(s->character + oldlength +
|
|
|
|
(totalwidth >
|
|
|
|
buf.length ? (totalwidth - buf.length) : 0) + dollarleft,
|
|
|
|
buf.character + dollarleft, buf.length - dollarleft);
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (dollarleft)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
s->character[oldlength] = '$';
|
|
|
|
}
|
2014-11-07 18:16:14 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
String_destroy(&buf);
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
if (width > 0)
|
|
|
|
{
|
|
|
|
size_t blanks =
|
|
|
|
(this->u.string.length <
|
|
|
|
width ? (width - this->u.string.length) : 0);
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
String_size(s, oldlength + width);
|
|
|
|
memcpy(s->character + oldlength, this->u.string.character,
|
|
|
|
blanks ? this->u.string.length : width);
|
|
|
|
if (blanks)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
memset(s->character + oldlength + this->u.string.length, ' ',
|
|
|
|
blanks);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendString(s, &this->u.string);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return s;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_toStringUsing(struct Value *this, struct String *s,
|
|
|
|
struct String *using, size_t * usingpos)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
char pad = ' ';
|
2014-10-27 14:53:12 +01:00
|
|
|
int headingsign;
|
2014-11-02 02:01:20 +01:00
|
|
|
int width = 0;
|
|
|
|
int commas = 0;
|
|
|
|
int dollar = 0;
|
|
|
|
int dollarleft = 0;
|
|
|
|
int precision = -1;
|
|
|
|
int exponent = 0;
|
|
|
|
int trailingsign = 0;
|
|
|
|
|
|
|
|
headingsign = (using->length ? 0 : -1);
|
|
|
|
if (*usingpos == using->length)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
*usingpos = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
while (*usingpos < using->length)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (using->character[*usingpos])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case '_': /* output next char */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
++(*usingpos);
|
|
|
|
if (*usingpos < using->length)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendChar(s, using->character[(*usingpos)++]);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
return Value_new_ERROR(this, MISSINGCHARACTER);
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case '!': /* output first character of string */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
width = 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
++(*usingpos);
|
2014-11-02 02:01:20 +01:00
|
|
|
goto work;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case '\\': /* output n characters of string */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
width = 1;
|
|
|
|
++(*usingpos);
|
|
|
|
while (*usingpos < using->length &&
|
|
|
|
using->character[*usingpos] == ' ')
|
|
|
|
{
|
|
|
|
++(*usingpos);
|
|
|
|
++width;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (*usingpos < using->length &&
|
|
|
|
using->character[*usingpos] == '\\')
|
|
|
|
{
|
|
|
|
++(*usingpos);
|
|
|
|
++width;
|
|
|
|
goto work;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
return Value_new_ERROR(this, IOERROR,
|
|
|
|
_("unpaired \\ in format"));
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
|
|
|
}
|
2023-02-22 06:39:24 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case '&': /* output string */
|
|
|
|
{
|
|
|
|
width = 0;
|
|
|
|
++(*usingpos);
|
|
|
|
goto work;
|
|
|
|
}
|
2023-02-22 06:39:24 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case '*':
|
|
|
|
case '$':
|
|
|
|
case '0':
|
|
|
|
case '+':
|
|
|
|
case '#':
|
|
|
|
case '.':
|
|
|
|
{
|
|
|
|
if (using->character[*usingpos] == '+')
|
|
|
|
{
|
|
|
|
headingsign = 1;
|
|
|
|
++(*usingpos);
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
while (*usingpos < using->length &&
|
|
|
|
strchr("$#*0,", using->character[*usingpos]))
|
|
|
|
{
|
|
|
|
switch (using->character[*usingpos])
|
|
|
|
{
|
|
|
|
case '$':
|
|
|
|
if (width == 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
dollarleft = 1;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (++dollar > 1)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
++width;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case '*':
|
|
|
|
pad = '*';
|
|
|
|
++width;
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case '0':
|
|
|
|
pad = '0';
|
|
|
|
++width;
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case ',':
|
|
|
|
commas = 1;
|
|
|
|
++width;
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
++width;
|
|
|
|
}
|
2023-02-22 06:39:24 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
++(*usingpos);
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2023-02-22 06:39:24 +01:00
|
|
|
if (*usingpos < using->length &&
|
|
|
|
using->character[*usingpos] == '.')
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
++(*usingpos);
|
|
|
|
++width;
|
|
|
|
precision = 0;
|
|
|
|
while (*usingpos < using->length &&
|
|
|
|
strchr("*#", using->character[*usingpos]))
|
|
|
|
{
|
|
|
|
++(*usingpos);
|
|
|
|
++precision;
|
|
|
|
++width;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (width == 1 && precision == 0)
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
return Value_new_ERROR(this, BADFORMAT);
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2023-02-22 06:39:24 +01:00
|
|
|
if (*usingpos < using->length &&
|
|
|
|
using->character[*usingpos] == '-')
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
++(*usingpos);
|
|
|
|
if (headingsign == 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
headingsign = 2;
|
|
|
|
}
|
2023-02-22 06:39:24 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
trailingsign = -1;
|
|
|
|
}
|
|
|
|
else if (*usingpos < using->length &&
|
|
|
|
using->character[*usingpos] == '+')
|
|
|
|
{
|
|
|
|
++(*usingpos);
|
|
|
|
if (headingsign == 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
headingsign = 2;
|
|
|
|
}
|
2023-02-22 06:39:24 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
trailingsign = 1;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
while (*usingpos < using->length &&
|
|
|
|
using->character[*usingpos] == '^')
|
|
|
|
{
|
|
|
|
++(*usingpos);
|
|
|
|
++exponent;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
goto work;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
String_appendChar(s, using->character[(*usingpos)++]);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
work:
|
2023-02-22 06:39:24 +01:00
|
|
|
Value_toString(this, s, pad, headingsign, width, commas, dollar,
|
|
|
|
dollarleft, precision, exponent, trailingsign);
|
2014-11-02 02:01:20 +01:00
|
|
|
if ((this->type == V_INTEGER || this->type == V_REAL) && width == 0 &&
|
|
|
|
precision == -1)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendChar(s, ' ');
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
while (*usingpos < using->length)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
switch (using->character[*usingpos])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case '_': /* output next char */
|
|
|
|
{
|
|
|
|
++(*usingpos);
|
|
|
|
if (*usingpos < using->length)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendChar(s, using->character[(*usingpos)++]);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
Value_destroy(this);
|
|
|
|
return Value_new_ERROR(this, MISSINGCHARACTER);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case '!':
|
|
|
|
case '\\':
|
|
|
|
case '&':
|
|
|
|
case '*':
|
|
|
|
case '0':
|
|
|
|
case '+':
|
|
|
|
case '#':
|
|
|
|
case '.':
|
|
|
|
return this;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
String_appendChar(s, using->character[(*usingpos)++]);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct String *Value_toWrite(struct Value *this, struct String *s)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (this->type)
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
String_appendPrintf(s, "%ld", this->u.integer);
|
2014-10-27 14:53:12 +01:00
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
|
|
|
double x;
|
|
|
|
int p = DBL_DIG;
|
2023-02-22 06:39:24 +01:00
|
|
|
int n;
|
|
|
|
int o;
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
x = (this->u.real < 0.0 ? -this->u.real : this->u.real);
|
|
|
|
while (x > 1.0 && p > 0)
|
|
|
|
{
|
|
|
|
x /= 10.0;
|
|
|
|
--p;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
o = s->length;
|
|
|
|
String_appendPrintf(s, "%.*f", p, this->u.real);
|
|
|
|
n = s->length;
|
|
|
|
if (memchr(s->character + o, '.', n - o))
|
|
|
|
{
|
|
|
|
while (s->character[s->length - 1] == '0')
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
--s->length;
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
if (s->character[s->length - 1] == '.')
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
--s->length;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_STRING:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
size_t l = this->u.string.length;
|
|
|
|
char *data = this->u.string.character;
|
|
|
|
|
|
|
|
String_appendChar(s, '"');
|
|
|
|
while (l--)
|
|
|
|
{
|
|
|
|
if (*data == '"')
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
String_appendChar(s, '"');
|
|
|
|
}
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
String_appendChar(s, *data);
|
|
|
|
++data;
|
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
String_appendChar(s, '"');
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return s;
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
struct Value *Value_nullValue(enum ValueType type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-02-22 06:39:24 +01:00
|
|
|
static struct Value integer =
|
|
|
|
{
|
|
|
|
V_INTEGER
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct Value real =
|
|
|
|
{
|
|
|
|
V_REAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct Value string =
|
|
|
|
{
|
|
|
|
V_STRING
|
|
|
|
};
|
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
static int init = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
if (!init)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
integer.u.integer = 0;
|
|
|
|
real.u.real = 0.0;
|
|
|
|
string.u.string.length = 0;
|
2023-02-22 06:39:24 +01:00
|
|
|
string.u.string.character = "";
|
|
|
|
init = 1;
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
switch (type)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
case V_INTEGER:
|
|
|
|
return &integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_REAL:
|
|
|
|
return ℜ
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_STRING:
|
|
|
|
return &string;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-03 16:11:25 +01:00
|
|
|
|
|
|
|
long int lrint(double d)
|
|
|
|
{
|
|
|
|
return d;
|
|
|
|
}
|