This is an API demo created by Charles Pegge with assistance from Armando I. Rivera (AIR) that was used in a SB embedding code challenge from awhile back. I converted it to run under BaCon.
sbapi.bac
' ScriptBasic API Demo
PRAGMA OPTIONS -I/home/jrs/sb/source
PRAGMA LDFLAGS scriba pthread
PRAGMA INCLUDE scriba.h getopt.h
PROTO scriba_destroy
DECLARE pProgram TYPE pSbProgram
DECLARE ReturnData TYPE SbData
DECLARE ArgData[4] TYPE SbData
lVal = 11
' RUN SB SCRIPT
pProgram = scriba_new(malloc,free)
ok = scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf")
ok = scriba_SetFileName(pProgram, "E03.sb")
ok = scriba_LoadSourceProgram(pProgram)
ok = scriba_Run(pProgram,"")
' ACCESSING GLOBAL DATA
vsn = scriba_LookupVariableByName(pProgram, "main::a")
ok = scriba_SetVariable(pProgram, vsn, 2, 500, 0, "", 0)
' CALLING SIMPLE SUBROUTINE
f1 = scriba_LookupFunctionByName(pProgram, "main::dprint")
ok = 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)
FOR cArgs = 0 TO 3
ArgData[cArgs].type = SBT_LONG
ArgData[cArgs].size = 0
ArgData[cArgs].v.l = lVal+cArgs
NEXT cArgs
ok = scriba_CallArgEx(pProgram, f2, &ReturnData, cArgs, ArgData)
PRINT "Return type:", ReturnData.type
PRINT "Value: ";
' READ RETURNED VALUE
SELECT ReturnData.type
CASE SBT_UNDEF
PRINT "Undefined"
CASE SBT_DOUBLE
PRINT ReturnData.v.d
CASE SBT_LONG
PRINT ReturnData.v.l
CASE SBT_STRING;
CASE SBT_ZCHAR
r$ = ReturnData.v.s
PRINT r$
END SELECT
scriba_destroy(pProgram)
E03.sb
' ----------------
' GLOBAL VARIABLES
' ================
'
a=42
b=sqr(2)
s="hello world!"
q=dprint()
q=eprint(1,2,3,4)
' ---------
' FUNCTIONS
' =========
function dprint
sp=" "
cr="\n"
tab=chr$(9)
print "Dprint Globals: " & s & sp & a & sp & b & cr
dprint=1
end function
function eprint(a,b,c,d)
sp=" "
cr="\n"
tab=chr$(9)
print "Eprint Args: " & a & sp & b & sp & c & sp & d & cr
ans=a+b+c+d
eprint="Sum = " & ans
end function
jrs@laptop:~/BaCon/B29$ ./bacon sbapi.bac
Converting 'sbapi.bac'... done.
Compiling 'sbapi.bac'... done.
Program 'sbapi' ready.
jrs@laptop:~/BaCon/B29$ ./sbapi
Dprint Globals: hello world! 42 1.414214
Eprint Args: 1 2 3 4
Dprint Globals: hello world! 500 1.414214
Eprint Args: 11 12 13 14
Return type:3
Value: Sum = 50
jrs@laptop:~/BaCon/B29$
You can also load a script and not run it. This comes in handy if you have a library of script functions/subs and want to call them from your BaCon program.
ok = scriba_NoRun(pProgram)
Notice the change in results when the initial global variables aren't set.
jrs@laptop:~/BaCon/B29$ ./sbapi
Dprint Globals: 500
Eprint Args: 11 12 13 14
Return type:3
Value: Sum = 50
jrs@laptop:~/BaCon/B29$