+added LIST, DICT; +added TYPE; +added mb_get_type_string.

This commit is contained in:
tony 2015-10-05 17:22:00 +08:00
parent f6814ac00f
commit fffb567dd8
7 changed files with 1758 additions and 31 deletions

View File

@ -1,3 +1,9 @@
Oct. 5 2015
Added a LIST collection
Added a DICT collection
Added TYPE statement
Added an mb_get_type_string function
Sep. 30 2015
Improved UTF8 string manipulation

View File

@ -20,7 +20,7 @@ Why are existing script interpreters so complex? Why is it so difficult to integ
## Introduction
MY-BASIC is a lightweight cross-platform easy extendable BASIC interpreter written in pure C with about 7000 lines of source code. Its grammar is similar to structured BASIC. It is aimed to be either an embeddable scripting language or a standalone interpreter. The core is pretty light; all in a C source file and an associated header file. You are able to easily combine MY-BASIC with an existing project in C, C++, Objective-C, etc. Scripting driven can make your projects more powerful, elegant and neat.
MY-BASIC is a lightweight cross-platform easy extendable BASIC interpreter written in pure C with about 10,000 lines of source code. Its grammar is similar to structured BASIC. It is aimed to be either an embeddable scripting language or a standalone interpreter. The core is pretty light; all in a C source file and an associated header file. You are able to easily combine MY-BASIC with an existing project in C, C++, Objective-C, etc. Scripting driven can make your projects more powerful, elegant and neat.
## Main features
@ -36,6 +36,7 @@ MY-BASIC is a lightweight cross-platform easy extendable BASIC interpreter writt
* Structured user costomized sub routine definition by `DEF/ENDDEF` support, including tail recursion optimization
* `GOTO/GOSUB-RETURN` support
* Standard numeric functions, and standard string functions
* Collection manipulation functions for `LIST` and `DICT`
* Debug APIs
* Customizable memory pool
* High expansibility, easy to use APIs, easy to write customized scripting interfaces

File diff suppressed because it is too large Load Diff

View File

@ -42,6 +42,10 @@ extern "C" {
# define MB_MAX_DIMENSION_COUNT 4
#endif /* MB_MAX_DIMENSION_COUNT */
#ifndef MB_ENABLE_COLLECTION_LIB
# define MB_ENABLE_COLLECTION_LIB
#endif /* MB_ENABLE_COLLECTION_LIB */
#ifndef MB_ENABLE_ALLOC_STAT
# define MB_ENABLE_ALLOC_STAT
#endif /* MB_ENABLE_ALLOC_STAT */
@ -215,6 +219,7 @@ typedef enum mb_error_e {
SE_RN_TYPE_NOT_MATCH,
SE_RN_INVALID_STRING,
SE_RN_INDEX_OUT_OF_BOUND,
SE_RN_CANNOT_FIND_WITH_GIVEN_INDEX,
SE_RN_ILLEGAL_BOUND,
SE_RN_DIMENSION_TOO_MUCH,
SE_RN_OPERATION_FAILED,
@ -254,6 +259,11 @@ typedef enum mb_error_e {
SE_RN_DONT_MIX_INSTRUCTIONAL_AND_STRUCTURED,
SE_RN_ROUTINE_EXPECTED,
SE_RN_DUPLICATE_ROUTINE,
SE_RN_COLLECTION_EXPECTED,
SE_RN_ITERATOR_EXPECTED,
SE_RN_COLLECTION_OR_ITERATOR_EXPECTED,
SE_RN_INVALID_ITERATOR,
SE_RN_EMPTY_COLLECTION,
/** Extended abort */
SE_EA_EXTENDED_ABORT,
/** Extra */
@ -262,21 +272,35 @@ typedef enum mb_error_e {
typedef enum mb_data_e {
MB_DT_NIL = 0,
MB_DT_INT = 1 << 1,
MB_DT_REAL = 1 << 2,
MB_DT_STRING = 1 << 3,
MB_DT_USERTYPE = 1 << 4,
MB_DT_CLASS = 1 << 5,
MB_DT_ARRAY = 1 << 12
MB_DT_UNKNOWN = 1 << 1,
MB_DT_TYPE = 1 << 2,
MB_DT_INT = 1 << 3,
MB_DT_REAL = 1 << 4,
MB_DT_STRING = 1 << 5,
MB_DT_USERTYPE = 1 << 6,
MB_DT_ARRAY = 1 << 12,
#ifdef MB_ENABLE_COLLECTION_LIB
MB_DT_LIST = 1 << 13,
MB_DT_LIST_IT = 1 << 14,
MB_DT_DICT = 1 << 15,
MB_DT_DICT_IT = 1 << 16
#endif /* MB_ENABLE_COLLECTION_LIB */
} mb_data_e;
typedef union mb_value_u {
mb_data_e type;
int_t integer;
real_t float_point;
char* string;
void* usertype;
void* instance;
void* array;
#ifdef MB_ENABLE_COLLECTION_LIB
void* list;
void* list_it;
void* dict;
void* dict_it;
#endif /* MB_ENABLE_COLLECTION_LIB */
} mb_value_u;
typedef struct mb_value_t {
@ -342,6 +366,7 @@ MBAPI int mb_debug_get(struct mb_interpreter_t* s, const char* n, mb_value_t* va
MBAPI int mb_debug_set(struct mb_interpreter_t* s, const char* n, mb_value_t val);
MBAPI int mb_debug_set_stepped_handler(struct mb_interpreter_t* s, mb_debug_stepped_handler_t h);
MBAPI const char* mb_get_type_string(mb_data_e t);
MBAPI mb_error_e mb_get_last_error(struct mb_interpreter_t* s);
MBAPI const char* mb_get_error_desc(mb_error_e err);
MBAPI int mb_set_error_handler(struct mb_interpreter_t* s, mb_error_handler_t h);

Binary file not shown.

Binary file not shown.

View File

@ -131,6 +131,8 @@ static int pool_count = 0;
static _pool_t* pool = 0;
static long alloc_count = 0;
#define _POOL_NODE_ALLOC(size) (((char*)malloc(sizeof(_pool_tag_t) + size)) + sizeof(_pool_tag_t))
#define _POOL_NODE_PTR(s) (s - sizeof(_pool_tag_t))
#define _POOL_NODE_NEXT(s) (*((void**)(s - sizeof(_pool_tag_t))))
@ -217,12 +219,14 @@ static char* _pop_mem(unsigned s) {
result = pl->stack;
pl->stack = _POOL_NODE_NEXT(result);
_POOL_NODE_SIZE(result) = (_pool_chunk_size_t)s;
++alloc_count;
return result;
} else {
/* Create a new node */
result = _POOL_NODE_ALLOC(s);
_POOL_NODE_SIZE(result) = (_pool_chunk_size_t)s;
++alloc_count;
return result;
}
@ -233,6 +237,7 @@ static char* _pop_mem(unsigned s) {
/* Allocate directly */
result = _POOL_NODE_ALLOC(s);
_POOL_NODE_SIZE(result) = (_pool_chunk_size_t)0;
++alloc_count;
return result;
}
@ -241,6 +246,10 @@ static void _push_mem(char* p) {
int i = 0;
_pool_t* pl = 0;
if(--alloc_count < 0) {
mb_assert(0 && "Multiple free");
}
if(pool_count) {
for(i = 0; i < pool_count; i++) {
pl = &pool[i];
@ -840,6 +849,9 @@ static void _on_exit(void) {
c = 0;
#ifdef _USE_MEM_POOL
if(alloc_count > 0) {
mb_assert(0 && "Memory leak");
}
_close_mem_pool();
#endif /* _USE_MEM_POOL */