Skip to content

IfBody

INCLUDE FILE
#include "Body/IfBody.h"

class IfBody

Represents a conditional body. Parts of this body will be discarded, based on the evaluation of the conditions.

DEFINITION
class IfBody;

constructor IfBody

Construct a new IfBody.

DEFINITION
IfBody ( std::vector< IfBodyClause >  clauses );
PARAMETERDESCRIPTION
clauses

The clauses for the conditional body.

method cloneconst virtual

DEFINITION
virtual Body  clone ( const CloneContext & context ) const;
PARAMETERDESCRIPTION
context
RETURNSDESCRIPTION
Body

method createstatic

Create an IfBody from a list of clauses.

This might not create an IfBody if the clauses can be simplified.

DEFINITION
static Body  create ( const std::vector< IfBodyClause > & clauses );
PARAMETERDESCRIPTION
clauses

The clauses for the conditional body.

RETURNSDESCRIPTION
Body

The created IfBody.

method encodevirtual

Encode the BodyElement into a byte string.

The default implementation does nothing. Subclasses that represent data or instructions should override this method.

DEFINITION
virtual void  encode ( std::string & bytes,
const Memory * memory );
PARAMETERDESCRIPTION
bytes

The byte string to encode into.

memory

The memory context for encoding.

method evaluatevirtual

Evaluate the BodyElement in a given context.

Subclasses must implement this method.

If an error occurs during evaluation, either an error should be reported via DiagnosticOutput and the BodyElement marked as invalid, or an exception should be thrown.

DEFINITION
virtual std::optional< Body >  evaluate ( const EvaluationContext & context );
PARAMETERDESCRIPTION
context

The evaluation context.

RETURNSDESCRIPTION
std::optional< Body >

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

method serializeconst virtual

Serialize the BodyElement to a stream.

Subclasses must implement this method.

DEFINITION
virtual void  serialize ( std::ostream & stream,
const std::string & prefix ) const;
PARAMETERDESCRIPTION
stream

The stream to serialize to.

prefix

The prefix for each line of the serialized output.

method simplifystatic private

Simplify the clauses of the IfBody.

DEFINITION
static std::optional< Body >  simplify ( std::vector< IfBodyClause > & clauses,
bool  always_create );
PARAMETERDESCRIPTION
clauses
always_create
RETURNSDESCRIPTION
std::optional< Body >

method traversevirtual

Traverse the BodyElement.

This function is used by the default implementations of resolve() and expand_calls(). Unless a subclass overrides both of these methods, it should override traverse().

DEFINITION
virtual void  traverse ( std::function< void(Body &)>  body_callable,
std::function< void(Expression &)>  expression_callable );
PARAMETERDESCRIPTION
body_callable

A callable to apply to each sub-body-element.

expression_callable

A callable to apply to each sub-expression.

member clausesprivate

The clauses for the conditional body.

DEFINITION
std::vector< IfBodyClause >  clauses;

class IfBodyClause

Represents a clause in an IfBody, consisting of a condition and a body.

If no condition is provided, the clause is considered the "else" clause and will be executed if all previous conditions are false.

DEFINITION
class IfBodyClause;

constructor IfBodyClause

Construct a new IfBodyClause.

DEFINITION
IfBodyClause ( std::optional< Expression >  condition,
Body  body );
PARAMETERDESCRIPTION
condition

The condition for the clause. If not provided, this is the "else" clause.

body

The body to execute if the condition is true.

method cloneconst

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

method is_falseconst

Check if the clause's condition evaluates to false.

DEFINITION
bool  is_false ( ) const;
RETURNSDESCRIPTION
bool

true if the condition is known to be false, false otherwise.

method is_trueconst

Check if the clause's condition evaluates to true.

DEFINITION
bool  is_true ( ) const;
RETURNSDESCRIPTION
bool

true if the condition is known to be true, false otherwise.

member body

The body to execute if the condition is true.

DEFINITION
Body  body;

member condition

The condition for the clause. If not provided, this is the "else" clause.

DEFINITION
std::optional< Expression >  condition;