1. -- 
  2. -- Copyright (c) 2012 Kevin Wellwood 
  3. -- All rights reserved. 
  4. -- 
  5. -- This source code is distributed under the Modified BSD License. For terms and 
  6. -- conditions, see license.txt. 
  7. -- 
  8.  
  9. with Objects;                           use Objects; 
  10. with Scripting.Parsers;                 use Scripting.Parsers; 
  11. with Tokens.Scanners;                   use Tokens.Scanners; 
  12.  
  13. package Scripting.Evaluators is 
  14.  
  15.     -- An Evaluator can parse and evaluate scripts and expressions. Currently 
  16.     -- only expressions are supported for evaluation. Symbols and functions in 
  17.     -- expressions are resolved by an object implementing the Evaluation_Node 
  18.     -- interface. The Evaluator can be reused to perform multiple evaluations on 
  19.     -- different input and it is recommended that objects not be created and 
  20.     -- deleted for every evaluation, to limit overhead. 
  21.     type Evaluator is new Limited_Object with private; 
  22.     type A_Evaluator is access all Evaluator'Class; 
  23.  
  24.     -- Creates a new Evaluator to evaluate text script/expressions. 
  25.     function Create_Evaluator return A_Evaluator; 
  26.     pragma Postcondition( Create_Evaluator'Result /= null ); 
  27.  
  28.     -- Evaluates the text of 'expression' and returns 'result'. Specify 
  29.     -- 'evalNode' to evaluate symbols and functions by name during evaluation. 
  30.     -- If the expression couldn't be evaluated due to a runtime error, Null will 
  31.     -- be returned. (For example, a variable couldn't be resolved or a value 
  32.     -- type mismatch occured.) If a parsing error occurs then Parse_Exception 
  33.     -- will be raised. If an error occurs while evaluating symbols or functions 
  34.     -- with the evaluation node, Evaluation_Exception will be raised. 
  35.     function Evaluate_Expression( this       : not null access Evaluator'Class; 
  36.                                   expression : String; 
  37.                                   evalNode   : A_Evaluation_Node ) return Value_Ptr; 
  38.  
  39.     -- Evaluates a script in 'text', using 'resolver' and 'func_eval' as 
  40.     -- necessary to resolve variable names and evaluate functions. A script is 
  41.     -- simply a list of statements terminated by semicolons. If a parsing error 
  42.     -- occurs then Parse_Exception will be raised. If an error occurs while 
  43.     -- evaluating symbols or functions with the evaluation node, 
  44.     -- Evaluation_Exception will be raised. 
  45.     procedure Evaluate_Script( this     : not null access Evaluator'Class; 
  46.                                text     : String; 
  47.                                evalNode : A_Evaluation_Node ); 
  48.  
  49.     -- Deletes the Evaluator. 
  50.     procedure Delete( this : in out A_Evaluator ); 
  51.     pragma Postcondition( this = null ); 
  52.  
  53. private 
  54.  
  55.      type Evaluator is new Limited_Object with 
  56.         record 
  57.             scanner : A_Token_Scanner := Create_Token_Scanner; 
  58.             parser  : A_Parser := Create_Parser; 
  59.         end record; 
  60.  
  61.     procedure Delete( this : in out Evaluator ); 
  62.  
  63. end Scripting.Evaluators;