My_basic interpreter

General discussion for topics related to the FreeBASIC project or its community.
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: My_basic interpreter

Post by aurelVZAB »

Anyway, this probably isn't the right place to discuss C, and we've kind of hi-jacked the thread (It was about My_basic). If you want to discuss it more, feel free to email me.
Well we are under topic about basic interpreter written in C ....right?
Jwilla basic is also written in C ...ok?
Maybe we can continue under new topic if some other members are interested...?
And here are people who have experience in FreeBasic much more than Me.
You know i am better with o2 than with fb....heh

ps.By the way jj2007 our old masm friend said ...most people here visit this forum
just because sentimental reasons ,,,
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: My_basic interpreter

Post by aurelVZAB »

I will just add this ..for fun

'define in pseudo code or not?
'in C
'int (*prg[PRGSZ])(void),(**pc)(void),cpc,lmap[PRGSZ]; /* COMPILED PROGRAM */
'is this basic like ?
int PRGSZ = 1024
int *prg[PRGSZ]) ' this is nothing else than integer array
'or
int prg[1024]
declare function (int pc) as INT
int cpc=0
int lmap[PRGSZ] 'this is nothing else than integer array
Ed Davis
Posts: 37
Joined: Jul 28, 2008 23:24

Re: My_basic interpreter

Post by Ed Davis »

aurelVZAB wrote:I will just add this ..for fun

'define in pseudo code or not?
'in C
'int (*prg[PRGSZ])(void),(**pc)(void),cpc,lmap[PRGSZ]; /* COMPILED PROGRAM */
'is this basic like ?
int PRGSZ = 1024
int *prg[PRGSZ]) ' this is nothing else than integer array
'or
int prg[1024]
declare function (int pc) as INT
int cpc=0
int lmap[PRGSZ] 'this is nothing else than integer array
But remember, it is: int (*prg[PRGSZ])(void),(**pc)(void)

That says that prg is an array of pointers to functions, taking 0 parameters, and returning an integer.
And it makes pc a pointer to essentially the same thing.

For fun, go to here: https://cdecl.org/ and paste in: int (*prg[100])(void); and you'll be told what it means.

For instance:int (*prg[100])(void);
declare prg as array 100 of pointer to function (void) returning int

And: int (**pc)(void)
declare pc as pointer to pointer to function (void) returning int
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: My_basic interpreter

Post by aurelVZAB »

Yes Ed
I know that you know all that things ...

and i see ..heh.... pointer to pointer with two stars **pc

But my question will be
Is this approach is really nececery for this tyoe of program ...?

i think not ...
but again maybe i am totaly stupid for all this pointered functions
Ed Davis
Posts: 37
Joined: Jul 28, 2008 23:24

Re: My_basic interpreter

Post by Ed Davis »

aurelVZAB wrote:Yes Ed
I know that you know all that things ...
Nope. And I apologize if I come across that way. I only know enough to be dangerous. I'm ignorant in so many areas that it makes me very frustrated.
aurelVZAB wrote:and i see ..heh.... pointer to pointer with two stars **pc
But my question will be
Is this approach is really nececery for this tyoe of program ...?
i think not ...
No it isn't necessary. However, some folks (myself included) think that a table of function pointerss is much nicer than a huge case statement. But that is the nice thing about programming - you are free to do it the way you like best.
aurelVZAB wrote:but again maybe i am totaly stupid for all this pointered functions
Nah, you've written several Basic interpreters, and I've seen your posts on other topics. You're plenty smart. You just don't have lots of C experience.
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: My_basic interpreter

Post by aurelVZAB »

You just don't have lots of C experience.
Yes Ed you have a right !
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: My_basic interpreter

Post by angros47 »

How to use a FreeBasic function from My_Basic:

Code: Select all

#include "my_basic.bi"
function maximum cdecl (s as any ptr, byref l as any ptr) as integer
	dim as integer result = MB_FUNC_OK
	dim as integer  m = 0
	dim as integer n = 0
	dim as integer r = 0
	mb_attempt_open_bracket(s, l)
	mb_pop_int(s, l, m)
	mb_pop_int(s, l, n)
	mb_attempt_close_bracket(s, l)
	r = iif(m > n, m , n)
	mb_push_int(s, l, r)
	return result
end function



dim bas as any ptr

mb_init()
mb_open(bas)
mb_register_func(bas, "maximum", @maximum)
mb_load_string(bas, "print maximum(22 , 7);", 1)
mb_run(bas, 1)
mb_close(bas)
mb_dispose
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: My_basic interpreter

Post by angros47 »

I was thinking to create a programming environment in FreeBasic, using My Basic as an interpreter. FreeBasic would be used to create the window, and to provide input. I wonder how should I manage the code input: one option is to make the program only able to read and execute a source code from a file (or from a pipe). Another is to make a simple text editor in FreeBasic, perhaps in text more, perhaps in graphic mode. I need ideas. Any suggestions?

Such an environment could be also integrated with OpenB3D, providing the ability to control it from a Basic script
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: My_basic interpreter

Post by BasicCoder2 »

I confess I am ignorant of these "scripting" languages and their utility value. How is it different to a normal BASIC interpreter?

It is unclear what you want to do that is different to what is already available?

What sort of programming project would require a scripting language?
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: My_basic interpreter

Post by angros47 »

Actually, "scripting" means the purpose a language is created for. While "interpreter" means the method used by that language to work.

What I would like to do is: since my_basic has already been used to create some interesting programs, like https://paladin-t.github.io/b8/ and https://my-basic.github.io/awesome/, I'd like to try making something similar, using FreeBasic and OpenB3D, perhaps
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: My_basic interpreter

Post by angros47 »

With the latest version, the API has changed a little. Here is the upgraded FreeBasic header:

Code: Select all

#define MB_CODES
#define MB_FUNC_OK 0
#define MB_FUNC_IGNORE 1
#define MB_FUNC_WARNING 2
#define MB_FUNC_ERR 3
#define MB_FUNC_BYE 4
#define MB_FUNC_SUSPEND 5
#define MB_FUNC_END 6
#define MB_LOOP_BREAK 101
#define MB_LOOP_CONTINUE 102
#define MB_SUB_RETURN 103
#define MB_EXTENDED_ABORT 201

enum mb_error_e
   SE_NO_ERR = 0,
   ' Common
   SE_CM_FUNC_EXISTS,
   SE_CM_FUNC_NOT_EXISTS,
   SE_CM_NOT_SUPPORTED,
   ' Parsing
   SE_PS_OPEN_FILE_FAILED,
   SE_PS_SYMBOL_TOO_LONG,
   SE_PS_INVALID_CHAR,
   SE_PS_INVALID_MODULE,
   SE_PS_DUPLICATE_IMPORT,
   ' Running
   SE_RN_EMPTY_PROGRAM,
   SE_RN_PROGRAM_TOO_LONG,
   SE_RN_SYNTAX_ERROR,
   SE_RN_OUT_OF_MEMORY,
   SE_RN_OVERFLOW,
   SE_RN_UNEXPECTED_TYPE,
   SE_RN_INVALID_STRING,
   SE_RN_INTEGER_EXPECTED,
   SE_RN_NUMBER_EXPECTED,
   SE_RN_STRING_EXPECTED,
   SE_RN_VAR_EXPECTED,
   SE_RN_INDEX_OUT_OF_BOUND,
   SE_RN_CANNOT_FIND_WITH_GIVEN_INDEX,
   SE_RN_TOO_MANY_DIMENSIONS,
   SE_RN_RANK_OUT_OF_BOUND,
   SE_RN_INVALID_ID_USAGE,
   SE_RN_CANNOT_ASSIGN_TO_RESERVED_WORD,
   SE_RN_DUPLICATE_ID,
   SE_RN_INCOMPLETE_STRUCTURE,
   SE_RN_LABEL_NOT_EXISTS,
   SE_RN_NO_RETURN_POINT,
   SE_RN_COLON_EXPECTED,
   SE_RN_COMMA_EXPECTED,
   SE_RN_COMMA_OR_SEMICOLON_EXPECTED,
   SE_RN_OPEN_BRACKET_EXPECTED,
   SE_RN_CLOSE_BRACKET_EXPECTED,
   SE_RN_NESTED_TOO_MUCH,
   SE_RN_OPERATION_FAILED,
   SE_RN_OPERATOR_EXPECTED,
   SE_RN_ASSIGN_OPERATOR_EXPECTED,
   SE_RN_THEN_EXPECTED,
   SE_RN_ELSE_EXPECTED,
   SE_RN_ENDIF_EXPECTED,
   SE_RN_TO_EXPECTED,
   SE_RN_NEXT_EXPECTED,
   SE_RN_UNTIL_EXPECTED,
   SE_RN_LOOP_VAR_EXPECTED,
   SE_RN_JUMP_LABEL_EXPECTED,
   SE_RN_CALCULATION_ERROR,
   SE_RN_INVALID_EXPRESSION,
   SE_RN_DIVIDE_BY_ZERO,
   SE_RN_REACHED_TO_WRONG_FUNCTION,
   SE_RN_CANNOT_SUSPEND_HERE,
   SE_RN_CANNOT_MIX_INSTRUCTIONAL_AND_STRUCTURED,
   SE_RN_INVALID_ROUTINE,
   SE_RN_ROUTINE_EXPECTED,
   SE_RN_DUPLICATE_ROUTINE,
   SE_RN_INVALID_CLASS,
   SE_RN_CLASS_EXPECTED,
   SE_RN_DUPLICATE_CLASS,
   SE_RN_HASH_AND_COMPARE_MUST_BE_PROVIDED_TOGETHER,
   SE_RN_INVALID_LAMBDA,
   SE_RN_EMPTY_COLLECTION,
   SE_RN_LIST_EXPECTED,
   SE_RN_INVALID_ITERATOR,
   SE_RN_ITERABLE_EXPECTED,
   SE_RN_COLLECTION_EXPECTED,
   SE_RN_COLLECTION_OR_ITERATOR_EXPECTED,
   SE_RN_REFERENCED_TYPE_EXPECTED,
   ' Extended abort
   SE_EA_EXTENDED_ABORT,
   ' Extra
   SE_COUNT
end enum

enum mb_data_e
   MB_DT_NIL = 0,
   MB_DT_UNKNOWN = 1 shl 0,
   MB_DT_INT = 1 shl 1,
   MB_DT_REAL = 1 shl 2,
   MB_DT_NUM = MB_DT_INT or MB_DT_REAL,
   MB_DT_STRING = 1 shl 3,
   MB_DT_TYPE = 1 shl 4,
   MB_DT_USERTYPE = 1 shl 5,

   MB_DT_ARRAY = 1 shl 7,
   MB_DT_ROUTINE = 1 shl 13
end enum

enum mb_meta_func_e
   MB_MF_IS = 1 shl 0,
   MB_MF_ADD = 1 shl 1,
   MB_MF_SUB = 1 shl 2,
   MB_MF_MUL = 1 shl 3,
   MB_MF_DIV = 1 shl 4,
   MB_MF_NEG = 1 shl 5,
   MB_MF_CALC = MB_MF_IS or MB_MF_ADD or MB_MF_SUB or MB_MF_MUL or MB_MF_DIV or MB_MF_NEG,
   MB_MF_COLL = 1 shl 6,
   MB_MF_FUNC = 1 shl 7
end enum

enum mb_meta_status_e
   MB_MS_NONE = 0,
   MB_MS_DONE = 1 shl 0,
   MB_MS_RETURNED = 1 shl 1
end enum

enum mb_routine_type_e
   MB_RT_NONE,
   MB_RT_SCRIPT,
   MB_RT_LAMBDA,
   MB_RT_NATIVE
end enum

union mb_value_u field = 1
   integer_ as integer
   float_point as single
   string_ as zstring ptr
   type_ as long'mb_data_e
   usertype as any ptr
   array as any ptr
   routine as any ptr
   bytes as unsigned byte
end union

type mb_value_t field = 1
   type_ as long'mb_data_e
   value as mb_value_u
end type


extern "C"
declare function mb_init() as integer
declare function mb_dispose() as integer
declare function mb_open(byref s as any ptr) as integer
declare function mb_close(byref s as any ptr) as integer
declare function mb_reset(byref s as any ptr, clear_funcs as unsigned byte, clear_vars as unsigned byte) as integer

declare function mb_fork(byref s as any ptr, r as any ptr, clear_forked as unsigned byte) as integer
declare function mb_join(byref s as any ptr) as integer
declare function mb_get_forked_from(s as any ptr, byref src as any ptr) as integer

declare function mb_register_func(s as any ptr, n as zstring ptr, f as function cdecl (as any ptr, byref as any ptr) as integer) as integer
declare function mb_remove_func(s as any ptr, n as zstring ptr) as integer
declare function mb_remove_reserved_func(s as any ptr, n as zstring ptr) as integer
declare function mb_begin_module(s as any ptr, n as zstring ptr) as integer
declare function mb_end_module(s as any ptr) as integer

declare function mb_attempt_func_begin(s as any ptr, byref l as any ptr) as integer
declare function mb_attempt_func_end(s as any ptr, byref l as any ptr) as integer
declare function mb_attempt_open_bracket(s as any ptr, byref l as any ptr) as integer
declare function mb_attempt_close_bracket(s as any ptr, byref l as any ptr) as integer
declare function mb_has_arg(s as any ptr, byref l as any ptr) as integer
declare function mb_pop_int(s as any ptr, byref l as any ptr, byref val_ as integer) as integer
declare function mb_pop_real(s as any ptr, byref l as any ptr, byref val_ as single) as integer
declare function mb_pop_string(s as any ptr, byref l as any ptr, byref val_ as zstring ptr) as integer
declare function mb_pop_usertype(s as any ptr, byref l as any ptr, byref val_ as any ptr) as integer
declare function mb_pop_value(s as any ptr, byref l as any ptr, byref val_ as mb_value_t) as integer
declare function mb_push_int(s as any ptr, byref l as any ptr, val_ as integer) as integer
declare function mb_push_real(s as any ptr, byref l as any ptr, val_ as single) as integer
declare function mb_push_string(s as any ptr, byref l as any ptr, val_ as zstring ptr) as integer
declare function mb_push_usertype(s as any ptr, byref l as any ptr, val_ as any ptr) as integer
declare function mb_push_value(s as any ptr, byref l as any ptr, val_ as mb_value_t) as integer

declare function mb_begin_class(s as any ptr, byref l as any ptr, n as zstring ptr, byref meta as mb_value_t ptr, c as integer, byref out_ as mb_value_t) as integer
declare function mb_end_class(s as any ptr, byref l as any ptr) as integer
declare function mb_get_class_userdata(s as any ptr, byref l as any ptr, byref d as any ptr) as integer
declare function mb_set_class_userdata(s as any ptr, byref l as any ptr, byref d as any ptr) as integer

declare function mb_get_value_by_name(s as any ptr, byref l as any ptr, n as zstring ptr, byref val_ as mb_value_t) as integer
declare function mb_get_vars(s as any ptr,  byref l as any ptr, r as sub cdecl(as any ptr, as zstring ptr, as integer) , stack_offset as integer) as integer
declare function mb_add_var(s as any ptr, byref l as any ptr, n as zstring ptr, val_ as mb_value_t, force as unsigned byte) as integer
declare function mb_get_var(s as any ptr, byref l as any ptr, byref v as any ptr, redir as unsigned byte) as integer
declare function mb_get_var_name(s as any ptr, v as any ptr, byref n as zstring ptr) as integer
declare function mb_get_var_value(s as any ptr, v as any ptr, byref val_ as mb_value_t) as integer
declare function mb_set_var_value(s as any ptr, v as any ptr, val as mb_value_t) as integer
declare function mb_init_array(s as any ptr, byref l as any ptr, t as mb_data_e, byref d as integer, c as integer, byref a as any ptr) as integer
declare function mb_get_array_len(s as any ptr, byref l as any ptr, a as any ptr, r as integer, byref i as integer) as integer
declare function mb_get_array_elem(s as any ptr, byref l as any ptr, a as any ptr, byref d as integer, c as integer, byref val_ as mb_value_t) as integer
declare function mb_set_array_elem(s as any ptr, byref l as any ptr, a as any ptr, byref d as integer, c as integer, val_ as mb_value_t) as integer
declare function mb_init_coll(s as any ptr, byref l as any ptr, byref coll as mb_value_t) as integer
declare function mb_get_coll(s as any ptr, byref l as any ptr, coll as mb_value_t, idx as mb_value_t, byref val_ as mb_value_t) as integer
declare function mb_set_coll(s as any ptr, byref l as any ptr, coll as mb_value_t, idx as mb_value_t, val_ as mb_value_t) as integer
declare function mb_remove_coll(s as any ptr, byref l as any ptr, coll as mb_value_t, idx as mb_value_t) as integer
declare function mb_count_coll(s as any ptr, byref l as any ptr, coll as mb_value_t, byref c as integer) as integer
declare function mb_keys_of_coll(s as any ptr, byref l as any ptr, coll as mb_value_t, byref keys as mb_value_t, c as integer) as integer
declare function mb_make_ref_value(s as any ptr, val_ as any ptr, byref out_ as mb_value_t, un as sub cdecl (as any ptr, as any ptr), cl as function cdecl (as any ptr, as any ptr) as any ptr, hs as function cdecl (as any ptr, as any ptr) as unsigned integer, cp as function cdecl (as any ptr, as any ptr, as any ptr) as integer, ft as function cdecl (as any ptr, as any ptr, as zstring ptr, as unsigned integer) as integer) as integer
declare function mb_get_ref_value(s as any ptr, byref l as any ptr, val_ as mb_value_t, byref out_ as any ptr) as integer
declare function mb_ref_value(s as any ptr, byref l as any ptr, val_ as mb_value_t) as integer
declare function mb_unref_value(s as any ptr, byref l as any ptr, val_ as mb_value_t) as integer
declare function mb_set_alive_checker(s as any ptr, f as sub cdecl (as any ptr, as any ptr, as sub cdecl (as any ptr, as any ptr, as mb_value_t))) as integer
declare function mb_set_alive_checker_of_value(s as any ptr, byref l as any ptr, val as mb_value_t, f as sub cdecl (as any ptr, as any ptr, as mb_value_t, as sub cdecl (as any ptr, as any ptr, as mb_value_t))) as integer
declare function mb_override_value(s as any ptr, byref l as any ptr, val_ as mb_value_t, m as mb_meta_func_e, f as any ptr) as integer
declare function mb_dispose_value(s as any ptr, val_ as mb_value_t) as integer

declare function mb_get_routine(s as any ptr, byref l as any ptr, n as zstring ptr, byref val_ as mb_value_t) as integer
declare function mb_set_routine(s as any ptr, byref l as any ptr, n as zstring ptr, f as function cdecl(as any ptr, byref as any ptr, byref as mb_value_t, as unsigned integer, as any ptr, as function cdecl (as any ptr, byref as any ptr, byref as mb_value_t, as unsigned integer, byref as unsigned integer, as any ptr) as integer, as function cdecl (as any ptr, byref as any ptr, byref as mb_value_t, as unsigned integer, byref as unsigned integer, as any ptr, byref as mb_value_t) as integer) as integer, force as unsigned byte) as integer
declare function mb_eval_routine(s as any ptr, byref l as any ptr, val_ as mb_value_t, args as mb_value_t, argc as unsigned integer, byref ret_ as mb_value_t) as integer
declare function mb_get_routine_type(s as any ptr, val_ as mb_value_t, byref y as mb_routine_type_e) as integer

declare function mb_load_string(s as any ptr, l as zstring ptr, reset as unsigned byte) as integer
declare function mb_load_file(s as any ptr, f as zstring ptr) as integer
declare function mb_run(s as any ptr, clear_parser as unsigned byte) as integer
declare function mb_suspend(ss as any ptr, byref l as any ptr) as integer
declare function mb_schedule_suspend(s as any ptr, t as integer) as integer

declare function mb_debug_get(s as any ptr, n as zstring ptr, byref val_ as mb_value_t) as integer
declare function mb_debug_set(s as any ptr, n as zstring ptr, val_ as mb_value_t) as integer
declare function mb_debug_count_stack_frames(s as any ptr) as integer
declare function mb_debug_get_stack_trace(s as any ptr, byref fs as zstring ptr, fc as unsigned integer) as integer
declare function mb_debug_set_stepped_handler(s as any ptr, prev as function cdecl (as any ptr, byref as any ptr, as zstring ptr, as integer, as unsigned short, as unsigned short) as integer, post as function cdecl (as any ptr, byref as any ptr, as zstring ptr, as integer, as unsigned short, as unsigned short) as integer) as integer

declare function mb_get_type_string(t as mb_data_e) as zstring ptr

declare function mb_raise_error(s as any ptr, byref l as any ptr, err_ as mb_error_e, ret_ as integer) as integer
declare function mb_get_last_error(s as any ptr, byref file as zstring ptr, byref pos_ as integer, byref row_ as unsigned short, byref col_ as unsigned short) as mb_error_e
declare function mb_get_error_desc(err_ as mb_error_e) as zstring ptr
declare function mb_set_error_handler(s as any ptr, h as sub cdecl (as any ptr, as mb_error_e, as zstring ptr, as zstring ptr, as integer, as unsigned short, as unsigned short, as integer)) as integer

declare function mb_set_printer(s as any ptr, p as function cdecl(as any ptr, as zstring ptr, ...) as integer) as integer
declare function mb_set_inputer(s as any ptr, p as function cdecl(as any ptr, as zstring ptr, as zstring ptr, as integer) as integer) as integer

declare function mb_set_import_handler(s as any ptr, h as function cdecl(as any ptr, as zstring ptr) as integer) as integer
declare function mb_set_memory_manager(a as function cdecl (as unsigned integer) as ubyte ptr, f as sub cdecl (as ubyte ptr)) as integer
declare function mb_get_gc_enabled(s as any ptr) as unsigned byte
declare function mb_set_gc_enabled(s as any ptr, gc as unsigned byte) as integer
declare function mb_gc(s as any ptr, byref collected as integer) as integer
declare function mb_get_userdata(s as any ptr, byref d as any ptr) as integer
declare function mb_set_userdata(s as any ptr, d as any ptr) as integer
declare function mb_gets(s as any ptr, pmt as zstring ptr, buf as ubyte ptr, n as integer) as integer
declare function mb_memdup(val_ as zstring ptr, size as unsigned integer) as zstring ptr
end extern

Edit: fixed a bug, thanks to srvaldez
Post Reply