Message Boards > Compilers

FreeBASIC Embedded Scripting Engine

(1/1)

John:
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 ---  #include once "windows.bi"  #include once "win/ole2.bi"  #include once "scriba.bi"   dim as long pProgram,iError,cArgs  dim as long f1,f2,v  dim as long n,m  dim as SbData ReturnData,ArgData(3)  dim as sbdata ptr pdat  dim as long qdat    function newmem CDECL (ByVal le As Long) As bstr Export    function=SysAllocStringByteLen (0,le)  end function   sub freemem CDECL (ByVal p As bstr) Export    SysFreeString p  end sub    SetConsoleTitle "FreeBasic ScriptBasic Embedding Test"   '  'LOADING AND RUNNING A PROGRAM  '-----------------------------  '  pProgram=scriba_new (cast(long,@newmem), cast(long,@freemem))  'scriba_LoadConfiguration(pProgram,"c:\scriptbasic\bin\scriba.conf")  scriba_SetFileName(pProgram,"E03.bas")   iError=scriba_LoadSourceProgram(pProgram)  if iError then goto ending   iError=scriba_Run(pProgram,"Hello")  if iError then goto ending    '  'ACCESSING GLOBAL DATA   '---------------------  '  v=scriba_LookupVariableByName(pProgram,"main::a")    scriba_GetVariable(pProgram,v,qdat)  pdat=cast(sbdata ptr,qdat)  m=pdat->lng+100  scriba_SetVariable(pProgram,v,2,m,0,"",0)  'scriba_GetVariable(pProgram,v,*pdat)  '  'CALLING SIMPLE SUBROUTINE  '-------------------------  '  f1=scriba_LookupFunctionByName(pProgram,"main::dprint")  if f1=0 then print "Unable to locat Dprint" : goto ending    iError=scriba_Call(pProgram,f1)  if iError then goto ending  '  '  '  'CALLING FUNCTION, RETURNING DATA AND GETTING ALTERED PARAMS  '-----------------------------------------------------------   f2=scriba_LookupFunctionByName(pProgram,"main::eprint")  if f2=0 then print "Unable to locate Eprint" : goto ending     'SETUP ARGUMENTS  '---------------   'these can be used for both input and output    ArgData(0).typ=SBT_DOUBLE  ArgData(1).typ=SBT_DOUBLE  ArgData(2).typ=SBT_DOUBLE  ArgData(3).typ=SBT_DOUBLE    ArgData(0).siz=0  ArgData(1).siz=0  ArgData(2).siz=0  ArgData(3).siz=0     ArgData(0).dbl=11  ArgData(1).dbl=12  ArgData(2).dbl=13  ArgData(3).dbl=14   cArgs=4  '  iError=scriba_CallArgEx(pProgram, f2, ReturnData, cArgs, ArgData(0))  if iError then goto ending   print "Return type:",ReturnData.typ  print "Value:",  '  'READ RETURNED VALUE  '-------------------  '  select case ReturnData.typ    case SBT_UNDEF  : print "Undefined"    case SBT_DOUBLE : print ReturnData.dbl    case SBT_LONG   : print ReturnData.lng    case SBT_STRING : print *ReturnData.str    case SBT_ZCHAR  : print *ReturnData.str  end select   '------  ending:  '======    scriba_destroy(pProgram)   if iError then print "ERROR:  " + hex$(iError)  ' + "  " + hex$(pProgram)   'messageBox 0,"ok:  "+hex$(iError)+"  "+hex$(pProgram),"FreeBasic",0  sleep 
E03.bas

--- Code: Script BASIC ---'' SCRIPTBASIC' ' ----------------' GLOBAL VARIABLES' ================'a=42b=sqr(2)s="hello world!"  q=dprint()q=eprint(1,2,3,4) 'print "Enter: "'line input q  ' ---------' FUNCTIONS' =========  function dprint  sp="  "  cr="\n"  tab=chr$(9)  print "Dprint Globals: " & s & sp & a & sp & b & cr  ' line input q  dprint=1end function  function eprint(a,b,c,d)  sp="  "  cr="\n"  tab=chr$(9)  print "Eprint Args: " & a & sp & b & sp & c & sp & d & cr  ' line input q  ans=a+b+c+d  eprint="Sum = " & ansend function 

 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.





John:
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 ---#include once "windows.bi"#include once "win/ole2.bi"#include once "scriba.bi" dim as long pProgram function newmem CDECL (ByVal bufptr As Long) As bstr Export  function = SysAllocStringByteLen (0, bufptr)end function sub freemem CDECL (ByVal buf As bstr) Export  SysFreeString bufend sub  pProgram = scriba_new (cast(long,@newmem), cast(long,@freemem))scriba_LoadConfiguration (pProgram, "C:\scriptbasic\bin\SCRIBA.INI")scriba_SetFileName (pProgram, command(1))scriba_LoadSourceProgram (pProgram)scriba_Run (pProgram, command(2))scriba_destroy(pProgram) 
testsqlite3.sb

--- Code: Script BASIC ---import sqlite.bas db = sqlite::open("sqlite3.db") sqlite::execute(db,"create table demo (someval integer, sometxt text);")sqlite::execute(db,"insert into demo values (123,'hello');")sqlite::execute(db, "INSERT INTO demo VALUES (234, 'cruel');")sqlite::execute(db, "INSERT INTO demo VALUES (345, 'world');") stmt = sqlite::query(db,"SELECT * FROM demo") while (sqlite::row(stmt) = sqlite::SQLITE3_ROW)  if sqlite::fetchhash(stmt,column) then    print column{"someval"},"\t-\t",column{"sometxt"},"\n"  end ifwend sqlite::close(db) Print "\nGood-Bye ", command(), ".\n" 

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 ---        .intel_syntax noprefix .section .text.balign 16 .globl _NEWMEM_NEWMEM:push ebpmov ebp, espsub esp, 4mov dword ptr [ebp-4], 0.Lt_0833:push dword ptr [ebp+8]push 0call _SysAllocStringByteLen@8mov dword ptr [ebp-4], eax.Lt_0834:mov eax, dword ptr [ebp-4]mov esp, ebppop ebpret.balign 16 .globl _FREEMEM_FREEMEM:push ebpmov ebp, esp.Lt_0835:push dword ptr [ebp+8]call _SysFreeString@4.Lt_0836:mov esp, ebppop ebpret.balign 16_fb_ctor__fbi:push ebpmov ebp, espsub esp, 28.Lt_0002:mov dword ptr [ebp-4], 0push offset _FREEMEMpush offset _NEWMEMcall _scriba_new@8mov dword ptr [ebp-4], eaxpush offset _Lt_0839push dword ptr [ebp-4]call _scriba_LoadConfiguration@8mov dword ptr [ebp-16], 0mov dword ptr [ebp-12], 0mov dword ptr [ebp-8], 0push 0push -1push 1call _fb_Command@4push eaxpush -1lea eax, [ebp-16]push eaxcall _fb_StrAssign@20push dword ptr [ebp-16]push dword ptr [ebp-4]call _scriba_SetFileName@8lea eax, [ebp-16]push eaxcall _fb_StrDelete@4push dword ptr [ebp-4]call _scriba_LoadSourceProgram@4mov dword ptr [ebp-28], 0mov dword ptr [ebp-24], 0mov dword ptr [ebp-20], 0push 0push -1push 2call _fb_Command@4push eaxpush -1lea eax, [ebp-28]push eaxcall _fb_StrAssign@20push dword ptr [ebp-28]push dword ptr [ebp-4]call _scriba_Run@8lea eax, [ebp-28]push eaxcall _fb_StrDelete@4push dword ptr [ebp-4]call _scriba_destroy@4.Lt_0003:mov esp, ebppop ebpret .section .fbctinf.ascii "-l\0".ascii "kernel32\0".ascii "-l\0".ascii "gdi32\0".ascii "-l\0".ascii "msimg32\0".ascii "-l\0".ascii "user32\0".ascii "-l\0".ascii "version\0".ascii "-l\0".ascii "advapi32\0".ascii "-l\0".ascii "imm32\0".ascii "-l\0".ascii "ole32\0".ascii "-l\0".ascii "uuid\0".ascii "-l\0".ascii "oleaut32\0".ascii "-l\0".ascii "libscriba\0".ascii "-l\0".ascii "libscriba.dll\0"  .section .data.balign 4_Lt_0839:       .ascii  "C:\\scriptbasic\\bin\\SCRIBA.INI\0" .section .drectve        .ascii " -export:NEWMEM"         .ascii " -export:FREEMEM"  .section .ctors.int _fb_ctor__fbi 

Navigation

[0] Message Index

Go to full version