AST#

namespace stapl
namespace ast#

Namespace of AST nodes and utility functions.

Typedefs

using ExprNode = std::variant<LiteralExprNode<int>, LiteralExprNode<double>, LiteralExprNode<bool>, VariableExprNode, std::unique_ptr<struct UnaryExprNode>, std::unique_ptr<struct BinaryExprNode>, std::unique_ptr<struct CallExprNode>>#

Variant for expression nodes.

using StmtNode = std::variant<LetStmtNode, AssignmentStmtNode, std::unique_ptr<struct IfStmtNode>, std::unique_ptr<struct WhileStmtNode>, BreakStmtNode, ContinueStmtNode, ReturnStmtNode, std::unique_ptr<struct CompoundStmtNode>>#

Variant for statement nodes.

using DeclNode = std::variant<FunctionDeclNode>#

Variant for declaration nodes.

Functions

template<typename T>
inline bool operator==(const std::unique_ptr<T> &p1, const std::unique_ptr<T> &p2)#

Comparision operator overload for unique pointers of AST nodes.

Parameters:
  • p1std::unique_ptr on the LHS.

  • p2std::unique_ptr on the RHS.

Returns:

Whether the objects referenced by p1 and p2 are equal.

template<typename T>
struct LiteralExprNode#
#include <ast.h>

AST node for literal expressions, such as numbers.

Todo:

Add support for types other than int, double and bool.

Public Functions

LiteralExprNode(LiteralExprNode<T>&&) = default#

Move constructor.

explicit LiteralExprNode(T value)#

Instantiate from literal value.

Parameters:

value – Literal value.

LiteralExprNode<T> &operator=(LiteralExprNode<T>&&) = default#

Move assignment operator.

bool operator==(const LiteralExprNode<T> &rhs) const = default#

Comparision operator overload.

Parameters:

rhsLiteralExprNode<T> on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

T value#

Literal value.

std::optional<std::string> expr_type = {}#

Type of the literal expression.

struct VariableExprNode#
#include <ast.h>

AST node for variable expressions.

Public Functions

VariableExprNode(VariableExprNode&&) = default#

Move constructor.

explicit VariableExprNode(const std::string &name)#

Instantiate from variable name.

Parameters:

name – Variable name.

VariableExprNode &operator=(VariableExprNode&&) = default#

Move assignment operator.

bool operator==(const VariableExprNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsVariableExprNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

std::string name#

Variable name.

std::optional<std::string> expr_type = {}#

Type of the variable expression.

struct UnaryExprNode#
#include <ast.h>

AST node for unary expressions.

Public Functions

UnaryExprNode(UnaryExprNode&&) = default#

Move constructor.

explicit UnaryExprNode(const std::string &op, ExprNode rhs)#

Instantiate from operator and operand.

Parameters:
  • op – Operator of the unary expression.

  • rhs – Operand of the unary expression.

UnaryExprNode &operator=(UnaryExprNode&&) = default#

Move assignment operator.

bool operator==(const UnaryExprNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsUnaryExprNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

std::string op#

Operator of the unary expression.

ExprNode rhs#

Operand of the unary expression.

std::optional<std::string> expr_type = {}#

Type of the unary expression.

struct BinaryExprNode#
#include <ast.h>

AST node for binary expressions.

Public Functions

BinaryExprNode(BinaryExprNode&&) = default#

Move constructor.

explicit BinaryExprNode(const std::string &op, ExprNode lhs, ExprNode rhs)#

Instantiate from operator and operands.

Parameters:
  • op – Operator of the binary expression.

  • lhs – LHS of the binary expression.

  • rhs – RHS of the binary expression.

BinaryExprNode &operator=(BinaryExprNode&&) = default#

Move assignment operator.

bool operator==(const BinaryExprNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsBinaryExprNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

std::string op#

Operator of the binary expression.

ExprNode lhs#

LHS of the binary expression.

ExprNode rhs#

RHS of the binary expression.

std::optional<std::string> expr_type = {}#

Type of the binary expression.

struct CallExprNode#
#include <ast.h>

AST node for function call expressions.

Public Functions

CallExprNode(CallExprNode&&) = default#

Move constructor.

explicit CallExprNode(const std::string &callee, std::vector<ExprNode> args)#

Instantiate from function name and arguments.

Parameters:
  • callee – Function to call.

  • args – Arguments of the function call.

CallExprNode &operator=(CallExprNode&&) = default#

Move assignment operator.

bool operator==(const CallExprNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsCallExprNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

std::string callee#

Function to call.

std::vector<ExprNode> args#

Arguments of the function call.

std::optional<std::string> expr_type = {}#

Type of the function call expression.

struct PrototypeNode#
#include <ast.h>

AST node for function prototype.

Public Functions

PrototypeNode(PrototypeNode&&) = default#

Move constructor.

explicit PrototypeNode(const std::string &name, std::vector<std::pair<std::string, std::string>> args, const std::string &return_type)#

Instantiate from function name, pair of argument names and types, and return type.

Parameters:
  • name – Function name.

  • args – Arguments names and types of the function.

  • return_type – Return type name of the function.

PrototypeNode &operator=(PrototypeNode&&) = default#

Move assignment operator.

bool operator==(const PrototypeNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsPrototypeNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

std::string name#

Function name.

std::string return_type#

Return type name of the function.

std::vector<std::pair<std::string, std::string>> args#

Arguments names and types of the function.

struct LetStmtNode#
#include <ast.h>

AST node for let statement.

Public Functions

LetStmtNode(LetStmtNode&&) = default#

Move constructor.

explicit LetStmtNode(const std::string &var_name, const std::string &var_type)#

Instantiate from variable name and type.

Parameters:
  • var_name – Variable name.

  • var_type – Type name of the variable.

LetStmtNode &operator=(LetStmtNode&&) = default#

Move assignment operator.

bool operator==(const LetStmtNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsLetStmtNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

std::string var_name#

Variable name.

std::string var_type#

Type name of the variable.

struct AssignmentStmtNode#
#include <ast.h>

AST node for assignment statement.

Public Functions

AssignmentStmtNode(AssignmentStmtNode&&) = default#

Move constructor.

explicit AssignmentStmtNode(const std::string &var_name, ExprNode assign_expr)#

Instantiate from variable name and assignment expression.

Parameters:
  • var_name – Name of the variable to be assigned.

  • assign_expr – Expression to be assigned.

AssignmentStmtNode &operator=(AssignmentStmtNode&&) = default#

Move assignment operator.

bool operator==(const AssignmentStmtNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsAssignmentStmtNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

std::string var_name#

Name of the variable to be assigned.

ExprNode assign_expr#

Expression to be assigned.

struct ReturnStmtNode#
#include <ast.h>

AST node for return statement.

Public Functions

ReturnStmtNode(ReturnStmtNode&&) = default#

Move constructor.

explicit ReturnStmtNode(ExprNode return_expr)#

Instantiate from expression to be returned.

Parameters:

return_expr – Expression to be returned.

ReturnStmtNode &operator=(ReturnStmtNode&&) = default#

Move assignment operator.

bool operator==(const ReturnStmtNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsReturnStmtNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

ExprNode return_expr#

Expression to be returned.

struct BreakStmtNode#
#include <ast.h>

AST node for break statement.

Public Functions

BreakStmtNode(BreakStmtNode&&) = default#

Move constructor.

explicit BreakStmtNode() = default#

Default constructor.

BreakStmtNode &operator=(BreakStmtNode&&) = default#

Move assignment operator.

bool operator==(const BreakStmtNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsBreakStmtNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

struct ContinueStmtNode#
#include <ast.h>

AST node for continue statement.

Public Functions

ContinueStmtNode(ContinueStmtNode&&) = default#

Move constructor.

explicit ContinueStmtNode() = default#

Default constructor.

ContinueStmtNode &operator=(ContinueStmtNode&&) = default#

Move assignment operator.

bool operator==(const ContinueStmtNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsContinueStmtNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

struct IfStmtNode#
#include <ast.h>

AST node for if statement.

Public Functions

IfStmtNode(IfStmtNode&&) = default#

Move constructor.

explicit IfStmtNode(ExprNode condition, StmtNode then_stmt, StmtNode else_stmt)#

Instantiate from condition expression, statement to be executed if the condition is true, and statement to be executed if the condition is false.

Parameters:
  • condition – Condition expression.

  • then_stmt – Statement to be executed if the condition is true.

  • else_stmt – Statement to be executed if the condition is false.

IfStmtNode &operator=(IfStmtNode&&) = default#

Move assignment operator.

bool operator==(const IfStmtNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsIfStmtNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

ExprNode condition#

Condition expression.

StmtNode then_stmt#

Statement to be executed if the condition is true.

StmtNode else_stmt#

Statement to be executed if the condition is false.

struct WhileStmtNode#
#include <ast.h>

AST node for while statement.

Public Functions

WhileStmtNode(WhileStmtNode&&) = default#

Move constructor.

explicit WhileStmtNode(ExprNode condition, StmtNode body)#

Instantiate from condition expression and statement to be executed if the condition is true.

Parameters:
  • condition – Condition expression.

  • body – Statement to be executed if the condition is true.

WhileStmtNode &operator=(WhileStmtNode&&) = default#

Move assignment operator.

bool operator==(const WhileStmtNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsWhileStmtNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

ExprNode condition#

Condition expression.

StmtNode body#

Statement to be executed if the condition is true.

struct CompoundStmtNode#
#include <ast.h>

AST node for compound statement.

Public Functions

CompoundStmtNode(CompoundStmtNode&&) = default#

Move constructor.

explicit CompoundStmtNode(std::vector<StmtNode> stmts)#

Instantiate from statements in the compound statement.

Parameters:

stmts – Statements in the compound statement.

CompoundStmtNode &operator=(CompoundStmtNode&&) = default#

Move assignment operator.

bool operator==(const CompoundStmtNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsCompoundStmtNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

std::vector<StmtNode> stmts#

Statements in the compound statement.

struct FunctionDeclNode#
#include <ast.h>

AST node for function definition.

Public Functions

FunctionDeclNode(FunctionDeclNode&&) = default#

Move constructor.

explicit FunctionDeclNode(PrototypeNode proto, StmtNode func_body)#

Instantiate from prototype and function body.

Parameters:
  • proto – Prototype of the function.

  • func_body – Body of the function.

explicit FunctionDeclNode(PrototypeNode proto)#

Constructor for extern functions.

FunctionDeclNode &operator=(FunctionDeclNode&&) = default#

Move assignment operator.

bool operator==(const FunctionDeclNode &rhs) const = default#

Comparision operator overload.

Parameters:

rhsFunctionDeclNode on the RHS.

Returns:

Whether the objects referenced by this and rhs are equal.

Public Members

PrototypeNode proto#

Prototype of the function.

std::optional<StmtNode> func_body#

Body of the function.

struct Module#
#include <ast.h>

Module struct.

Public Functions

Module(Module&&) = default#

Move constructor.

explicit Module(const std::string &name, std::vector<DeclNode> decls)#

Instantiate from name and declarations.

Parameters:
  • name – Name of the module.

  • decls – Declarations in the module.

Module &operator=(Module&&) = default#

Move assignment operator.

Public Members

std::string name#

Name of the module.

std::vector<DeclNode> decls#

Declarations in the module.

namespace stapl
namespace ast

Namespace of AST nodes and utility functions.

class ASTPrinter#
#include <ast_printer.h>

A visitor for printing AST nodes.

Public Functions

std::string operator()(const LiteralExprNode<int> &node) const#

Represent LiteralExprNode<int> as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const LiteralExprNode<double> &node) const#

Represent LiteralExprNode<double> as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const LiteralExprNode<bool> &node) const#

Represent LiteralExprNode<bool> as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const VariableExprNode &node) const#

Represent VariableExprNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const std::unique_ptr<UnaryExprNode> &node) const#

Represent UnaryExprNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const std::unique_ptr<BinaryExprNode> &node) const#

Represent BinaryExprNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const std::unique_ptr<CallExprNode> &node) const#

Represent CallExprNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const PrototypeNode &node) const#

Represent PrototypeNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const LetStmtNode &node) const#

Represent LetStmtNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const AssignmentStmtNode &node) const#

Represent AssignmentStmtNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const std::unique_ptr<IfStmtNode> &node) const#

Represent IfStmtNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const std::unique_ptr<WhileStmtNode> &node) const#

Represent WhileStmtNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const BreakStmtNode &node) const#

Represent BreakStmtNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const ContinueStmtNode &node) const#

Represent ContinueStmtNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const ReturnStmtNode &node) const#

Represent ReturnStmtNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const std::unique_ptr<CompoundStmtNode> &node) const#

Represent CompoundStmtNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.

std::string operator()(const FunctionDeclNode &node) const#

Represent FunctionDeclNode as string.

Parameters:

node – The node to represent.

Returns:

The string representation of the node.