Functional-lang  0.0
An example functional programming language
Public Member Functions | Protected Attributes | List of all members
Expr Class Reference

Main expression handle (should only be used as parent class). More...

#include <syntax.hpp>

Inheritance diagram for Expr:
GCObj AnyExpr AtomExpr BiOpExpr FunctionExpr IdExpr IfExpr IntExpr LambdaExpr LetExpr NumExpr UnOpExpr

Public Member Functions

 Expr (GCMain &gc, ExprType type, const TokenPos &pos)
 
std::size_t getDepth () const noexcept
 
bool hasLastEval () const noexcept
 
const TokenPos & getTokenPos () const noexcept
 
virtual std::string toString () const noexcept
 
ExprType getExpressionType () const noexcept
 
virtual Expr * eval (GCMain &gc, Environment &env) noexcept
 Evaluates expression one time. More...
 
Expr * evalWithLookup (GCMain &gc, Environment &env) noexcept
 
virtual Expr * replace (GCMain &gc, const std::string &name, Expr *expr) const noexcept
 Replace all identifiers equal to name with expr. More...
 
virtual bool equals (const Expr *expr, bool exact=false) const noexcept
 Checks if this expression and expr have the same structure, Every expression has the same structure as '_' (it's called ANY after all). More...
 
virtual std::vector< std::string > getIdentifiers () const noexcept
 
virtual void mark (GCMain &gc) noexcept override
 If not marked mark itself and children.
 
virtual Expr * optimize (GCMain &gc) noexcept
 
- Public Member Functions inherited from GCObj
 GCObj (GCMain &main) noexcept
 Initialize as not marked.
 
virtual bool isMarked (GCMain &main) const noexcept
 

Protected Attributes

std::size_t depth
 
Expr * lastEval = nullptr
 

Additional Inherited Members

- Protected Member Functions inherited from GCObj
virtual void markSelf (GCMain &main)
 Marks itself.
 

Detailed Description

Main expression handle (should only be used as parent class).

See also
StackFrameObj

To use Expr as local heap variable, use a StackFrameObj.

Member Function Documentation

◆ equals()

virtual bool Expr::equals ( const Expr *  expr,
bool  exact = false 
) const
inlinevirtualnoexcept

Checks if this expression and expr have the same structure, Every expression has the same structure as '_' (it's called ANY after all).

Parameters
exprThe expression to compare to this.
exactIf true any must equal any and nothing else. Otherwise any is equal to anyting else.

Reimplemented in LetExpr, AnyExpr, IfExpr, AtomExpr, LambdaExpr, IdExpr, IntExpr, NumExpr, UnOpExpr, and BiOpExpr.

◆ eval()

virtual Expr* Expr::eval ( GCMain &  gc,
Environment &  env 
)
inlinevirtualnoexcept

Evaluates expression one time.

Parameters
gc
envEnvironment for accessing variables.
Returns
Returns itself if nothing evaluated, otherwise a new expression.

Reimplemented in FunctionExpr, LetExpr, IfExpr, IdExpr, UnOpExpr, and BiOpExpr.

◆ evalWithLookup()

Expr* Expr::evalWithLookup ( GCMain &  gc,
Environment &  env 
)
noexcept
Returns
Returns Last evaluation if already computed (except assignments). If no last evaluation, evaluate. To reset evaluation "lastEval = nullptr".
Parameters
gc
env

◆ getExpressionType()

ExprType Expr::getExpressionType ( ) const
inlinenoexcept
Returns
Returns the type of expression.
See also
ExprType

◆ getIdentifiers()

virtual std::vector<std::string> Expr::getIdentifiers ( ) const
inlinevirtualnoexcept
Returns
Returns all identifiers used in expression.

Reimplemented in LetExpr, IfExpr, LambdaExpr, IdExpr, and BiOpExpr.

◆ getTokenPos()

const TokenPos& Expr::getTokenPos ( ) const
inlinenoexcept
Returns
Returns position of token in code.

◆ hasLastEval()

bool Expr::hasLastEval ( ) const
inlinenoexcept
Returns
Returns true, if has last evaluation (not nullptr), otherwise false.

◆ optimize()

virtual Expr* Expr::optimize ( GCMain &  gc)
virtualnoexcept
Returns
Returns an optimized version of this expression. If nothing was optimized, returns itself.

Reimplemented in LetExpr, IfExpr, LambdaExpr, UnOpExpr, and BiOpExpr.

◆ replace()

virtual Expr* Expr::replace ( GCMain &  gc,
const std::string &  name,
Expr *  expr 
) const
inlinevirtualnoexcept

Replace all identifiers equal to name with expr.

Parameters
gc
nameIdentifer name to replace. If name is empty, every identifier will be replaced by AnyExpr(expr_any).
exprThe expression to replaces identifiers.
Returns
Returns new expression, where matching identifiers are replaced. Returns itself if not possible (expression can't contain any identifiers) or if expression is a lambda function, where the <id> is equal to name.

Reimplemented in LetExpr, IfExpr, LambdaExpr, IdExpr, and BiOpExpr.

◆ toString()

virtual std::string Expr::toString ( ) const
inlinevirtualnoexcept
Returns
Returns expression in functional-lang (the programming language to parse).

Reimplemented in FunctionExpr, LetExpr, AnyExpr, IfExpr, AtomExpr, LambdaExpr, IdExpr, IntExpr, NumExpr, and BiOpExpr.


The documentation for this class was generated from the following file: