Author Topic: FreeBASIC Embedded Scripting Engine  (Read 8364 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
FreeBASIC Embedded Scripting Engine
« on: August 24, 2016, 10:28:41 PM »
I had originally started this thread on the Plantet Squires forum the FireFly author runs but was ban I assume due to taking attention away from Jose Roca that is consuming the forum focus with his PowerBASIC port of his INCLUDE files to FreeBASIC.

A special thanks to Charles Pegge (OxygenBasic author) for creating the example and the Script BASIC API include file for FreeBASIC.

FBembeds3.bas
Code: FreeBasic
  1.   #include once "windows.bi"
  2.   #include once "win/ole2.bi"
  3.   #include once "scriba.bi"
  4.  
  5.   dim as long pProgram,iError,cArgs
  6.   dim as long f1,f2,v
  7.   dim as long n,m
  8.   dim as SbData ReturnData,ArgData(3)
  9.   dim as sbdata ptr pdat
  10.   dim as long qdat
  11.  
  12.   function newmem CDECL (ByVal le As Long) As bstr Export
  13.     function=SysAllocStringByteLen (0,le)
  14.   end function
  15.  
  16.   sub freemem CDECL (ByVal p As bstr) Export
  17.     SysFreeString p
  18.   end sub
  19.  
  20.   SetConsoleTitle "FreeBasic ScriptBasic Embedding Test"
  21.  
  22.   '
  23.   'LOADING AND RUNNING A PROGRAM
  24.   '-----------------------------
  25.   '
  26.   pProgram=scriba_new (cast(long,@newmem), cast(long,@freemem))
  27.   'scriba_LoadConfiguration(pProgram,"c:\scriptbasic\bin\scriba.conf")
  28.   scriba_SetFileName(pProgram,"E03.bas")
  29.  
  30.   iError=scriba_LoadSourceProgram(pProgram)
  31.   if iError then goto ending
  32.  
  33.   iError=scriba_Run(pProgram,"Hello")
  34.   if iError then goto ending
  35.  
  36.   '
  37.   'ACCESSING GLOBAL DATA
  38.   '---------------------
  39.   '
  40.   v=scriba_LookupVariableByName(pProgram,"main::a")  
  41.   scriba_GetVariable(pProgram,v,qdat)
  42.   pdat=cast(sbdata ptr,qdat)
  43.   m=pdat->lng+100
  44.   scriba_SetVariable(pProgram,v,2,m,0,"",0)
  45.   'scriba_GetVariable(pProgram,v,*pdat)
  46.   '
  47.   'CALLING SIMPLE SUBROUTINE
  48.   '-------------------------
  49.   '
  50.   f1=scriba_LookupFunctionByName(pProgram,"main::dprint")
  51.   if f1=0 then print "Unable to locat Dprint" : goto ending
  52.  
  53.   iError=scriba_Call(pProgram,f1)
  54.   if iError then goto ending
  55.   '
  56.   '
  57.   '
  58.   'CALLING FUNCTION, RETURNING DATA AND GETTING ALTERED PARAMS
  59.   '-----------------------------------------------------------
  60.  
  61.   f2=scriba_LookupFunctionByName(pProgram,"main::eprint")
  62.   if f2=0 then print "Unable to locate Eprint" : goto ending
  63.  
  64.  
  65.   'SETUP ARGUMENTS
  66.   '---------------
  67.  
  68.   'these can be used for both input and output
  69.  
  70.   ArgData(0).typ=SBT_DOUBLE
  71.   ArgData(1).typ=SBT_DOUBLE
  72.   ArgData(2).typ=SBT_DOUBLE
  73.   ArgData(3).typ=SBT_DOUBLE
  74.  
  75.   ArgData(0).siz=0
  76.   ArgData(1).siz=0
  77.   ArgData(2).siz=0
  78.   ArgData(3).siz=0
  79.    
  80.   ArgData(0).dbl=11
  81.   ArgData(1).dbl=12
  82.   ArgData(2).dbl=13
  83.   ArgData(3).dbl=14
  84.  
  85.   cArgs=4
  86.   '
  87.   iError=scriba_CallArgEx(pProgram, f2, ReturnData, cArgs, ArgData(0))
  88.   if iError then goto ending
  89.  
  90.   print "Return type:",ReturnData.typ
  91.   print "Value:",
  92.   '
  93.   'READ RETURNED VALUE
  94.   '-------------------
  95.   '
  96.   select case ReturnData.typ
  97.     case SBT_UNDEF  : print "Undefined"
  98.     case SBT_DOUBLE : print ReturnData.dbl
  99.     case SBT_LONG   : print ReturnData.lng
  100.     case SBT_STRING : print *ReturnData.str
  101.     case SBT_ZCHAR  : print *ReturnData.str
  102.   end select
  103.  
  104.   '------
  105.   ending:
  106.   '======
  107.  
  108.   scriba_destroy(pProgram)
  109.  
  110.   if iError then print "ERROR:  " + hex$(iError)  ' + "  " + hex$(pProgram)
  111.  
  112.   'messageBox 0,"ok:  "+hex$(iError)+"  "+hex$(pProgram),"FreeBasic",0
  113.   sleep
  114.  

E03.bas
Code: ScriptBasic
  1. '
  2. ' SCRIPTBASIC
  3. '
  4.  
  5. ' ----------------
  6. ' GLOBAL VARIABLES
  7. ' ================
  8. '
  9. a=42
  10. b=sqr(2)
  11. s="hello world!"
  12.  
  13.  
  14. q=dprint()
  15. q=eprint(1,2,3,4)
  16.  
  17. 'print "Enter: "
  18. 'line input q
  19.  
  20.  
  21. ' ---------
  22. ' FUNCTIONS
  23. ' =========
  24.  
  25.  
  26. function dprint
  27.   sp="  "
  28.   cr="\n"
  29.   tab=chr$(9)
  30.   print "Dprint Globals: " & s & sp & a & sp & b & cr
  31.   ' line input q
  32.  dprint=1
  33. end function
  34.  
  35.  
  36. function eprint(a,b,c,d)
  37.   sp="  "
  38.   cr="\n"
  39.   tab=chr$(9)
  40.   print "Eprint Args: " & a & sp & b & sp & c & sp & d & cr
  41.   ' line input q
  42.  ans=a+b+c+d
  43.   eprint="Sum = " & ans
  44. end function
  45.  


 Directory of C:\FreeBASIC-1.05.0-win32\FBembedsSB_3

08/22/2016  03:15 AM    <DIR>          .
08/22/2016  03:15 AM    <DIR>          ..
09/24/2009  10:01 AM               594 E03.bas
09/24/2009  10:43 AM             2,874 FBembeds3.bas
09/24/2009  10:44 AM            26,112 FBembeds3.exe
09/23/2009  11:31 AM            52,768 liblibscriba.dll.a
08/29/2009  02:08 AM           524,288 libscriba.dll
09/23/2009  11:29 AM             1,727 libscriba.dll.def
09/23/2009  03:35 PM            61,089 scriba.bi
               7 File(s)        669,452 bytes
               2 Dir(s)  15,457,370,112 bytes free

C:\FreeBASIC-1.05.0-win32\FBembedsSB_3>fbc FBembeds3.bas

C:\FreeBASIC-1.05.0-win32\FBembedsSB_3>FBembeds3
Dprint Globals: hello world!  42  1.414214
Eprint Args: 1  2  3  4
Dprint Globals: hello world!  142  1.414214
Eprint Args: 11.000000  12.000000  13.000000  14.000000
Return type:   3
Value:        Sum = 50

C:\FreeBASIC-1.05.0-win32\FBembedsSB_3>



Note: The attach zip contains everything you need to compile / run the above example as well as any other embedded script you would like to use.





« Last Edit: August 24, 2016, 10:32:19 PM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3598
    • ScriptBasic Open Source Project
Re: FreeBASIC Embedded Scripting Engine
« Reply #1 on: August 25, 2016, 02:42:48 PM »
This is a minimalistic Script BASIC console interpreter compiled with FreeBASIC instead of like scriba in C which is a static linked implementation.

fbi.bas
Code: FreeBasic
  1. #include once "windows.bi"
  2. #include once "win/ole2.bi"
  3. #include once "scriba.bi"
  4.  
  5. dim as long pProgram
  6.  
  7. function newmem CDECL (ByVal bufptr As Long) As bstr Export
  8.   function = SysAllocStringByteLen (0, bufptr)
  9. end function
  10.  
  11. sub freemem CDECL (ByVal buf As bstr) Export
  12.   SysFreeString buf
  13. end sub
  14.  
  15. pProgram = scriba_new (cast(long,@newmem), cast(long,@freemem))
  16. scriba_LoadConfiguration (pProgram, "C:\scriptbasic\bin\SCRIBA.INI")
  17. scriba_SetFileName (pProgram, command(1))
  18. scriba_LoadSourceProgram (pProgram)
  19. scriba_Run (pProgram, command(2))
  20. scriba_destroy(pProgram)
  21.  

testsqlite3.sb
Code: ScriptBasic
  1. import sqlite.bas
  2.  
  3. db = sqlite::open("sqlite3.db")
  4.  
  5. sqlite::execute(db,"create table demo (someval integer, sometxt text);")
  6. sqlite::execute(db,"insert into demo values (123,'hello');")
  7. sqlite::execute(db, "INSERT INTO demo VALUES (234, 'cruel');")
  8. sqlite::execute(db, "INSERT INTO demo VALUES (345, 'world');")
  9.  
  10. stmt = sqlite::query(db,"SELECT * FROM demo")
  11.  
  12. while (sqlite::row(stmt) = sqlite::SQLITE3_ROW)
  13.   if sqlite::fetchhash(stmt,column) then
  14.     print column{"someval"},"\t-\t",column{"sometxt"},"\n"
  15.   end if
  16. wend
  17.  
  18. sqlite::close(db)
  19.  
  20. Print "\nGood-Bye ", command(), ".\n"
  21.  


C:\FreeBASIC-1.05.0-win32\fbsb>fbi testsqlite3.sb John
123     -       hello
234     -       cruel
345     -       world

Good-Bye John.

C:\FreeBASIC-1.05.0-win32\fbsb>dir fbi.exe
 Volume in drive C has no label.
 Volume Serial Number is 74A6-5E64

 Directory of C:\FreeBASIC-1.05.0-win32\fbsb

08/25/2016  07:34 PM            22,016 fbi.exe
               1 File(s)         22,016 bytes
               0 Dir(s)  16,074,199,040 bytes free

C:\FreeBASIC-1.05.0-win32\fbsb>


fbi.asm
Code: ASM
  1.         .intel_syntax noprefix
  2.  
  3. .section .text
  4. .balign 16
  5.  
  6. .globl _NEWMEM
  7. _NEWMEM:
  8. push ebp
  9. mov ebp, esp
  10. sub esp, 4
  11. mov dword ptr [ebp-4], 0
  12. .Lt_0833:
  13. push dword ptr [ebp+8]
  14. push 0
  15. call _SysAllocStringByteLen@8
  16. mov dword ptr [ebp-4], eax
  17. .Lt_0834:
  18. mov eax, dword ptr [ebp-4]
  19. mov esp, ebp
  20. pop ebp
  21. ret
  22. .balign 16
  23.  
  24. .globl _FREEMEM
  25. _FREEMEM:
  26. push ebp
  27. mov ebp, esp
  28. .Lt_0835:
  29. push dword ptr [ebp+8]
  30. call _SysFreeString@4
  31. .Lt_0836:
  32. mov esp, ebp
  33. pop ebp
  34. ret
  35. .balign 16
  36. _fb_ctor__fbi:
  37. push ebp
  38. mov ebp, esp
  39. sub esp, 28
  40. .Lt_0002:
  41. mov dword ptr [ebp-4], 0
  42. push offset _FREEMEM
  43. push offset _NEWMEM
  44. call _scriba_new@8
  45. mov dword ptr [ebp-4], eax
  46. push offset _Lt_0839
  47. push dword ptr [ebp-4]
  48. call _scriba_LoadConfiguration@8
  49. mov dword ptr [ebp-16], 0
  50. mov dword ptr [ebp-12], 0
  51. mov dword ptr [ebp-8], 0
  52. push 0
  53. push -1
  54. push 1
  55. call _fb_Command@4
  56. push eax
  57. push -1
  58. lea eax, [ebp-16]
  59. push eax
  60. call _fb_StrAssign@20
  61. push dword ptr [ebp-16]
  62. push dword ptr [ebp-4]
  63. call _scriba_SetFileName@8
  64. lea eax, [ebp-16]
  65. push eax
  66. call _fb_StrDelete@4
  67. push dword ptr [ebp-4]
  68. call _scriba_LoadSourceProgram@4
  69. mov dword ptr [ebp-28], 0
  70. mov dword ptr [ebp-24], 0
  71. mov dword ptr [ebp-20], 0
  72. push 0
  73. push -1
  74. push 2
  75. call _fb_Command@4
  76. push eax
  77. push -1
  78. lea eax, [ebp-28]
  79. push eax
  80. call _fb_StrAssign@20
  81. push dword ptr [ebp-28]
  82. push dword ptr [ebp-4]
  83. call _scriba_Run@8
  84. lea eax, [ebp-28]
  85. push eax
  86. call _fb_StrDelete@4
  87. push dword ptr [ebp-4]
  88. call _scriba_destroy@4
  89. .Lt_0003:
  90. mov esp, ebp
  91. pop ebp
  92. ret
  93.  
  94. .section .fbctinf
  95. .ascii "-l\0"
  96. .ascii "kernel32\0"
  97. .ascii "-l\0"
  98. .ascii "gdi32\0"
  99. .ascii "-l\0"
  100. .ascii "msimg32\0"
  101. .ascii "-l\0"
  102. .ascii "user32\0"
  103. .ascii "-l\0"
  104. .ascii "version\0"
  105. .ascii "-l\0"
  106. .ascii "advapi32\0"
  107. .ascii "-l\0"
  108. .ascii "imm32\0"
  109. .ascii "-l\0"
  110. .ascii "ole32\0"
  111. .ascii "-l\0"
  112. .ascii "uuid\0"
  113. .ascii "-l\0"
  114. .ascii "oleaut32\0"
  115. .ascii "-l\0"
  116. .ascii "libscriba\0"
  117. .ascii "-l\0"
  118. .ascii "libscriba.dll\0"
  119.  
  120.  
  121. .section .data
  122. .balign 4
  123. _Lt_0839:       .ascii  "C:\\scriptbasic\\bin\\SCRIBA.INI\0"
  124.  
  125. .section .drectve
  126.         .ascii " -export:NEWMEM"
  127.  
  128.         .ascii " -export:FREEMEM"
  129.  
  130.  
  131. .section .ctors
  132. .int _fb_ctor__fbi
  133.  
« Last Edit: August 25, 2016, 11:16:36 PM by John »