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;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_new_NIL(struct Value *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
|
|
|
self->type = V_NIL;
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_new_ERROR(struct Value *self, int code,
|
2023-02-22 06:39:24 +01:00
|
|
|
const char *error, ...)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char buf[128];
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
2014-11-02 02:01:20 +01:00
|
|
|
va_start(ap, error);
|
|
|
|
vsprintf(buf, error, ap);
|
2014-10-27 14:53:12 +01:00
|
|
|
va_end(ap);
|
2023-09-11 06:02:05 +02:00
|
|
|
self->type = V_ERROR;
|
|
|
|
self->u.error.code = code;
|
|
|
|
self->u.error.msg = strdup(buf);
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_new_INTEGER(struct Value *self, int n)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
|
|
|
self->type = V_INTEGER;
|
|
|
|
self->u.integer = n;
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_new_REAL(struct Value *self, double n)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
|
|
|
self->type = V_REAL;
|
|
|
|
self->u.real = n;
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_new_STRING(struct Value *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
|
|
|
self->type = V_STRING;
|
|
|
|
String_new(&self->u.string);
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_new_VOID(struct Value *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
|
|
|
self->type = V_VOID;
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_new_null(struct Value *self, enum ValueType type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
2014-10-27 14:53:12 +01:00
|
|
|
switch (type)
|
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->type = V_INTEGER;
|
|
|
|
self->u.integer = 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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->type = V_REAL;
|
|
|
|
self->u.real = 0.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_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->type = V_STRING;
|
|
|
|
String_new(&self->u.string);
|
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_VOID:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->type = V_VOID;
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
int Value_isNull(const struct Value *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (self->type)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
case V_INTEGER:
|
2023-09-11 06:02:05 +02:00
|
|
|
return (self->u.integer == 0);
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_REAL:
|
2023-09-11 06:02:05 +02:00
|
|
|
return (self->u.real == 0.0);
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_STRING:
|
2023-09-11 06:02:05 +02:00
|
|
|
return (self->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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Value_destroy(struct Value *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
|
|
|
switch (self->type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_ERROR:
|
2023-09-11 06:02:05 +02:00
|
|
|
free(self->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:
|
2023-09-11 06:02:05 +02:00
|
|
|
String_destroy(&self->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
|
|
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
self->type = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_clone(struct Value *self, const struct Value *original)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self != (struct Value *)0);
|
2014-11-02 02:01:20 +01:00
|
|
|
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-09-11 06:02:05 +02:00
|
|
|
self->u.error.msg = strdup(original->u.error.msg);
|
|
|
|
self->u.error.code = original->u.error.code;
|
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_INTEGER:
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = original->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_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:
|
2023-09-11 06:02:05 +02:00
|
|
|
self->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:
|
2023-09-11 06:02:05 +02:00
|
|
|
String_clone(&self->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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
self->type = original->type;
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_uplus(struct Value *self, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (self->type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDUOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_uneg(struct Value *self, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (self->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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = -self->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real = -self->u.real;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDUOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_unot(struct Value *self, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (self->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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = ~self->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_retype(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = ~self->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDUOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_add(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer += x->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real += x->u.real;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
String_appendString(&self->u.string, &x->u.string);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_sub(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer -= x->u.integer;
|
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-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real -= x->u.real;
|
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-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_mult(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer *= x->u.integer;
|
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-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real *= x->u.real;
|
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-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_div(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.real == 0)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, UNDEFINED, "Division by zero");
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real /= x->u.real;
|
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-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.real == 0.0)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, UNDEFINED, "Division by zero");
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real /= x->u.real;
|
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-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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 13:49:06 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_idiv(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.integer == 0)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, UNDEFINED, "Division by zero");
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer /= x->u.integer;
|
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-10-27 14:53:12 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.real == 0.0)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, UNDEFINED, "Division by zero");
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real = Value_trunc(self->u.real / x->u.real);
|
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-10-27 14:53:12 +01:00
|
|
|
case V_STRING:
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_mod(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.integer == 0)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, UNDEFINED, "Modulo by zero");
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer %= x->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
|
|
|
if (x->u.real == 0.0)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, UNDEFINED, "Modulo by zero");
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real = fmod(self->u.real, x->u.real);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_pow(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->u.integer == 0 && x->u.integer == 0)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, UNDEFINED, "0^0");
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else if (x->u.integer > 0)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = pow(self->u.integer, x->u.integer);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
long int thisi = self->u.integer;
|
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_REAL(self, pow(thisi, 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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->u.real == 0.0 && x->u.real == 0.0)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, UNDEFINED, "0^0");
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real = pow(self->u.real, x->u.real);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_and(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer &= x->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_or(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer |= x->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_xor(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer ^= x->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_eqv(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = ~(self->u.integer ^ x->u.integer);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_imp(struct Value *self, struct Value *x, int calc)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
case V_INTEGER:
|
|
|
|
case V_REAL:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = (~self->u.integer) | x->u.integer;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, INVALIDOPERAND);
|
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);
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_lt(struct Value *self, struct Value *x, int calc)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = (self->u.integer < x->u.integer) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (self->u.real < x->u.real) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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:
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (String_cmp(&self->u.string, &x->u.string) < 0) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_le(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = (self->u.integer <= x->u.integer) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (self->u.real <= x->u.real) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (String_cmp(&self->u.string, &x->u.string) <= 0) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_eq(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = (self->u.integer == x->u.integer) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (self->u.real == x->u.real) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
2014-11-02 02:01:20 +01:00
|
|
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (String_cmp(&self->u.string, &x->u.string) == 0) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_ge(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = (self->u.integer >= x->u.integer) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (self->u.real >= x->u.real) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (String_cmp(&self->u.string, &x->u.string) >= 0) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_gt(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = (self->u.integer > x->u.integer) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (self->u.real > x->u.real) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (String_cmp(&self->u.string, &x->u.string) > 0) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_ne(struct Value *self, struct Value *x, int calc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (Value_commonType[self->type][x->type])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_INTEGER);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_INTEGER);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = (self->u.integer != x->u.integer) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
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;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
VALUE_RETYPE(self, V_REAL);
|
2014-11-02 02:01:20 +01:00
|
|
|
VALUE_RETYPE(x, V_REAL);
|
|
|
|
if (calc)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = (self->u.real != x->u.real) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
v = String_cmp(&self->u.string, &x->u.string) ? -1 : 0;
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
|
|
|
v = 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_INTEGER(self, v);
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
int Value_exitFor(struct Value *self,
|
2023-02-22 06:39:24 +01:00
|
|
|
struct Value *limit, struct Value *step)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (self->type)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
case V_INTEGER:
|
|
|
|
return
|
|
|
|
(step->u.integer < 0
|
2023-09-11 06:02:05 +02:00
|
|
|
? (self->u.integer < limit->u.integer)
|
|
|
|
: (self->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
|
2023-09-11 06:02:05 +02:00
|
|
|
? (self->u.real < limit->u.real) : (self->u.real > limit->u.real));
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_STRING:
|
2023-09-11 06:02:05 +02:00
|
|
|
return (String_cmp(&self->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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Value_errorPrefix(struct Value *self, 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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self->type == V_ERROR);
|
2014-11-02 02:01:20 +01:00
|
|
|
prefixlen = strlen(prefix);
|
2023-09-11 06:02:05 +02:00
|
|
|
msglen = strlen(self->u.error.msg);
|
|
|
|
self->u.error.msg = realloc(self->u.error.msg, prefixlen + msglen + 1);
|
|
|
|
memmove(self->u.error.msg + prefixlen, self->u.error.msg, msglen);
|
|
|
|
memcpy(self->u.error.msg, prefix, prefixlen);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Value_errorSuffix(struct Value *self, 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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self->type == V_ERROR);
|
2014-11-02 02:01:20 +01:00
|
|
|
suffixlen = strlen(suffix);
|
2023-09-11 06:02:05 +02:00
|
|
|
msglen = strlen(self->u.error.msg);
|
|
|
|
self->u.error.msg = realloc(self->u.error.msg, suffixlen + msglen + 1);
|
|
|
|
memcpy(self->u.error.msg + msglen, suffix, suffixlen + 1);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_new_typeError(struct Value *self, enum ValueType t1,
|
2014-11-02 02:01:20 +01:00
|
|
|
enum ValueType t2)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
assert(typestr[t1]);
|
|
|
|
assert(typestr[t2]);
|
2023-09-11 06:02:05 +02:00
|
|
|
return Value_new_ERROR(self, TYPEMISMATCH1,
|
2023-02-22 06:39:24 +01:00
|
|
|
_(typestr[t1]), _(typestr[t2]));
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
static void retypeError(struct Value *self, enum ValueType to)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType thisType = self->type;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
assert(typestr[thisType]);
|
|
|
|
assert(typestr[to]);
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, TYPEMISMATCH1, _(typestr[thisType]), _(typestr[to]));
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_retype(struct Value *self, enum ValueType type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (self->type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
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:
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real = self->u.integer;
|
|
|
|
self->type = type;
|
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_VOID:
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_VOID(self);
|
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:
|
2023-09-11 06:02:05 +02:00
|
|
|
retypeError(self, type);
|
2014-11-02 02:01:20 +01:00
|
|
|
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:
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = Value_toi(self->u.real, &overflow);
|
|
|
|
self->type = V_INTEGER;
|
2014-11-02 02:01:20 +01:00
|
|
|
if (overflow)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_ERROR(self, OUTOFRANGE, typestr[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_REAL:
|
|
|
|
break;
|
2014-11-02 13:49:06 +01:00
|
|
|
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_VOID:
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_VOID(self);
|
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:
|
2023-09-11 06:02:05 +02:00
|
|
|
retypeError(self, type);
|
2014-11-02 02:01:20 +01:00
|
|
|
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:
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
Value_new_VOID(self);
|
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:
|
2023-09-11 06:02:05 +02:00
|
|
|
retypeError(self, type);
|
2014-11-02 02:01:20 +01:00
|
|
|
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:
|
2023-09-11 06:02:05 +02:00
|
|
|
retypeError(self, type);
|
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 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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct String *Value_toString(struct Value *self, struct String *s, char pad,
|
2014-11-02 02:01:20 +01:00
|
|
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (self->type)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
case V_ERROR:
|
2023-09-11 06:02:05 +02:00
|
|
|
String_appendChars(s, self->u.error.msg);
|
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:
|
|
|
|
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);
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->type == V_INTEGER)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->u.integer < 0)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
sign = -1;
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.integer = -self->u.integer;
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
2023-09-11 06:02:05 +02:00
|
|
|
else if (self->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
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->u.real < 0.0)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
sign = -1;
|
2023-09-11 06:02:05 +02:00
|
|
|
self->u.real = -self->u.real;
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
2023-09-11 06:02:05 +02:00
|
|
|
else if (self->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;
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->type == V_INTEGER)
|
2014-11-02 02:01:20 +01:00
|
|
|
{
|
|
|
|
if (precision > 0 || exponent)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
format_double(&buf, (double)self->u.integer, width,
|
2023-02-22 06:39:24 +01:00
|
|
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
String_appendPrintf(&buf, "%lu.", self->u.integer);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
String_appendPrintf(&buf, "%lu", self->u.integer);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
format_double(&buf, self->u.real, width, precision, exponent);
|
2014-11-02 13:49:06 +01:00
|
|
|
}
|
|
|
|
|
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 =
|
2023-09-11 06:02:05 +02:00
|
|
|
(self->u.string.length <
|
|
|
|
width ? (width - self->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);
|
2023-09-11 06:02:05 +02:00
|
|
|
memcpy(s->character + oldlength, self->u.string.character,
|
|
|
|
blanks ? self->u.string.length : width);
|
2014-11-02 02:01:20 +01:00
|
|
|
if (blanks)
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
memset(s->character + oldlength + self->u.string.length, ' ',
|
2014-11-02 13:49:06 +01:00
|
|
|
blanks);
|
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-02 13:49:06 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
String_appendString(s, &self->u.string);
|
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
|
|
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Value *Value_toStringUsing(struct Value *self, struct String *s,
|
2014-11-02 02:01:20 +01:00
|
|
|
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
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
return Value_new_ERROR(self, MISSINGCHARACTER);
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
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
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
return Value_new_ERROR(self, IOERROR,
|
2014-11-02 02:01:20 +01:00
|
|
|
_("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)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
return Value_new_ERROR(self, BADFORMAT);
|
2014-11-02 02:01:20 +01:00
|
|
|
}
|
|
|
|
}
|
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-09-11 06:02:05 +02:00
|
|
|
Value_toString(self, s, pad, headingsign, width, commas, dollar,
|
2023-02-22 06:39:24 +01:00
|
|
|
dollarleft, precision, exponent, trailingsign);
|
2023-09-11 06:02:05 +02:00
|
|
|
if ((self->type == V_INTEGER || self->type == V_REAL) && width == 0 &&
|
2014-11-02 02:01:20 +01:00
|
|
|
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
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(self);
|
|
|
|
return Value_new_ERROR(self, MISSINGCHARACTER);
|
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 '!':
|
|
|
|
case '\\':
|
|
|
|
case '&':
|
|
|
|
case '*':
|
|
|
|
case '0':
|
|
|
|
case '+':
|
|
|
|
case '#':
|
|
|
|
case '.':
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 02:01:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct String *Value_toWrite(struct Value *self, struct String *s)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
switch (self->type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 02:01:20 +01:00
|
|
|
case V_INTEGER:
|
2023-09-11 06:02:05 +02:00
|
|
|
String_appendPrintf(s, "%ld", self->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
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
x = (self->u.real < 0.0 ? -self->u.real : self->u.real);
|
2014-11-02 02:01:20 +01:00
|
|
|
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;
|
2023-09-11 06:02:05 +02:00
|
|
|
String_appendPrintf(s, "%.*f", p, self->u.real);
|
2014-11-02 02:01:20 +01:00
|
|
|
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
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
size_t l = self->u.string.length;
|
|
|
|
char *data = self->u.string.character;
|
2014-11-02 02:01:20 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|