BASIC Developer & Support Resources > Scripting Languages

CBASIC

(1/3) > >>

John:
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: ---DIM wrkstr AS SB_STRING
--- End code ---

This allows me use this string to call any of the ScriptBasic API (syntax) without using the ScriptBasic execution interpreter.

John:
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 ---// gcc sbstr.c -I/home/jrs/sb/source -lscriba -lpthread -o sbstr #include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include "scriba.h"#include "cbasic.h" MAINBEGIN_FUNCTION  DIM pSbProgram AS pProgram;  DIM pSbData AS SBStrPtr;  pProgram = scriba_new(malloc,free);  SBStrPtr = scriba_NewSbString(pProgram, "Hello CBASIC");  PRINT("%i\n", SBStrPtr -> type); //SBT_STRING = 3  PRINT("%li\n", SBStrPtr -> size);   PRINT("%s\n",SBStrPtr -> v.s);  scriba_DestroySbData(pProgram, SBStrPtr);   scriba_destroy(pProgram);  RETURN_FUNCTION(0);END_FUNCTION 

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$

John:
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 ---// gcc myvars.c -I/home/jrs/sb/source -lscriba -lpthread -o myvars #include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include "scriba.h"#include "cbasic.h" MAINBEGIN_FUNCTION  DIM pSbProgram AS pProgram;  DIM pSbData AS MyData;  pProgram = scriba_new(malloc,free);  MyData = scriba_NewSbArgs(pProgram,"i r s", 99, 3.14, "Hello CBASIC");  PRINT("%li\n", MyData[0].v.l);  PRINT("%0.2f\n", MyData[1].v.d);  PRINT("%s\n", MyData[2].v.s);  scriba_destroy(pProgram);  RETURN_FUNCTION(0);END_FUNCTION 

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

John:
This is an example of calling the ScriptBasic REPLACE() function wrapper from CBASIC.


--- Code: C ---// gcc replace.c -I/home/jrs/sb/source -lscriba -lpthread -o replace #include <stdio.h>#include <stdlib.h>#include <string.h>#include <getopt.h>#include "scriba.h"#include "cbasic.h" MAINBEGIN_FUNCTION  DIM pSbProgram AS pProgram;  DIM pSbData AS ArgData;  DIM unsigned long AS fnsn;  DIM SbData AS FunctionResult;  pProgram = scriba_new(malloc,free);  scriba_SetFileName(pProgram, "sbfunctions");  scriba_LoadSourceProgram(pProgram);  scriba_Run(pProgram, "");  ArgData = scriba_NewSbArgs(pProgram,"s s s i i", "Hello World", "Hello", "Goodbye", 1, 1);  fnsn = scriba_LookupFunctionByName(pProgram, "main::sb_replace");  scriba_CallArgEx(pProgram, fnsn, AT FunctionResult, 5, ArgData);  PRINT("%s\n", FunctionResult.v.s);  scriba_destroy(pProgram);  RETURN_FUNCTION(0);END_FUNCTION 
sbfunctions

--- Code: Script BASIC ---' sbfunctions - ScriptBasic CBASIC function wrappers FUNCTION sb_replace(basestr, searchstr, replacestr, occurances, startpos)  sb_replace = REPLACE(basestr, searchstr, replacestr, occurances, startpos)END FUNCTION 


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

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

John:
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 ---// gcc hello_iup.c -liup -o hello_iup #include <stdlib.h>#include <iup.h>#include "cbasic.h" MAINBEGIN_FUNCTION  IupOpen(0, 0);  IupMessage("", "Hello World - IUP / CBASIC");  IupClose();  RETURN_FUNCTION(EXIT_SUCCESS);END_FUNCTION 

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$



Navigation

[0] Message Index

[#] Next page

Go to full version