Author Topic: CBASIC  (Read 280 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3016
    • 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: 3016
    • 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: 3016
    • 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: 3016
    • 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: 3016
    • 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: 3016
    • 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: 3016
    • 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: 3016
    • 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 »