Author Topic: CBASIC  (Read 1399 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
CBASIC
« on: February 14, 2021, 09:24:49 pm »
I'm thinking about enhancing CBASIC for writing ScriptBasic extension modules. My plan is to use ScriptBasic's own API for strings, arrays and other BASIC functions missing in C. Doing this keeps variables within ScriptBasic's thread safe memory model.

Most of the extension modules I have done in the past are using CBASIC.

Example:
Code: [Select]
DIM wrkstr AS SB_STRING
This allows me use this string to call any of the ScriptBasic API (syntax) without using the ScriptBasic execution interpreter.
« Last Edit: February 14, 2021, 10:30:12 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #1 on: February 17, 2021, 07:54:40 pm »
I have been testing what is needed to use ScriptBasic as a library to supplement my CBASIC effort. The following is a minimal example creating a ScriptBasic string. Only Scriba_New() is needed to get a program object pointer without having to load a ScriptBasic program. This object pointer is used by most of the other ScriptBasic API functions.

Code: C
  1. // gcc sbstr.c -I/home/jrs/sb/source -lscriba -lpthread -o sbstr
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS SBStrPtr;
  14.   pProgram = scriba_new(malloc,free);
  15.   SBStrPtr = scriba_NewSbString(pProgram, "Hello CBASIC");
  16.   PRINT("%i\n", SBStrPtr -> type); //SBT_STRING = 3
  17.   PRINT("%li\n", SBStrPtr -> size);
  18.   PRINT("%s\n",SBStrPtr -> v.s);
  19.   scriba_DestroySbData(pProgram, SBStrPtr);
  20.   scriba_destroy(pProgram);
  21.   RETURN_FUNCTION(0);
  22. END_FUNCTION
  23.  


jrs@RPi-Dev:~/sb/cbasic$ time ./sbstr
3
12
Hello CBASIC

real   0m0.012s
user   0m0.007s
sys   0m0.005s
jrs@RPi-Dev:~/sb/cbasic$ ls -l sbstr
-rwxrwxr-x 1 jrs jrs 9504 Feb 17 20:38 sbstr
jrs@RPi-Dev:~/sb/cbasic$

« Last Edit: February 18, 2021, 06:37:02 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #2 on: February 18, 2021, 08:13:38 pm »
The scriba_NewSbArgs function is a nice way to create SbData object arrays that you can use when calling ScriptBasic functions. ScriptBasic doesn't have API functions to call specific syntax like REPLACE() but I can load a file containing function wrappers that I can call from CBASIC.

Code: C
  1. // gcc myvars.c -I/home/jrs/sb/source -lscriba -lpthread -o myvars
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS MyData;
  14.   pProgram = scriba_new(malloc,free);
  15.   MyData = scriba_NewSbArgs(pProgram,"i r s", 99, 3.14, "Hello CBASIC");
  16.   PRINT("%li\n", MyData[0].v.l);
  17.   PRINT("%0.2f\n", MyData[1].v.d);
  18.   PRINT("%s\n", MyData[2].v.s);
  19.   scriba_destroy(pProgram);
  20.   RETURN_FUNCTION(0);
  21. END_FUNCTION
  22.  


jrs@RPi-Dev:~/sb/cbasic$ ./myvars
99
3.14
Hello CBASIC
jrs@RPi-Dev:~/sb/cbasic$

« Last Edit: February 18, 2021, 10:19:19 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #3 on: February 19, 2021, 11:28:36 pm »
This is an example of calling the ScriptBasic REPLACE() function wrapper from CBASIC.

Code: C
  1. // gcc replace.c -I/home/jrs/sb/source -lscriba -lpthread -o replace
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS ArgData;
  14.   DIM unsigned long AS fnsn;
  15.   DIM SbData AS FunctionResult;
  16.   pProgram = scriba_new(malloc,free);
  17.   scriba_SetFileName(pProgram, "sbfunctions");
  18.   scriba_LoadSourceProgram(pProgram);
  19.   scriba_Run(pProgram, "");
  20.   ArgData = scriba_NewSbArgs(pProgram,"s s s i i", "Hello World", "Hello", "Goodbye", 1, 1);
  21.   fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_replace");
  22.   scriba_CallArgEx(pProgram, fnsn, AT FunctionResult, 5, ArgData);
  23.   PRINT("%s\n", FunctionResult.v.s);
  24.   scriba_destroy(pProgram);
  25.   RETURN_FUNCTION(0);
  26. END_FUNCTION
  27.  

sbfunctions
Code: Script BASIC
  1. ' sbfunctions - ScriptBasic CBASIC function wrappers
  2.  
  3. FUNCTION sb_replace(basestr, searchstr, replacestr, occurances, startpos)
  4.   sb_replace = REPLACE(basestr, searchstr, replacestr, occurances, startpos)
  5. END FUNCTION
  6.  



jrs@RPi-Dev:~/sb/cbasic$ time ./replace
Goodbye World

real   0m0.008s
user   0m0.000s
sys   0m0.009s
jrs@RPi-Dev:~/sb/cbasic$

« Last Edit: February 20, 2021, 12:50:31 am by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #4 on: February 20, 2021, 05:47:19 pm »
An example of IUP working with CBASIC. First problem was there is no binary distribution for IUP/CD/IM on Ubuntu 64 ARM. (RPi 4B) I had to build it from source which was not easy.

Code: C
  1. // gcc hello_iup.c -liup -o hello_iup
  2.  
  3. #include <stdlib.h>
  4. #include <iup.h>
  5. #include "cbasic.h"
  6.  
  7. MAIN
  8. BEGIN_FUNCTION
  9.   IupOpen(0, 0);
  10.   IupMessage("", "Hello World - IUP / CBASIC");
  11.   IupClose();
  12.   RETURN_FUNCTION(EXIT_SUCCESS);
  13. END_FUNCTION
  14.  


rs@RPi-Dev:~/sb/cbasic$ ls -l hello_iup
-rwxrwxr-x 1 jrs jrs 9320 Feb 20 17:49 hello_iup
jrs@RPi-Dev:~/sb/cbasic$



« Last Edit: February 20, 2021, 05:55:49 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #5 on: February 20, 2021, 09:34:21 pm »
This is an example of IUP OpenGL spinning cube in CBASIC.

Code: C
  1. // gcc glcube.c -liup -liupgl -lGL -lGLU -o glcube
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <iup.h>
  6. #include <iupgl.h>
  7. #include <GL/gl.h>
  8. #include <GL/glu.h>
  9. #include "cbasic.h"
  10.  
  11. DIM LOCAL Ihandle AS *canvas;
  12. DIM LOCAL float AS t=0;
  13.  
  14. LOCAL SUB polygon(int a, int b, int c, int d)
  15. BEGIN_SUB
  16.   DIM double AS vertices[][3]={{-1,-1, 1}, {-1, 1, 1}, { 1, 1, 1}, { 1,-1, 1},
  17.                               {-1,-1,-1}, {-1, 1,-1}, { 1, 1,-1}, { 1,-1,-1}};
  18.   glBegin(GL_POLYGON);
  19.   glVertex3dv(vertices[a]);
  20.   glVertex3dv(vertices[b]);
  21.   glVertex3dv(vertices[c]);
  22.   glVertex3dv(vertices[d]);
  23.   glEnd();
  24. END_SUB
  25.  
  26. LOCAL SUB colorCube(void)  
  27. BEGIN_SUB
  28.   glColor3f(1,0,0);
  29.   glNormal3f(1,0,0);
  30.   polygon(2,3,7,6);
  31.   glColor3f(0,1,0);
  32.   glNormal3f(0,1,0);
  33.   polygon(1,2,6,5);
  34.   glColor3f(0,0,1);
  35.   glNormal3f(0,0,1);
  36.   polygon(0,3,2,1);
  37.   glColor3f(1,0,1);
  38.   glNormal3f(0,-1,0);
  39.   polygon(3,0,4,7);
  40.   glColor3f(1,1,0);
  41.   glNormal3f(0,0,-1);
  42.   polygon(4,5,6,7);
  43.   glColor3f(0,1,1);
  44.   glNormal3f(-1,0,0);
  45.   polygon(5,4,0,1);
  46. END_SUB
  47.  
  48. LOCAL FUNCTION int repaint_cb(Ihandle *self)
  49. BEGIN_FUNCTION
  50.   IupGLMakeCurrent(self);
  51.   glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
  52.   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  53.   glEnable(GL_DEPTH_TEST);
  54.   glMatrixMode(GL_MODELVIEW);
  55.   glPushMatrix();
  56.   glTranslatef( 0.0f, 0.0f , 0.0f );
  57.   glScalef( 1.0f, 1.0f, 1.0f );
  58.   glRotatef(t,0,0,1);
  59.   colorCube();
  60.   glPopMatrix();
  61.   IupGLSwapBuffers(self);
  62.   RETURN_FUNCTION(IUP_DEFAULT);
  63. END_FUNCTION
  64.  
  65. LOCAL FUNCTION int resize_cb(Ihandle *self, int width, int height)
  66. BEGIN_FUNCTION
  67.   IupGLMakeCurrent(self);
  68.   glViewport(0, 0, width, height);
  69.   glMatrixMode(GL_MODELVIEW);
  70.   glLoadIdentity();
  71.   glMatrixMode(GL_PROJECTION);
  72.   glLoadIdentity();
  73.   gluPerspective(60,4./3.,1.,15);
  74.   gluLookAt(3,3,3, 0,0,0, 0,0,1);
  75.   RETURN_FUNCTION(IUP_DEFAULT);
  76. END_FUNCTION
  77.  
  78. FUNCTION int idle_cd(void)
  79. BEGIN_FUNCTION
  80.   t+=1;
  81.   repaint_cb(canvas);
  82.   RETURN_FUNCTION(IUP_DEFAULT);
  83. END_FUNCTION
  84.  
  85. FUNCTION int exit_cb(void)
  86. BEGIN_FUNCTION
  87.   RETURN_FUNCTION(IUP_CLOSE);
  88. END_FUNCTION
  89.  
  90. FUNCTION Ihandle* initDialog(void)
  91. BEGIN_FUNCTION
  92.   DIM Ihandle* AS dialog;
  93.   canvas = IupGLCanvas(NULL);
  94.   IupSetAttribute(canvas,"RASTERSIZE","640x480");
  95.   IupSetAttribute(canvas, "BUFFER","DOUBLE");
  96.   IupSetAttribute(canvas, "DEPTH_SIZE", "16");
  97.   IupSetCallback(canvas, "ACTION", (Icallback) repaint_cb);  
  98.   IupSetCallback(canvas, "RESIZE_CB",(Icallback) resize_cb);
  99.   dialog = IupDialog(canvas);
  100.   IupSetAttribute(dialog, "TITLE", "IUP 3D OpenGL");
  101.   IupSetCallback(dialog, "CLOSE_CB", (Icallback) exit_cb);
  102.   IupSetFunction("IDLE_ACTION", (Icallback)idle_cd);
  103.   RETURN_FUNCTION(dialog);
  104. END_FUNCTION
  105.  
  106. MAIN
  107. BEGIN_FUNCTION
  108.   DIM Ihandle* AS dialog;
  109.   IupOpen(0, 0);
  110.   IupGLCanvasOpen();
  111.   dialog = initDialog();
  112.   IupShowXY(dialog, IUP_CENTER, IUP_CENTER);
  113.   IupMainLoop();
  114.   IupClose();
  115. END_FUNCTION
  116.  
« Last Edit: February 20, 2021, 10:48:15 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #6 on: February 21, 2021, 12:39:33 am »
I was able to get my ScriptBasic IDE/Debugger going. I haven't converted it to CBASIC yet but I thought I would share a screen shot.

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #7 on: February 21, 2021, 06:57:00 pm »
This is an example of using CBASIC with the Raspberry Pi Pico micro controller.

Code: C
  1. // RPi Pico onboard LED blink
  2.  
  3. #include "pico/stdlib.h"
  4. #include "pico/binary_info.h"
  5. #include "cbasic.h"
  6.  
  7. CONST uint AS LED_PIN = 25;
  8.  
  9. MAIN
  10. BEGIN_FUNCTION
  11.   bi_decl(bi_program_description("First Blink"));
  12.   bi_decl(bi_1pin_with_name(LED_PIN, "On-board LED"));
  13.   gpio_init(LED_PIN);
  14.   gpio_set_dir(LED_PIN, GPIO_OUT);
  15.   DEF_WHILE(1)
  16.   BEGIN_WHILE
  17.     gpio_put(LED_PIN, 0);
  18.     sleep_ms(250);
  19.     gpio_put(LED_PIN, 1);
  20.     sleep_ms(1000);
  21.   WEND
  22. END_FUNCTION
  23.  


My original Pico kit I bought from Vilros didn't have the headers soldered in place. My attempt to install them trashed the board. 😥 I purchased the following as a replacement.

DIYmall ESP32-PICO-KIT
« Last Edit: February 22, 2021, 10:12:52 am by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #8 on: February 25, 2021, 11:35:31 pm »
This is an example of the IUP GTK WebBrowser control.

Code: C
  1. // gcc webbrowser.c -liup -liupweb -o webbrowser
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <iup.h>
  7. #include <iupweb.h>
  8. #include "cbasic.h"
  9.  
  10. LOCAL FUNCTION int navigate_cb(Ihandle* self, char* url)
  11. BEGIN_FUNCTION
  12.   PRINT("NAVIGATE_CB: %s\n", url);
  13.   (void)self;
  14.   IF (strstr(url, "download")!=NULL) THEN
  15.     RETURN_FUNCTION(IUP_IGNORE);
  16.   END_IF
  17.   RETURN_FUNCTION(IUP_DEFAULT);
  18. END_FUNCTION
  19.                    
  20. LOCAL FUNCTION int error_cb(Ihandle* self, char* url)
  21. BEGIN_FUNCTION
  22.   PRINT("ERROR_CB: %s\n", url);
  23.   (void)self;
  24.   RETURN_FUNCTION(IUP_DEFAULT);
  25. END_FUNCTION
  26.  
  27. LOCAL FUNCTION int completed_cb(Ihandle* self, char* url)
  28. BEGIN_FUNCTION
  29.   PRINT("COMPLETED_CB: %s\n", url);
  30.   (void)self;
  31.   RETURN_FUNCTION(IUP_DEFAULT);
  32. END_FUNCTION
  33.  
  34. LOCAL FUNCTION int newwindow_cb(Ihandle* self, char* url)
  35. BEGIN_FUNCTION
  36.   PRINT("NEWWINDOW_CB: %s\n", url);
  37.   IupSetAttribute(self, "VALUE", url);
  38.   RETURN_FUNCTION(IUP_DEFAULT);
  39. END_FUNCTION
  40.  
  41. LOCAL FUNCTION int back_cb(Ihandle* self)
  42. BEGIN_FUNCTION
  43.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  44.   IupSetAttribute(web, "BACKFORWARD", "-1");
  45.   RETURN_FUNCTION(IUP_DEFAULT);
  46. END_FUNCTION
  47.  
  48. LOCAL FUNCTION int forward_cb(Ihandle* self)
  49. BEGIN_FUNCTION
  50.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  51.   IupSetAttribute(web, "BACKFORWARD", "1");
  52.   RETURN_FUNCTION(IUP_DEFAULT);
  53. END_FUNCTION
  54.  
  55. LOCAL FUNCTION int stop_cb(Ihandle* self)
  56. BEGIN_FUNCTION
  57.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  58.   IupSetAttribute(web, "STOP", NULL);
  59.   RETURN_FUNCTION(IUP_DEFAULT);
  60. END_FUNCTION
  61.  
  62. LOCAL FUNCTION int reload_cb(Ihandle* self)
  63. BEGIN_FUNCTION
  64.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  65.   IupSetAttribute(web, "RELOAD", NULL);
  66.   RETURN_FUNCTION(IUP_DEFAULT);
  67. END_FUNCTION
  68.  
  69. LOCAL FUNCTION int load_cb(Ihandle* self)
  70. BEGIN_FUNCTION
  71.   DIM Ihandle* AS txt  = (Ihandle*)IupGetAttribute(self, "MY_TEXT");
  72.   DIM Ihandle* AS web  = (Ihandle*)IupGetAttribute(self, "MY_WEB");
  73.   IupSetAttribute(web, "VALUE", IupGetAttribute(txt, "VALUE"));
  74.   RETURN_FUNCTION(IUP_DEFAULT);
  75. END_FUNCTION
  76.  
  77. SUB WebBrowserTest(void)
  78. BEGIN_SUB
  79.   DIM Ihandle AS *txt, *dlg, *web;
  80.   DIM Ihandle AS *btLoad, *btReload, *btBack, *btForward, *btStop;
  81.   IupWebBrowserOpen();              
  82.   web = IupWebBrowser();
  83.   dlg = IupDialog(IupVbox(IupHbox(btBack = IupButton("Back", NULL),
  84.         btForward = IupButton("Forward", NULL),
  85.         txt = IupText(""),
  86.         btLoad = IupButton("Load", NULL),
  87.         btReload = IupButton("Reload", NULL),
  88.         btStop = IupButton("Stop", NULL),
  89.         NULL),
  90.         web, NULL));
  91.   IupSetAttribute(dlg, "TITLE", "IupWebBrowser");
  92.   IupSetAttribute(dlg, "MY_TEXT", (char*)txt);
  93.   IupSetAttribute(dlg, "MY_WEB", (char*)web);
  94.   IupSetAttribute(dlg, "RASTERSIZE", "800x600");
  95.   IupSetAttribute(dlg, "MARGIN", "10x10");
  96.   IupSetAttribute(dlg, "GAP", "10");
  97.   IupSetAttribute(txt, "VALUE", "https://allbasic.info/forum");
  98.   IupSetAttribute(web, "VALUE", IupGetAttribute(txt, "VALUE"));
  99.   IupSetAttributeHandle(dlg, "DEFAULTENTER", btLoad);
  100.   IupSetAttribute(txt, "EXPAND", "HORIZONTAL");
  101.   IupSetCallback(btLoad, "ACTION", (Icallback)load_cb);
  102.   IupSetCallback(btReload, "ACTION", (Icallback)reload_cb);
  103.   IupSetCallback(btBack, "ACTION", (Icallback)back_cb);
  104.   IupSetCallback(btForward, "ACTION", (Icallback)forward_cb);
  105.   IupSetCallback(btStop, "ACTION", (Icallback)stop_cb);
  106.   IupSetCallback(web, "NEWWINDOW_CB", (Icallback)newwindow_cb);
  107.   IupSetCallback(web, "NAVIGATE_CB", (Icallback)navigate_cb);
  108.   IupSetCallback(web, "ERROR_CB", (Icallback)error_cb);
  109.   IupSetCallback(web, "COMPLETED_CB", (Icallback)completed_cb);
  110.   IupShow(dlg);
  111. END_SUB
  112.  
  113. MAIN
  114. BEGIN_FUNCTION
  115.   IupOpen(0, 0);
  116.   WebBrowserTest();
  117.   IupMainLoop();
  118.   IupClose();
  119.   RETURN_FUNCTION(EXIT_SUCCESS);
  120. END_FUNCTION
  121.  


jrs@RPi-Dev:~/sb/cbasic$ gcc webbrowser.c -liup -liupweb -o webbrowser
jrs@RPi-Dev:~/sb/cbasic$ ./webbrowser
NAVIGATE_CB: https://allbasic.info/forum
NAVIGATE_CB: https://allbasic.info/forum/
COMPLETED_CB: https://allbasic.info/forum/
HISTORY ITEMS
Current  00: (null)
jrs@RPi-Dev:~/sb/cbasic$

« Last Edit: February 26, 2021, 12:14:32 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
CBASIC - Windows
« Reply #9 on: May 08, 2021, 05:57:04 pm »
I have used CBASIC extensively building ScriptBasic extension modules. Most of this work was done under Linux. I have decided to expanding CBASIC to work with Windows embedding ScriptBasic as a support library. The following example uses the ScriptBasic REPLACE function from CBASIC. (gcc with libscriba.dll) A Linux example of this can be found in a previous post in this thread. (RPi 4B - Ubuntu 64)

Code: C
  1. // gcc replace.c -IC:\sbgcc\source libscriba.dll -lpthread -o replace
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS ArgData;
  14.   DIM unsigned long AS fnsn;
  15.   DIM SbData AS FunctionResult;
  16.   pProgram = scriba_new(malloc,free);
  17.   scriba_SetFileName(pProgram, "sbfunctions");
  18.   scriba_LoadSourceProgram(pProgram);
  19.   scriba_Run(pProgram, "");
  20.   ArgData = scriba_NewSbArgs(pProgram,"s s s i i", "Hello World", "Hello", "Goodbye", 1, 1);
  21.   fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_replace");
  22.   scriba_CallArgEx(pProgram, fnsn, AT FunctionResult, 5, ArgData);
  23.   PRINT("%s\n", FunctionResult.v.s);
  24.   scriba_destroy(pProgram);
  25.   RETURN_FUNCTION(0);
  26. END_FUNCTION
  27.  

sbfunctions
Code: Script BASIC
  1. ' sbfunctions - ScriptBasic CBASIC function wrappers
  2.  
  3. FUNCTION sb_replace(basestr, searchstr, replacestr, occurances, startpos)
  4.   sb_replace = REPLACE(basestr, searchstr, replacestr, occurances, startpos)
  5. END FUNCTION
  6.  


C:\ScriptBASIC\cbasic>replace
Goodbye World

05/08/2021  05:39 PM            45,402 replace.exe


« Last Edit: May 08, 2021, 06:02:00 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
CBASIC - VB6
« Reply #10 on: May 09, 2021, 04:44:49 pm »
This is an example I did in Oxygen Basic which uses the ScriptBasic COM/OLE extension module. This is the CBASIC version of it.

Code: C
  1. // gcc vblogin.c -IC:\sbgcc\source libscriba.dll sbwgui.o -lpthread -o vblogin
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS ArgData;
  14.   DIM pSbData AS FunctionResult;
  15.   DIM int AS sb_call = 1;
  16.   DIM int AS sb_get  = 2;
  17.   DIM int AS sb_let  = 4;
  18.   DIM int AS sb_set  = 8;
  19.   DIM char AS src[] = "IMPORT sbembed.sbi\nIMPORT \"callback.sb\"\n";
  20.  
  21.   pProgram = scriba_new(malloc,free);
  22.   scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");
  23.   scriba_SetFileName(pProgram, "none");
  24.   scriba_LoadProgramString(pProgram, src, strlen(src));
  25.   scriba_Run(pProgram, "");
  26.  
  27.   FunctionResult = scriba_NewSbArgs(pProgram,"u");
  28.   DIM int AS fn1 = scriba_LookupFunctionByName(pProgram, "main::sb_create");
  29.   DIM int AS fn2 = scriba_LookupFunctionByName(pProgram, "main::sb_cbn");
  30.   DIM int AS fn3 = scriba_LookupFunctionByName(pProgram, "main::sb_release");
  31.   DIM int AS fn4 = scriba_LookupFunctionByName(pProgram, "main::set_callback");
  32.  
  33.   ArgData = scriba_NewSbArgs(pProgram, "s", "O2VB.Login");
  34.   scriba_CallArgEx(pProgram, fn1, FunctionResult, 1, ArgData);
  35.   DIM unsigned long AS vbobj = FunctionResult[0].v.l;
  36.   ArgData = scriba_NewSbArgs(pProgram, "i", vbobj);
  37.   scriba_CallArgEx(pProgram, fn4, FunctionResult, 1, ArgData);
  38.   ArgData = scriba_NewSbArgs(pProgram, "i s i", vbobj, "ShowOCXForm", sb_call);
  39.   scriba_CallArgEx(pProgram, fn2, FunctionResult, 3, ArgData);
  40.   DIM unsigned long AS ufunc = scriba_LookupVariableByName(pProgram, "main::userid");
  41.   scriba_GetVariable(pProgram, ufunc, AT ArgData);
  42.   PRINT("%s\n", ArgData[0].v.s);
  43.   ArgData = scriba_NewSbArgs(pProgram, "i", vbobj);
  44.   scriba_CallArgEx(pProgram, fn3, FunctionResult, 1, ArgData);
  45.  
  46.   scriba_DestroySbArgs(pProgram, ArgData, 3);
  47.   scriba_DestroySbArgs(pProgram, FunctionResult, 1);
  48.   scriba_destroy(pProgram);
  49.  
  50.   RETURN_FUNCTION(0);
  51. END_FUNCTION  
  52.  


callback.sb
Code: Script BASIC
  1. ' CBASIC SB Callback Functions
  2.  
  3. FUNCTION OK_Button_Click(user)
  4.   userid = user
  5.   OK_Button_Click = TRUE
  6. END FUNCTION
  7.  
  8. FUNCTION Set_Callback(vbobj)
  9.   oCollection = CBN(vbobj, "CallBackHandlers", sb_get)
  10.   CBN(oCollection, "Add", sb_call, ADDRESS(OK_Button_Click()), "Login_Form.OK_Button_Click")
  11. END FUNCTION
  12.  

VB6 Login Form
Code: Visual Basic
  1. Option Explicit
  2.  
  3. Private Declare Function ext_SBCallBack Lib "COM.dll" Alias "SBCallBack" (ByVal EntryPoint As Long, ByVal arg As Long) As Long
  4. Private Declare Function ext_SBCallBackEx Lib "COM.dll" Alias "SBCallBackEx" (ByVal EntryPoint As Long, ByRef v As Variant) As Variant
  5.  
  6. Private m_owner As Login
  7. Public LoginSucceeded As Boolean
  8.  
  9. Function ShowMain_Form(owner As Login) As Long
  10.     On Error Resume Next
  11.     Set m_owner = owner
  12.     Me.Show 1
  13.     Set m_owner = Nothing
  14.     ShowMain_Form = 0
  15.     Unload Me
  16. End Function
  17.  
  18. Private Function TriggerCallBack(nodeID As Long, argValue As Long) As Long
  19.     TriggerCallBack = ext_SBCallBack(nodeID, argValue)
  20. End Function
  21.  
  22. Private Function TriggerCallBackEx(nodeID As Long, v() As Variant)
  23.     TriggerCallBackEx = ext_SBCallBackEx(nodeID, v)
  24. End Function
  25.  
  26. Private Sub Cancel_Button_Click()
  27.     LoginSucceeded = False
  28.     Me.Hide
  29. End Sub
  30.  
  31. Private Sub OK_Button_Click()
  32.     Dim nodeID As Long
  33.     Dim arg(0) As Variant
  34.     Dim rtnVal As Long
  35.     If Password_Text = "OxygenBasic" Then
  36.         LoginSucceeded = True
  37.         nodeID = m_owner.CallBackHandlers("Login_Form.OK_Button_Click")
  38.         arg(0) = Login_Form.User_Text
  39.         rtnVal = TriggerCallBackEx(nodeID, arg)
  40.         Me.Hide
  41.     Else
  42.         MsgBox "Invalid Password, try again!", , "Login"
  43.         Password_Text.SetFocus
  44.         SendKeys "{Home}+{End}"
  45.     End If
  46. End Sub
  47.  

VB6 Login Class
Code: Visual Basic
  1. Public CallBackHandlers As New Collection
  2.  
  3. Public Function ShowOCXForm() As Long
  4.     ShowOCXForm = Login_Form.ShowMain_Form(Me)
  5. End Function
  6.  


C:\ScriptBASIC\cbasic>vblogin
John

C:\ScriptBASIC\cbasic>

« Last Edit: May 09, 2021, 06:26:33 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
CBASIC - OpenWeather
« Reply #11 on: May 09, 2021, 08:19:10 pm »
This example uses the ScriptBasic cURL extension module to fetch the current weather data. It also uses the ScriptBasic LIKE / MATCH functions to parse the returned JSON response.

https://openweathermap.org/

CBASIC offers the a cross platform solution using ScriptBasic's high level C functions to supplement what C doesn't provide native. One can call standard C libraries, ScriptBasic C extension modules and ScriptBasic native C based language functions. It also supports ScriptBasic's variant based thread safe variable structure.

Code: C
  1. // gcc weather.c -IC:\sbgcc\source libscriba.dll -lpthread -o weather
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS arg;
  14.   DIM pSbData AS argr;
  15.   DIM int AS fnsn;
  16.  
  17.   pProgram = scriba_new(malloc,free);
  18.   scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");
  19.   scriba_SetFileName(pProgram, "weather.sb");
  20.   scriba_LoadSourceProgram(pProgram);
  21.   scriba_Run(pProgram, "");
  22.  
  23.   argr = scriba_NewSbArgs(pProgram,"u");
  24.   arg = scriba_NewSbArgs(pProgram, "s", "Anacortes,US");
  25.   fnsn = scriba_LookupFunctionByName(pProgram, "main::get_weather");
  26.   scriba_CallArgEx(pProgram, fnsn, argr, 1, arg);
  27.   PRINT("%s\n\n", argr[0].v.s);
  28.   DIM char AS mask[] = "*temp\":*,*";
  29.   arg = scriba_NewSbArgs(pProgram, "s s", argr[0].v.s, mask);
  30.   fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_like");
  31.   scriba_CallArgEx(pProgram, fnsn, argr, 2, arg);
  32.   arg = scriba_NewSbArgs(pProgram, "i", 2);
  33.   fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_match");
  34.   scriba_CallArgEx(pProgram, fnsn, argr, 1, arg);
  35.   PRINT("Anacortes Temperature is %s F\n", argr[0].v.s);
  36.  
  37.   scriba_DestroySbArgs(pProgram, arg, 2);
  38.   scriba_DestroySbArgs(pProgram, argr, 1);
  39.   scriba_destroy(pProgram);
  40.  
  41.   RETURN_FUNCTION(0);
  42. END_FUNCTION  
  43.  

weather.sb
Code: Script BASIC
  1. ' OpenWeather - Curl Example
  2.  
  3. IMPORT sbembed.sbi
  4. IMPORT curl.sbi
  5.  
  6. FUNCTION Get_Weather(place)
  7.   ch = curl::init()
  8.   curl::option(ch, "URL", "http://api.openweathermap.org/data/2.5/weather?q=" & place & "&units=imperial&appid=MY_API_KEY")
  9.   curl::option(ch, "CUSTOMREQUEST", "GET")
  10.   response = curl::perform(ch)
  11.   curl::finish(ch)
  12.   Get_Weather = response
  13. END FUNCTION
  14.  

LIKE / MATCH FUNCTIONS
Code: Script BASIC
  1. ' LIKE
  2. FUNCTION sb_like(basestr, mask)
  3.   ok = basestr LIKE mask
  4.   sb_like = ok
  5. END FUNCTION
  6.  
  7. ' MATCH
  8. FUNCTION sb_match(segment)
  9.   sb_match = JOKER(segment)
  10. END FUNCTION
  11.  


PS C:\ScriptBASIC\cbasic> Measure-Command { .\weather.exe | Out-Host }
{"coord":{"lon":-122.6127,"lat":48.5126},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":52.61,"feels_like":50.9,"temp_min":51.8,"temp_max":54,"pressure":1024,"humidity":71},"visibility":10000,"wind":{"speed":1.9,"deg":286,"gust":3.2},"clouds":{"all":1},"dt":1620617857,"sys":{"type":1,"id":3524,"country":"US","sunrise":1620563861,"sunset":1620617767},"timezone":-25200,"id":5785657,"name":"Anacortes","cod":200}

Anacortes Temperature is 52.61 F

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 296
Ticks             : 2962516
TotalDays         : 3.42883796296296E-06
TotalHours        : 8.22921111111111E-05
TotalMinutes      : 0.00493752666666667
TotalSeconds      : 0.2962516
TotalMilliseconds : 296.2516


46,312  bytes - weather.exe

« Last Edit: May 10, 2021, 09:12:23 am by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
CBASIC - Excel
« Reply #12 on: May 10, 2021, 10:10:09 pm »
This is the Excel example in CBASIC. This example uses the ScriptBasic COM/OLE automation extension module.

Code: C
  1. // gcc cbasic_excel.c -IC:\sbgcc\source libscriba.dll -lpthread -o cbasic_excel
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <getopt.h>
  7. #include "scriba.h"
  8. #include "cbasic.h"
  9.  
  10. MAIN
  11. BEGIN_FUNCTION
  12.   DIM pSbProgram AS pProgram;
  13.   DIM pSbData AS arg;
  14.   DIM pSbData AS argr;
  15.   DIM int AS sb_call = 1;
  16.   DIM int AS sb_get  = 2;
  17.   DIM int AS sb_let  = 4;
  18.   DIM int AS sb_set  = 8;
  19.   DIM int AS i, j;
  20.   DIM char AS src[] = "IMPORT sbembed.sbi\n";
  21.  
  22.   pProgram = scriba_new(malloc,free);
  23.   scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");
  24.   scriba_SetFileName(pProgram, "none");
  25.   scriba_LoadProgramString(pProgram, src, strlen(src));
  26.   scriba_Run(pProgram, "");
  27.  
  28.   argr = scriba_NewSbArgs(pProgram,"u");
  29.   DIM int AS fn1 = scriba_LookupFunctionByName(pProgram, "main::sb_create");
  30.   DIM int AS fn2 = scriba_LookupFunctionByName(pProgram, "main::sb_cbn");
  31.   DIM int AS fn3 = scriba_LookupFunctionByName(pProgram, "main::sb_release");
  32.   arg = scriba_NewSbArgs(pProgram, "s", "Excel.Application");
  33.   scriba_CallArgEx(pProgram, fn1, argr, 1, arg);
  34.   DIM unsigned long AS oexcel = argr[0].v.l;
  35.   arg = scriba_NewSbArgs(pProgram, "i s i", oexcel, "Workbooks", sb_get);
  36.   scriba_CallArgEx(pProgram, fn2, argr, 3, arg);
  37.   DIM unsigned long AS oWorkBook = argr[0].v.l;
  38.   arg = scriba_NewSbArgs(pProgram, "i s i", oWorkBook, "Add", sb_call);
  39.   scriba_CallArgEx(pProgram, fn2, argr, 3, arg);
  40.   DIM unsigned long AS oExcelWorkbook = argr[0].v.l;
  41.   arg = scriba_NewSbArgs(pProgram, "i s i i", oExcelWorkbook, "Worksheets", sb_get, 1);
  42.   scriba_CallArgEx(pProgram, fn2, argr, 4, arg);
  43.   DIM unsigned long AS oExcelSheet = argr[0].v.l;
  44.   DIM unsigned long AS oCell;
  45.   DIM char AS cell_text[16];
  46.   DEF_FOR (i = 1 TO i <= 10 STEP INCR i)
  47.   BEGIN_FOR
  48.     DEF_FOR (j = 1 TO j <= 10 STEP INCR j)
  49.     BEGIN_FOR
  50.       arg = scriba_NewSbArgs(pProgram, "i s i i i", oExcelSheet, "Cells", sb_get, i, j);
  51.       scriba_CallArgEx(pProgram, fn2, argr, 5, arg);
  52.       oCell = argr[0].v.l;
  53.       sprintf(cell_text, "test-%i-%i", i, j);
  54.       arg = scriba_NewSbArgs(pProgram, "i s i s",  oCell, "Value", sb_let, cell_text);
  55.       scriba_CallArgEx(pProgram, fn2, argr, 4, arg);
  56.       >> Release oCell before next assignment
  57.       arg = scriba_NewSbArgs(pProgram, "i", oCell);
  58.       scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  59.     NEXT
  60.   NEXT
  61.   arg = scriba_NewSbArgs(pProgram, "i s i s", oExcelWorkbook, "SaveAs", sb_call, "C:\\ScriptBASIC\\cbasic\\CBASIC_Excel.xls");
  62.   scriba_CallArgEx(pProgram, fn2, argr, 4, arg);
  63.   arg = scriba_NewSbArgs(pProgram, "i s i", oExcelWorkbook, "Close", sb_call);
  64.   scriba_CallArgEx(pProgram, fn2, argr, 3, arg);
  65.   arg = scriba_NewSbArgs(pProgram, "i s i", oexcel, "Quit", sb_call);
  66.   scriba_CallArgEx(pProgram, fn2, argr, 3, arg);
  67.  
  68.   PRINT("Spreadsheet Created\n");
  69.  
  70.   arg = scriba_NewSbArgs(pProgram, "i", oExcelSheet);
  71.   scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  72.   arg = scriba_NewSbArgs(pProgram, "i", oExcelWorkbook);
  73.   scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  74.   arg = scriba_NewSbArgs(pProgram, "i", oWorkBook);
  75.   scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  76.   arg = scriba_NewSbArgs(pProgram, "i", oexcel);
  77.   scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  78.  
  79.   scriba_DestroySbArgs(pProgram, arg, 5);
  80.   scriba_DestroySbArgs(pProgram, argr, 1);
  81.   scriba_destroy(pProgram);
  82.  
  83.   RETURN_FUNCTION(0);
  84. END_FUNCTION  
  85.  


C:\ScriptBASIC\cbasic>cbasic_excel
Spreadsheet Created

C:\ScriptBASIC\cbasic>


This is the C source after the pre-processor expansion of Defines and Macros for the CBASIC program above.

Code: C
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <getopt.h>
  5. #include "scriba.h"
  6. #include "cbasic.h"
  7.  
  8. int main (int argc, char** argv)
  9. {
  10.   pSbProgram pProgram;
  11.   pSbData arg;
  12.   pSbData argr;
  13.   int sb_call = 1;
  14.   int sb_get = 2;
  15.   int sb_let = 4;
  16.   int sb_set = 8;
  17.   int i, j;
  18.   char src[] = "IMPORT sbembed.sbi\n";
  19.  
  20.   pProgram = scriba_new(malloc,free);
  21.   scriba_LoadConfiguration(pProgram, "C:\\Windows\\SCRIBA.INI");
  22.   scriba_SetFileName(pProgram, "none");
  23.   scriba_LoadProgramString(pProgram, src, strlen(src));
  24.   scriba_Run(pProgram, "");
  25.  
  26.   argr = scriba_NewSbArgs(pProgram,"u");
  27.   int fn1 = scriba_LookupFunctionByName(pProgram, "main::sb_create");
  28.   int fn2 = scriba_LookupFunctionByName(pProgram, "main::sb_cbn");
  29.   int fn3 = scriba_LookupFunctionByName(pProgram, "main::sb_release");
  30.   arg = scriba_NewSbArgs(pProgram, "s", "Excel.Application");
  31.   scriba_CallArgEx(pProgram, fn1, argr, 1, arg);
  32.   unsigned long oexcel = argr[0].v.l;
  33.   arg = scriba_NewSbArgs(pProgram, "i s i", oexcel, "Workbooks", sb_get);
  34.   scriba_CallArgEx(pProgram, fn2, argr, 3, arg);
  35.   unsigned long oWorkBook = argr[0].v.l;
  36.   arg = scriba_NewSbArgs(pProgram, "i s i", oWorkBook, "Add", sb_call);
  37.   scriba_CallArgEx(pProgram, fn2, argr, 3, arg);
  38.   unsigned long oExcelWorkbook = argr[0].v.l;
  39.   arg = scriba_NewSbArgs(pProgram, "i s i i", oExcelWorkbook, "Worksheets", sb_get, 1);
  40.   scriba_CallArgEx(pProgram, fn2, argr, 4, arg);
  41.   unsigned long oExcelSheet = argr[0].v.l;
  42.   unsigned long oCell;
  43.   char cell_text[16];
  44.   for (i = 1 ; i <= 10 ; ++ i)
  45.   {
  46.     for (j = 1 ; j <= 10 ; ++ j)
  47.     {
  48.       arg = scriba_NewSbArgs(pProgram, "i s i i i", oExcelSheet, "Cells", sb_get, i, j);
  49.       scriba_CallArgEx(pProgram, fn2, argr, 5, arg);
  50.       oCell = argr[0].v.l;
  51.       sprintf(cell_text, "test-%i-%i", i, j);
  52.       arg = scriba_NewSbArgs(pProgram, "i s i s", oCell, "Value", sb_let, cell_text);
  53.       scriba_CallArgEx(pProgram, fn2, argr, 4, arg);
  54.       arg = scriba_NewSbArgs(pProgram, "i", oCell);
  55.       scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  56.     }
  57.   }
  58.   arg = scriba_NewSbArgs(pProgram, "i s i s", oExcelWorkbook, "SaveAs", sb_call, "C:\\ScriptBASIC\\cbasic\\CBASIC_Excel.xls");
  59.   scriba_CallArgEx(pProgram, fn2, argr, 4, arg);
  60.   arg = scriba_NewSbArgs(pProgram, "i s i", oExcelWorkbook, "Close", sb_call);
  61.   scriba_CallArgEx(pProgram, fn2, argr, 3, arg);
  62.   arg = scriba_NewSbArgs(pProgram, "i s i", oexcel, "Quit", sb_call);
  63.   scriba_CallArgEx(pProgram, fn2, argr, 3, arg);
  64.  
  65.   printf("Spreadsheet Created\n");
  66.  
  67.   arg = scriba_NewSbArgs(pProgram, "i", oExcelSheet);
  68.   scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  69.   arg = scriba_NewSbArgs(pProgram, "i", oExcelWorkbook);
  70.   scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  71.   arg = scriba_NewSbArgs(pProgram, "i", oWorkBook);
  72.   scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  73.   arg = scriba_NewSbArgs(pProgram, "i", oexcel);
  74.   scriba_CallArgEx(pProgram, fn3, argr, 1, arg);
  75.  
  76.   scriba_DestroySbArgs(pProgram, arg, 5);
  77.   scriba_DestroySbArgs(pProgram, argr, 1);
  78.   scriba_destroy(pProgram);
  79.  
  80.   return(0);
  81. }
  82.  
« Last Edit: May 16, 2021, 09:57:31 pm by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #13 on: May 13, 2021, 12:34:22 am »
A good example of CBASIC is the SDL_gfx extension module I wrote. CBASIC is a weaning language to help BASIC programmers learn C and still feel like home.

You can use as much of CBASIC syntax you wish with your C programs. CBASIC just substitutes C syntax with BASIC.

https://basic-sandbox.us/scriptbasic/extensions/-/blob/master/extensions/gfx/interface.c
« Last Edit: May 16, 2021, 02:08:12 am by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3114
    • ScriptBasic Open Source Project
Re: CBASIC
« Reply #14 on: May 14, 2021, 10:12:38 pm »
I'm working on extending the CBASIC language syntax by including the ScriptBasic embedded interface and its language function wrappers.

I'll get a CBASIC project going in the sandbox to document and provide examples of its use.