| 1 |
/***************************************************************************** |
| 2 |
* Free42 -- an HP-42S calculator simulator |
| 3 |
* Copyright (C) 2004-2026 Thomas Okken |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License, version 2, |
| 7 |
* as published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, see http://www.gnu.org/licenses/. |
| 16 |
*****************************************************************************/ |
| 17 |
|
| 18 |
#ifndef CORE_VARIABLES_H |
| 19 |
#define CORE_VARIABLES_H 1 |
| 20 |
|
| 21 |
#include "core_phloat.h" |
| 22 |
|
| 23 |
/***********************/ |
| 24 |
/* Variable data types */ |
| 25 |
/***********************/ |
| 26 |
|
| 27 |
#define TYPE_NULL 0 |
| 28 |
#define TYPE_REAL 1 |
| 29 |
#define TYPE_COMPLEX 2 |
| 30 |
#define TYPE_REALMATRIX 3 |
| 31 |
#define TYPE_COMPLEXMATRIX 4 |
| 32 |
#define TYPE_STRING 5 |
| 33 |
#define TYPE_LIST 6 |
| 34 |
|
| 35 |
struct vartype { |
| 36 |
int type; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
struct vartype_real { |
| 41 |
int type; |
| 42 |
phloat x; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
struct vartype_complex { |
| 47 |
int type; |
| 48 |
phloat re, im; |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
struct realmatrix_data { |
| 53 |
int refcount; |
| 54 |
phloat *data; |
| 55 |
char *is_string; |
| 56 |
}; |
| 57 |
|
| 58 |
struct vartype_realmatrix { |
| 59 |
int type; |
| 60 |
int4 rows; |
| 61 |
int4 columns; |
| 62 |
realmatrix_data *array; |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
struct complexmatrix_data { |
| 67 |
int refcount; |
| 68 |
phloat *data; |
| 69 |
}; |
| 70 |
|
| 71 |
struct vartype_complexmatrix { |
| 72 |
int type; |
| 73 |
int4 rows; |
| 74 |
int4 columns; |
| 75 |
complexmatrix_data *array; |
| 76 |
}; |
| 77 |
|
| 78 |
|
| 79 |
/* Maximum short string length in a stand-alone variable */ |
| 80 |
#define SSLENV ((int) (sizeof(char *) < 8 ? 8 : sizeof(char *))) |
| 81 |
/* Maximum short string length in a matrix element */ |
| 82 |
#define SSLENM ((int) sizeof(phloat) - 1) |
| 83 |
|
| 84 |
struct vartype_string { |
| 85 |
int type; |
| 86 |
int4 length; |
| 87 |
/* When length <= SSLENV, use buf; otherwise, use ptr */ |
| 88 |
union { |
| 89 |
char buf[SSLENV]; |
| 90 |
char *ptr; |
| 91 |
} t; |
| 92 |
char *txt() { |
| 93 |
return length > SSLENV ? t.ptr : t.buf; |
| 94 |
} |
| 95 |
const char *txt() const { |
| 96 |
return length > SSLENV ? t.ptr : t.buf; |
| 97 |
} |
| 98 |
void trim1(); |
| 99 |
}; |
| 100 |
|
| 101 |
|
| 102 |
struct list_data { |
| 103 |
int refcount; |
| 104 |
vartype **data; |
| 105 |
}; |
| 106 |
|
| 107 |
struct vartype_list { |
| 108 |
int type; |
| 109 |
int4 size; |
| 110 |
list_data *array; |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
vartype *new_real(phloat value); |
| 115 |
vartype *new_complex(phloat re, phloat im); |
| 116 |
vartype *new_string(const char *s, int slen); |
| 117 |
vartype *new_realmatrix(int4 rows, int4 columns); |
| 118 |
vartype *new_complexmatrix(int4 rows, int4 columns); |
| 119 |
vartype *new_list(int4 size); |
| 120 |
void free_vartype(vartype *v); |
| 121 |
void clean_vartype_pools(); |
| 122 |
void free_long_strings(char *is_string, phloat *data, int4 n); |
| 123 |
void get_matrix_string(vartype_realmatrix *rm, int4 i, char **text, int4 *length); |
| 124 |
void get_matrix_string(const vartype_realmatrix *rm, int4 i, const char **text, int4 *length); |
| 125 |
bool put_matrix_string(vartype_realmatrix *rm, int4 i, const char *text, int4 length); |
| 126 |
vartype *dup_vartype(const vartype *v); |
| 127 |
bool disentangle(vartype *v); |
| 128 |
int lookup_var(const char *name, int namelength); |
| 129 |
vartype *recall_var(const char *name, int namelength); |
| 130 |
bool ensure_var_space(int n); |
| 131 |
int store_var(const char *name, int namelength, vartype *value, bool local = false); |
| 132 |
bool purge_var(const char *name, int namelength, bool global = true, bool local = true); |
| 133 |
void purge_all_vars(); |
| 134 |
bool vars_exist(int section); |
| 135 |
bool contains_strings(const vartype_realmatrix *rm); |
| 136 |
int matrix_copy(vartype *dst, const vartype *src); |
| 137 |
vartype *recall_private_var(const char *name, int namelength); |
| 138 |
vartype *recall_and_purge_private_var(const char *name, int namelength); |
| 139 |
int store_private_var(const char *name, int namelength, vartype *value); |
| 140 |
|
| 141 |
#endif |