Author Topic: MY-BASIC  (Read 118116 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #30 on: April 21, 2015, 09:43:55 AM »
Here is the Script BASIC only equivalent.

Code: ScriptBasic
  1. FOR i = 3 TO 5000
  2.   FOR j = 2 TO SQR(i)
  3.     IF i % j = 0 THEN GOTO nDone
  4.   NEXT
  5.   PRINT i," "
  6.   nDone:
  7. NEXT
  8. PRINTNL
  9.  


jrs@laptop:~/sb/sb22/test$ time scriba pnsqr2.sb
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4903 4909 4919 4931 4933 4937 4943 4951 4957 4967 4969 4973 4987 4993 4999

real   0m0.034s
user   0m0.028s
sys   0m0.004s
jrs@laptop:~/sb/sb22/test$


Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #31 on: April 21, 2015, 12:13:02 PM »
@Tony -


Q. What is the best way to print a new line? I noticed that if I use PRINT alone on a line, MY-BASIC complains.

A. If you end the PRINT line with a comma, a newline will be generated.

Q. I'm assuming at this point that MY-BASIC doesn't support FUNCTION/SUB ?


Comment: I had to get use to the older style IF statement. Script BASIC doesn't support multiple statements separated by :'s.
 
« Last Edit: April 21, 2015, 02:19:31 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #32 on: April 21, 2015, 03:45:50 PM »
I added the mb_load_file and mb_reset functions to the extension module.

Code: C
  1. // MY-BASIC - Script BASIC extension module
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include <ctype.h>
  8. #include <math.h>
  9. #include <time.h>
  10. #include "../../basext.h"
  11. #include "cbasic.h"
  12.  
  13. #include "my_basic.h"
  14.  
  15.  
  16. /****************************
  17.  Extension Module Functions
  18. ****************************/
  19.  
  20. besVERSION_NEGOTIATE
  21.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  22. besEND
  23.  
  24. besSUB_START
  25.   DIM AS long PTR p;
  26.   besMODULEPOINTER = besALLOC(sizeof(long));
  27.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  28.   p = (long PTR)besMODULEPOINTER;
  29.   RETURN_FUNCTION(0);
  30. besEND
  31.  
  32. besSUB_FINISH
  33.   DIM AS long PTR p;
  34.   p = (long PTR)besMODULEPOINTER;
  35.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  36.   RETURN_FUNCTION(0);
  37. besEND
  38.  
  39.  
  40. /********************
  41.  MY-BASIC Functions
  42. ********************/
  43.  
  44. static struct mb_interpreter_t* bas = 0;
  45.  
  46. besFUNCTION(mbas_init)
  47.   besRETURN_LONG(mb_init());
  48. besEND
  49.  
  50. besFUNCTION(mbas_dispose)
  51.   besRETURN_LONG(mb_dispose());
  52. besEND
  53.  
  54. besFUNCTION(mbas_open)
  55.   besRETURN_LONG(mb_open(AT bas));
  56. besEND
  57.  
  58. besFUNCTION(mbas_close)
  59.   besRETURN_LONG(mb_close(AT bas));
  60. besEND
  61.  
  62. besFUNCTION(mbas_load_str)
  63.   DIM AS const char PTR pgm;
  64.   besARGUMENTS("z")
  65.     AT pgm
  66.   besARGEND
  67.   besRETURN_LONG(mb_load_string(bas, pgm));
  68. besEND
  69.  
  70. besFUNCTION(mbas_load_file)
  71.   DIM AS const char PTR pgm;
  72.   besARGUMENTS("z")
  73.     AT pgm
  74.   besARGEND
  75.   besRETURN_LONG(mb_load_file(bas, pgm));
  76. besEND
  77.  
  78. besFUNCTION(mbas_run)
  79.   besRETURN_LONG(mb_run(bas));
  80. besEND
  81.  
  82. besFUNCTION(mbas_reset)
  83.   besRETURN_LONG(mb_reset(bas, false));
  84. besEND
  85.  

mbprime.sb
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_load_file ALIAS "mbas_load_file" LIB "mb"
  7. DECLARE SUB mb_run ALIAS "mbas_run" LIB "mb"
  8. DECLARE SUB mb_reset ALIAS "mbas_reset" LIB "mb"
  9.  
  10. mb_init()
  11. mb_open()
  12. mb_load_file("mbprime.bas")
  13. mb_run()
  14. mb_close()
  15. mb_dispose()
  16.  

mbprime.bas (MY-BASIC)
Code: [Select]
FOR i = 3 TO 5000
  f = 1
  FOR j = 2 TO SQR(i)
    IF i MOD j = 0 THEN f = 0 : EXIT
  NEXT j
  IF f = 1 THEN PRINT i," "
NEXT i
PRINT ,


Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #33 on: April 21, 2015, 04:08:04 PM »
If this is going to be of any use, Script BASIC needs to be able to at the minimum retrieve MY-BASIC variables. Can someone post a C example of doing this?

Is there a way I could return back to Script BASIC the current program in MY-BASIC workspace? (LIST to string variable)
« Last Edit: April 21, 2015, 07:16:53 PM by John »

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #34 on: April 21, 2015, 07:17:44 PM »
@John

1. In MY-BAIC use semicolon like 'PRINT ;' to generate a new line.
2. MB supports GOTO/GOSUB/RETURN, doesn't support FUNCTION/SUB routine yet.
3. Use mb_pop_XXX to retrieve values, and mb_push_XXX to return values, see the file extension https://github.com/paladin-t/my_basic/wiki/File-module for demo; search for 'mb_pop_'.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #35 on: April 21, 2015, 07:28:46 PM »
Quote
3. Use mb_pop_XXX to retrieve values, and mb_push_XXX to return values, see the file extension https://github.com/paladin-t/my_basic/wiki/File-module for demo; search for 'mb_pop_'.

Lets say I run a MY-BASIC script which sets an integer variable called myint. Once SB gets control again after the mb_run, I would like to get the value of myint from Script BASIC.

Can you post a C code snippet showing how to do the above?

I see no reference showing a script being run and getting a variable by name.
« Last Edit: April 21, 2015, 08:26:08 PM by John »

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #36 on: April 21, 2015, 09:21:53 PM »
You can't get a specific variable in C, but you can pass a variable from script.

Step 1. Write a C function:
Code: C
  1.         int watch(struct mb_interpreter_t* s, void** l) {
  2.                 int result = MB_FUNC_OK;
  3.                 int_t arg = 0;
  4.  
  5.                 mb_assert(s && l);
  6.  
  7.                 mb_check(mb_attempt_open_bracket(s, l));
  8.  
  9.                 mb_check(mb_pop_int(s, l, &arg)); // That's it!
  10.  
  11.                 mb_check(mb_attempt_close_bracket(s, l));
  12.  
  13.                 // arg is what you want.
  14.  
  15.                 return result;
  16.         }
  17.  

Step 2. Register it: mb_reg_fun(bas, watch);

Step 3. Now call it wherever in your script to watch a value of an expression:
Code: Text
  1.         WATCH(FOO)
  2.         WATCH(A + B * C)
  3.         WATCH(123)
  4.         ...
  5.  

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #37 on: April 21, 2015, 09:40:50 PM »
void** l

What is l?

From Script BASIC I assume I'm passing a string constant  to my watch wrapped function which I assume is void** l.

You have seen the source to the Script BASIC MY-BASIC extension module. Can cobble together a function that would work within that environment? This is a surprise. I would of thought getting and setting of variables is core functionality when offering an embedded solution.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #38 on: April 21, 2015, 09:58:23 PM »
@Tony - Are the functions in the PDF for the C programmer to create, set, read MY-BASIC variables from the host language? If that is true, can't I create these variables first and assign them with the MY-BASIC script? I find it hard to believe you would create an embeddable BASIC and not allow the host and MY-BASIC to communicate. (passing variables between them) Remember you have no IO functionality to fall back on.


Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #39 on: April 21, 2015, 10:30:41 PM »
I gave it a shot trying to create a mb_getint() function but I'm back to the assert issue.

Code: C
  1. // MY-BASIC - Script BASIC extension module
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdarg.h>
  7. #include <ctype.h>
  8. #include <math.h>
  9. #include <time.h>
  10. #include "../../basext.h"
  11. #include "cbasic.h"
  12.  
  13. #include "my_basic.h"
  14.  
  15.  
  16. /****************************
  17.  Extension Module Functions
  18. ****************************/
  19.  
  20. besVERSION_NEGOTIATE
  21.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  22. besEND
  23.  
  24. besSUB_START
  25.   DIM AS long PTR p;
  26.   besMODULEPOINTER = besALLOC(sizeof(long));
  27.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  28.   p = (long PTR)besMODULEPOINTER;
  29.   RETURN_FUNCTION(0);
  30. besEND
  31.  
  32. besSUB_FINISH
  33.   DIM AS long PTR p;
  34.   p = (long PTR)besMODULEPOINTER;
  35.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  36.   RETURN_FUNCTION(0);
  37. besEND
  38.  
  39.  
  40. /********************
  41.  MY-BASIC Functions
  42. ********************/
  43.  
  44. static struct mb_interpreter_t* bas = 0;
  45.  
  46. static int watch(struct mb_interpreter_t* s, void** l) {
  47.   int result = MB_FUNC_OK;
  48.   int_t arg = 0;
  49.   mb_assert(s && l);
  50.   mb_check(mb_attempt_open_bracket(s, l));
  51.   mb_check(mb_pop_int(s, l, &arg)); // That's it!
  52.   mb_check(mb_attempt_close_bracket(s, l));
  53.   // arg is what you want.
  54.   return result;
  55. }
  56.  
  57. besFUNCTION(mbas_init)
  58.   besRETURN_LONG(mb_init());
  59. besEND
  60.  
  61. besFUNCTION(mbas_dispose)
  62.   besRETURN_LONG(mb_dispose());
  63. besEND
  64.  
  65. besFUNCTION(mbas_open)
  66.   besRETURN_LONG(mb_open(AT bas));
  67. besEND
  68.  
  69. besFUNCTION(mbas_close)
  70.   besRETURN_LONG(mb_close(AT bas));
  71. besEND
  72.  
  73. besFUNCTION(mbas_load_str)
  74.   DIM AS const char PTR pgm;
  75.   besARGUMENTS("z")
  76.     AT pgm
  77.   besARGEND
  78.   besRETURN_LONG(mb_load_string(bas, pgm));
  79. besEND
  80.  
  81. besFUNCTION(mbas_load_file)
  82.   DIM AS const char PTR pgm;
  83.   besARGUMENTS("z")
  84.     AT pgm
  85.   besARGEND
  86.   besRETURN_LONG(mb_load_file(bas, pgm));
  87. besEND
  88.  
  89. besFUNCTION(mbas_run)
  90.   besRETURN_LONG(mb_run(bas));
  91. besEND
  92.  
  93. besFUNCTION(mbas_reset)
  94.   besRETURN_LONG(mb_reset(bas, false));
  95. besEND
  96.  
  97. besFUNCTION(mbas_getint)
  98.   DIM AS const char PTR expr;
  99.   besARGUMENTS("z")
  100.     AT expr
  101.   besARGEND
  102.   mb_reg_fun(bas, watch);
  103.   besRETURN_LONG(watch(bas, expr));
  104. besEND
  105.  

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_load_file ALIAS "mbas_load_file" LIB "mb"
  7. DECLARE SUB mb_run ALIAS "mbas_run" LIB "mb"
  8. DECLARE SUB mb_reset ALIAS "mbas_reset" LIB "mb"
  9. DECLARE SUB mb_getint ALIAS "mbas_getint" LIB "mb"
  10.  
  11. mb_init()
  12. mb_open()
  13. mb_load_file("mbprime.bas")
  14. mb_run()
  15. PRINT mb_getint("i"),"\n"
  16. mb_close()
  17. mb_dispose()
  18.  


jrs@laptop:~/sb/sb22/mybasic$ scriba mbprime.sb
3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811 1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423 2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079 3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 3581 3583 3593 3607 3613 3617 3623 3631 3637 3643 3659 3671 3673 3677 3691 3697 3701 3709 3719 3727 3733 3739 3761 3767 3769 3779 3793 3797 3803 3821 3823 3833 3847 3851 3853 3863 3877 3881 3889 3907 3911 3917 3919 3923 3929 3931 3943 3947 3967 3989 4001 4003 4007 4013 4019 4021 4027 4049 4051 4057 4073 4079 4091 4093 4099 4111 4127 4129 4133 4139 4153 4157 4159 4177 4201 4211 4217 4219 4229 4231 4241 4243 4253 4259 4261 4271 4273 4283 4289 4297 4327 4337 4339 4349 4357 4363 4373 4391 4397 4409 4421 4423 4441 4447 4451 4457 4463 4481 4483 4493 4507 4513 4517 4519 4523 4547 4549 4561 4567 4583 4591 4597 4603 4621 4637 4639 4643 4649 4651 4657 4663 4673 4679 4691 4703 4721 4723 4729 4733 4751 4759 4783 4787 4789 4793 4799 4801 4813 4817 4831 4861 4871 4877 4889 4scriba: symbol lookup error: /home/jrs/sb/sb22/modules/mb.so: undefined symbol: assert
jrs@laptop:~/sb/sb22/mybasic$

« Last Edit: April 21, 2015, 10:43:11 PM by John »

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #40 on: April 22, 2015, 02:16:02 AM »
@John

You need only register once when open an interpreter structure, see the registering of 'beep' in '_on_startup' at 'main.c' for example.

Of course you could declare and assign variables in MY-BASIC script and communicate between MB and host; but you couldn't get or set it with the variable identifier. The way in MB to do script -> host is mb_pop_XXX functions, and host -> script with mb_push_XXX.

Eg. somehow in script like this:

Code: Text
  1. A = 0 ' Create and assign a variable.
  2. A = WATCH(A) ' Send it to host and assign with a new value sent from host.
  3.  

Add mb_push_XXX in 'watch' to send values from host to script.

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #41 on: April 22, 2015, 04:21:17 AM »
'watch' should be like:

Code: C
  1. int watch(struct mb_interpreter_t* s, void** l) {
  2.         int result = MB_FUNC_OK;
  3.         int_t arg = 0;
  4.  
  5.         mb_assert(s && l);
  6.  
  7.         mb_check(mb_attempt_open_bracket(s, l));
  8.  
  9.         mb_check(mb_pop_int(s, l, &arg)); // That's it!
  10.  
  11.         mb_check(mb_attempt_close_bracket(s, l));
  12.  
  13.         // do what ever you want with 'arg'.
  14.  
  15.         mb_check(mb_push_int(s, l, arg));
  16.  
  17.         return result;
  18. }
  19.  

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #42 on: April 22, 2015, 08:31:32 AM »
This solution looks like I can only communicate with one variable at  a time.  :-\  To be honest, this is a kludge and you need a better way for the host and MY-BASIC to share its variables. I would like to be able to do something like the Perl extension module for SB.

Code: ScriptBasic
  1. DECLARE SUB pl_Init ALIAS "pl_Init" LIB "sbperl"
  2. DECLARE SUB pl_Eval ALIAS "pl_Eval" LIB "sbperl"
  3. DECLARE SUB pl_GetInt ALIAS "pl_GetInt" LIB "sbperl"
  4. DECLARE SUB pl_GetDbl ALIAS "pl_GetDbl" LIB "sbperl"
  5. DECLARE SUB pl_GetStr ALIAS "pl_GetStr" LIB "sbperl"
  6. DECLARE SUB pl_Destroy ALIAS "pl_Destroy" LIB "sbperl"
  7.  
  8. pl_Init
  9.  
  10. pl_code = """
  11. sub Average{
  12.   # get total number of arguments passed.
  13.   $n = scalar(@_);
  14.   $sum = 0;
  15.  
  16.   foreach $item (@_){
  17.      $sum += $item;
  18.   }
  19.   $average = $sum / $n;
  20.  
  21.   return $average;
  22. }
  23. """
  24. pl_Eval pl_code
  25. pl_Eval "$num = Average(10, 20, 30);"
  26. PRINT pl_GetInt("num"),"\n"
  27.  
  28. pl_Destroy
  29.  


jrs@laptop:~/sb/sb22/test$ scriba perlfunc.sb
20
jrs@laptop:~/sb/sb22/test$


Here is an example of embedding Script BASIC in BaCon and using the SQLite3 extension module and passing the resulting record set back to BaCon.

Code: [Select]
PRAGMA OPTIONS -I/home/jrs/sb/source
PRAGMA LDFLAGS scriba pthread
PRAGMA INCLUDE scriba.h getopt.h

PROTO scriba_destroy

DECLARE pProgram TYPE pSbProgram
DECLARE pVariable TYPE pSbData

pProgram = scriba_new(malloc,free)
ok = scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf")
ok = scriba_SetFileName(pProgram, "E05.sb")
ok = scriba_LoadSourceProgram(pProgram)
ok = scriba_Run(pProgram,"")

vsn = scriba_LookupVariableByName(pProgram, "main::dbstr")
ok = scriba_GetVariable(pProgram, vsn, &pVariable)

dbstr$ = pVariable[0].v.s
SPLIT dbstr$ BY "|" TO row$ SIZE cnt

FOR x = 0 TO cnt-1
PRINT row$[x]
NEXT x

jrs@laptop:~/BaCon/B29$ ./bacon sqlext.bac
Converting 'sqlext.bac'... done.
Compiling 'sqlext.bac'... done.
Program 'sqlext' ready.
jrs@laptop:~/BaCon/B29$ ./sqlext
123-hello
234-cruel
345-world

jrs@laptop:~/BaCon/B29$

E05.sb
Code: ScriptBasic
  1. IMPORT sqlite.bas
  2.  
  3. db = sqlite::open("testsql")
  4.  
  5. sqlite::execute(db,"CREATE TABLE demo (someval integer, sometxt text);")
  6. sqlite::execute(db,"INSERT INTO demo VALUES (123,'hello');")
  7. sqlite::execute(db, "INSERT INTO demo VALUES (234, 'cruel');")
  8. sqlite::execute(db, "INSERT INTO demo VALUES (345, 'world');")
  9.  
  10. stmt = sqlite::query(db,"SELECT * FROM demo")
  11.  
  12. WHILE (sqlite::row(stmt) = sqlite::SQLITE3_ROW)
  13.   IF sqlite::fetchhash(stmt,column) THEN
  14.     dbstr &= column{"someval"} & "-" & column{"sometxt"} & "|"
  15.   END IF
  16. WEND
  17.  
  18. sqlite::close(db)
  19.  
« Last Edit: April 22, 2015, 09:51:37 AM by John »

wangrenxin

  • Guest
Re: MY-BASIC
« Reply #43 on: April 22, 2015, 07:15:26 PM »
Ability of retrieving variables from host is useful in some case. I consider to implement not only retrieving, but also setting break point and single step evaluating; that will make up a debug API set.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: MY-BASIC
« Reply #44 on: April 22, 2015, 07:23:26 PM »
Ability of retrieving variables from host is useful in some case. I consider to implement not only retrieving, but also setting break point and single step evaluating; that will make up a debug API set.

Script BASIC has a debugging mode (internal preprocessor) that Dave (SB VB/COM contributor) took advantage of in the IDE/Debugger he wrote.