  'COMPILING WITH OXYGEN
  '11:08 13/12/2013
  '03:30 31/07/2018
  '01:35 15/10/2022
  '
  'Charles E V Pegge
  '
  $ dll
  $ filename "thinBasic_Oxygen.dll"
  uses RTL32

  % late_binding
  % TB_TRUE  1
  % TB_FALSE 0

  ' Return Types from functions/Subs
  '=================================

  % thinBasic_ReturnNone          0  'Used in thinBasic_LoadSymbol to define a sub
  % thinBasic_ReturnNumber       20  'Used in thinBasic_LoadSymbol to define a function returning a EXT number
  % thinBasic_ReturnString       30  'Used in thinBasic_LoadSymbol to define a function returning a string
  % thinBasic_ReturnCodeByte      1
  % thinBasic_ReturnCodeInteger   2
  % thinBasic_ReturnCodeWord      3
  % thinBasic_ReturnCodeDWord     4
  % thinBasic_ReturnCodeLong      5
  % thinBasic_ReturnCodeQuad      6
  % thinBasic_ReturnCodeSingle    7 
  % thinBasic_ReturnCodeDouble    8
  % thinBasic_ReturnCodeCurrency  9
  % thinBasic_ReturnCodeExt      10
  % thinBasic_ReturnCodeString   30
  % thinBasic_ForceOverWrite      1  'Used in thinBasic_LoadSymbol to force symbol over writing

  extern stdcall lib "thinCore.dll"
  =================================

  ! thinBasic_ParseLong                  (sys*sResult)
  ! thinBasic_ParseString                (string*sResult) as double
  ! thinBasic_CheckOpenParens_Optional   () as sys
  ! thinBasic_CheckCloseParens_Mandatory () as sys
  ! thinBasic_GetRunTimeInfo             (string InfoName) as string
  ! thinBasic_VariableGetInfoEX          (string SearchKey, sys *pMainType,*pSubType,*pIsArray,*pDataPtr,*pnElements,WhichLevel) as sys
  ! thinBasic_LoadSymbol                 (string SymbolName,sys ReturnCode,FunctionOrSubPointer,ForceOverWrite) as sys

  end extern

  sys oxygen
  int ParensPresent
  string src,cod
  double dv
  '
  extern stdcall
  ==============
  '
  'DECLARATIONS FOR LATE BINDING TO OXYGEN.DLL
  '
  !* o2_abst      (string s) as string
  !* o2_basic     (string s) as sys
  !* o2_exec      (sys p=0)  as sys
  !* o2_buf       (int n)    as sys
  !* o2_errno     ()         as int
  !* o2_error     ()         as string
  !* o2_len       ()         as int
  !* o2_lib       ()         as sys
  !* o2_link      (string s) as sys
  !* o2_mode      (int m)
  !* o2_pathcall  (sys m)
  !* o2_prep      (string s) as string
  !* o2_varcall   (sys m)
  !* o2_version   () as string
  !* o2_view      (string s) as string


  function GetvarPtr(char*z) as sys, callback
  ===========================================
  '
  string VarName=z
  sys MainType, SubType, IsArray, DataPtr, nElements, WhichLevel
  thinBasic_VariableGetInfoEX Varname, MainType, SubType, IsArray,
    DataPtr, nElements, WhichLevel
  return DataPtr
  end function


  'Function GetRuntimeInfo(char*z) as string, callback
  '===================================================
  '
  'static string n=z
  ''n="APP_PATH" ' also "SCRIPT_PATH"  "HWND"
  'function=thinBasic_GetRunTimeInfo(n)
  'End Function


  Function inclpath(char*z) as string, callback
  =============================================
  '
  string wd=lcase(z)
  if left(wd,17)="%app_includepath%" then
    wd=thinBasic_GetRuntimeInfo("APP_PATH")+"inc"+mid(wd,18)
  end if
  return wd
  end Function


  #define  HOSTED


  Function tbo2_len() as int
  ==========================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  function=o2_len()
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  Function tbo2_buf() as sys
  ==========================
  '
  sys p
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  thinBasic_ParseLong(p)
  function=o2_buf(p)
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  sub tbo2_basic()
  ================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  dv=thinBasic_ParseString(src)
  o2_basic(src)
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Sub


  Function tbo2_exec() as sys
  ===========================
  '
  sys p
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  If ParensPresent = TB_TRUE Then
    dv=thinBasic_ParseString(cod)
    p=strptr cod
    thinBasic_CheckCloseParens_Mandatory()
  end if
  function=o2_exec(p)
  End Function


  Function tbo2_run() as sys
  ===========================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  dv=thinBasic_ParseString(src)
  if ParensPresent = TB_TRUE
    thinBasic_CheckCloseParens_Mandatory()
  endif
  o2_basic(src)
  if o2_errno()
    print o2_error
  else
    function=o2_exec(0)
  endif
  End Function


  Function tbO2_Link() as sys
  ===========================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  dv=thinBasic_ParseString(src)
  function=o2_link(src)
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  Function tbo2_View() as string
  ==============================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  dv=thinBasic_ParseString(src)
  function=o2_view(src)
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  Function tbo2_Prep() as string
  ==============================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  dv=thinBasic_ParseString(src)
  function=o2_prep(src)
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  Function tbo2_Abst() as string
  ==============================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  dv=thinBasic_ParseString(src)
  function=o2_abst(src)
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  Function tbo2_Error() as string
  ===============================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  function=o2_error()
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  Function tbo2_Errno() as int
  '===========================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  function=o2_errno()
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  Function tbo2_Version() as string
  =================================
  '
  ParensPresent = thinBasic_CheckOpenParens_Optional()
  function=o2_version()
  If ParensPresent = TB_TRUE Then thinBasic_CheckCloseParens_Mandatory()
  End Function


  Function finish() external
  ==========================
  if oxygen then FreeLibrary oxygen
  terminate
  End Function



  Function LoadLocalSymbols cdecl (string sPath) as sys, export 
  =============================================================
  /*
  This function is automatically called by thinCore whenever this DLL module is loaded.
  This function MUST (repeat MUST) be present in every external DLL module you want to use
  with thinBasic
  Use this function to initialize every you need and for loading the
  new symbol (read Keyword) you have created.
  */
  'LATE BINDING TO OXYGEN.DLL
  '
  oxygen=loadlibrary spath+"oxygen.dll"
  '
  if oxygen=0
    print "Oxygen.dll not here: " spath
    exit function
  end if
  '
  def lp
  ======
    @%1=GetProcAddress oxygen,"%1"
    'if @%1=0 then print "%1 not bound" : exit sub
  end def
  '
  lp o2_mode
  lp o2_len
  lp o2_buf
  lp o2_exec
  lp o2_lib
  lp o2_link
  lp o2_basic
  lp o2_abst
  lp o2_prep
  lp o2_view
  lp o2_error
  lp o2_errno
  lp o2_varcall
  lp o2_pathcall
  lp o2_version
  '
  o2_mode(9) 'bstrings UTF8
  o2_varcall  @GetVarPtr 'var callback pointer. proc for passing host variable name and returning variable pointer
  o2_pathcall @InclPath  'path callback to get path
  '
  thinBasic_LoadSymbol ("O2_Abst",     thinBasic_ReturnString,   @tbo2_Abst,    thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Basic",    thinBasic_ReturnNone,     @tbo2_Basic,   thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Buf",      thinBasic_ReturnCodeLong, @tbO2_Buf,     thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Errno",    thinBasic_ReturnCodeLong, @tbo2_Errno,   thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Error",    thinBasic_ReturnString,   @tbo2_Error,   thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Eval",     thinBasic_ReturnString,   @tbo2_Link,    thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Exec",     thinBasic_ReturnCodeLong, @tbO2_Exec,    thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Len",      thinBasic_ReturnCodeLong, @tbO2_Len,     thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Link",     thinBasic_ReturnString,   @tbo2_Link,    thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Prep",     thinBasic_ReturnString,   @tbo2_Prep,    thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Run",      thinBasic_ReturnCodeLong, @tbO2_Run,     thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_View",     thinBasic_ReturnString,   @tbo2_View,    thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("O2_Version",  thinBasic_ReturnString,   @tbo2_Version, thinBasic_ForceOverWrite)
  thinBasic_LoadSymbol ("Oxygen_Eval", thinBasic_ReturnString,   @tbo2_Link,    thinBasic_ForceOverWrite)
  End Function


  Function UnLoadLocalSymbols cdecl (string sPath) as sys, export
  ===============================================================
  End Function

  end extern

