-
Notifications
You must be signed in to change notification settings - Fork 0
/
syexpr.h
163 lines (139 loc) · 4.52 KB
/
syexpr.h
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
/*
* syexpr.h - C expression evaluator based on shunting yard algo header
*
* Evaluate C integer expressions (like the ones generated by cpp)
*
* (C) Copyright Pantelis Antoniou <[email protected]>
*
* 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)The name of the author may not be used to
* endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 SYEXPR_H
#define SYEXPR_H
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <inttypes.h>
/* options */
#define SY_VERBOSE_ERROR_REPORTING
#define SY_DEBUG
#define SY_VAL_BITS 64
#ifndef SY_VAL_BITS
#define SY_VAL_BITS 64
#endif
#if SY_VAL_BITS == 64
typedef uint64_t sy_val_v_t;
typedef int64_t sy_signed_val_v_t;
#define SY_PRIx PRIx64
#define SY_PRIu PRIu64
#define SY_PRIs PRId64
/* 2 + strlen("18446744073709551615") + 1 + 1 */
#define SY_VAL_STR_LEN_MAX (2 + 20 + 1 + 1)
#elif SY_VAL_BITS == 32
typedef uint32_t sy_val_v_t;
typedef int32_t sy_signed_val_v_t;
#define SY_PRIx PRIx32
#define SY_PRIu PRIu32
#define SY_PRIs PRId32
/* 2 + strlen("4294967295") + 1 + 1 */
#define SY_VAL_STR_LEN_MAX (2 + 10 + 1 + 1)
#elif SY_VAL_BITS == 16
typedef uint16_t sy_val_v_t;
typedef int16_t sy_signed_val_v_t;
#define SY_PRIx PRIx16
#define SY_PRIu PRIu16
#define SY_PRIs PRId16
/* 2 + strlen("65535") + 1 + 1 */
#define SY_VAL_STR_LEN_MAX (2 + 5 + 1 + 1)
#else
#error Bad SY_VAL_BITS value
#endif
typedef unsigned char sy_op_t;
typedef struct sy_val {
sy_val_v_t v; /* value */
bool u : 1; /* unsigned */
bool x : 1; /* hex */
} sy_val_t;
typedef void (*sy_debugf_t)(void *arg, const char *fmt, ...)
__attribute__ ((__format__ (__printf__, 2, 0)));
struct sy_state {
int last_token;
unsigned int valq_next;
sy_val_t *valq;
unsigned int opstack_next;
sy_op_t *opstack;
int pos;
int errcode;
#ifdef SY_DEBUG
sy_debugf_t debugf;
void *debugarg;
#endif
};
#define SY_NO_OP_IN_STACK -1
#define SY_NOT_ENOUGH_TERNARY_VALUES -2
#define SY_NOT_ENOUGH_BINARY_VALUES -3
#define SY_NO_UNARY_VALUE -4
#define SY_DIV_BY_ZERO -5
#define SY_MOD_BY_ZERO -6
#define SY_UNKNOWN_TERNARY_OP -7
#define SY_UNKNOWN_BINARY_OP -8
#define SY_UNKNOWN_UNARY_OP -9
#define SY_UNKNOWN_OP -10
#define SY_SYNTAX_ERROR -11
#define SY_OUT_OF_MEMORY -12
#define SY_NUMBER_OUT_OF_RANGE -13
#define SY_NUMBER_PARSE_ERROR -14
#define SY_BAD_CHAR_LITERAL -15
#define SY_OUT_OF_INPUT -16
#define SY_UNMATCHED_RPAREN -17
#define SY_UNMATCHED_COLON -18
#define SY_UNMATCHED_UNKNOWN -19
#define SY_BAD_EOF_VALUE_QUEUE -20
#define SY_BAD_EOF_OP_STACK -21
#define SY_INTERNAL_ERROR -22
#define SY_WORKBUF_TOO_SMALL -23
#define SY_STRAY_QMARK -24
struct sy_config {
void *workbuf;
unsigned int size;
#ifdef SY_DEBUG
sy_debugf_t debugf;
void *debugarg;
#endif
};
/* worst case scenario single digit numbers seperated via an operator */
static inline unsigned int sy_workbuf_size_max(int len)
{
return (len / 2 + 1) * (sizeof(sy_val_t) + sizeof(sy_op_t));
}
void sy_init(struct sy_state *sy, const struct sy_config *cfg);
int sy_get_error(struct sy_state *sy, int *pos, const char **errmsg);
int sy_eval(struct sy_state *sy, const char *str, int len, sy_val_t *valp);
const char *sy_val_str_multi(const sy_val_t *val, char **buf, int *size);
const char *sy_val_str(const sy_val_t *val, char *buf, int bufsize);
#endif