with Objects; use Objects;
with Symbol_Resolvers; use Symbol_Resolvers;
with Tokens; use Tokens;
with Values; use Values;
private with Ada.Unchecked_Deallocation;
package Expressions is
-- An Expression represents a mathematical or logical expression tree that
-- can be evaluated to produce a value. It is composed of operators and
-- operands. The three base data types used in expressions are numeric,
-- string, and boolean, which are represented with the Values hierarchy.
type Expression is abstract new Limited_Object with private;
type A_Expression is access all Expression'Class;
-- Deletes the Expression.
procedure Delete( this : in out A_Expression );
private
type Eval_Context is tagged
record
resolver : A_Symbol_Resolver;
end record;
type A_Eval_Context is access all Eval_Context'Class;
-- Creates a new Eval_Context using 'resolver' to resolve symbol names.
function Create_Eval_Context( resolver : A_Symbol_Resolver ) return A_Eval_Context;
pragma Postcondition( Create_Eval_Context'Result /= null );
-- Resolves symbol name 'symbol', returning the result. null will be
-- returned if the symbol can't be resolved.
function Resolve( this : access Eval_Context; symbol : String ) return A_Value;
-- Deletes the Eval_Context object.
procedure Delete is new Ada.Unchecked_Deallocation( Eval_Context'Class, A_Eval_Context );
----------------------------------------------------------------------------
type Expression is abstract new Limited_Object with
record
loc : Token_Location;
end record;
procedure Construct( this : access Expression; loc : Token_Location );
-- Evaluates the expression and it's subtree(s). Returns null if the
-- expression can't be evaluated due to mismatched types or unresolved
-- symbols.
--
-- This function must be overridden. It is not declared abstract to avoid
-- declaring it publicly and exposing the Eval_Context class. The default
-- implementation raises Constraint_Error.
function Evaluate( this : access Expression;
context : not null A_Eval_Context ) return A_Value;
end Expressions;