summaryrefslogtreecommitdiff
path: root/apps/interpreters/bas/value.h
blob: 56e62a01e02a061fe65f62fe43f45249d858391e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/****************************************************************************
 * apps/interpreters/bas/value.h
 *
 *   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.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * 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.
 *
 * Adapted to NuttX and re-released under a 3-clause BSD license:
 *
 *   Copyright (C) 2014 Gregory Nutt. All rights reserved.
 *   Authors: Alan Carvalho de Assis <Alan Carvalho de Assis>
 *            Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#ifndef __APPS_EXAMPLES_BAS_VALUE_H
#define __APPS_EXAMPLES_BAS_VALUE_H

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include "str.h"

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

#define VALUE_NEW_INTEGER(this,n) ((this)->type=V_INTEGER,(this)->u.integer=(n))
#define VALUE_NEW_REAL(this,n) ((this)->type=V_REAL,(this)->u.real=(n))
#define VALUE_RETYPE(v,t) ((v)->type==(t) ? (v) : Value_retype(v,t))
#define VALUE_DESTROY(this) assert((this)!=(struct Value*)0); \
  switch ((this)->type) \
  { \
    case V_ERROR: free((this)->u.error.msg); break; \
    case V_INTEGER: break; \
    case V_NIL: break; \
    case V_REAL: break; \
    case V_STRING: String_destroy(&(this)->u.string); break; \
    case V_VOID: break; \
    default: assert(0); \
  } \
  (this)->type=0;

/****************************************************************************
 * Public Types
 ****************************************************************************/

enum ValueType
{
  V_ERROR=1,
  V_INTEGER,
  V_NIL,
  V_REAL,
  V_STRING,
  V_VOID
};

struct Value
{
  enum ValueType type;
  union
  {
    /* V_ERROR   */ struct { char *msg; long int code; } error;
    /* V_INTEGER */ long int integer;
    /* V_NIL     */
    /* V_REAL    */ double real;
    /* V_STRING  */ struct String string;
    /* V_VOID    */
  } u;
};

/****************************************************************************
 * Public Data
 ****************************************************************************/

extern const enum ValueType Value_commonType[V_VOID+1][V_VOID+1];

/****************************************************************************
 * Public Function Prototypes
 ****************************************************************************/

long int lrint(double d);
double Value_trunc(double d);
double Value_round(double d);
long int Value_toi(double d, int *overflow);
long int Value_vali(const char *s, char **end, int *overflow);
double Value_vald(const char *s, char **end, int *overflow);

struct Value *Value_new_NIL(struct Value *this);
struct Value *Value_new_ERROR(struct Value *this, int code,
                              const char *error, ...);
struct Value *Value_new_INTEGER(struct Value *this, int n);
struct Value *Value_new_REAL(struct Value *this, double n);
struct Value *Value_new_STRING(struct Value *this);
struct Value *Value_new_VOID(struct Value *this);
struct Value *Value_new_null(struct Value *this, enum ValueType type);
int Value_isNull(const struct Value *this);
void Value_destroy(struct Value *this);
void Value_errorPrefix(struct Value *this, const char *prefix);
void Value_errorSuffix(struct Value *this, const char *suffix);
struct Value *Value_new_typeError(struct Value *this, enum ValueType t1,
                                  enum ValueType t2);
struct Value *Value_retype(struct Value *this, enum ValueType type);
struct Value *Value_clone(struct Value *this, const struct Value *original);
struct Value *Value_uplus(struct Value *this, int calc);
struct Value *Value_uneg(struct Value *this, int calc);
struct Value *Value_unot(struct Value *this, int calc);
struct Value *Value_add(struct Value *this, struct Value *x, int calc);
struct Value *Value_sub(struct Value *this, struct Value *x, int calc);
struct Value *Value_mult(struct Value *this, struct Value *x, int calc);
struct Value *Value_div(struct Value *this, struct Value *x, int calc);
struct Value *Value_idiv(struct Value *this, struct Value *x, int calc);
struct Value *Value_mod(struct Value *this, struct Value *x, int calc);
struct Value *Value_pow(struct Value *this, struct Value *x, int calc);
struct Value *Value_and(struct Value *this, struct Value *x, int calc);
struct Value *Value_or(struct Value *this, struct Value *x, int calc);
struct Value *Value_xor(struct Value *this, struct Value *x, int calc);
struct Value *Value_eqv(struct Value *this, struct Value *x, int calc);
struct Value *Value_imp(struct Value *this, struct Value *x, int calc);
struct Value *Value_lt(struct Value *this, struct Value *x, int calc);
struct Value *Value_le(struct Value *this, struct Value *x, int calc);
struct Value *Value_eq(struct Value *this, struct Value *s, int calc);
struct Value *Value_ge(struct Value *this, struct Value *x, int calc);
struct Value *Value_gt(struct Value *this, struct Value *x, int calc);
struct Value *Value_ne(struct Value *this, struct Value *x, int calc);
int Value_exitFor(struct Value *this, struct Value *limit,
                  struct Value *step);
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);
struct Value *Value_toStringUsing(struct Value *this, struct String *s,
                                  struct String *using, size_t *usingpos);
struct String *Value_toWrite(struct Value *this, struct String *s);
struct Value *Value_nullValue(enum ValueType type);

#endif /* __APPS_EXAMPLES_BAS_VALUE_H */