-
Notifications
You must be signed in to change notification settings - Fork 0
/
badcode.h
224 lines (195 loc) · 5.33 KB
/
badcode.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* @file badcode.h
* @author masscry
*
* Bad Code Programming Language Public Header.
*/
#pragma once
#ifndef DECI_SPACE_BADCODE_HEADER
#define DECI_SAPCE_BADCODE_HEADER
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#ifdef __cplusplus
#define BCAPI extern "C"
#else
#define BCAPI
#endif
/**
* Full version of BadCode library
*/
#define BC_VER_FULL (0x00010000)
/**
* Major part of BadCode library version
*/
#define BC_VER_MAJOR (0x00)
/**
* Minor part of BadCode library version
*/
#define BC_VER_MINOR (0x01)
/**
* Patch part of BadCode library version
*/
#define BC_VER_PATCH (0x0000)
/**
* BadcCode library version string.
*/
#define BC_VER_STRING ("0.1.0")
/**
* Main evaluation engine.
*/
typedef struct bcCore_t* BC_CORE;
/**
* Simple box structure implements "VARIANT" pattern
*/
typedef struct bcValue_t* BC_VALUE;
/**
* Status codes.
*/
typedef enum bcStatus_t
{
BC_OK = 0, /**< No Errors */
BC_INVALID_ARG, /**< Argument check failed */
BC_NOT_IMPLEMENTED, /**< Required command is not implemented (?yet) */
BC_NO_MEMORY, /**< Memory allocation failed */
BC_TOO_MANY_CONSTANTS, /**< There are too many constants in interpreted source code */
BC_UNDERFLOW, /**< Not enough elements on stack */
BC_OVERFLOW, /**< There are too many elements on stack */
BC_MALFORMED_CODE, /**< Invalid opcode */
BC_CONST_NOT_FOUND, /**< Requested constant is not found */
BC_HALT_EXPECTED, /**< Program end reached before HALT */
BC_DIVIDE_BY_ZERO, /**< Divide by Zero error */
BC_CANT_CONVERT, /**< Conversion failed */
BC_TOO_SMALL, /**< Buffer is to small */
BC_INVALID_ID, /**< Invalid ID */
BC_NOT_DEFINED, /**< Variable is not defined */
BC_PARSE_NOT_FINISHED, /**< More input expected */
BC_EMPTY_EXPR, /**< Empty expression */
BC_STATUS_TOTAL /**< Total status codes */
} bcStatus_t;
BCAPI const char* bcStatusString(bcStatus_t status);
/**
* Version of loaded BadCode library.
*/
BCAPI uint32_t bcVersion();
/**
* Create new BadCode core instance.
*
* @return NULL on errors, new BC_CORE otherwise.
*/
bcStatus_t bcCoreNew(BC_CORE* pCore);
/**
* Delete BadCode core instance.
*
* If NULL pointer is passed as core, function has no effect.
*
* @param core[in] core to delete
*
*/
BCAPI void bcCoreDelete(BC_CORE core);
/**
* Execute code on given core.
*
* @param core[in] valid core to execute code on
* @param code[in] string of code to execute
* @param endp[out,opt] if not NULL, set to last valid executed command
*
* @return BC_OK if execution completed successfully, error code otherwise.
*/
BCAPI bcStatus_t bcCoreExecute(BC_CORE core, const char* code, char** endp);
/**
* Get value on top of stack.
*
* @param core[in] valid core to get value on top of stack
* @param pVal[out] pointer where to store value from stack.
*/
BCAPI bcStatus_t bcCoreTop(const BC_CORE core, BC_VALUE* pVal);
BCAPI bcStatus_t bcCoreResult(const BC_CORE core, BC_VALUE* pVal);
/**
* Remove value from stack.
*/
BCAPI bcStatus_t bcCorePop(BC_CORE core);
/**
* Box an integer.
*
* @param[in] val value to box in BC_VALUE
*
* @return NULL on errors, new value otherwise
*/
BCAPI BC_VALUE bcValueInteger(int64_t val);
/**
* Box a string.
*
* @param[in] str string to box in BC_VALUE
*
* @return NULL on errors, new value otherwise
*/
BCAPI BC_VALUE bcValueString(const char* str);
/**
* Copy stored value to given pointer.
*
* If bufSize != 0 and *pBuf != NULL, value copied to buffer, otherwise
* new buffer is allocated and set to *pBuf.
*
* @param[in] val boxed value to extract string
* @param[in,out] pBuf pointer to buffer of given size or pointer to NULL
* @param[in] bufSize size of passed buffer, or 0
*
* @return
* BC_OK conversion completed
* BC_NO_MEMORY failed to allocated buffer of enough size
* BC_INVALID_ARG when invalid arguments passed
* BC_TOO_SMALL passed buffer is to small
* BC_NOT_IMPLEMENTED such conversion is not implemented
*/
BCAPI bcStatus_t bcValueAsString(const BC_VALUE val, char** pBuf, size_t bufSize);
/**
* Create copy of given box.
*
* @param[in] val valid value box
*
* @return NULL on errors, new copy otherwise
*/
BCAPI BC_VALUE bcValueCopy(const BC_VALUE val);
/**
* Free value box.
*
* @param[in] value valid box to cleanup.
*/
BCAPI bcStatus_t bcValueCleanup(BC_VALUE value);
/**
* Get stored value as integer.
*
* @param[in] val valid BC_VALUE
* @param[out] oval pointer to store boxed value
*
* @return BC_OK if completed sucessfuly, error code otherwise
*/
BCAPI bcStatus_t bcValueAsInteger(const BC_VALUE val, int64_t* oval);
/**
* Box a number.
*
* @param[in] val value to box in BC_VALUE
*
* @return NULL on errors, new value otherwise
*/
BCAPI BC_VALUE bcValueNumber(double val);
/**
* Get stored value as number.
*
* @param[in] val valid BC_VALUE
* @param[out] oval pointer to store boxed value
*
* @return BC_OK if completed sucessfuly, error code otherwise
*/
BCAPI bcStatus_t bcValueAsNumber(const BC_VALUE val, double* oval);
/**
* Print value to stream.
*
* @param[in] stream valid stream
* @param[in] val valid value to print
*
* @return total bytes printer, or -1 on error
*/
BCAPI int bcValuePrint(FILE* stream, const BC_VALUE val);
#endif /* DECI_SPACE_BADCODE_HEADER */