MY-BASIC Quick Reference
MY-BASIC Quick Reference
Quick Reference
Getting started
You can download the latest MY-BASIC package from
https://github.com/paladin-t/my_basic/archive/master.zip or check
out the source code to make a build manually. You can get the latest
revision by git clone https://github.com/paladin-t/my_basic.git. In
this part let’s get start using the MY-BASIC interpreter, which comes
as follow:
Multi-line comment
MY-BASIC also supports multi-line comment, which is a small
advantage comparing to other dialects. It’s surrounded by ‘[ and ’],
eg.
print “Begin”;
‘[
print “This line won’t be executed!”;
print “This is also ignored”;
‘]
print “End”;
日本語 = "こんにちは"
print 日本語, ", ", len(日本語);
Keywords
There are some keywords and reserved functions in MY-BASIC as
follow:
Keywords REM, NIL, MOD, AND, OR, NOT, IS, LET,
DIM, IF, THEN, ELSEIF, ELSE, ENDIF, FOR,
IN, TO, STEP, NEXT, WHILE, WEND, DO,
UNTIL, EXIT, GOTO, GOSUB, RETURN,
CALL, DEF, ENDDEF, CLASS, ENDCLASS,
ME, NEW, VAR, REFLECT, LAMBDA, MEM,
TYPE, IMPORT, END
Reserved Standard ABS, SGN, SQR, FLOOR, CEIL, FIX, ROUND,
functions library SRND, RND, SIN, COS, TAN, ASIN, ACOS,
ATAN, EXP, LOG, ASC, CHR, LEFT, LEN,
MID, RIGHT, STR, VAL, PRINT, INPUT
Collection LIST, DICT, PUSH, POP, BACK, INSERT,
library SORT, EXISTS, INDEX_OF, GET, SET,
REMOVE, CLEAR, CLONE, TO_ARRAY,
ITERATOR, MOVE_NEXT
It is not accepted to use these words for user-defined identifiers; in
addition there are two more predefined boolean constants, a.k.a.
TRUE and FALSE, as implied literally, represent boolean value true and
false, it’s not accepted to reassign these symbols with other values.
Details of keywords and functions will be explained latter in this
manual.
Operators
All operators in MY-BASIC as follow:
Operators +, -, *, /, ^, =, <, >, <=, >=, <>
These operators are used in calculation or comparison expressions.
Besides, the keywords MOD, AND, OR, NOT, IS also work as operators.
An expression is evaluated from left to right, with top down priorities
as follow:
Level Operation
1 ( ) (explicit priority indicator)
2 - (negative), NOT
3 ^
4 *, /, MOD
5 +, - (minus)
6 <, >, <=, >=, <>, = (equal comparison)
7 AND, OR, IS
8 = (assignment)
MOD stands for modulus, a.k.a. % in some other programming
languages. The caret symbol ^ stands for power operation.
dim nums(10)
dim strs$(2, 5)
Structured routine
It is possible to extract reusable code blocks with sub routines.
MY-BASIC supports both structured routine with the
CALL/DEF/ENDDEF statements and instructional routine with the
GOSUB/RETURN (and GOTO) statements, but they can’t be mixed
together in one program.
A structured routine begins with a DEF statement and ends with
ENDDEF, you can define routines with any arity. Calling a sub routine
is similar to calling a native scripting interface. It requires an explicit
CALL statement, if a routine is lexically defined after calling. A routine
returns the value of the last expression to its caller, or returns
explicitly with the RETURN statement. Eg.
a= 1
b= 0
def fun(d)
d = call bar(d)
sin(10)
return d ' Try comment this line
enddef
def foo(b)
a= 2
return a + b
enddef
def bar(c)
return foo(c)
enddef
r = fun(2 * 5)
print r; a; b; c;
Note that it requires a pair of brackets to get the value, or it’s a calling
execution.
Instructional routine
Traditional instructional routine is reserved as well. A label is used to
tag the start point of an instructional routine. Use a GOSUB statement
wherever in a program to call that labeled routine. The RETURN
statement is used to exit a routine and transfer control back to its
caller.
Control structures
There are three kinds of execution flows in MY-BASIC.
Serial structure, the most basic one, executes statements line by line.
MY-BASIC supports GOTO statement which provides unconditional
control transfer ability. Use it similar to GOSUB as GOTO label. The
difference is that an instructional routine can return back from a
callee, but unconditional GOTO cannot. The END statement can be
placed anywhere in source code to terminate the whole execution of
a program.
Conditional structures consist of some condition jump statements:
IF/THEN/ELSEIF/ELSE/ENDIF. These statements check condition
expressions then perform an action in a case of true condition branch,
otherwise in a case of false it performs something else as you write.
Conditional IF statements in a single line:
Or multiple lines:
input n
if n = 1 then
print "One";
elseif n = 2 then
print "Two";
elseif n = 3 then
print "Three";
else
print "More than that";
endif
for i = 1 to 10 step 1
print i;
next i
for i in list(1 to 5)
print i;
next
The WHILE/WEND and DO/UNTIL loops are used to loop for uncertain
steps, or wait for specific conditions. Eg.
a= 1
while a <= 10
print a;
a= a+1
wend
a= 1
do
print a;
a= a+1
until a > 10
Using class
MY-BASIC supports prototype-based OOP (Object-Oriented
Programming). It is also as known as “prototypal”,
“prototype-oriented”, “classless”, or “instance-based” programming.
Use a pair of CLASS/ENDCLASS statements to define a prototype
object (a class). Use VAR to declare a member variable in a class. It’s
possible to define member function (a.k.a. “method”) in a prototype
with the DEF/ENDDEF statements as well. Write another prototype
surrounding with a pair of parentheses after a declaration statement
to inherit from it (meaning using it as meta class). Use the NEW
statement to create a new instance of a prototype. Eg.
class foo
var a = 1
def fun(b)
return a + b
enddef
endclass
bar simply links foo as meta class. inst creates a new clone of bar and
keep the foo meta linkage.
MY-BASIC supports reflection on a prototype with the REFLECT
statement. It iterates all variable fields and sub routines in a class and
its meta class, and stores name/value pairs of variables and
name/type pairs of sub routines to a dictionary. Eg.
class base
var b = "Base"
def fun()
print b;
enddef
endclass
class derived(base)
var d = "Derived"
def fun()
print d;
enddef
endclass
i = new(derived)
i.fun();
r = reflect(i)
f = iterator(r)
while move_next(f)
k = get(f)
v = r(k)
print k, ": ", v;
wend
g = get(i, “fun”);
g()
Using Lambda
A lambda abstraction (a.k.a. “anonymous function” or “function
literal”) is a function definition that is not bound to an identifier.
Lambda function is often used as:
1. argument passed to other functions, or
2. return value from a function.
A lambda becomes a closure after it captured some values in outer
scope.
MY-BASIC offers full support for lambda, including invokable as a
value, higher order function, closure and currying, etc.
A lambda expression begins with a LAMBDA keyword. Eg.
Pass a type value to the STR statement to get the type name in string.
foo = 1
Importing a module
It’s also possible to put some native scripting interfaces in a module
(a.k.a. “namespace”) to avoid naming pollution. MY-BASIC doesn’t
support making modules in BASIC for the moment. Use IMPORT
“@xxx” to import a native module, all symbols in that module could
be used without module prefix.
4. Collection Libraries
MY-BASIC offers a set of LIST, DICT manipulation functions, which
provide creation, accessing, iteration, etc. as follow:
Name Description
LIST Creates a list
DICT Creates a dictionary
PUSH Pushes a value to the tail of a list, overridable for
referenced usertype and class instance
POP Pops a value from the tail of a list, overridable for
referenced usertype and class instance
BACK Peeks the value at tail of a list, overridable for
referenced usertype and class instance
INSERT Inserts a value at a specific position of a list,
overridable for referenced usertype and class
instance
SORT Sorts a list increasingly, overridable for referenced
usertype and class instance
EXISTS Tells whether a list contains a specific value, or
whether a dictionary contains a specific key,
overridable for referenced usertype and class
instance
INDEX_OF Gets the index of a value in a list, overridable for
referenced usertype and class instance
GET Returns the value at a specific index in a list, or the
value with a specific key in a dictionary, or a
member of a class instance, overridable for
referenced usertype and class instance
SET Sets the value at a specific index in a list, or the
value with a specific key in a dictionary, or a
member variable of a class instance, overridable
for referenced usertype and class instance
REMOVE Removes the element at a specific index in a list,
or the element with a specific key in a dictionary,
overridable for referenced usertype and class
instance
CLEAR Clears a list or a dictionary, overridable for
referenced usertype and class instance
CLONE Clones a collection, or a referenced usertype
TO_ARRAY Copies all elements from a list to an array
ITERATOR Gets an iterator of a list or a dictionary,
overridable for referenced usertype and class
instance
MOVE_NEXT Moves an iterator to next position over a list or a
dictionary, overridable for referenced usertype
and class instance
Using collections, eg.
l = list(1, 2, 3, 4)
set(l, 1, “B”)
print exists(l, 2); pop(l); back(l); len(l);
d = dict()
d(1) = 2
print d(1);
Interpreter structure
MY-BASIC uses an interpreter structure to store necessary data during
parsing and running; including registered function, AST (Abstract
Syntax Tree), parsing context, running context, scope, error
information, etc. An interpreter structure is a unit of context.
Meta information
unsigned long mb_ver(void);
Returns the version number of the current interpreter.
const char* mb_ver_string(void);
Returns the version text of the current interpreter.
Forking
These functions are used to fork and join an interpreter.
int mb_fork(struct mb_interpreter_t** s,
struct mb_interpreter_t* r,
bool_t clear_forked);
This function forks a new interpreter, from r to s. All forked
environments share the same registered functions, parsed code, etc.
but uses its own running context. Pass true to clear_forked to let the
source instance collects and manages data in the forked one.
int mb_join(struct mb_interpreter_t** s);
This function joins a forked interpreter. Use this to close a forked
interpreter.
int mb_get_forked_from(struct mb_interpreter_t* s,
struct mb_interpreter_t** src);
This function gets the source interpreter of a forked one.
Function registration/unregistration
These functions are used to register or unregister native scripting
interfaces.
int mb_register_func(struct mb_interpreter_t* s,
const char* n,
mb_func_t f);
This function registers a function pointer into an interpreter with a
specific name. The function must have signature as int (*
mb_func_t)(struct mb_interpreter_t*, void**). A registered function
can be called in MY-BASIC code. This function returns how many
entries have been influenced, thus non-zero means success. The
specified identifier will be stored in upper case.
int mb_remove_func(struct mb_interpreter_t* s,
const char* n);
This function removes a registered function out of an interpreter with
a specific registered name. This function returns how many entries
have been influenced, thus non-zero means success.
int mb_remove_reserved_func(struct mb_interpreter_t* s,
const char* n);
This function removes a reserved function out of an interpreter with
a specific name. Do not use this function unless you really want to, Eg.
remove or replace built-in interfaces. This function returns how many
entries have been influenced, thus non-zero means success.
int mb_begin_module(struct mb_interpreter_t* s, const char* n);
This function begins a module context with a name. All functions
registered after it will be put into that module. Module is as known as
namespace, use the IMPORT statement to get shortcut to it.
int mb_end_module(struct mb_interpreter_t* s);
This function ends the current module context.
Interacting
These functions are used in extended functions to communicate with
the core.
int mb_attempt_func_begin(struct mb_interpreter_t* s,
void** l);
This function checks whether BASIC is invoking an extended function
in a legal beginning way. Call it when beginning an extended function
without parameters.
int mb_attempt_func_end(struct mb_interpreter_t* s,
void** l);
This function checks whether BASIC is invoking an extended function
in a legal ending way. Call it when ending an extended function
without parameters.
int mb_attempt_open_bracket(struct mb_interpreter_t* s,
void** l);
This function checks whether BASIC is invoking an extended function
in a legal way that begins with an open bracket.
int mb_attempt_close_bracket(struct mb_interpreter_t* s,
void** l);
This function checks whether BASIC is invoking an extended function
in a legal way that ends with a close bracket after argument list.
int mb_has_arg(struct mb_interpreter_t* s,
void** l);
This function detects whether there is any more argument at current
execution position. Use this function to implement a variadic function.
Returns zero for no more, otherwise non-zero.
int mb_pop_int(struct mb_interpreter_t* s,
void** l,
int_t* val);
This function tries to pop an argument as int_t from an interpreter.
And stores the result to *val.
int mb_pop_real(struct mb_interpreter_t* s,
void** l,
real_t* val);
This function tries to pop an argument as real_t from an interpreter.
And stores the result to *val.
int mb_pop_string(struct mb_interpreter_t* s,
void** l,
char** val);
This function tries to pop an argument as char* (string) from an
interpreter. And stores the pointer to *val. You don’t need to know
how and when a popped string will be disposed, but note that a
popped string may be disposed when popping next string argument,
so, just process it or cache it in time.
int mb_pop_usertype(struct mb_interpreter_t* s,
void** l,
void** val);
This function tries to pop an argument as void* (usertype) from an
interpreter. Use mb_pop_value instead if a usertype is larger than
void*.
int mb_pop_value(struct mb_interpreter_t* s,
void** l,
mb_value_t* val);
This function tries to pop an argument as mb_value_t from an
interpreter. Use this function instead of mb_pop_int, mb_pop_real
and mb_pop_string if an extended function accepts argument in
different types, or popping other advanced data types.
int mb_push_int(struct mb_interpreter_t* s,
void** l,
int_t val);
This function pushes a value as int_t to an interpreter.
int mb_push_real(struct mb_interpreter_t* s,
void** l,
real_t val);
This function pushes a value as real_t to an interpreter.
int mb_push_string(struct mb_interpreter_t* s,
void** l,
char* val);
This function pushes a value as char* (string) to an interpreter. The
memory of char* val must be allocated and disposable by MY-BASIC.
Consider use mb_memdup to make it before pushing. Eg.
mb_push_string(s, l, mb_memdup(str, (unsigned)(strlen(str) + 1));.
int mb_push_usertype(struct mb_interpreter_t* s,
void** l,
void* val);
This function pushes a value as void* (usertype) to an interpreter. Use
mb_push_value if a usertype is larger than void*.
int mb_push_value(struct mb_interpreter_t* s,
void** l,
mb_value_t val);
This function pushes a value in mb_value_t to an interpreter. Use this
function instead of mb_push_int, mb_push_real and mb_push_string
if an extended function returns generics types. Or pushing other
advanced data types.
Class definition
These functions are used to define a class manually at native side.
int mb_begin_class(struct mb_interpreter_t* s,
void** l,
const char* n,
mb_value_t** meta,
int c,
mb_value_t* out);
This function begins a class definition with a specific name. n is the
class name in upper case. meta is an array of mb_value_t*, which are
optional meta classes; c is the element count of the meta array. The
generated class will be returned into *out.
int mb_end_class(struct mb_interpreter_t* s,
void** l);
This function ends a class definition.
int mb_get_class_userdata(struct mb_interpreter_t* s,
void** l,
void** d);
This function gets the userdata of a class instance. The returned data
will be stored into *d.
int mb_set_class_userdata(struct mb_interpreter_t* s,
void** l,
void* d);
This function sets the userdata of a class instance with data d.
Value manipulation
These functions manipulate values.
int mb_get_value_by_name(struct mb_interpreter_t* s,
void** l,
const char* n,
mb_value_t* val);
This function gets the value of an identifier with a specific name in
upper case. n is the expected name text. It returns a value to *val.
int mb_get_vars(struct mb_interpreter_t* s,
void** l,
mb_var_retrieving_func_t r,
int stack_offset);
This function retrieves the name and value of active variables in the
specific stack frame, then returns the retrieved variable count.
stack_offset indicates the stack offset, 0 for the current frame, -1 for
the root, other positive values for offset from the top (active) frame.
int mb_add_var(struct mb_interpreter_t* s,
void** l,
const char* n,
mb_value_t val,
bool_t force);
This function adds a variable with a specific identifier name in upper
case and a value to an interpreter. n is the name text. val is the value
of the variable. force indicates whether overwrite existing value.
int mb_get_var(struct mb_interpreter_t* s,
void** l,
void** v,
bool_t redir);
This function gets a token literally, and stores it in the parameter *v if
it’s a variable. redir indicates whether to redirect result variable to
any member variable of a class instance.
int mb_get_var_name(struct mb_interpreter_t* s,
void** v,
char** n);
This function gets the name of a variable, then stores it in the
parameter *n.
int mb_get_var_value(struct mb_interpreter_t* s,
void** l,
mb_value_t* val);
This function gets the value of a variable into *val.
int mb_set_var_value(struct mb_interpreter_t* s,
void** l,
mb_value_t val);
This function sets the value of a variable from val.
int mb_init_array(struct mb_interpreter_t* s,
void** l,
mb_data_e t,
int* d,
int c,
void** a);
This function initializes an array which can be used in BASIC. The
parameter mb_data_e t stands for the type of elements in the array,
can be either MB_DT_REAL or MB_DT_STRING; disable the
MB_SIMPLE_ARRAY macro to use a complex array with passing
MB_DT_NIL to it. The int* d and int c stand for ranks of dimensions
and dimension count. The function stores the created array to void**
a.
int mb_get_array_len(struct mb_interpreter_t* s,
void** l,
void* a,
int r,
int* i);
This function gets the length of an array. int r means which dimension
to get.
int mb_get_array_elem(struct mb_interpreter_t* s,
void** l,
void* a,
int* d,
int c,
mb_value_t* val);
This function gets the value of an element in an array.
int mb_set_array_elem(struct mb_interpreter_t* s,
void** l,
void* a,
int* d,
int c,
mb_value_t val);
This function sets the value of an element in an array.
int mb_init_coll(struct mb_interpreter_t* s,
void** l,
mb_value_t* coll);
This function initializes a collection; pass a valid mb_value_t pointer
with a specific collection type you’d like to initialize.
int mb_get_coll(struct mb_interpreter_t* s,
void** l,
mb_value_t coll,
mb_value_t idx,
mb_value_t* val);
This function gets an element in a collection. It accepts LIST index or
DICT key with mb_value_t idx.
int mb_set_coll(struct mb_interpreter_t* s,
void** l,
mb_value_t coll,
mb_value_t idx,
mb_value_t val);
This function sets an element in a collection. It accepts LIST index or
DICT key with mb_value_t idx.
int mb_remove_coll(struct mb_interpreter_t* s,
void** l,
mb_value_t coll,
mb_value_t idx);
This function removes an element from a collection. It accepts LIST
index or DICT key with mb_value_t idx.
int mb_count_coll(struct mb_interpreter_t* s,
void** l,
mb_value_t coll,
int* c);
This function returns the count of elements in a collection.
int mb_keys_of_coll(struct mb_interpreter_t* s,
void** l,
mb_value_t coll,
mb_value_t* keys,
int c);
This function retrieves all keys of a collection. It gets indices of a LIST
or keys of a DICT; and stores them in mb_value_t* keys.
int mb_make_ref_value(struct mb_interpreter_t* s,
void* val,
mb_value_t* out,
mb_dtor_func_t un,
mb_clone_func_t cl,
mb_hash_func_t hs,
mb_cmp_func_t cp,
mb_fmt_func_t ft);
This function makes a referenced usertype mb_value_t object which
holds void* val as raw userdata. Note you need to provide some
functors to the core to manipulate it.
int mb_get_ref_value(struct mb_interpreter_t* s,
void** l,
mb_value_t val,
void** out);
This function gets the raw userdata from a referenced usertype.
int mb_ref_value(struct mb_interpreter_t* s,
void** l,
mb_value_t val);
This function increases the reference count of a referenced value.
int mb_unref_value(struct mb_interpreter_t* s,
void** l,
mb_value_t val);
This function decreases the reference count of a referenced value.
int mb_set_alive_checker(struct mb_interpreter_t* s,
mb_alive_checker_t f);
This function sets an object aliveness checker globally.
int mb_set_alive_checker_of_value(struct mb_interpreter_t* s,
void** l,
mb_value_t val,
mb_alive_value_checker_t f);
This function sets an object aliveness checker on a specific referenced
usertype value.
int mb_override_value(struct mb_interpreter_t* s,
void** l,
mb_value_t val,
mb_meta_func_e m,
void* f);
This function overrides a meta function of a specific referenced
usertype value.
int mb_dispose_value(struct mb_interpreter_t* s,
mb_value_t val);
This function disposes a value popped from an interpreter. Used for
strings only.
Invokable manipulation
int mb_get_routine(struct mb_interpreter_t* s,
void** l,
const char* n,
mb_value_t* val);
This function gets a routine value by name in upper case.
int mb_set_routine(struct mb_interpreter_t* s,
void** l,
const char* n,
mb_routine_func_t f,
bool_t force);
This function sets a routine value with a specific name in upper case
using a native functor.
int mb_eval_routine(struct mb_interpreter_t* s,
void** l,
mb_value_t val,
mb_value_t* args,
unsigned argc,
mb_value_t* ret);
This function evaluates an invokable. mb_value_t* args is a pointer to
an array of arguments, unsigned argc is the count of it. The last
optional parameter mb_value_t* ret receives return value; pass NULL
to it if it’s not used.
int mb_get_routine_type(struct mb_interpreter_t* s,
mb_value_t val,
mb_routine_type_e* y);
This function gets the sub type of an invokable value. mb_value_t val
is a value of an invokable value, and the result will be assigned to
mb_routine_type_e* y.
Type handling
const char* mb_get_type_string(mb_data_e t);
This function returns the string value of a specific type.
Error handling
int mb_raise_error(struct mb_interpreter_t* s,
void** l,
mb_error_e err,
int ret);
This function raises an error manually.
mb_error_e mb_get_last_error(struct mb_interpreter_t* s,
const char** file,
int* pos,
unsigned short* row,
unsigned short* col);
This function returns the latest error information in an interpreter
structure, and detail location. It also clears the latest error
information.
const char* mb_get_error_desc(mb_error_e err);
This function returns the string value of a specific error.
int mb_set_error_handler(struct mb_interpreter_t* s,
mb_error_handler_t h);
This function sets an error handler of an interpreter.
IO redirection
int mb_set_printer(struct mb_interpreter_t* s,
mb_print_func_t p);
This function sets a PRINT handler of an interpreter. Use this to
customize an output handler for the PRINT statement. The function
to be set must be a pointer of int (* mb_print_func_t)(struct
mb_interpreter_t*, const char*, …). Defaults to printf.
int mb_set_inputer(struct mb_interpreter_t* s,
mb_input_func_t p);
This function sets the INPUT handler of an interpreter. Use this to
customize an input handler for the INPUT statement. The function to
be set must be a pointer of int (* mb_input_func_t)( struct
mb_interpreter_t*, const char*, char*, int). Defaults to mb_gets. The
first parameter is an optional prompt text as INPUT “Some text”, A$.
Omit it if you don’t need.
Miscellaneous
int mb_set_import_handler(struct mb_interpreter_t* s,
mb_import_handler_t h);
This function sets a customized importing handler for BASIC code.
int mb_set_memory_manager(mb_memory_allocate_func_t a,
mb_memory_free_func_t f);
This function sets a memory allocator and a freer of MY-BASIC
globally.
bool_t mb_get_gc_enabled(struct mb_interpreter_t* s);
This function gets whether the garbage collecting is enabled.
int mb_set_gc_enabled(struct mb_interpreter_t* s,
bool_t gc);
This function sets whether the garbage collecting is enabled. Can be
used to pause and resume GC.
int mb_gc(struct mb_interpreter_t* s,
int_t* collected);
This function tries to trigger garbage collecting manually. And gets
how much memory has been collected.
int mb_get_userdata(struct mb_interpreter_t* s,
void** d);
This function gets the userdata of an interpreter instance.
int mb_set_userdata(struct mb_interpreter_t* s,
void* d);
This function sets the userdata of an interpreter instance.
int mb_gets(struct mb_interpreter_t* s,
const char* pmt,
char* buf,
int s);
A more safety evolvement of the standard C gets. Returns the length
of input text.
char* mb_memdup(const char* val,
unsigned size);
This function duplicates a block of memory of a MY-BASIC
manageable buffer; use this function before pushing a string
argument. Note this function only allocates and copies bytes with the
specific size, so you have to increase an extra byte to size for ending
“\0”. Eg.
mb_push_string(s, l, mb_memdup(str, (unsigned)(strlen(str) + 1));.
7. Customizing MY-BASIC
Redirecting PRINT and INPUT
Include a header file to use variadic:
#include <stdarg.h>
return ret;
}
return result;
}
mb_set_printer(bas, my_print);
mb_set_inputer(bas, my_input);
mb_check(mb_attempt_open_bracket(s, l));
mb_check(mb_pop_int(s, l, &m));
mb_check(mb_pop_int(s, l, &n));
mb_check(mb_attempt_close_bracket(s, l));
r = m > n ? m : n;
mb_check(mb_push_int(s, l, r));
return result;
}
MB_SIMPLE_ARRAY
Enabled by default. An entire array uses a unified type mark, which
means there are only two kinds of array, string and real_t.
Disable this macro if you would like to store generic type values in an
array including int_t, real_t, usertype, etc. Besides, array of string is
still another type. Note non simple array requires extra memory to
store type mark of each element.
MB_ENABLE_ARRAY_REF
Enabled by default. Compiles with referenced array if this macro is
defined, otherwise compiles as value type array.
MB_MAX_DIMENSION_COUNT
4 by default. Change this to support more array dimensions. Note it
cannot be greater than the maximum number which an unsigned
char can hold.
MB_ENABLE_COLLECTION_LIB
Enabled by default. Compiles with LIST and DICT libraries if this macro
is defined.
MB_ENABLE_USERTYPE_REF
Enabled by default. Compiles with referenced usertype support if this
macro is defined.
MB_ENABLE_ALIVE_CHECKING_ON_USERTYPE_REF
Enabled by default. Compiles with object aliveness checking on
referenced usertype if this macro is defined.
MB_ENABLE_CLASS
Enabled by default. Compiles with class (prototype) support if this
macro is defined.
MB_ENABLE_LAMBDA
Enabled by default. Compiles with lambda (anonymous function)
support if this macro is defined.
MB_ENABLE_MODULE
Enabled by default. Compiles with module (namespace) support if
this macro is defined. Use IMPORT “@xxx” to import a module, and
all symbols in that module could be used without the module prefix.
MB_ENABLE_UNICODE
Enabled by default. Compiles with UTF8 manipulation ability if this
macro is defined, to handle UTF8 string properly with functions such
as LEN, LEFT, RIGHT, MID, etc.
MB_ENABLE_UNICODE_ID
Enabled by default. Compiles with UTF8 token support if this macro is
defined, this feature requires MB_ENABLE_UNICODE enabled.
MB_ENABLE_FORK
Enabled by default. Compiles with fork support if this macro is
defined.
MB_GC_GARBAGE_THRESHOLD
16 by default. It will trigger a sweep-collect GC cycle when this
number of deallocation occurred.
MB_ENABLE_ALLOC_STAT
Enabled by default. Use MEM to tell how much memory in bytes has
been allocated by MY-BASIC. Note statistics of each allocation takes
sizeof(intptr_t) more bytes.
MB_ENABLE_SOURCE_TRACE
Enabled by default. To trace where an error occurs.
Disable this to reduce some memory occupation. Only do this on
memory sensitive platforms.
MB_ENABLE_STACK_TRACE
Enabled by default. MY-BASIC will record stack frames including sub
routines and native functions if this macro is defined.
MB_ENABLE_FULL_ERROR
Enabled by default. Prompts detailed error message. Otherwise all
error types will prompts an “Error occurred” message. However, it’s
always possible to get specific error type by checking error code in
the callback.
MB_CONVERT_TO_INT_LEVEL
Describes how to handle real numbers after an expression is
evaluated. Just leave it a s real when it’s defined as
MB_CONVERT_TO_INT_LEVEL_NONE; otherwise try to convert it to
an integer if it doesn’t contains decimal part when it’s defined as
MB_CONVERT_TO_INT_LEVEL_ALL. Also you could use the
mb_convert_to_int_if_posible macro to deal with an mb_value_t in
your own scripting interface functions.
MB_PRINT_INPUT_PROMPT
Enabled by default. Prefers to output the specified input prompt to
the interpreter’s output function.
MB_PRINT_INPUT_CONTENT
Disabled by default. Prefers to output the inputted content to the
interpreter’s output function.
MB_PREFER_SPEED
Enabled by default. Prefers running speed over space occupation as
much as possible. Disable this to reduce memory footprint.
MB_COMPACT_MODE
Enabled by default. C struct may use a compact layout.
This might cause some strange pointer accessing bugs with some
compilers (for instance, some embedded systems). Try to disable this
if you met any strange bugs.
_WARNING_AS_ERROR
0 by default.
Define this macro as 1 in my_basic.c to treat warnings as error, or
they will be ignored silently.
Something like divide by zero, wrong typed arguments passed will
trigger warnings.
_HT_ARRAY_SIZE_DEFAULT
193 by default. Change this in my_basic.c to resize the hash tables.
Smaller value will reduce some memory occupation, size of hash
table will influence tokenization and parsing time during loading,
won’t influence running performance most of the time (except for
cross scope identifier lookup).
_SINGLE_SYMBOL_MAX_LENGTH
128 by default. Max length of a lexical symbol.
8. Memory Occupation
Memory footprint is often a sensitive bottleneck under some
memory constrained platforms. MY-BASIC provides a method to
count how much memory an interpreter has allocated. Write script as
follow to tell it in bytes:
print mem;