- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbasic.h
More file actions
Latest commit
360 lines (293 loc) · 11.1 KB
/
Copy pathbasic.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
#defineTYPE_INT32 1
#defineTYPE_FX16Q16 2
#defineTYPE_FLOAT32 3
#defineON 1
#defineOFF 0
/* MAIN CONFIGURATION OPTIONS */
#defineENABLE_COMPILER ON
#defineENABLE_CHECK_STACK ON
#defineENABLE_DASM ON
#defineENABLE_MATH OFF
//#define DATATYPE TYPE_INT32
//#define DATATYPE TYPE_FX16Q16
#defineDATATYPE TYPE_FLOAT32
#defineMAX_STR_LEN 1000
/* CONFIGURATION DEFINES FOR RUN-TIME */
#defineFMTSZ 128 /* FORMAT STRING MAX LEN */
#defineOPTSZ 64 /* OPCODE INDEX TABLE SIZE */
#defineOPNSZ 16 /* OPCODE NAME LEN */
#defineSYMSZ 32 /* SYMBOL SIZE */
#if (ENABLE_COMPILER==ON)
/* CONFIGURATION DEFINES FOR COMPILER */
#definePRGSZ 65535 /* PROGRAM SIZE */
#defineSTRSZ 65535 /* STRING TABLE SIZE */
#defineDIMSZ 1000 /* DIM ARRAY MEMORY POOL*/
#defineLOCS 100 /* LOCAL COUNT */
#defineVARS 100 /* MAX VARIABLES */
#defineSTKSZ 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>
typedeffloatTData;
#elif (DATATYPE==TYPE_INT32)
typedefint32_tTData;
#elif (DATATYPE==TYPE_FX16Q16)
#include<fixmath.h>
typedeffix16_tTData;
#else
#error NO VALID DATATYPE DEFINED!
#endif
/* INTERNAL BOOL REPRESENTATION */
#defineBS_FALSE 0
#defineBS_TRUE 1
/* EXIT CODES */
#defineEXIT_SUCCESS 0
#defineEXIT_ERROR 1
#defineEXIT_ERROR_COMPILE 2
#defineEXIT_ERROR_RUNTIME 3
#defineEXIT_ERROR_ALLOC 4
#defineEXIT_ERROR_VERSION 5
#defineEXIT_BYE 6
/* COMPILER ERROR MESSAGE */
#defineERR_BAD_TOKEN 1
#defineERR_SYNTAX 2
#defineERR_COUNT 3
#defineERR_EXPRESSION 4
#defineERR_COMP_MODE 5
#defineERR_STA 6
#defineERR_TOK_STA 7
#defineERR_OUT_OF_RANGE 8
/* RUN-TIME ERROR MESSAGE */
#defineERR_BOUNDS 1
#defineERR_DIM 2
#defineERR_STACK_OVERFLOW 3
#defineERR_MATH_OVERFLOW 4
#defineERR_DIV_ZERO 5
#defineERR_MOD_ZERO 6
#defineERR_CODE_OVERFLOW 7
#defineERR_INVALID_OPCODE 8
#defineERR_UNDEFINED_FN 9
#defineERR_DYN_FN 10
/* LISTING VERBOSITY */
#defineLST_PRINT_REPORT 0x01
#defineLST_PRINT_LISTING 0x02
#defineLST_PRINT_ALL 0xff
#defineGET_OPCODE(V) (V & 0xff)
#defineGET_OPLEN(V) ((V >> 8 ) & 0x00ff)
#defineGET_LINEn(V) ((V >> 16) & 0xffff)
/* Dynamic function argument access */
#defineDYF_NARG() ((ctx->sp[0]).i)
#defineDYF_ARG(N) (ctx->sp[DYF_NARG()-N+1])
#defineDYF_RETVAL(VAL) (ctx->sp+=(ctx->sp[0]).i, (ctx->sp)->v = VAL ) /*(ctx->sp+=DYF_NARG()+1, (--ctx->sp)->v = VAL )*/
typedefunionTValueTValue;
typedefstructTBASIC_CTXTBASIC_CTX;
typedefint32_t(*TCode)(TBASIC_CTX*ctx); /* BYTE-CODE */
typedefvoid (*TErrorHook)(TBASIC_CTX*ctx, int32_tnerr, int32_tnline, int32_taddr, char*msg); /* ERROR HOOK */
typedefint32_t (*TTraceHook)(TBASIC_CTX*ctx, int32_taddr, int32_topcode, int32_tline); /* KEYWORD HOOK */
typedefint32_t (*TFormatHook)(char*str, constchar*fmt, va_listap);
typedefint32_t (*TPutchHook)(int32_tc);
#if (ENABLE_COMPILER==ON)
typedefint32_t (*TKwdHook)(TBASIC_CTX*ctx, char*kwd); /* KEYWORD HOOK */
typedefint32_t (*TFunHook)(TBASIC_CTX*ctx, char*kwd, int32_tn); /* FUNCTION CALL HOOK */
#endif
structTDynDesc
{
uint32_tname_hash;
TCodefn;
};
unionTValue
{
TDatav;
int32_ti;
void*p;
};
structTBASIC_CTX
{
/* COMPILED PROGRAM */
int32_t*prg;
int32_tprg_sz;
int32_tpc;
/* RUN-TIME STACK */
TValue*stk;
TValue*sp;
int32_tstk_sz;
/* VARIABLE VALUES */
TValue*var;
int32_tvar_sz;
/* DIM() memoty pool */
TValue*dmem;
int32_tdmem_sz;
/* SUBROUTINE LOCAL VAR INDEXES */
int32_t*sub;
int32_tsloc_sz;
/* STRING TABLE */
char*stab;
int32_tstab_sz;
/* HASH OF VAR/SUB NAME */
uint32_t*vhash;
int32_tvhash_sz;
/* VAR EXPORT TABLE */
char*vname;
int32_tvname_sz;
/* DYNAMIC FUNCTION SUPPORT */
structTDynDesc*dynf_list;
int32_tdynf_sz;
int32_tdtop; /* TOP INDEX IN dmem */
TValueret; /* FUNCTION RETURN VALUE */
jmp_buftrap; /* TRAP ERRORS */
int32_tnerr; /* LAST ERROR CODE */
TErrorHookerrhook; /* Called before trap when error detected */
TFormatHookfmthook; /* Called from FORMAT */
TPutchHookputchook; /* Called for output char to console */
void*user_ptr;
#if (ENABLE_COMPILER==ON)
int32_tcpc; /*COMPILER PC*/
chartokn[SYMSZ], *lp; /* LEXER STATE */
int32_tlnum, tok, ungot; /* LEXER STATE */
TValuetokv;
charname[VARS][SYMSZ]; /* VARIABLE NAMES */
int32_tmode[VARS]; /* 0=NONE, 1=DIM, 2=SUB, 3=FUN */
int32_tcstk[STKSZ], *csp; /* COMPILER STACK */
int32_tnvar, cursub, temp, fcompile, ipc, (**opc)(); /* COMPILER STATE */
char*stabp; /* STRING TABLE PTR*/
TKwdHookkwdhook; /* KEYWORD HOOK */
TFunHookfunhook; /* FUNCTION HOOK */
#endif
};
/* FUNCTION PROTOTYPES */
/* Low-level init. Assume manual memory allocation */
int32_tbs_init(
TBASIC_CTX*ctx,
int32_t*prg, int32_tprg_sz,
TValue*stk, int32_tstk_sz,
TValue*var, int32_tvar_sz,
TValue*dmem, int32_tdmem_sz,
int32_t*sub, int32_tsloc_sz,
char*stab, int32_tstab_sz,
TFormatHookfmthook, // callback for format string function (like vsprintf()); must be defined.
TPutchHookputchook, // callback for output char to console (like putchar()); must be defined.
TErrorHookerrhook, // callback for error processing; can be NULL
void*user_ptr);
/* Load p-code image from buffer and allocate memory from heap */
int32_tbs_init_load(
TBASIC_CTX*ctx,
void*pcode,
TFormatHookfmthook, // callback for format string function (like vsprintf()); must be defined.
TPutchHookputchook, // callback for output char to console (like putchar()); must be defined.
TErrorHookerrhook, // callback for error processing; can be NULL
void*user_ptr);
/* Freeing memory allocated for TBASIC_CTX*/
int32_tbs_free(TBASIC_CTX*ctx);
/* Get last error desctiption (msg), address (addr) and line number (lnum)*/
int32_tbs_last_error(TBASIC_CTX*ctx, int32_t*lnum, int32_t*addr, char**msg);
/* Reset program counter and initialize stack */
int32_tbs_reset(TBASIC_CTX*ctx);
/* Execute p-code; Call thook() for every command */
int32_tbs_run(TBASIC_CTX*ctx, TTraceHookthook);
/* Register new user opcode */
int32_tbs_reg_opcode(TBASIC_CTX*ctx, TCodecode, char*name);
/* Register new user-defined dynamic function (function can be defined during run-time) */
int32_tbs_reg_dyn_fn(TBASIC_CTX*ctx, TCodecode, char*name);
/* Get variable by name; Accept array name like ARRAY(1); ARRAY(0) return array size */
int32_tbs_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_tbs_set_var(TBASIC_CTX*ctx, char*name, TValueval);
/* Convert TData to string; str should by allocated by user */
int32_tbs_val2str(TBASIC_CTX*ctx, char*str, TDataval);
#if (ENABLE_DASM==ON)
/* Disassemble p-code command located by specified addr; asml should by allocated by user */
int32_tbs_dasm(TBASIC_CTX*ctx, int32_taddr, char*asml);
#endif
#if (ENABLE_COMPILER==ON)
/* Allocate memory from heap and initialize TBASIC_CTX */
int32_tbs_init_alloc(
TBASIC_CTX*ctx,
TFormatHookfmthook, // callback for format string function (like vsprintf()); must be defined.
TPutchHookputchook, // callback for output char to console (like putchar()); must be defined.
TErrorHookerrhook, // callback for error processing; can be NULL
void*user_ptr);
/* Return expressions count separated by comma */
int32_tbs_get_nparam(TBASIC_CTX*ctx);
/* Parse n expressions, separated by comma */
voidbs_explist(TBASIC_CTX*ctx, int32_tn);
/* Insert one-arg instruction to program memory */
voidbs_inst1(TBASIC_CTX*ctx, TCodeopcode);
/* Insert two-arg instruction to program memory */
voidbs_inst2(TBASIC_CTX*ctx, TCodeopcode, int32_tx);
/* Parse expression */
int32_tbs_expr(TBASIC_CTX*ctx);
/* Register new user-defined keyword */
int32_tbs_reg_keyword(TBASIC_CTX*ctx, TKwdHookkhook);
/* Register new user-defined static function (function(s) must be defined during compile and run in same order!) */
int32_tbs_reg_func(TBASIC_CTX*ctx, TFunHookfhook);
/* Compile Basic source to p-code */
int32_tbs_compile(TBASIC_CTX*ctx, char*bsrc);
/* Export p-code to image; image buffer allocated inside and should be freeing by user */
int32_tbs_export_pcode(TBASIC_CTX*ctx, void**pcode, int32_t*pcode_sz);
/* Generate compile listing and report. see LST_PRINT_XXX */
int32_tbs_listing(TBASIC_CTX*ctx, char*bsrc, int32_tlevel, FILE*lst);
#endif
#ifdef__cplusplus
}
#endif/* __cplusplus */
#endif/* _BASIC_H_ */