Skip to content

Expression

INCLUDE FILE
#include "Expression/Expression.h"

class Expression

Represents an expression. This is the class other parts of the program interact with. It is a wrapper around the BaseExpression hierarchy.

DEFINITION
class Expression;

constructor Expression

Construct a void Expression.

DEFINITION
Expression ( );

constructor Expression

Construct an Expression from a BaseExpression.

DEFINITION
Expression ( std::shared_ptr< BaseExpression >  expression );
PARAMETERDESCRIPTION
expression

The BaseExpression to wrap.

constructor Expression

Construct an Expression by parsing it from a Tokenizer.

DEFINITION
Expression ( Tokenizer & tokenizer );
PARAMETERDESCRIPTION
tokenizer

The Tokenizer to use for parsing the expression.

method asconst

Get the expression as a specific type.

DEFINITION
template <typename T>
const T * as ( ) const;
RETURNSDESCRIPTION
const T *

A pointer to the expression as type T, or nullptr if it is not of that type.

method as

DEFINITION
template <typename T>
T * as ( );
RETURNSDESCRIPTION
T *

method cloneconst

Create a deep copy of the expression.

If the expression does not change during evaluation, it may be reused instead of cloned.

DEFINITION
Expression  clone ( const CloneContext & context ) const;
PARAMETERDESCRIPTION
context
RETURNSDESCRIPTION
Expression

A new Expression that is a deep copy of this one.

method createstatic

DEFINITION
static Expression  create ( const Token & token );
PARAMETERDESCRIPTION
token
RETURNSDESCRIPTION
Expression

method evaluate

Evaluate the expression in a given context.

DEFINITION
void  evaluate ( const EvaluationContext & context );
PARAMETERDESCRIPTION
context

The evaluation context.

method expand_calls

Expand function calls in the expression.

If a function call cannot be expanded, either an error should be reported via DiagnosticOutput and the expression marked as invalid, or an exception should be thrown.

DEFINITION
std::optional< Expression >  expand_calls ( );
RETURNSDESCRIPTION
std::optional< Expression >

The Expression to replace this Expression with, {} if no replacement is needed.

method has_typeconst

Check if the expression has a specific type.

If type is an aggregate type (like Value::INTEGER), this method will return true if the expression's type is a subtype of it.

DEFINITION
std::optional< bool >  has_type ( Value::Type  type ) const;
PARAMETERDESCRIPTION
type

The type to check against.

RETURNSDESCRIPTION
std::optional< bool >

true if the expression has the specified type, false if it has a different type, and {} if the type cannot be determined.

method has_valueconst

Check if the value of the expression is known.

DEFINITION
bool  has_value ( ) const;
RETURNSDESCRIPTION
bool

true if the value is known, false otherwise.

method isconst

Check if the expression is of a specific type.

DEFINITION
template <typename T>
bool  is ( ) const;
RETURNSDESCRIPTION
bool

true if the expression is of type T, false otherwise.

method locationconst

Get the location in the source code where the expression was defined.

DEFINITION
const Location & location ( ) const;
RETURNSDESCRIPTION
const Location &

method maximum_valueconst

Get the maximum possible value of the expression.

DEFINITION
std::optional< Value >  maximum_value ( ) const;
RETURNSDESCRIPTION
std::optional< Value >

The maximum possible value of the expression if it can be determined, {} otherwise.

method minimum_valueconst

Get the minimum possible value of the expression.

DEFINITION
std::optional< Value >  minimum_value ( ) const;
RETURNSDESCRIPTION
std::optional< Value >

The minimum possible value of the expression. if it can be determined, {} otherwise.

method resolve

Resolve all names in the expression.

DEFINITION
void  resolve ( Scope * scope,
Entity * containing_entity );
PARAMETERDESCRIPTION
scope

The scope to resolve names in.

containing_entity

The entity that contains the expression.

method serializeconst

Serialize the expression to a stream.

DEFINITION
void  serialize ( std::ostream & stream ) const;
PARAMETERDESCRIPTION
stream

The output stream to serialize the expression to.

method typeconst

Get the type of the expression's value.

DEFINITION
std::optional< Value::Type >  type ( ) const;
RETURNSDESCRIPTION
std::optional< Value::Type >

The type of the expression's value if it can be determined, {} otherwise.

method type_nameconst

DEFINITION
std::string_view  type_name ( ) const;
RETURNSDESCRIPTION
std::string_view

method validconst

Check if the expression is valid.

If a translation error occurred while translating the expression, it is considered invalid. This allows the expression to be used in further evaluations without causing additional errors.

DEFINITION
bool  valid ( ) const;
RETURNSDESCRIPTION
bool

True if the expression is valid, false otherwise.

method valueconst

Get the value of the expression.

DEFINITION
std::optional< Value >  value ( ) const;
RETURNSDESCRIPTION
std::optional< Value >

The value of the expression if it is known, {} otherwise.

method variable_nameconst

If it is a variable expression, return the variable name.

DEFINITION
Symbol  variable_name ( ) const;
RETURNSDESCRIPTION
Symbol

The variable name, or the empty Symbol if it isn't a variable expression.

member expressionprivate

A shared pointer to the BaseExpression that this Expression wraps.

DEFINITION
std::shared_ptr< BaseExpression >  expression;

member void_expressionstatic private

A shared pointer to a void expression, used for constructing void expressions.

DEFINITION
static std::shared_ptr< BaseExpression >  void_expression;