67 lines
1.2 KiB
C++

#ifndef STMT_HH
#define STMT_HH
#include "expr.hh"
#include <memory>
#include <variant>
struct Stmt;
struct _Expression;
struct _Print;
struct _Var;
enum class StmtType {
EXPRESSION,
PRINT,
VAR,
};
using StmtVariant = std::variant<
std::shared_ptr<_Expression>,
std::shared_ptr<_Print>,
std::shared_ptr<_Var>
>;
#define Expression(EXPR) (Stmt{StmtType::EXPRESSION, std::make_shared<_Expression>(EXPR)})
#define Print(EXPR) (Stmt{StmtType::PRINT, std::make_shared<_Print>(EXPR)})
#define Var(NAME, EXPR) (Stmt{StmtType::VAR, std::make_shared<_Var>(NAME, EXPR)})
struct Stmt {
Stmt(StmtType type, StmtVariant value);
// Copy constructor
Stmt(const Stmt &other);
// Move constructor
Stmt(Stmt &&other) noexcept;
// Copy assignment
Stmt &operator=(const Stmt &other);
// Move assignment
Stmt &operator=(Stmt &&other) noexcept;
~Stmt() = default;
StmtType type;
StmtVariant value;
};
struct _Expression {
_Expression(const Expr &expr) : expr{expr} {}
Expr expr;
};
struct _Print {
_Print(const Expr &expr) : expr{expr} {}
Expr expr;
};
struct _Var {
_Var(const Token &name, const Expr &expr) : name{name}, expr{expr} {}
Token name;
Expr expr;
};
#endif