OK,

you got thinBasic SDK for Delphi. You open the demo Delphi project but still you didn't figure out how interface is done. I will give you here some few points in some post.

All starts when thinBasic parser find the USES keyword.
When thinBasic encounter USES it start to find the specified module as described in USES help.
So a sentence like
[code=thinbasic]
USES "MyDelphi"
[/code]
will load thinBasic_MyDelphi.dll. To know from where this dll will be loaded see USES help.

OK, now the dll is loaded but what now?
thinBasic search inside the dll for a function named LoadLocalSymbols or _LoadLocalSymbols or LoadLocalSymbols_Delphi.
If none will be found, operation end but script will continue to be executed (but I can imagine a runtime error will be quite close)
If one of those function will be found, it will be executed.
LoadLocalSymbols (and its possible variants) have the duty to initialize any needed code by the module and link new keyword names to internal dll functions using thinBasic_LoadSymbol or thinBasic_LoadSymbol_Delphi function. Please see ThinCore.pas included in thinBasic SDK for Delphi ZIP file for thinBasic_LoadSymbol_Delphi syntax.


OK, but how can thinBasic knows how is the syntax of my new keyword?
Imagine you want to add a new keyword that calculates the length of the hypotenuse of a right triangle.
Delphi has the Hypot function in Math unit. So you want to add to thinBasic keywords.

  • 1. Decide the syntax you want thinBasic will have to parse
    Native Hypot Delphi syntax is: n = Hypot(const X: Extended; const Y: Extended): Extended;
    thinBasic syntax can be something like: n = Hypot(Side1, Side2)

    So thinBasic has to parse:
    • an open (
    • a first numeric expression (Side1)
    • a comma ,
    • a second numeric expression (Side2)
    • a closed )



  • 2. Start parsing using exported thinCore.dll fucntionalities.
    Responsability to parse the syntax is moved at module level, so you as programmer has the duty to do it. Here is a possible code example of the above syntax:


[code=delphi] //----------------------------------------------------------------------------
// Hypot returns the length of the hypotenuse of a right triangle
//--
// thinBasic Syntax: n = Hypot(FirstSide, SecondSide)
//----------------------------------------------------------------------------
function Exec_Hypot() : extended ; stdcall;
var FirstSide, SecondSide : extended;

begin

//---Init to zero to avoid compiler warning about possible undefined return
Exec_Hypot := 0;

//---Parse the open parenthesis
if thinBasic_CheckOpenParens = true then
begin
//---Parse the X numeric expression
thinBasic_ParseNumber(FirstSide);

//---Parse the comma
if thinBasic_CheckComma = true then
begin
//---Parse the Y numeric expression
thinBasic_ParseNumber(SecondSide);

//---Parse the close parenthesis
if thinBasic_CheckCloseParens = true then
begin
//---Execute the function and assign as return value
Exec_Hypot := Hypot(FirstSide, SecondSide) ;
end;

end;

end;

end;[/code]

So, to summarize:
1. parse the open parens with thinBasic_CheckOpenParens function
2. parse the first number using thinBasic_ParseNumber(...) passing an extended variable byref
3. parse a comma with thinBasic_CheckComma function
4. parse the second number using thinBasic_ParseNumber(...) passing an extended variable byref
5. parse the close parens with thinBasic_CheckCloseParens function

All those functions has internal error checking so if something goes wrong a thinBasic script run time error is generated.
If all is ok you will have now 2 numbers to be used to calculate hypotenuse.
Assign result value to function return value and the trick is done.

More info will follow if more interest will be expressed.

Regards
Eros