home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d456 / CAJSCRPT.ZIP / ifps3 / help / sample1.dpr < prev    next >
Text File  |  2002-06-20  |  2KB  |  73 lines

  1. program sample1;
  2.  
  3. uses
  4.   ifpscomp,
  5.   ifps3,
  6.   ifps3lib_std,
  7.   ifps3lib_stdr;
  8.  
  9. function ScriptOnUses(Sender: TIFPSPascalCompiler; const Name: string): Boolean;
  10. { the OnUses callback function is called for each "uses" in the script. 
  11.   It's always called with the parameter 'SYSTEM' at the top of the script. 
  12.   For example: uses ii1, ii2;   
  13.   This will call this function 3 times. First with 'SYSTEM' then 'II1' and then 'II2'.
  14. }
  15. begin
  16.   if Name = 'SYSTEM' then
  17.   begin
  18.     RegisterStandardLibrary_C(Sender);
  19.     { Register the standard library. The standard library is in the 
  20.       ifps3lib_std.pas unit. This will register all standard functions like
  21.       Copy, Pos, Length.
  22.     }
  23.     Result := True;
  24.   end else
  25.     Result := False;
  26. end;
  27.  
  28. procedure ExecuteScript(const Script: string);
  29. var
  30.   Compiler: TIFPSPascalCompiler;
  31.   { TIFPSPascalCompiler is the compiler part of the scriptengine. This will 
  32.     translate a Pascal script into a compiled for the executer understands. } 
  33.   Exec: TIFPSExec;
  34.    { TIFPSExec is the executer part of the scriptengine. It uses the output of
  35.     the compiler to run a script. }
  36.   Data: string;
  37. begin
  38.   Compiler := TIFPSPascalCompiler.Create; // create an instance of the compiler.
  39.   Compiler.OnUses := ScriptOnUses; // assign the OnUses event.
  40.   if not Compiler.Compile(Script) then  // Compile the Pascal script into bytecode.
  41.   begin
  42.     Compiler.Free;
  43.      // You could raise an exception here.
  44.     Exit;
  45.   end;
  46.  
  47.   Compiler.GetOutput(Data); // Save the output of the compiler in the string Data.
  48.   Compiler.Free; // After compiling the script, there is no need for the compiler anymore.
  49.  
  50.   Exec := TIFPSExec.Create;  // Create an instance of the executer.
  51.   RegisterStandardLibrary_R(Exec);
  52.   { The functions registered at compile time also need to be registered at runtime. These
  53.     functions can be found in the ifps3lib_stdr.pas unit. }
  54.   if not  Exec.LoadData(Data) then // Load the data from the Data string.
  55.   begin
  56.     { For some reason the script could not be loaded. This is usually the case when a 
  57.       library that has been used at compile time isn't registered at runtime. }
  58.     Exec.Free;
  59.      // You could raise an exception here.
  60.     Exit;
  61.   end;
  62.  
  63.   Exec.RunScript; // Run the script.
  64.   Exec.Free; // Free the executer.
  65. end;
  66.  
  67. const
  68.   Script = 'var s: string; begin s := ''Test''; S := s + ''ing;''; end.';
  69.  
  70. begin
  71.   ExecuteScript(Script);
  72. end.
  73.