Author Topic: MY-BASIC  (Read 118076 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #105 on: October 19, 2015, 03:04:53 PM »
I can confirm.

Code: ScriptBasic
  1. DECLARE SUB mb_init ALIAS "mbas_init" LIB "mb"
  2. DECLARE SUB mb_dispose ALIAS "mbas_dispose" LIB "mb"
  3. DECLARE SUB mb_open ALIAS "mbas_open" LIB "mb"
  4. DECLARE SUB mb_close ALIAS "mbas_close" LIB "mb"
  5. DECLARE SUB mb_load_str ALIAS "mbas_load_str" LIB "mb"
  6. DECLARE SUB mb_run ALIAS "mbas_run" LIB "mb"
  7.  
  8. mb_code = """
  9. def bar(nul)
  10.  PRINT "In parameteless SUB";
  11. enddef
  12.  
  13. bar()
  14. """
  15. mb_init()
  16. mb_open()
  17. mb_load_str(mb_code)
  18. mb_run()
  19. mb_close()
  20. mb_dispose()
  21.  


jrs@laptop:~/sb/sb22/mybasic$ scriba mbnoprams.sb
In parameteless SUB
jrs@laptop:~/sb/sb22/mybasic$

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #106 on: October 20, 2015, 01:24:26 AM »
Anything that is a valid variable will work as a placeholder.

Code: ScriptBasic
  1. DECLARE SUB mb_init ALIAS "mbas_init" LIB "mb"
  2. DECLARE SUB mb_dispose ALIAS "mbas_dispose" LIB "mb"
  3. DECLARE SUB mb_open ALIAS "mbas_open" LIB "mb"
  4. DECLARE SUB mb_close ALIAS "mbas_close" LIB "mb"
  5. DECLARE SUB mb_load_str ALIAS "mbas_load_str" LIB "mb"
  6. DECLARE SUB mb_run ALIAS "mbas_run" LIB "mb"
  7.  
  8. mb_code = """
  9. def bar(void)
  10.  PRINT void;
  11. enddef
  12.  
  13. bar(69)
  14. """
  15. mb_init()
  16. mb_open()
  17. mb_load_str(mb_code)
  18. mb_run()
  19. mb_close()
  20. mb_dispose()
  21.  


jrs@laptop:~/sb/sb22/mybasic$ scriba mbnoprams.sb
69
jrs@laptop:~/sb/sb22/mybasic$

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #107 on: October 20, 2015, 05:12:32 AM »
Hi guys,

I've figured out it's a bug, and fixed in latest revision. I think test coverage for new features are increasing, thank you.

I also added many new features recently, but not documented yet, such as generic LIST, DICT collections, garbage collection management for a new referenced usertype, and evaluating sub from C.

A simple LIST, DICT usage eg:

Code: [Select]
l = list(1, 2, 3)
d = dict("hah", l)
c = clone(d)
print get(get(c, "hah"), 0);
i = iterator(get(c, "hah"))
while move_next(i)
    print get(i);
wend

The memory is managed by Reference Counter and GC, so don't worry about leak or reference cycles.

Another example for referenced usertype. Assuming a FOO defined:

Code: C
  1. static void _str_dtor(struct mb_interpreter_t* s, void* ptr) {
  2.         char* str = (char*)ptr;
  3.  
  4.         mb_assert(s);
  5.  
  6.         free(str);
  7. }
  8.  
  9. static void* _str_clone(struct mb_interpreter_t* s, void* ptr) {
  10.         char* str = (char*)ptr;
  11.  
  12.         mb_assert(s);
  13.  
  14.         ptr = malloc(strlen(str) + 1);
  15.         memcpy(ptr, str, strlen(str) + 1);
  16.  
  17.         return ptr;
  18. }
  19.  
  20. static void _str_fmt(struct mb_interpreter_t* s, void* ptr, mb_print_func_t f) {
  21.         char* str = (char*)ptr;
  22.  
  23.         mb_assert(s);
  24.  
  25.         f(str);
  26. }
  27.  
  28. static int foo(struct mb_interpreter_t* s, void** l) {
  29.         int result = MB_FUNC_OK;
  30.         char* str = 0;
  31.         mb_value_t ret;
  32.  
  33.         mb_assert(s && l);
  34.  
  35.         mb_check(mb_attempt_open_bracket(s, l));
  36.         mb_check(mb_attempt_close_bracket(s, l));
  37.  
  38.         str = (char*)malloc(16);
  39.         memcpy(str, "hello", 6);
  40.         mb_check(mb_make_ref_value(s, str, &ret, _str_dtor, _str_clone, 0, 0, _str_fmt));
  41.         mb_check(mb_push_value(s, l, ret));
  42.  
  43.         return result;
  44. }
  45.  

We could use it as:

Code: [Select]
l = list()
push(l, foo())
a = clone(l)
l = nil
print pop(a);

A referenced usertype is also benefited by RC and GC.
« Last Edit: October 20, 2015, 05:20:18 AM by wangrenxin »

Offline Cybermonkey342

  • BASIC Developer
  • Posts: 34
Re: MY-BASIC
« Reply #108 on: October 20, 2015, 01:39:15 PM »
Is there anything I have to call now additionally to the mb_close and mb_dispose functions? Now everything works fine except when the interpreter closes then I got a memory allocation error. (I think it's that, because I got a German error description: "Speicherzugriffsfehler".)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #109 on: October 20, 2015, 09:51:24 PM »
I'm seeing the same thing on exit. On a positive note the SUB now works without passing an argument.

Code: ScriptBasic
  1. DECLARE SUB mb_init ALIAS "mbas_init" LIB "mb"
  2. DECLARE SUB mb_dispose ALIAS "mbas_dispose" LIB "mb"
  3. DECLARE SUB mb_open ALIAS "mbas_open" LIB "mb"
  4. DECLARE SUB mb_close ALIAS "mbas_close" LIB "mb"
  5. DECLARE SUB mb_load_str ALIAS "mbas_load_str" LIB "mb"
  6. DECLARE SUB mb_run ALIAS "mbas_run" LIB "mb"
  7.  
  8. mb_code = """
  9. def bar()
  10.  PRINT "It Works!";
  11. enddef
  12.  
  13. bar()
  14. """
  15. mb_init()
  16. mb_open()
  17. mb_load_str(mb_code)
  18. mb_run()
  19. mb_close()
  20. mb_dispose()
  21.  


jrs@laptop:~/sb/sb22/mybasic$ scriba mbnoprams.sb
It Works!
Segmentation fault (core dumped)
jrs@laptop:~/sb/sb22/mybasic$


Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #110 on: October 20, 2015, 10:14:19 PM »
Cybermonkey342,

Will I be able to embed AllegroBASIC in Script BASIC like Tony's version?

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #111 on: October 21, 2015, 12:02:11 AM »
Is there anything I have to call now additionally to the mb_close and mb_dispose functions? Now everything works fine except when the interpreter closes then I got a memory allocation error. (I think it's that, because I got a German error description: "Speicherzugriffsfehler".)

Nothing additionally is needed, I didn't change "mb_xxx" function disciplines. Try the latest revision in which I've corrected some uninitialized memory issues please, it might solve the issue. It doesn't say any error when exiting, I compiled it with VC, PellesC, TCC on Windows, I'll test it with GCC and Clang on Linux and OSX later. Did it say "Speicherzugriffsfehler" with GCC?

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #112 on: October 21, 2015, 03:11:04 AM »
Good: It no longer errors on exit.

Bad: It no longer executes the parameterless SUB like it did in the last round.

Code: Text
  1. def bar()
  2.   PRINT "It Works!";
  3. enddef
  4.  
  5. bar()
  6.  

Prints nothing.
« Last Edit: October 21, 2015, 03:13:10 AM by John »

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #113 on: October 21, 2015, 04:36:48 AM »
John,
The parameterless SUB works fine on my machine with your sample code, have you tried to rebuild it?

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #114 on: October 21, 2015, 10:09:49 AM »
Your lastest version doesn't execute any code for me. (SUB or not) It doesn't error on exit like the previous version though.

I don't know what to say.

Offline Cybermonkey342

  • BASIC Developer
  • Posts: 34
Re: MY-BASIC
« Reply #115 on: October 21, 2015, 01:44:44 PM »
Is there anything I have to call now additionally to the mb_close and mb_dispose functions? Now everything works fine except when the interpreter closes then I got a memory allocation error. (I think it's that, because I got a German error description: "Speicherzugriffsfehler".)

Nothing additionally is needed, I didn't change "mb_xxx" function disciplines. Try the latest revision in which I've corrected some uninitialized memory issues please, it might solve the issue. It doesn't say any error when exiting, I compiled it with VC, PellesC, TCC on Windows, I'll test it with GCC and Clang on Linux and OSX later. Did it say "Speicherzugriffsfehler" with GCC?
Ah, "segmentation fault" is the correct term. Sorry, guys. I just compiled the latest version and still got the same error message. I compiled it with TCC.
EDIT: I got the same seg fault compiling it with GCC. That's on a Linux x64.
« Last Edit: October 21, 2015, 01:49:27 PM by Cybermonkey342 »

Offline Cybermonkey342

  • BASIC Developer
  • Posts: 34
Re: MY-BASIC
« Reply #116 on: October 21, 2015, 01:47:21 PM »
Cybermonkey342,

Will I be able to embed AllegroBASIC in Script BASIC like Tony's version?
Hm, I don't think so. AllegroBASIC is just a programm which calls a few Allegro functions and bind them to MY-BASIC.
That's the main function:
Code: [Select]
int main(int argc, char **argv)
{
atexit(_on_exit);
_on_startup();

if (argc>1) {
if(mb_load_file(bas, argv[1]) == MB_FUNC_OK)
mb_run(bas);
}
else {
printf ("AllegroBASIC 0.2\nUsage: allegrobasic[.exe] script.bas.\n");
}

return 0;
}

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #117 on: October 21, 2015, 06:59:52 PM »
Hi guys,

I got what's going wrong, there's a difference, you are probably using standard memory "malloc" "free" function, whereas I'm using the memory pool in "main.c" which doesn't fill released memory with any value, and it won't trigger error when accessing already released memory.

I'll deal with it. For the moment uncomment the "MB_ENABLE_GC" macro in "my_basic.h" to get around it.
« Last Edit: October 21, 2015, 07:12:37 PM by wangrenxin »

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #118 on: October 21, 2015, 08:29:04 PM »
Hi all,

Fixed the seg fault with GC now.

FYI. it's recommended to use a memory pool (or object pool) instead of raw allocator. MB will generate many small objects which could be a bottleneck and cause memory fragments. A pool will relieve this situation. With the simple code below:

Code: [Select]
t = ticks()
for i = 0 to 10000000
s = s + i
next
t = ticks() - t
print t / 1000, "s";

On i7-4790, it takes ~4.3s with the pool in "main.c", ~11.1s without it.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #119 on: October 21, 2015, 08:42:37 PM »
Quote
I'll deal with it. For the moment uncomment the "MB_ENABLE_GC" macro in "my_basic.h" to get around it.

There is no such code in my_basic.h.

I will try your latest fix and see if things improve.