PDA

View Full Version : Database of programs (minimalistic)



Charles Pegge
07-02-2010, 14:30
This demonstrates how to hold Oxygen programs in a database and use them interactively.

When a program is located the user can enter input data, which is then patched directly into the source code.
The program is then compiled and executed, returning the results to the user in a thinBasic string.

Here is the essence of the idea: We can add to it and post more advance versions in the attachment below.



'
'ProgBase
'
'
'----------------------------------
'LOCATE PROGRAM IN DATABASE
'THEN COMPILE AND RUN IT
'==================================



uses "oxygen"

type ProgBaseRecord
descript as string
keywords as string
program as string
input as string
end type



'CREATE DATABASE WITH SOME SAMPLE RECORDS

dim rec(100) as ProgBaseRecord

'RECORD 1

rec(1).descript="Hello Program"
rec(1).keywords="hello, helo hi, hola, bonjour, bore da,"
rec(1).input="What is your name?"
rec(1).program="
dim as bstr a at #ans
a=`Hello #input#!`
"

'RECORD 2

rec(2).descript="Calculator"
rec(2).keywords="calculat, express, evaluate"
rec(2).input="Enter Expression"
rec(2).program="
dim as bstr a at #ans
a=str #input#
"

rec(3).descript=""
rec(3).keywords=""
rec(3).input=""
rec(3).program=""


dim keyw,src,qes,ans as string
dim as long sb,ind


do
'
'ENTER KEYWORDS TO IDENTIFY PROGRAM
'
keyw=inputbox$ "Enter keyword to locate program: ","PROGRAM DATABASE",keyw
if keyw="" then exit do
keyw=lcase$(keyw) : ind=1 : sb=0
'
'
'LOCATE PROGRAM BY KEYWORDS
'
do
if ind>100 then exit do
sb=instr(rec(ind).keywords, keyw)
if sb>0 then exit do
ind+=1
loop
'
if sb=0 then iterate do 'TRY ANOTHER KEYWORD
'
'RUN PROGRAM SESSIONS
'
do
src=rec(ind).program
qes=inputbox$ rec(ind).input+$crlf+$crlf+$crlf+$crlf+"Result:"+$crlf+ans,rec(ind).descript,qes
if qes="" then exit do
'
'INSERT INPUTS
'
sb=instr(src,"#input#")
if sb then src=left$(src,sb-1)+qes+mid$(src,sb+7)
'
'COMPILE SOURCE STRING
'
o2_basic src
if len(o2_error) then
'
'REPORT COMPILE ERROR
'
'msgbox 0,o2_error
ans="(ERROR)"
'
else
'
'EXECUTE
'
o2_exec
end if
loop
loop