-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.h
More file actions
360 lines (293 loc) · 11.1 KB
/
basic.h
File metadata and controls
360 lines (293 loc) · 11.1 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Copyright 2018 Alex Ovchinikov
Copyright 2011 Jerry Williams Jr
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 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.
*/
/*
Contributors:
Alex Ovchinikov (AlexO) changes list:
- Interactive mode removed (commands BREAK, RESUME)
- Added: support for floating point (see DATATYPE definition)
- Added: fixed-point Q16.16 support (see DATATYPE definition)
- Added: math support for float/fixed point (see ENABLE_MATH)
- Added: run-time stack checking (see ENABLE_CHECK_STACK)
- Tagret can be compiled as run-time only (ENABLE_COMPILER option)
- The bytecode is platform-indpendent now
- The instruction format is:
bits 0..7 - instruction index (in optable[]),
bits 8..15 - instruction length,
bits 16..31 - source code line number
- All global variables moved to TBASIC_CTX struct; code is fully reenterant now.
- Added: p-code load / save functions
- the modulo operator '\' changed to '%'
- Fixed bug "no underscore symbol in identificators'
- FORMAT specifier string can include '%' char.
Example: FORMAT "%%1=%, %%2=%", 10, 20
will be print: %1=10, %2=20
- DIM arrays allocated from 'dmem' pool (see TBASIC_CTX struct)
- heap is not used now
- Added: run-time DIM() allocation checking
- Added: compile listing with 'disasembler'
- Added: function for retrieve variable by hash of name
- Some code refactoring
- Added: some tests
- Added: dynamic function support (dynamic function is function (re)defined during run-time)
- Identifiers is not capitalized now
- Added: semicolon character (;) treated as comment begin (same as #)
*/
#ifndef _BASIC_H_
#define _BASIC_H_
#ifdef __cplusplus
extern "C"
{
#endif
#define TYPE_INT32 1
#define TYPE_FX16Q16 2
#define TYPE_FLOAT32 3
#define ON 1
#define OFF 0
/* MAIN CONFIGURATION OPTIONS */
#define ENABLE_COMPILER ON
#define ENABLE_CHECK_STACK ON
#define ENABLE_DASM ON
#define ENABLE_MATH OFF
//#define DATATYPE TYPE_INT32
//#define DATATYPE TYPE_FX16Q16
#define DATATYPE TYPE_FLOAT32
#define MAX_STR_LEN 1000
/* CONFIGURATION DEFINES FOR RUN-TIME */
#define FMTSZ 128 /* FORMAT STRING MAX LEN */
#define OPTSZ 64 /* OPCODE INDEX TABLE SIZE */
#define OPNSZ 16 /* OPCODE NAME LEN */
#define SYMSZ 32 /* SYMBOL SIZE */
#if (ENABLE_COMPILER == ON)
/* CONFIGURATION DEFINES FOR COMPILER */
#define PRGSZ 65535 /* PROGRAM SIZE */
#define STRSZ 65535 /* STRING TABLE SIZE */
#define DIMSZ 1000 /* DIM ARRAY MEMORY POOL*/
#define LOCS 100 /* LOCAL COUNT */
#define VARS 100 /* MAX VARIABLES */
#define STKSZ 1024 /* STACK SIZE */
#endif
#if (ENABLE_MATH == ON) && (DATATYPE == TYPE_INT32)
#error NO MATH SUPPORT FOR INT32 AVAILABLE!
#endif
/* INCLUDES */
#include <setjmp.h>
#include <stdint.h>
#include <stdarg.h>
#if (DATATYPE == TYPE_FLOAT32)
#include <math.h>
typedef float TData;
#elif (DATATYPE == TYPE_INT32)
typedef int32_t TData;
#elif (DATATYPE == TYPE_FX16Q16)
#include <fixmath.h>
typedef fix16_t TData;
#else
#error NO VALID DATATYPE DEFINED!
#endif
/* INTERNAL BOOL REPRESENTATION */
#define BS_FALSE 0
#define BS_TRUE 1
/* EXIT CODES */
#define EXIT_SUCCESS 0
#define EXIT_ERROR 1
#define EXIT_ERROR_COMPILE 2
#define EXIT_ERROR_RUNTIME 3
#define EXIT_ERROR_ALLOC 4
#define EXIT_ERROR_VERSION 5
#define EXIT_BYE 6
/* COMPILER ERROR MESSAGE */
#define ERR_BAD_TOKEN 1
#define ERR_SYNTAX 2
#define ERR_COUNT 3
#define ERR_EXPRESSION 4
#define ERR_COMP_MODE 5
#define ERR_STA 6
#define ERR_TOK_STA 7
#define ERR_OUT_OF_RANGE 8
/* RUN-TIME ERROR MESSAGE */
#define ERR_BOUNDS 1
#define ERR_DIM 2
#define ERR_STACK_OVERFLOW 3
#define ERR_MATH_OVERFLOW 4
#define ERR_DIV_ZERO 5
#define ERR_MOD_ZERO 6
#define ERR_CODE_OVERFLOW 7
#define ERR_INVALID_OPCODE 8
#define ERR_UNDEFINED_FN 9
#define ERR_DYN_FN 10
/* LISTING VERBOSITY */
#define LST_PRINT_REPORT 0x01
#define LST_PRINT_LISTING 0x02
#define LST_PRINT_ALL 0xff
#define GET_OPCODE(V) (V & 0xff)
#define GET_OPLEN(V) ((V >> 8 ) & 0x00ff)
#define GET_LINEn(V) ((V >> 16) & 0xffff)
/* Dynamic function argument access */
#define DYF_NARG() ((ctx->sp[0]).i)
#define DYF_ARG(N) (ctx->sp[DYF_NARG()-N+1])
#define DYF_RETVAL(VAL) (ctx->sp+=(ctx->sp[0]).i, (ctx->sp)->v = VAL ) /*(ctx->sp+=DYF_NARG()+1, (--ctx->sp)->v = VAL )*/
typedef union TValue TValue;
typedef struct TBASIC_CTX TBASIC_CTX;
typedef int32_t(*TCode)(TBASIC_CTX *ctx); /* BYTE-CODE */
typedef void (*TErrorHook)(TBASIC_CTX *ctx, int32_t nerr, int32_t nline, int32_t addr, char *msg); /* ERROR HOOK */
typedef int32_t (*TTraceHook)(TBASIC_CTX *ctx, int32_t addr, int32_t opcode, int32_t line); /* KEYWORD HOOK */
typedef int32_t (*TFormatHook)(char *str, const char *fmt, va_list ap);
typedef int32_t (*TPutchHook)(int32_t c);
#if (ENABLE_COMPILER == ON)
typedef int32_t (*TKwdHook)(TBASIC_CTX *ctx, char *kwd); /* KEYWORD HOOK */
typedef int32_t (*TFunHook)(TBASIC_CTX *ctx, char *kwd, int32_t n); /* FUNCTION CALL HOOK */
#endif
struct TDynDesc
{
uint32_t name_hash;
TCode fn;
};
union TValue
{
TData v;
int32_t i;
void* p;
};
struct TBASIC_CTX
{
/* COMPILED PROGRAM */
int32_t* prg;
int32_t prg_sz;
int32_t pc;
/* RUN-TIME STACK */
TValue* stk;
TValue* sp;
int32_t stk_sz;
/* VARIABLE VALUES */
TValue* var;
int32_t var_sz;
/* DIM() memoty pool */
TValue* dmem;
int32_t dmem_sz;
/* SUBROUTINE LOCAL VAR INDEXES */
int32_t* sub;
int32_t sloc_sz;
/* STRING TABLE */
char* stab;
int32_t stab_sz;
/* HASH OF VAR/SUB NAME */
uint32_t* vhash;
int32_t vhash_sz;
/* VAR EXPORT TABLE */
char* vname;
int32_t vname_sz;
/* DYNAMIC FUNCTION SUPPORT */
struct TDynDesc* dynf_list;
int32_t dynf_sz;
int32_t dtop; /* TOP INDEX IN dmem */
TValue ret; /* FUNCTION RETURN VALUE */
jmp_buf trap; /* TRAP ERRORS */
int32_t nerr; /* LAST ERROR CODE */
TErrorHook errhook; /* Called before trap when error detected */
TFormatHook fmthook; /* Called from FORMAT */
TPutchHook putchook; /* Called for output char to console */
void* user_ptr;
#if (ENABLE_COMPILER == ON)
int32_t cpc; /*COMPILER PC*/
char tokn[SYMSZ], *lp; /* LEXER STATE */
int32_t lnum, tok, ungot; /* LEXER STATE */
TValue tokv;
char name[VARS][SYMSZ]; /* VARIABLE NAMES */
int32_t mode[VARS]; /* 0=NONE, 1=DIM, 2=SUB, 3=FUN */
int32_t cstk[STKSZ], *csp; /* COMPILER STACK */
int32_t nvar, cursub, temp, fcompile, ipc, (**opc)(); /* COMPILER STATE */
char *stabp; /* STRING TABLE PTR*/
TKwdHook kwdhook; /* KEYWORD HOOK */
TFunHook funhook; /* FUNCTION HOOK */
#endif
};
/* FUNCTION PROTOTYPES */
/* Low-level init. Assume manual memory allocation */
int32_t bs_init(
TBASIC_CTX *ctx,
int32_t* prg, int32_t prg_sz,
TValue* stk, int32_t stk_sz,
TValue* var, int32_t var_sz,
TValue* dmem, int32_t dmem_sz,
int32_t* sub, int32_t sloc_sz,
char* stab, int32_t stab_sz,
TFormatHook fmthook, // callback for format string function (like vsprintf()); must be defined.
TPutchHook putchook, // callback for output char to console (like putchar()); must be defined.
TErrorHook errhook, // callback for error processing; can be NULL
void* user_ptr);
/* Load p-code image from buffer and allocate memory from heap */
int32_t bs_init_load(
TBASIC_CTX *ctx,
void* pcode,
TFormatHook fmthook, // callback for format string function (like vsprintf()); must be defined.
TPutchHook putchook, // callback for output char to console (like putchar()); must be defined.
TErrorHook errhook, // callback for error processing; can be NULL
void* user_ptr);
/* Freeing memory allocated for TBASIC_CTX*/
int32_t bs_free(TBASIC_CTX *ctx);
/* Get last error desctiption (msg), address (addr) and line number (lnum)*/
int32_t bs_last_error(TBASIC_CTX *ctx, int32_t* lnum, int32_t* addr, char** msg);
/* Reset program counter and initialize stack */
int32_t bs_reset(TBASIC_CTX *ctx);
/* Execute p-code; Call thook() for every command */
int32_t bs_run(TBASIC_CTX *ctx, TTraceHook thook);
/* Register new user opcode */
int32_t bs_reg_opcode(TBASIC_CTX *ctx, TCode code, char* name);
/* Register new user-defined dynamic function (function can be defined during run-time) */
int32_t bs_reg_dyn_fn(TBASIC_CTX *ctx, TCode code, char* name);
/* Get variable by name; Accept array name like ARRAY(1); ARRAY(0) return array size */
int32_t bs_get_var(TBASIC_CTX *ctx, char* name, TValue* val);
/* Set variable by name; Accept array name like ARRAY(1); ARRAY(0) return array size */
int32_t bs_set_var(TBASIC_CTX *ctx, char* name, TValue val);
/* Convert TData to string; str should by allocated by user */
int32_t bs_val2str(TBASIC_CTX *ctx, char* str, TData val);
#if (ENABLE_DASM == ON)
/* Disassemble p-code command located by specified addr; asml should by allocated by user */
int32_t bs_dasm(TBASIC_CTX *ctx, int32_t addr, char* asml);
#endif
#if (ENABLE_COMPILER == ON)
/* Allocate memory from heap and initialize TBASIC_CTX */
int32_t bs_init_alloc(
TBASIC_CTX *ctx,
TFormatHook fmthook, // callback for format string function (like vsprintf()); must be defined.
TPutchHook putchook, // callback for output char to console (like putchar()); must be defined.
TErrorHook errhook, // callback for error processing; can be NULL
void* user_ptr);
/* Return expressions count separated by comma */
int32_t bs_get_nparam(TBASIC_CTX *ctx);
/* Parse n expressions, separated by comma */
void bs_explist(TBASIC_CTX *ctx, int32_t n);
/* Insert one-arg instruction to program memory */
void bs_inst1(TBASIC_CTX *ctx, TCode opcode);
/* Insert two-arg instruction to program memory */
void bs_inst2(TBASIC_CTX *ctx, TCode opcode, int32_t x);
/* Parse expression */
int32_t bs_expr(TBASIC_CTX *ctx);
/* Register new user-defined keyword */
int32_t bs_reg_keyword(TBASIC_CTX *ctx, TKwdHook khook);
/* Register new user-defined static function (function(s) must be defined during compile and run in same order!) */
int32_t bs_reg_func(TBASIC_CTX *ctx, TFunHook fhook);
/* Compile Basic source to p-code */
int32_t bs_compile(TBASIC_CTX *ctx, char *bsrc);
/* Export p-code to image; image buffer allocated inside and should be freeing by user */
int32_t bs_export_pcode(TBASIC_CTX *ctx, void** pcode, int32_t* pcode_sz);
/* Generate compile listing and report. see LST_PRINT_XXX */
int32_t bs_listing(TBASIC_CTX *ctx, char *bsrc, int32_t level, FILE* lst);
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _BASIC_H_ */