Raspberry BASIC

Author Topic: Nim SB  (Read 879 times)

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 162
    • View Profile
    • ScriptBasic
Nim SB
« on: December 11, 2019, 05:38:36 PM »
I'm working on embedding ScriptBasic into Nim. Basically it's a port of the SBT extension module as Nim extension. This will allow the full functionality of SB and its extensions to be available to Nim dynamically at runtime.

@AIR - I may need some help creating the Nim interface if you have time.
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 162
    • View Profile
    • ScriptBasic
Re: Nim SB
« Reply #1 on: December 12, 2019, 12:32:28 AM »
I thought I would get the Nim SB project kicked off by showing what already exists and needing a Nim extension interface. SBT is everything needed to make the interface with Nim work. It happens to be using ScriptBasic as its host language. The advantage is that it can also use the MT extension module to communicate with threads.

interface.c

Code: C
  1. /*  SBT (ScriptBasic Threads) - Extension Module
  2. UXLIBS: -lscriba -lpthread -lm
  3. BAS: sbt.bas
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include <math.h>
  11. #include <time.h>
  12. #include <unistd.h>
  13. #include "../../basext.h"
  14. #include "../../scriba.h"
  15. #include "cbasic.h"
  16.  
  17.  
  18. /****************************
  19.  Extension Module Functions
  20. ****************************/
  21.  
  22. besVERSION_NEGOTIATE
  23.   RETURN_FUNCTION((int)INTERFACE_VERSION);
  24. besEND
  25.  
  26. besSUB_START
  27.   DIM AS long PTR p;
  28.   besMODULEPOINTER = besALLOC(sizeof(long));
  29.   IF (besMODULEPOINTER EQ NULL) THEN_DO RETURN_FUNCTION(0);
  30.   p = (long PTR)besMODULEPOINTER;
  31.   RETURN_FUNCTION(0);
  32. besEND
  33.  
  34. besSUB_FINISH
  35.   DIM AS long PTR p;
  36.   p = (long PTR)besMODULEPOINTER;
  37.   IF (p EQ NULL) THEN_DO RETURN_FUNCTION(0);
  38.   RETURN_FUNCTION(0);
  39. besEND
  40.  
  41.  
  42. /**********************
  43.  Script BASIC Instance
  44. **********************/
  45.  
  46. /******************
  47.  Support Routines
  48. ******************/
  49.  
  50. struct _RunServiceProgram {
  51.   char *pszProgramFileName;
  52.   char *pszCmdLineArgs;
  53.   char *pszConfigFileName;
  54.   pSbProgram pTProgram;
  55.   int iRestart;
  56.   };
  57.  
  58. static void ExecuteProgramThread(void *p){
  59.   pSbProgram pProgram;
  60.   char szInputFile[1024];
  61.   int iErrorCode;
  62.   struct _RunServiceProgram *pRSP;
  63.   pRSP = p;
  64.   strcpy(szInputFile,pRSP->pszProgramFileName);
  65.   pProgram = scriba_new(malloc,free);
  66.   pRSP->pTProgram = pProgram;
  67.   if( pProgram == NULL )return;
  68.   scriba_SetFileName(pProgram,szInputFile);
  69.   if (pRSP->pszConfigFileName != NULL){
  70.         strcpy(szInputFile,pRSP->pszConfigFileName);
  71.         scriba_LoadConfiguration(pProgram, pRSP->pszConfigFileName);
  72.   }else{
  73.         scriba_SetProcessSbObject(pProgram,pProgram);
  74.   }
  75.   scriba_LoadSourceProgram(pProgram);
  76.   if (pRSP->pszCmdLineArgs != NULL){
  77.         strcpy(szInputFile,pRSP->pszCmdLineArgs);
  78.     iErrorCode = scriba_Run(pProgram,pRSP->pszCmdLineArgs);
  79.   }else{
  80.     iErrorCode = scriba_Run(pProgram,NULL);
  81.   }
  82. //  scriba_destroy(pProgram);
  83.   return;
  84. }
  85.  
  86. besFUNCTION(SB_New)
  87.   DIM AS pSbProgram sbobj;
  88.   sbobj = scriba_new(malloc,free);
  89.   besRETURN_LONG(sbobj);
  90. besEND
  91.  
  92. besFUNCTION(SB_Configure)
  93.   DIM AS unsigned long sbobj;
  94.   DIM AS char PTR cfgfilename;
  95.   DIM AS int rtnval = -1;
  96.   besARGUMENTS("iz")
  97.     AT sbobj, AT cfgfilename
  98.   besARGEND
  99.   rtnval = scriba_LoadConfiguration(sbobj, cfgfilename);
  100.   besRETURN_LONG(rtnval);
  101. besEND
  102.  
  103. besFUNCTION(SB_Load)
  104.   DIM AS unsigned long sbobj;
  105.   DIM AS char PTR sbfilename;
  106.   DIM AS int rtnval = -1;
  107.   besARGUMENTS("iz")
  108.     AT sbobj, AT sbfilename
  109.   besARGEND
  110.   rtnval = scriba_SetFileName(sbobj, sbfilename);
  111.   scriba_LoadSourceProgram(sbobj);
  112.   besRETURN_LONG(rtnval);
  113. besEND
  114.  
  115. besFUNCTION(SB_LoadStr)
  116.   DIM AS unsigned long sbobj;
  117.   DIM AS char PTR sbpgm;
  118.   DIM AS int rtnval = -1;
  119.   besARGUMENTS("iz")
  120.     AT sbobj, AT sbpgm
  121.   besARGEND
  122.   scriba_SetFileName(sbobj, "fake");
  123.   rtnval = scriba_LoadProgramString(sbobj, sbpgm, strlen(sbpgm));
  124.   besRETURN_LONG(rtnval);
  125. besEND
  126.  
  127. besFUNCTION(SB_Run)
  128.   DIM AS unsigned long sbobj;
  129.   DIM AS int rtnval;
  130.   DIM AS char PTR sbcmdline;
  131.   besARGUMENTS("iz")
  132.     AT sbobj, AT sbcmdline
  133.   besARGEND
  134.   IF (besARGNR < 2) THEN_DO sbcmdline = "";
  135.   rtnval = scriba_Run(sbobj, sbcmdline);
  136.   besRETURN_LONG(rtnval);
  137. besEND
  138.  
  139. besFUNCTION(SB_NoRun)
  140.   DIM AS unsigned long sbobj;
  141.   DIM AS int rtnval;
  142.   besARGUMENTS("i")
  143.     AT sbobj
  144.   besARGEND
  145.   rtnval = scriba_NoRun(sbobj);
  146.   besRETURN_LONG(rtnval);
  147. besEND
  148.  
  149. besFUNCTION(SB_ThreadStart)
  150.   DIM AS struct _RunServiceProgram PTR pRSP;
  151.   DIM AS THREADHANDLE T;
  152.   DIM AS char PTR pszProgramFileName;
  153.   DIM AS char PTR pszCmdLineArgs;
  154.   DIM AS char PTR pszConfigFileName;
  155.   DIM AS unsigned long rtnval;
  156.   besARGUMENTS("z[z][z]")
  157.     AT pszProgramFileName, AT pszCmdLineArgs, AT pszConfigFileName
  158.   besARGEND
  159.   pRSP = (struct _RunServiceProgram PTR)malloc( sizeof(struct _RunServiceProgram) );
  160.   pRSP->pszProgramFileName = (char PTR)malloc(strlen(pszProgramFileName) + 1);
  161.   strcpy(pRSP->pszProgramFileName,pszProgramFileName);
  162.   IF (pszCmdLineArgs NE NULL) THEN
  163.     pRSP->pszCmdLineArgs = (char PTR)malloc(strlen(pszCmdLineArgs) + 1);
  164.     strcpy(pRSP->pszCmdLineArgs,pszCmdLineArgs);
  165.   ELSE
  166.         pRSP->pszCmdLineArgs = NULL;
  167.   END_IF
  168.   IF (pszConfigFileName NE NULL) THEN
  169.     pRSP->pszConfigFileName = (char PTR)malloc(strlen(pszConfigFileName) + 1);
  170.     strcpy(pRSP->pszConfigFileName,pszConfigFileName);
  171.   ELSE
  172.         pRSP->pszConfigFileName = NULL;
  173.   END_IF
  174.   pRSP->iRestart = 0;
  175.   thread_CreateThread(AT T,ExecuteProgramThread,pRSP);
  176.   usleep(500);
  177.   rtnval = pRSP->pTProgram;
  178.   besRETURN_LONG(rtnval);
  179. besEND
  180.  
  181. besFUNCTION(SB_ThreadEnd)
  182.   thread_ExitThread();
  183.   besRETURNVALUE = NULL;
  184. besEND
  185.  
  186. besFUNCTION(SB_Destroy)
  187.   DIM AS unsigned long sbobj;
  188.   besARGUMENTS("i")
  189.     AT sbobj
  190.   besARGEND
  191.   scriba_destroy(sbobj);
  192.   RETURN_FUNCTION(0);
  193. besEND
  194.  
  195. besFUNCTION(SB_CallSub)
  196.   DIM AS unsigned long sbobj;
  197.   DIM AS int funcsernum;
  198.   DIM AS char PTR funcname;
  199.   besARGUMENTS("iz")
  200.     AT sbobj, AT funcname
  201.   besARGEND
  202.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  203.   besRETURN_LONG(scriba_Call(sbobj, funcsernum));
  204. besEND
  205.  
  206. besFUNCTION(SB_CallSubArgs)
  207.   DIM AS VARIABLE Argument;
  208.   DIM AS SbData ArgData[8];
  209.   DIM AS SbData FunctionResult;
  210.   DIM AS unsigned long sbobj;
  211.   DIM AS char PTR funcname;
  212.   DIM AS int i, sbtype, fnsn;
  213.  
  214.   Argument = besARGUMENT(1);
  215.   besDEREFERENCE(Argument);
  216.   sbobj = LONGVALUE(Argument);
  217.  
  218.   Argument = besARGUMENT(2);
  219.   besDEREFERENCE(Argument);
  220.   funcname = STRINGVALUE(Argument);
  221.  
  222.   DEF_FOR (i = 3 TO i <= besARGNR STEP INCR i)
  223.   BEGIN_FOR
  224.     Argument = besARGUMENT(i);
  225.     besDEREFERENCE(Argument);
  226.     SELECT_CASE (sbtype = TYPE(Argument))
  227.     BEGIN_SELECT
  228.       CASE VTYPE_LONG:
  229.         ArgData[i-3] = PTR scriba_NewSbLong(sbobj, LONGVALUE(Argument));
  230.         END_CASE
  231.       CASE VTYPE_DOUBLE:
  232.         ArgData[i-3] = PTR scriba_NewSbDouble(sbobj, DOUBLEVALUE(Argument));
  233.         END_CASE
  234.       CASE VTYPE_STRING:
  235.         ArgData[i-3] = PTR scriba_NewSbString(sbobj, STRINGVALUE(Argument));
  236.         END_CASE
  237.       CASE_ELSE
  238.         ArgData[i-3] = PTR scriba_NewSbUndef(sbobj);
  239.         END_CASE
  240.     END_SELECT
  241.   NEXT
  242.  
  243.   fnsn = scriba_LookupFunctionByName(sbobj, funcname);
  244.   scriba_CallArgEx(sbobj, fnsn, AT FunctionResult, besARGNR - 2, AT ArgData);
  245.  
  246.   SELECT_CASE (FunctionResult.type)
  247.   BEGIN_SELECT
  248.     CASE SBT_LONG:
  249.       besRETURN_LONG(FunctionResult.v.l);
  250.       END_CASE
  251.     CASE SBT_DOUBLE:
  252.       besRETURN_DOUBLE(FunctionResult.v.d);
  253.       END_CASE
  254.     CASE SBT_STRING:
  255.       besRETURN_STRING(FunctionResult.v.s);
  256.       END_CASE
  257.     CASE SBT_UNDEF:
  258.       besRETURNVALUE = NULL;
  259.       END_CASE
  260.   END_SELECT
  261. besEND
  262.  
  263. besFUNCTION(SB_GetVar)
  264.   DIM AS pSbData varobj;
  265.   DIM AS unsigned long sbobj;
  266.   DIM AS int vsn;
  267.   DIM AS char PTR varname;
  268.   besARGUMENTS("iz")
  269.     AT sbobj, AT varname
  270.   besARGEND
  271.   vsn = scriba_LookupVariableByName(sbobj, varname);
  272.   scriba_GetVariable(sbobj, vsn, AT varobj);
  273.   SELECT_CASE (scriba_GetVariableType(sbobj, vsn))
  274.   BEGIN_SELECT
  275.     CASE SBT_LONG   :
  276.       besRETURN_LONG(varobj[0].v.l);
  277.       END_CASE
  278.     CASE SBT_DOUBLE :
  279.       besRETURN_DOUBLE(varobj[0].v.d);
  280.       END_CASE
  281.     CASE SBT_STRING :
  282.       besRETURN_STRING(varobj[0].v.s);
  283.       END_CASE
  284.     CASE SBT_UNDEF  :
  285.       besRETURNVALUE = NULL;
  286.       END_CASE
  287.   END_SELECT
  288. besEND
  289.  
  290. besFUNCTION(SB_SetUndef)
  291.   DIM AS pSbData varobj;
  292.   DIM AS unsigned long sbobj;
  293.   DIM AS int vsn;
  294.   DIM AS char PTR varname;
  295.   besARGUMENTS("iz")
  296.     AT sbobj, AT varname
  297.   besARGEND
  298.   vsn = scriba_LookupVariableByName(sbobj, varname);
  299.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn, SBT_UNDEF, NULL, 0, "", 0));
  300. besEND
  301.  
  302. besFUNCTION(SB_SetInt)
  303.   DIM AS VARIABLE Argument;
  304.   DIM AS pSbData varobj;
  305.   DIM AS unsigned long sbobj;
  306.   DIM AS int vsn, usrval, i;
  307.   DIM AS char PTR varname;
  308.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  309.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  310.   BEGIN_FOR
  311.     Argument = besARGUMENT(i);
  312.     besDEREFERENCE(Argument);
  313.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  314.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  315.     IF (i EQ 3) THEN_DO usrval = LONGVALUE(Argument);
  316.   NEXT
  317.   vsn = scriba_LookupVariableByName(sbobj, varname);
  318.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn, SBT_LONG, usrval, 0, "", 0));
  319. besEND
  320.  
  321. besFUNCTION(SB_SetDbl)
  322.   DIM AS VARIABLE Argument;
  323.   DIM AS pSbData varobj;
  324.   DIM AS unsigned long sbobj;
  325.   DIM AS int vsn, i;
  326.   DIM AS char PTR varname;
  327.   DIM AS double usrval;
  328.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  329.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  330.   BEGIN_FOR
  331.     Argument = besARGUMENT(i);
  332.     besDEREFERENCE(Argument);
  333.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  334.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  335.     IF (i EQ 3) THEN_DO usrval = DOUBLEVALUE(Argument);
  336.   NEXT
  337.   vsn = scriba_LookupVariableByName(sbobj, varname);
  338.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_DOUBLE, 0, usrval, "", 0));
  339. besEND
  340.  
  341. besFUNCTION(SB_SetStr)
  342.   DIM AS VARIABLE Argument;
  343.   DIM AS pSbData varobj;
  344.   DIM AS unsigned long sbobj;
  345.   DIM AS int vsn, i;
  346.   DIM AS char PTR varname;
  347.   DIM AS char PTR usrval;
  348.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  349.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  350.   BEGIN_FOR
  351.     Argument = besARGUMENT(i);
  352.     besDEREFERENCE(Argument);
  353.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  354.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  355.     IF (i EQ 3) THEN_DO usrval = STRINGVALUE(Argument);
  356.   NEXT
  357.   vsn = scriba_LookupVariableByName(sbobj, varname);
  358.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_STRING, 0, 0, usrval, strlen(usrval)));
  359. besEND
  360.  
  361. besFUNCTION(SB_ResetVars)
  362.   DIM AS unsigned long sbobj;
  363.   besARGUMENTS("i")
  364.     AT sbobj
  365.   besARGEND
  366.   scriba_ResetVariables(sbobj);
  367.   besRETURNVALUE = NULL;
  368. besEND
  369.  
  370. besFUNCTION(SB_msSleep)
  371.   //DIM AS int msval, t;
  372.   DIM AS long t;
  373.   besARGUMENTS("i")
  374.     AT t
  375.   besARGEND
  376.   usleep(t);
  377.   besRETURNVALUE = NULL;
  378. besEND
  379.  

sbt.inc - interpreter extension module include
Code: Script BASIC
  1. DECLARE SUB SB_New ALIAS "SB_New" LIB "sbt"
  2. DECLARE SUB SB_Configure ALIAS "SB_Configure" LIB "sbt"
  3. DECLARE SUB SB_Load ALIAS "SB_Load" LIB "sbt"
  4. DECLARE SUB SB_LoadStr ALIAS "SB_LoadStr" LIB "sbt"
  5. DECLARE SUB SB_Run ALIAS "SB_Run" LIB "sbt"
  6. DECLARE SUB SB_NoRun ALIAS "SB_NoRun" LIB "sbt"
  7. DECLARE SUB SB_ThreadStart ALIAS "SB_ThreadStart" LIB "sbt"
  8. DECLARE SUB SB_ThreadEnd ALIAS "SB_ThreadEnd" LIB "sbt"
  9. DECLARE SUB SB_GetVar ALIAS "SB_GetVar" LIB "sbt"
  10. DECLARE SUB SB_SetUndef ALIAS "SB_SetUndef" LIB "sbt"
  11. DECLARE SUB SB_SetInt ALIAS "SB_SetInt" LIB "sbt"
  12. DECLARE SUB SB_SetDbl ALIAS "SB_SetDbl" LIB "sbt"
  13. DECLARE SUB SB_SetStr ALIAS "SB_SetStr" LIB "sbt"
  14. DECLARE SUB SB_ResetVars ALIAS "SB_ResetVars" LIB "sbt"
  15. DECLARE SUB SB_CallSub ALIAS "SB_CallSub" LIB "sbt"
  16. DECLARE SUB SB_CallSubArgs ALIAS "SB_CallSubArgs" LIB "sbt"
  17. DECLARE SUB SB_Destroy ALIAS "SB_Destroy" LIB "sbt"
  18.  

sbtdemo.sb
Code: Script BASIC
  1. ' SBT (ScriptBasic Threads) - Example Script
  2.  
  3. IMPORT sbt.inc
  4.  
  5. sb_code = """
  6. FUNCTION prtvars(a, b, c)
  7.  PRINT a,"\\n"
  8.  PRINT FORMAT("%g\\n", b)
  9.  PRINT c,"\\n"
  10.  prtvars = "Function Return"
  11. END FUNCTION
  12.  
  13. a = 0
  14. b = 0
  15. c = ""
  16. """
  17.  
  18. sb = SB_New()
  19. SB_Configure sb, "C:/Windows/SCRIBA.INI"
  20. SB_LoadStr sb, sb_code
  21. SB_NoRun sb
  22. funcrtn = SB_CallSubArgs(sb,"main::prtvars", 123, 1.23, "One, Two, Three")
  23. PRINT funcrtn,"\n"
  24. SB_Run sb, ""
  25. SB_SetInt sb, "main::a", 321
  26. SB_SetDbl sb, "main::b", 32.1
  27. SB_SetStr sb, "main::c", "Three,Two,One"
  28. SB_CallSubArgs sb, "main::prtvars", _
  29.           SB_GetVar(sb, "main::a"), _
  30.           SB_GetVar(sb, "main::b"), _
  31.           SB_GetVar(sb, "main::c")      
  32. SB_Destroy sb
  33.  

Output

123
1.23
One, Two, Three
Function Return
321
32.1
Three,Two,One



The goal is to create a Nim interface with the same function calls as in the ScriptBasic SBT include file. The first step is to convert the SBT shared object interface into a generic call format.
« Last Edit: December 12, 2019, 02:04:38 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 162
    • View Profile
    • ScriptBasic
Re: Nim SB
« Reply #2 on: December 12, 2019, 08:01:53 PM »
Silver lining is once I have the generic call conversion completed, the ScriptBasic shared object can be embedded in any language supporting a FFI interface. (compiled and interpretative)
« Last Edit: December 12, 2019, 08:47:19 PM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 162
    • View Profile
    • ScriptBasic
Re: Nim SB
« Reply #3 on: December 13, 2019, 07:49:45 AM »
I still have a few functions that need to be broken apart or reworked,

SB_CallSubArgs
SB_GetVar
SB_SetDbl
SB_SetStr

sbobj.c
Code: C
  1. // ScriptBasic Shared Object
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <math.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include "/home/ubuntu/sb-dev-master/basext.h"
  11. #include "/home/ubuntu/sb-dev-master/scriba.h"
  12. #include "cbasic.h"
  13.  
  14.  
  15. /***********************
  16.  Statc Support Routines
  17. ***********************/
  18.  
  19. struct _RunServiceProgram {
  20.   char *pszProgramFileName;
  21.   char *pszCmdLineArgs;
  22.   char *pszConfigFileName;
  23.   pSbProgram pTProgram;
  24.   int iRestart;
  25.   };
  26.  
  27. static void ExecuteProgramThread(void *p){
  28.   pSbProgram pProgram;
  29.   char szInputFile[1024];
  30.   int iErrorCode;
  31.   struct _RunServiceProgram *pRSP;
  32.   pRSP = p;
  33.   strcpy(szInputFile,pRSP->pszProgramFileName);
  34.   pProgram = scriba_new(malloc,free);
  35.   pRSP->pTProgram = pProgram;
  36.   if( pProgram == NULL )return;
  37.   scriba_SetFileName(pProgram,szInputFile);
  38.   if (pRSP->pszConfigFileName != NULL){
  39.         strcpy(szInputFile,pRSP->pszConfigFileName);
  40.         scriba_LoadConfiguration(pProgram, pRSP->pszConfigFileName);
  41.   }else{
  42.         scriba_SetProcessSbObject(pProgram,pProgram);
  43.   }
  44.   scriba_LoadSourceProgram(pProgram);
  45.   if (pRSP->pszCmdLineArgs != NULL){
  46.         strcpy(szInputFile,pRSP->pszCmdLineArgs);
  47.     iErrorCode = scriba_Run(pProgram,pRSP->pszCmdLineArgs);
  48.   }else{
  49.     iErrorCode = scriba_Run(pProgram,NULL);
  50.   }
  51. //  scriba_destroy(pProgram);
  52.   return;
  53. }
  54.  
  55.  
  56. /*****************************
  57.  ScriptBasic Object Functions
  58. *****************************/
  59.  
  60. FUNCTION long SB_New()
  61. BEGIN_FUNCTION
  62.   DIM AS pSbProgram sbobj;
  63.   sbobj = scriba_new(malloc,free);
  64.   RETURN_FUNCTION(sbobj);
  65. END_FUNCTION
  66.  
  67. FUNCTION int SB_Configure(unsigned long sbobj, char PTR cfgfilename)
  68. BEGIN_FUNCTION
  69.   DIM AS int rtnval = -1;
  70.   rtnval = scriba_LoadConfiguration(sbobj, cfgfilename);
  71.   RETURN_FUNCTION(rtnval);
  72. END_FUNCTION
  73.  
  74. FUNCTION int SB_Load(unsigned long sbobj, char PTR sbfilename)
  75. BEGIN_FUNCTION
  76.   DIM AS int rtnval = -1;
  77.   rtnval = scriba_SetFileName(sbobj, sbfilename);
  78.   scriba_LoadSourceProgram(sbobj);
  79.   RETURN_FUNCTION(rtnval);
  80. END_FUNCTION
  81.  
  82. FUNCTION int SB_LoadStr(unsigned long sbobj, char PTR sbpgm)
  83. BEGIN_FUNCTION
  84.   DIM AS int rtnval = -1;
  85.   scriba_SetFileName(sbobj, "fake");
  86.   rtnval = scriba_LoadProgramString(sbobj, sbpgm, strlen(sbpgm));
  87.   RETURN_FUNCTION(rtnval);
  88. END_FUNCTION
  89.  
  90. FUNCTION int SB_Run(unsigned long sbobj, char PTR sbcmdline)
  91. BEGIN_FUNCTION
  92.   DIM AS int rtnval;
  93.   IF (besARGNR < 2) THEN_DO sbcmdline = "";
  94.   rtnval = scriba_Run(sbobj, sbcmdline);
  95.   RETURN_FUNCTION(rtnval);
  96. END_FUNCTION
  97.  
  98. FUNCTION int SB_NoRun(unsigned long sbobj)
  99. BEGIN_FUNCTION
  100.   DIM AS int rtnval;
  101.   rtnval = scriba_NoRun(sbobj);
  102.   RETURN_FUNCTION(rtnval);
  103. END_FUNCTION
  104.  
  105. FUNCTION int SB_ThreadStart(char PTR pszProgramFileName, char PTR pszCmdLineArgs, char PTR pszConfigFileName)
  106. BEGIN_FUNCTION
  107.   DIM AS struct _RunServiceProgram PTR pRSP;
  108.   DIM AS THREADHANDLE T;
  109.   DIM AS unsigned long rtnval;
  110.   pRSP = (struct _RunServiceProgram PTR)malloc( sizeof(struct _RunServiceProgram) );
  111.   pRSP->pszProgramFileName = (char PTR)malloc(strlen(pszProgramFileName) + 1);
  112.   strcpy(pRSP->pszProgramFileName,pszProgramFileName);
  113.   IF (pszCmdLineArgs NE NULL) THEN
  114.     pRSP->pszCmdLineArgs = (char PTR)malloc(strlen(pszCmdLineArgs) + 1);
  115.     strcpy(pRSP->pszCmdLineArgs,pszCmdLineArgs);
  116.   ELSE
  117.         pRSP->pszCmdLineArgs = NULL;
  118.   END_IF
  119.   IF (pszConfigFileName NE NULL) THEN
  120.     pRSP->pszConfigFileName = (char PTR)malloc(strlen(pszConfigFileName) + 1);
  121.     strcpy(pRSP->pszConfigFileName,pszConfigFileName);
  122.   ELSE
  123.         pRSP->pszConfigFileName = NULL;
  124.   END_IF
  125.   pRSP->iRestart = 0;
  126.   thread_CreateThread(AT T,ExecuteProgramThread,pRSP);
  127.   usleep(500);
  128.   rtnval = pRSP->pTProgram;
  129.   RETURN_FUNCTION(rtnval);
  130. END_FUNCTION
  131.  
  132. SUB SB_ThreadEnd()
  133. BEGIN_SUB
  134.   thread_ExitThread();
  135. END_SUB
  136.  
  137. SUB SB_Destroy(unsigned long sbobj)
  138.   scriba_destroy(sbobj);
  139. END_SUB
  140.  
  141. FUNCTION int SB_CallSub(unsigned long sbobj, char PTR funcname)
  142.   DIM AS int funcsernum;
  143.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  144.   RETURN_FUNCTION(scriba_Call(sbobj, funcsernum));
  145. END_FUNCTION
  146.  
  147. besFUNCTION(SB_CallSubArgs)
  148.   DIM AS VARIABLE Argument;
  149.   DIM AS SbData ArgData[8];
  150.   DIM AS SbData FunctionResult;
  151.   DIM AS unsigned long sbobj;
  152.   DIM AS char PTR funcname;
  153.   DIM AS int i, sbtype, fnsn;
  154.  
  155.   Argument = besARGUMENT(1);
  156.   besDEREFERENCE(Argument);
  157.   sbobj = LONGVALUE(Argument);
  158.  
  159.   Argument = besARGUMENT(2);
  160.   besDEREFERENCE(Argument);
  161.   funcname = STRINGVALUE(Argument);
  162.  
  163.   DEF_FOR (i = 3 TO i <= besARGNR STEP INCR i)
  164.   BEGIN_FOR
  165.     Argument = besARGUMENT(i);
  166.     besDEREFERENCE(Argument);
  167.     SELECT_CASE (sbtype = TYPE(Argument))
  168.     BEGIN_SELECT
  169.       CASE VTYPE_LONG:
  170.         ArgData[i-3] = PTR scriba_NewSbLong(sbobj, LONGVALUE(Argument));
  171.         END_CASE
  172.       CASE VTYPE_DOUBLE:
  173.         ArgData[i-3] = PTR scriba_NewSbDouble(sbobj, DOUBLEVALUE(Argument));
  174.         END_CASE
  175.       CASE VTYPE_STRING:
  176.         ArgData[i-3] = PTR scriba_NewSbString(sbobj, STRINGVALUE(Argument));
  177.         END_CASE
  178.       CASE_ELSE
  179.         ArgData[i-3] = PTR scriba_NewSbUndef(sbobj);
  180.         END_CASE
  181.     END_SELECT
  182.   NEXT
  183.  
  184.   fnsn = scriba_LookupFunctionByName(sbobj, funcname);
  185.   scriba_CallArgEx(sbobj, fnsn, AT FunctionResult, besARGNR - 2, AT ArgData);
  186.  
  187.   SELECT_CASE (FunctionResult.type)
  188.   BEGIN_SELECT
  189.     CASE SBT_LONG:
  190.       besRETURN_LONG(FunctionResult.v.l);
  191.       END_CASE
  192.     CASE SBT_DOUBLE:
  193.       besRETURN_DOUBLE(FunctionResult.v.d);
  194.       END_CASE
  195.     CASE SBT_STRING:
  196.       besRETURN_STRING(FunctionResult.v.s);
  197.       END_CASE
  198.     CASE SBT_UNDEF:
  199.       besRETURNVALUE = NULL;
  200.       END_CASE
  201.   END_SELECT
  202. besEND
  203.  
  204. besFUNCTION(SB_GetVar)
  205.   DIM AS pSbData varobj;
  206.   DIM AS unsigned long sbobj;
  207.   DIM AS int vsn;
  208.   DIM AS char PTR varname;
  209.   besARGUMENTS("iz")
  210.     AT sbobj, AT varname
  211.   besARGEND
  212.   vsn = scriba_LookupVariableByName(sbobj, varname);
  213.   scriba_GetVariable(sbobj, vsn, AT varobj);
  214.   SELECT_CASE (scriba_GetVariableType(sbobj, vsn))
  215.   BEGIN_SELECT
  216.     CASE SBT_LONG   :
  217.       besRETURN_LONG(varobj[0].v.l);
  218.       END_CASE
  219.     CASE SBT_DOUBLE :
  220.       besRETURN_DOUBLE(varobj[0].v.d);
  221.       END_CASE
  222.     CASE SBT_STRING :
  223.       besRETURN_STRING(varobj[0].v.s);
  224.       END_CASE
  225.     CASE SBT_UNDEF  :
  226.       besRETURNVALUE = NULL;
  227.       END_CASE
  228.   END_SELECT
  229. besEND
  230.  
  231. FUNCTION int SB_SetUndef(unsigned long sbobj, char PTR varname)
  232. BEGIN_FUNCTION
  233.   DIM AS pSbData varobj;
  234.   DIM AS int vsn;
  235.   vsn = scriba_LookupVariableByName(sbobj, varname);
  236.   RETURN_FUNCTION(scriba_SetVariable(sbobj, vsn, SBT_UNDEF, NULL, 0, "", 0));
  237. END_FUNCTION
  238.  
  239. FUNCTION int SB_SetInt()
  240. BEGIN_FUNCTION
  241.   DIM AS VARIABLE Argument;
  242.   DIM AS pSbData varobj;
  243.   DIM AS unsigned long sbobj;
  244.   DIM AS int vsn, usrval, i;
  245.   DIM AS char PTR varname;
  246.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  247.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  248.   BEGIN_FOR
  249.     Argument = besARGUMENT(i);
  250.     besDEREFERENCE(Argument);
  251.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  252.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  253.     IF (i EQ 3) THEN_DO usrval = LONGVALUE(Argument);
  254.   NEXT
  255.   vsn = scriba_LookupVariableByName(sbobj, varname);
  256.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn, SBT_LONG, usrval, 0, "", 0));
  257. besEND
  258.  
  259. besFUNCTION(SB_SetDbl)
  260.   DIM AS VARIABLE Argument;
  261.   DIM AS pSbData varobj;
  262.   DIM AS unsigned long sbobj;
  263.   DIM AS int vsn, i;
  264.   DIM AS char PTR varname;
  265.   DIM AS double usrval;
  266.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  267.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  268.   BEGIN_FOR
  269.     Argument = besARGUMENT(i);
  270.     besDEREFERENCE(Argument);
  271.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  272.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  273.     IF (i EQ 3) THEN_DO usrval = DOUBLEVALUE(Argument);
  274.   NEXT
  275.   vsn = scriba_LookupVariableByName(sbobj, varname);
  276.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_DOUBLE, 0, usrval, "", 0));
  277. besEND
  278.  
  279. besFUNCTION(SB_SetStr)
  280.   DIM AS VARIABLE Argument;
  281.   DIM AS pSbData varobj;
  282.   DIM AS unsigned long sbobj;
  283.   DIM AS int vsn, i;
  284.   DIM AS char PTR varname;
  285.   DIM AS char PTR usrval;
  286.   IF (besARGNR < 3) THEN_DO RETURN_FUNCTION(EX_ERROR_TOO_FEW_ARGUMENTS);
  287.   DEF_FOR (i = 1 TO i <= 3 STEP INCR i)
  288.   BEGIN_FOR
  289.     Argument = besARGUMENT(i);
  290.     besDEREFERENCE(Argument);
  291.     IF (i EQ 1) THEN_DO sbobj = LONGVALUE(Argument);
  292.     IF (i EQ 2) THEN_DO varname = STRINGVALUE(Argument);
  293.     IF (i EQ 3) THEN_DO usrval = STRINGVALUE(Argument);
  294.   NEXT
  295.   vsn = scriba_LookupVariableByName(sbobj, varname);
  296.   besRETURN_LONG(scriba_SetVariable(sbobj, vsn,  SBT_STRING, 0, 0, usrval, strlen(usrval)));
  297. besEND
  298.  
  299. SUB SB_ResetVars(unsigned long sbobj)
  300. BEGIN_SUB
  301.   scriba_ResetVariables(sbobj);
  302. END_SUB
  303.  
  304. SUB SB_msSleep(long t)
  305. BEGIN_SUB
  306.   usleep(t);
  307. END_SUB
  308.  
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 162
    • View Profile
    • ScriptBasic
Re: Nim SB
« Reply #4 on: December 14, 2019, 04:20:00 AM »
AIR,

It looks like I'm going to need a varidac style function to handle the variable number of arguments for the SB_CallSubArgs function, (last function in  the code) The various return types is also an issue.

THIS post on AllBASIC may refresh our nmemories.

If you know a better way, I'm all ears.

Code: C
  1. // ScriptBasic Shared Object
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <math.h>
  8. #include <time.h>
  9. #include <unistd.h>
  10. #include "/home/ubuntu/sb-dev-master/basext.h"
  11. #include "/home/ubuntu/sb-dev-master/scriba.h"
  12. #include "cbasic.h"
  13.  
  14.  
  15. /***********************
  16.  Statc Support Routines
  17. ***********************/
  18.  
  19. struct _RunServiceProgram {
  20.   char *pszProgramFileName;
  21.   char *pszCmdLineArgs;
  22.   char *pszConfigFileName;
  23.   pSbProgram pTProgram;
  24.   int iRestart;
  25.   };
  26.  
  27. static void ExecuteProgramThread(void *p){
  28.   pSbProgram pProgram;
  29.   char szInputFile[1024];
  30.   int iErrorCode;
  31.   struct _RunServiceProgram *pRSP;
  32.   pRSP = p;
  33.   strcpy(szInputFile,pRSP->pszProgramFileName);
  34.   pProgram = scriba_new(malloc,free);
  35.   pRSP->pTProgram = pProgram;
  36.   if( pProgram == NULL )return;
  37.   scriba_SetFileName(pProgram,szInputFile);
  38.   if (pRSP->pszConfigFileName != NULL){
  39.         strcpy(szInputFile,pRSP->pszConfigFileName);
  40.         scriba_LoadConfiguration(pProgram, pRSP->pszConfigFileName);
  41.   }else{
  42.         scriba_SetProcessSbObject(pProgram,pProgram);
  43.   }
  44.   scriba_LoadSourceProgram(pProgram);
  45.   if (pRSP->pszCmdLineArgs != NULL){
  46.         strcpy(szInputFile,pRSP->pszCmdLineArgs);
  47.     iErrorCode = scriba_Run(pProgram,pRSP->pszCmdLineArgs);
  48.   }else{
  49.     iErrorCode = scriba_Run(pProgram,NULL);
  50.   }
  51. //  scriba_destroy(pProgram);
  52.   return;
  53. }
  54.  
  55.  
  56. /*****************************
  57.  ScriptBasic Object Functions
  58. *****************************/
  59.  
  60. FUNCTION long SB_New()
  61. BEGIN_FUNCTION
  62.   DIM AS pSbProgram sbobj;
  63.   sbobj = scriba_new(malloc,free);
  64.   RETURN_FUNCTION(sbobj);
  65. END_FUNCTION
  66.  
  67. FUNCTION int SB_Configure(unsigned long sbobj, char PTR cfgfilename)
  68. BEGIN_FUNCTION
  69.   DIM AS int rtnval = -1;
  70.   rtnval = scriba_LoadConfiguration(sbobj, cfgfilename);
  71.   RETURN_FUNCTION(rtnval);
  72. END_FUNCTION
  73.  
  74. FUNCTION int SB_Load(unsigned long sbobj, char PTR sbfilename)
  75. BEGIN_FUNCTION
  76.   DIM AS int rtnval = -1;
  77.   rtnval = scriba_SetFileName(sbobj, sbfilename);
  78.   scriba_LoadSourceProgram(sbobj);
  79.   RETURN_FUNCTION(rtnval);
  80. END_FUNCTION
  81.  
  82. FUNCTION int SB_LoadStr(unsigned long sbobj, char PTR sbpgm)
  83. BEGIN_FUNCTION
  84.   DIM AS int rtnval = -1;
  85.   scriba_SetFileName(sbobj, "fake");
  86.   rtnval = scriba_LoadProgramString(sbobj, sbpgm, strlen(sbpgm));
  87.   RETURN_FUNCTION(rtnval);
  88. END_FUNCTION
  89.  
  90. FUNCTION int SB_Run(unsigned long sbobj, char PTR sbcmdline)
  91. BEGIN_FUNCTION
  92.   DIM AS int rtnval;
  93.   IF (besARGNR < 2) THEN_DO sbcmdline = "";
  94.   rtnval = scriba_Run(sbobj, sbcmdline);
  95.   RETURN_FUNCTION(rtnval);
  96. END_FUNCTION
  97.  
  98. FUNCTION int SB_NoRun(unsigned long sbobj)
  99. BEGIN_FUNCTION
  100.   DIM AS int rtnval;
  101.   rtnval = scriba_NoRun(sbobj);
  102.   RETURN_FUNCTION(rtnval);
  103. END_FUNCTION
  104.  
  105. FUNCTION int SB_ThreadStart(char PTR pszProgramFileName, char PTR pszCmdLineArgs, char PTR pszConfigFileName)
  106. BEGIN_FUNCTION
  107.   DIM AS struct _RunServiceProgram PTR pRSP;
  108.   DIM AS THREADHANDLE T;
  109.   DIM AS unsigned long rtnval;
  110.   pRSP = (struct _RunServiceProgram PTR)malloc( sizeof(struct _RunServiceProgram) );
  111.   pRSP->pszProgramFileName = (char PTR)malloc(strlen(pszProgramFileName) + 1);
  112.   strcpy(pRSP->pszProgramFileName,pszProgramFileName);
  113.   IF (pszCmdLineArgs NE NULL) THEN
  114.     pRSP->pszCmdLineArgs = (char PTR)malloc(strlen(pszCmdLineArgs) + 1);
  115.     strcpy(pRSP->pszCmdLineArgs,pszCmdLineArgs);
  116.   ELSE
  117.         pRSP->pszCmdLineArgs = NULL;
  118.   END_IF
  119.   IF (pszConfigFileName NE NULL) THEN
  120.     pRSP->pszConfigFileName = (char PTR)malloc(strlen(pszConfigFileName) + 1);
  121.     strcpy(pRSP->pszConfigFileName,pszConfigFileName);
  122.   ELSE
  123.         pRSP->pszConfigFileName = NULL;
  124.   END_IF
  125.   pRSP->iRestart = 0;
  126.   thread_CreateThread(AT T,ExecuteProgramThread,pRSP);
  127.   usleep(500);
  128.   rtnval = pRSP->pTProgram;
  129.   RETURN_FUNCTION(rtnval);
  130. END_FUNCTION
  131.  
  132. SUB SB_ThreadEnd()
  133. BEGIN_SUB
  134.   thread_ExitThread();
  135. END_SUB
  136.  
  137. SUB SB_Destroy(unsigned long sbobj)
  138.   scriba_destroy(sbobj);
  139. END_SUB
  140.  
  141. FUNCTION long SB_GetLong(unsigned long sbobj, char PTR varname)
  142. BEGIN_FUNCTION
  143.   DIM AS pSbData varobj;
  144.   DIM AS int vsn;
  145.   vsn = scriba_LookupVariableByName(sbobj, varname);
  146.   scriba_GetVariable(sbobj, vsn, AT varobj);
  147.   RETURN_FUNCTION(varobj[0].v.l);
  148. END_FUNCTION
  149.  
  150. FUNCTION double SB_GetDouble(unsigned long sbobj, char PTR varname)
  151. BEGIN_FUNCTION
  152.   DIM AS pSbData varobj;
  153.   DIM AS int vsn;
  154.   vsn = scriba_LookupVariableByName(sbobj, varname);
  155.   scriba_GetVariable(sbobj, vsn, AT varobj);
  156.   RETURN_FUNCTION(varobj[0].v.d);
  157. END_FUNCTION
  158.  
  159. FUNCTION PTR char SB_GetString(unsigned long sbobj, char PTR varname)
  160. BEGIN_FUNCTION
  161.   DIM AS pSbData varobj;
  162.   DIM AS int vsn;
  163.   vsn = scriba_LookupVariableByName(sbobj, varname);
  164.   scriba_GetVariable(sbobj, vsn, AT varobj);
  165.   RETURN_FUNCTION(varobj[0].v.s);
  166. END_FUNCTION
  167.  
  168. FUNCTION int SB_SetUndef(unsigned long sbobj, char PTR varname)
  169. BEGIN_FUNCTION
  170.   DIM AS pSbData varobj;
  171.   DIM AS int vsn;
  172.   vsn = scriba_LookupVariableByName(sbobj, varname);
  173.   RETURN_FUNCTION(scriba_SetVariable(sbobj, vsn, SBT_UNDEF, NULL, 0, "", 0));
  174. END_FUNCTION
  175.  
  176. FUNCTION int SB_SetInt(unsigned long sbobj, char PTR varname, int usrval)
  177. BEGIN_FUNCTION
  178.   DIM AS pSbData varobj;
  179.   DIM AS int vsn;
  180.   vsn = scriba_LookupVariableByName(sbobj, varname);
  181.   RETURN_FUNCTION(scriba_SetVariable(sbobj, vsn, SBT_LONG, usrval, 0, "", 0));
  182. END_FUNCTION
  183.  
  184. FUNCTION int SB_SetDbl(unsigned long sbobj, char PTR varname, double usrval)
  185. BEGIN_FUNCTION
  186.   DIM AS pSbData varobj;
  187.   DIM AS int vsn;
  188.   vsn = scriba_LookupVariableByName(sbobj, varname);
  189.   RETURN_FUNCTION(scriba_SetVariable(sbobj, vsn,  SBT_DOUBLE, 0, usrval, "", 0));
  190. END_FUNCTION
  191.  
  192. FUNCTION int SB_SetStr(unsigned long sbobj, char PTR varname, char PTR usrval)
  193. BEGIN_FUNCTION
  194.   DIM AS pSbData varobj;
  195.   DIM AS int vsn;
  196.   vsn = scriba_LookupVariableByName(sbobj, varname);
  197.   RETURN_FUNCTION(scriba_SetVariable(sbobj, vsn,  SBT_STRING, 0, 0, usrval, strlen(usrval)));
  198. END_FUNCTION
  199.  
  200. SUB SB_ResetVars(unsigned long sbobj)
  201. BEGIN_SUB
  202.   scriba_ResetVariables(sbobj);
  203. END_SUB
  204.  
  205. SUB SB_msSleep(long t)
  206. BEGIN_SUB
  207.   usleep(t);
  208. END_SUB
  209.  
  210. FUNCTION int SB_CallSub(unsigned long sbobj, char PTR funcname)
  211.   DIM AS int funcsernum;
  212.   funcsernum = scriba_LookupFunctionByName(sbobj, funcname);
  213.   RETURN_FUNCTION(scriba_Call(sbobj, funcsernum));
  214. END_FUNCTION
  215.  
  216. besFUNCTION(SB_CallSubArgs)
  217.   DIM AS VARIABLE Argument;
  218.   DIM AS SbData ArgData[8];
  219.   DIM AS SbData FunctionResult;
  220.   DIM AS unsigned long sbobj;
  221.   DIM AS char PTR funcname;
  222.   DIM AS int i, sbtype, fnsn;
  223.  
  224.   Argument = besARGUMENT(1);
  225.   besDEREFERENCE(Argument);
  226.   sbobj = LONGVALUE(Argument);
  227.  
  228.   Argument = besARGUMENT(2);
  229.   besDEREFERENCE(Argument);
  230.   funcname = STRINGVALUE(Argument);
  231.  
  232.   DEF_FOR (i = 3 TO i <= besARGNR STEP INCR i)
  233.   BEGIN_FOR
  234.     Argument = besARGUMENT(i);
  235.     besDEREFERENCE(Argument);
  236.     SELECT_CASE (sbtype = TYPE(Argument))
  237.     BEGIN_SELECT
  238.       CASE VTYPE_LONG:
  239.         ArgData[i-3] = PTR scriba_NewSbLong(sbobj, LONGVALUE(Argument));
  240.         END_CASE
  241.       CASE VTYPE_DOUBLE:
  242.         ArgData[i-3] = PTR scriba_NewSbDouble(sbobj, DOUBLEVALUE(Argument));
  243.         END_CASE
  244.       CASE VTYPE_STRING:
  245.         ArgData[i-3] = PTR scriba_NewSbString(sbobj, STRINGVALUE(Argument));
  246.         END_CASE
  247.       CASE_ELSE
  248.         ArgData[i-3] = PTR scriba_NewSbUndef(sbobj);
  249.         END_CASE
  250.     END_SELECT
  251.   NEXT
  252.  
  253.   fnsn = scriba_LookupFunctionByName(sbobj, funcname);
  254.   scriba_CallArgEx(sbobj, fnsn, AT FunctionResult, besARGNR - 2, AT ArgData);
  255.  
  256.   SELECT_CASE (FunctionResult.type)
  257.   BEGIN_SELECT
  258.     CASE SBT_LONG:
  259.       besRETURN_LONG(FunctionResult.v.l);
  260.       END_CASE
  261.     CASE SBT_DOUBLE:
  262.       besRETURN_DOUBLE(FunctionResult.v.d);
  263.       END_CASE
  264.     CASE SBT_STRING:
  265.       besRETURN_STRING(FunctionResult.v.s);
  266.       END_CASE
  267.     CASE SBT_UNDEF:
  268.       besRETURNVALUE = NULL;
  269.       END_CASE
  270.   END_SELECT
  271. besEND
  272.  
« Last Edit: December 14, 2019, 05:28:26 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 162
    • View Profile
    • ScriptBasic
Re: Nim SB
« Reply #5 on: December 14, 2019, 05:36:43 AM »
Here is a ScriptBasic embedded example using MBC.

$execon "-I/home/jrs/sb/source /home/jrs/sb/source/lmt_none.c -lscriba"
 
#include <scriba.h>
#include <getopt.h>
 
Dim pProgram As pSbProgram
' Dim qdat As pSbData
Dim v As long
' Dim m As long
Dim f1 As long
Dim f2 As long
Dim dVal=11 as long
Dim cArgs As long
Dim ReturnData As SbData
dim ArgData[4] As SbData
 
' LOADING AND RUNNING THE PROGRAM
pProgram = scriba_new(malloc, free)
scriba_SetFileName(pProgram, "E03.bas")
scriba_LoadSourceProgram(pProgram)
scriba_NoRun(pProgram)
 
'' ACCESSING GLOBAL DATA
v = scriba_LookupVariableByName(pProgram, "main::a")
 
'** NO NEED TO PASS THE VARIABLE TO THE qdat STRUCT
'scriba_GetVariable(pProgram, v, &qdat)
 
'** NO NEED FOR EXTRA VARIABLE BELOW (OR A SBDATA POINTER TO qdat)
'~ m = qdat->v.l + 100
 
'** BECAUSE WE CAN SET THE VALUE IN THE STRUCT DIRECTLY, IF WE WANT
'qdat->v.l=100
 
'** THEN PASS IT TO THE FUNCTION
'scriba_SetVariable(pProgram, v, 2, qdat->v.l, 0, "", 0)
 
'** OR WE CAN PASS THE VALUE WE WANT TO SET TO THE RETRIEVED
'** SCRIPTBASIC "a" VARIABLE >>DIRECTLY<< IN THE FOURTH PARAMETER
scriba_SetVariable(pProgram, v, 2, 500, 0, "", 0)
 
'' CALLING SIMPLE SUBROUTINE
f1 = scriba_LookupFunctionByName(pProgram, "main::dprint")
scriba_Call(pProgram, f1)
 
' CALLING FUNCTION, RETURNING DATA AND GETTING ALTERED PARAMS
f2 = scriba_LookupFunctionByName(pProgram, "main::eprint")
 
' SETUP ARGUMENTS - these can be used for both input and output
 
' ArgData[0].type = SBT_DOUBLE
' ArgData[1].type = SBT_DOUBLE
' ArgData[2].type = SBT_DOUBLE
' ArgData[3].type = SBT_DOUBLE
'
' ArgData[0].size = 0
' ArgData[1].size = 0
' ArgData[2].size = 0
' ArgData[3].size = 0
'
' ArgData[0].v.d = 11
' ArgData[1].v.d = 12
' ArgData[2].v.d = 13
' ArgData[3].v.d = 14
'
' cArgs = 4
 
' SETUP ARGUMENTS - shorter code
 
for cArgs=0 to 3
  ArgData[cArgs].type = SBT_DOUBLE
  ArgData[cArgs].size = 0
  ArgData[cArgs].v.d = dVal+cArgs
next
 
scriba_CallArgEx(pProgram, f2, &ReturnData, cArgs, ArgData)
print "Return type:",ReturnData.type
print "Value:";
 
' READ RETURNED VALUE
select case ReturnData.type
 case SBT_UNDEF  :                print "Undefined"
 case SBT_DOUBLE :                print ReturnData.v.d
 case SBT_LONG   :                print ReturnData.v.l
 case =SBT_STRING or =SBT_ZCHAR : print (CHAR PTR)ReturnData.v.s
end select
 
scriba_destroy(pProgram)
« Last Edit: December 14, 2019, 05:38:42 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator

John Spikowski

  • BASIC Developer
  • ***
  • Posts: 162
    • View Profile
    • ScriptBasic
Re: Nim SB
« Reply #6 on: December 15, 2019, 04:29:27 AM »
I'm beginning to think the best way to deal with calling ScriptBasic functions is to pass and return sbdata structures. SB as the host made this a seamless call.

I think I'm going to create a SB_DataGet and SB_DataSet which will use a static SBData structure the exported functions will use. I don't want to force the host to use C data structures as arguments.
« Last Edit: December 15, 2019, 05:48:52 AM by John Spikowski »
ScriptBasic Project Manager/Facilitator