Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 25, 2012 5:06:59 GMT 1
I'm trying to get ScriptBasic embedded in BaCon to create a simple command line interpreter. PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA OPTIONS -L/usr/lib PRAGMA OPTIONS -lscriba PRAGMA OPTIONS -lpthread PRAGMA INCLUDE scriba.h PRAGMA INCLUDE getopt.h
PROTO scriba_new, scriba_LoadConfiguration, scriba_SetFileName, scriba_LoadSourceProgram, scriba_Run, scriba_destroy
USEC pSbProgram pProgram; END USEC
pProgram = scriba_new(malloc,free) scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf")
scriba_SetFileName(pProgram, "E01.bas") scriba_LoadSourceProgram(pProgram)
iError=scriba_Run(pProgram,"JRS")
scriba_destroy(pProgram)
jrs@laptop:~/BaCon/B29$ ./bacon bi.bac Converting 'bi.bac'... done. Compiling 'bi.bac'... Compiler emits messages! /tmp/ccSGjlki.o: In function `main': bi.bac.c:(.text+0x525e): undefined reference to `scriba_new' bi.bac.c:(.text+0x5273): undefined reference to `scriba_LoadConfiguration' bi.bac.c:(.text+0x5284): undefined reference to `scriba_SetFileName' bi.bac.c:(.text+0x5290): undefined reference to `scriba_LoadSourceProgram' bi.bac.c:(.text+0x52a1): undefined reference to `scriba_Run' bi.bac.c:(.text+0x52b6): undefined reference to `scriba_destroy' collect2: ld returned 1 exit status jrs@laptop:~/BaCon/B29$ Here is an example of this working in BCX. (MBC) Any help or suggestions to get this working would be great.
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 25, 2012 16:52:08 GMT 1
The example that AIR created on the BCX forum was done with Ubuntu 32 some time ago. I tried to recompile it on Ubuntu 64 and getting the same messages that the SB functions can't be found.
Any ideas or comments are welcome!
|
|
|
Post by Pjot on Dec 25, 2012 18:40:18 GMT 1
Can you try this, maybe it helps....?
BR, Peter
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA OPTIONS -L/usr/lib PRAGMA LDFLAGS -lscriba PRAGMA LDFLAGS -lpthread PRAGMA INCLUDE scriba.h PRAGMA INCLUDE getopt.h
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 25, 2012 18:51:54 GMT 1
Thanks!
I had to remove the -l from the LDFLAGS options but it worked.
bi.bac
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA INCLUDE scriba.h PRAGMA INCLUDE getopt.h PRAGMA LDFLAGS scriba PRAGMA LDFLAGS pthread
PROTO scriba_new, scriba_LoadConfiguration, scriba_SetFileName, scriba_LoadSourceProgram, scriba_Run, scriba_destroy
USEC pSbProgram pProgram; END USEC
pProgram = scriba_new(malloc,free) scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf")
scriba_SetFileName(pProgram, "E01.bas") scriba_LoadSourceProgram(pProgram)
iError=scriba_Run(pProgram,"JRS")
scriba_destroy(pProgram)
E01.bas
cmd = COMMAND()
PRINT "ARG = ",cmd,"\n"
FOR x = 1 TO 10 PRINT x,"\n" NEXT
jrs@laptop:~/BaCon/B29$ ./bacon bi.bac Converting 'bi.bac'... done. Compiling 'bi.bac'... Compiler emits messages!
/usr/bin/ld: cannot find -l-lscriba /usr/bin/ld: cannot find -l-lpthread collect2: ld returned 1 exit status
jrs@laptop:~/BaCon/B29$ ./bacon bi.bac Converting 'bi.bac'... done. Compiling 'bi.bac'... done. Program 'bi' ready. jrs@laptop:~/BaCon/B29$ ./bi ARG = JRS 1 2 3 4 5 6 7 8 9 10 jrs@laptop:~/BaCon/B29$
-rwxrwxr-x 1 jrs jrs 49902 Dec 25 09:44 bi
|
|
|
Post by Pjot on Dec 25, 2012 20:04:31 GMT 1
Ah yes, that is true. BaCon will add this '-l' for you. In fact, you should be able to do this:
PRAGMA OPTIONS -I/home/jrs/sb/source -L/usr/lib PRAGMA LDFLAGS scriba pthread PRAGMA INCLUDE scriba.h getopt.h
Regards Peter
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 25, 2012 20:26:49 GMT 1
Thanks again Peter. That worked. I added the ability to pass any SB script and argument(s) for the bi (<50 KB) Basic interpreter.
' BaCon Command Line Interpreter
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA LDFLAGS scriba pthread PRAGMA INCLUDE scriba.h getopt.h
PROTO scriba_new, scriba_LoadConfiguration, scriba_SetFileName, scriba_LoadSourceProgram, scriba_Run, scriba_destroy
USEC pSbProgram pProgram; END USEC
cmdln$ = ARGUMENT$ SPLIT cmdln$ BY " " TO opts$ SIZE cnt
pProgram = scriba_new(malloc,free) scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf")
scriba_SetFileName(pProgram, opts$[1]) scriba_LoadSourceProgram(pProgram)
iError=scriba_Run(pProgram,opts$[2])
scriba_destroy(pProgram)
jrs@laptop:~/BaCon/B29$ ./bacon bi.bac Converting 'bi.bac'... done. Compiling 'bi.bac'... done. Program 'bi' ready. jrs@laptop:~/BaCon/B29$ ./bi E01.bas JRS ARG = JRS 1 2 3 4 5 6 7 8 9 10 jrs@laptop:~/BaCon/B29$
|
|
|
Post by Pjot on Dec 25, 2012 20:45:47 GMT 1
You can optimize it even more, because I do not think the USEC is needed. Just use DECLARE or LOCAL.
Also, only external calls which do not appear in a function or assignment need to be mentioned in PROTO.
So from the top of my head this should work:
' BaCon Command Line Interpreter
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA LDFLAGS scriba pthread PRAGMA INCLUDE scriba.h getopt.h
PROTO scriba_LoadConfiguration, scriba_SetFileName, scriba_LoadSourceProgram, scriba_destroy
DECLARE pProgram TYPE pSbProgram
cmdln$ = ARGUMENT$ SPLIT cmdln$ BY " " TO opts$ SIZE cnt
pProgram = scriba_new(malloc,free) scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf")
scriba_SetFileName(pProgram, opts$[1]) scriba_LoadSourceProgram(pProgram)
iError=scriba_Run(pProgram,opts$[2])
scriba_destroy(pProgram)
Regards Peter
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 25, 2012 21:09:47 GMT 1
The above works great!
jrs@laptop:~/BaCon/B29$ ./bacon pbi.bac Converting 'pbi.bac'... done. Compiling 'pbi.bac'... done. Program 'pbi' ready. jrs@laptop:~/BaCon/B29$ ./pbi E01.bas JRS ARG = JRS 1 2 3 4 5 6 7 8 9 10 jrs@laptop:~/BaCon/B29$
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 25, 2012 22:15:44 GMT 1
This also works to reduce the PROTO requirement. scriba_destroy() returns a VOID and BaCon needs the PROTO for it.
' BaCon Command Line Interpreter
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA LDFLAGS scriba pthread PRAGMA INCLUDE scriba.h getopt.h
PROTO scriba_destroy
DECLARE pProgram TYPE pSbProgram
cmdln$ = ARGUMENT$ SPLIT cmdln$ BY " " TO opts$ SIZE cnt
pProgram = scriba_new(malloc,free) ok = scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf") ok = scriba_SetFileName(pProgram, opts$[1]) ok = scriba_LoadSourceProgram(pProgram) ok = scriba_Run(pProgram,opts$[2]) scriba_destroy(pProgram)
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 26, 2012 2:08:01 GMT 1
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$
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 26, 2012 20:59:26 GMT 1
Here is a Hello World example where the script is a BaCon string.
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA LDFLAGS scriba pthread PRAGMA INCLUDE scriba.h getopt.h
PROTO scriba_destroy
DECLARE pProgram TYPE pSbProgram
pgm$ = CONCAT$("PRINT ",CHR$(34),"Hello World\\n",CHR$(34),CHR$(10))
pProgram = scriba_new(malloc,free) ok = scriba_SetFileName(pProgram, "dummy") ok = scriba_LoadProgramString(pProgram, pgm$, LEN(pgm$)) ok = scriba_Run(pProgram,"") scriba_destroy(pProgram)
jrs@laptop:~/BaCon/B29$ ./bacon pgmstr.bac Converting 'pgmstr.bac'... done. Compiling 'pgmstr.bac'... done. Program 'pgmstr' ready. jrs@laptop:~/BaCon/B29$ ./pgmstr Hello World jrs@laptop:~/BaCon/B29$
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 26, 2012 22:14:54 GMT 1
This example is using the binary compiled form of the script. This loads and runs the scripts faster as there is no prep work to do. It also adds a level of security to your scripts as they are stored on disk in a binary format. Skipped Steps- scriba_ReadSource()
- scriba_DoLexicalAnalysis()
- scriba_DoSyntaxAnalysis()
- scriba_BuildCode()
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA LDFLAGS scriba pthread PRAGMA INCLUDE scriba.h getopt.h
PROTO scriba_destroy
DECLARE pProgram TYPE pSbProgram
cmdln$ = ARGUMENT$ SPLIT cmdln$ BY " " TO opts$ SIZE cnt
pProgram = scriba_new(malloc,free) ok = scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf") ok = scriba_SetFileName(pProgram, opts$[1]) ok = scriba_LoadBinaryProgram(pProgram) ok = scriba_Run(pProgram,opts$[2]) scriba_destroy(pProgram)
jrs@laptop:~/BaCon/B29$ scriba -o E01.bin E01.bas jrs@laptop:~/BaCon/B29$ ./bacon bbi.bac Converting 'bbi.bac'... done. Compiling 'bbi.bac'... done. Program 'bbi' ready. jrs@laptop:~/BaCon/B29$ ./bbi E01.bin secrete_password ARG = secrete_password 1 2 3 4 5 6 7 8 9 10 jrs@laptop:~/BaCon/B29$
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 27, 2012 16:56:34 GMT 1
The example Charles & AIR demonstrated of calling a function was in it's most advanced form. (passing arguments by reference, returning a value) If all your interested in is calling a SUB and passing arguments by value, ScriptBasic has a easier API call to work with.
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA LDFLAGS scriba pthread PRAGMA INCLUDE scriba.h getopt.h
PROTO scriba_destroy
DECLARE pProgram TYPE pSbProgram
pProgram = scriba_new(malloc,free) ok = scriba_SetFileName(pProgram, "E04.sb") ok = scriba_LoadSourceProgram(pProgram) ok = scriba_NoRun(pProgram)
s1 = scriba_LookupFunctionByName(pProgram, "main::prtargs") ok = scriba_CallArg(pProgram, s1, "i s r", 2012, "BaCon", 1.29)
scriba_destroy(pProgram)
SUB prtargs(arg1, arg2, arg3) PRINT arg1,"\n" PRINT arg2,"\n" PRINT FORMAT("%g",arg3),"\n" END SUB
jrs@laptop:~/BaCon/B29$ ./bacon callarg.bac Converting 'callarg.bac'... done. Compiling 'callarg.bac'... done. Program 'callarg' ready. jrs@laptop:~/BaCon/B29$ ./callarg 2012 BaCon 1.29 jrs@laptop:~/BaCon/B29$
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 27, 2012 23:01:58 GMT 1
I thought I would give my new remote (socket) SB debugger a try with an embedded BaCon application.
PRAGMA OPTIONS -I/home/jrs/sb/source PRAGMA LDFLAGS scriba pthread PRAGMA INCLUDE scriba.h getopt.h basext.h
PROTO scriba_destroy
DECLARE pProgram TYPE pSbProgram DECLARE *debuggers[1] TYPE char
debuggers[0] = "sdbg" debuggers[1] = NULL
pProgram = scriba_new(malloc,free) ok = scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf") ok = scriba_LoadInternalPreprocessor(pProgram, debuggers) ok = scriba_SetFileName(pProgram, "E04.sb") ok = scriba_LoadSourceProgram(pProgram) ok = scriba_Run(pProgram,"")
s1 = scriba_LookupFunctionByName(pProgram, "main::prtargs") ok = scriba_CallArg(pProgram, s1, "i s r", 2012, "BaCon", 1.29)
scriba_destroy(pProgram)
Console 1 jrs@laptop:~/BaCon/B29$ ./bacon debugit.bac Converting 'debugit.bac'... done. Compiling 'debugit.bac'... done. Program 'debugit' ready. jrs@laptop:~/BaCon/B29$ ./debugit 2012 BaCon 1.29 jrs@laptop:~/BaCon/B29$
Console 2 jrs@laptop:~$ cd sb/test jrs@laptop:~/sb/test$ scriba dbgcon.sb Application: ScriptBasic Remote Debugger Version: 1.0 Source-File-Count: 1 Source-File: E04.sb Current-Line: 1 -> l - [0001] SUB prtargs(arg1, arg2, arg3) [0002] PRINT arg1,"\n" [0003] PRINT arg2,"\n" [0004] PRINT FORMAT("%g",arg3),"\n" [0005] END SUB Current-Line: 1 -> s Current-Line: 2 -> s Current-Line: 3 -> s Current-Line: 4 -> s Current-Line: 5 -> q Debug session closed. jrs@laptop:~/sb/test$
|
|
Deleted
Deleted Member
Posts: 0
|
Post by Deleted on Dec 27, 2012 23:55:34 GMT 1
Here is an example of using the ScriptBasic SQLite3 extension module and passing the resulting record set back to BaCon.
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 pVariable TYPE pSbData
pProgram = scriba_new(malloc,free) ok = scriba_LoadConfiguration(pProgram,"/etc/scriba/basic.conf") ok = scriba_SetFileName(pProgram, "E05.sb") ok = scriba_LoadSourceProgram(pProgram) ok = scriba_Run(pProgram,"")
vsn = scriba_LookupVariableByName(pProgram, "main::dbstr") ok = scriba_GetVariable(pProgram, vsn, &pVariable)
dbstr$ = pVariable[0].v.s SPLIT dbstr$ BY "|" TO row$ SIZE cnt
FOR x = 0 TO cnt-1 PRINT row$[x] NEXT x
jrs@laptop:~/BaCon/B29$ ./bacon sqlext.bac Converting 'sqlext.bac'... done. Compiling 'sqlext.bac'... done. Program 'sqlext' ready. jrs@laptop:~/BaCon/B29$ ./sqlext 123-hello 234-cruel 345-world
jrs@laptop:~/BaCon/B29$
E05.sb
IMPORT sqlite.bas
db = sqlite::open("testsql")
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 dbstr &= column{"someval"} & "-" & column{"sometxt"} & "|" END IF WEND
sqlite::close(db)
|
|