Skip to content

Commit

Permalink
rebalance pt.1
Browse files Browse the repository at this point in the history
  • Loading branch information
amanuel2 committed Jun 23, 2024
1 parent 9968140 commit dd1a1f6
Show file tree
Hide file tree
Showing 24 changed files with 521 additions and 394 deletions.
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
"MIMode": "lldb",
"preLaunchTask": "make"
},
{
"name": "(lldb) Launch Test",
Expand All @@ -32,7 +33,8 @@
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
"MIMode": "lldb",
"preLaunchTask": "make"
}
]
}
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"atomic": "cpp",
"exception": "cpp",
"system_error": "cpp",
"thread": "cpp"
"thread": "cpp",
"type_traits": "cpp"
}
}
12 changes: 12 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,17 @@
"isDefault": true
}
},

// make the program
{
"type": "shell",
"label": "make",
"command": "make",
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
},
]
}
192 changes: 96 additions & 96 deletions compile_commands.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions configure

This file was deleted.

44 changes: 21 additions & 23 deletions include/ast/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <exception>
#include <ast/grmr.hh>
#include <ast/expr.hh>
#include <ast/stmt.hh>
#include <utils/arithmetic.hh>
Expand All @@ -33,40 +34,37 @@ namespace rift
{
public:
Eval();
~Eval();
~Eval() = default;
friend class EvalVisitor;

/// @brief Evaluates the given *expression*
std::string evaluate(const rift::ast::Expr::Expr<Token>& expr);
std::string evaluate(const rift::ast::Expr& expr);
/// @brief Evaluates the given *statement*
void evaluate(const rift::ast::Expr::Expr<void>& expr);
void evaluate(const rift::ast::Stmt& expr);

private:
EvalVisitor *visitor;
std::unique_ptr<Visitor> visitor;
};


class EvalVisitor : public rift::ast::Expr::Visitor<Token>, public rift::ast::StmtVisitor
{
using BinaryExpr = rift::ast::Expr::Binary<Token>;
using GroupingExpr = rift::ast::Expr::Grouping<Token>;
using LiteralExpr = rift::ast::Expr::Literal<Token>;
using UnaryExpr = rift::ast::Expr::Unary<Token>;
public:
EvalVisitor(Eval &eval);
virtual ~EvalVisitor();
Eval* eval;
// class EvalVisitor : public rift::ast::Visitor
// {
// public:
// EvalVisitor(Eval &eval);
// virtual ~EvalVisitor();
// Eval* eval;

/* expr.hh */
Token visit_binary(const BinaryExpr& expr) const override;
Token visit_grouping(const GroupingExpr& expr) const override;
Token visit_literal(const LiteralExpr& expr) const override;
Token visit_unary(const UnaryExpr& expr) const override;
// /* expr.hh */
// Token visit_binary(const Binary& expr) const override;
// Token visit_grouping(const Grouping& expr) const override;
// Token visit_literal(const Literal& expr) const override;
// Token visit_unary(const Unary& expr) const override;

/* stmt.hh */
void visit_print_stmt(const StmtPrint& expr) const override;
void visit_var_stmt(const StmtVar& expr) const override;
};
// /* stmt.hh */
// void visit_expr_stmt(const StmtExpr& expr) const override;
// void visit_print_stmt(const StmtPrint& expr) const override;
// void visit_var_stmt(const StmtVar& expr) const override;
// };

/// @class EvaluatorException
/// @brief The base exception for the evaluator
Expand Down
177 changes: 81 additions & 96 deletions include/ast/expr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,111 +21,96 @@
#include <iostream>
#include <stdlib.h>
#include <memory>
#include <ast/grmr.hh>

using namespace rift::scanner;

namespace rift
{
namespace ast
{
namespace Expr
/// @note forward declrations
__DEFAULT_FORWARD_NONE_VA(
Binary,
Grouping,
Literal,
Unary
);

/// @class Expr
/// @brief Base class for all expressions
/// @details Types of expression include
/// - Binary: An expression with two operands and an operator
/// - Example: 1 + 2
/// - Grouping: An expression with a single subexpression
/// - Example: (1 + 2)
/// - Literal: An expression with a single value
/// - Example: 1
/// - Unary: An expression with a single operator and a single operand
/// - Example: -1
class Expr : public Accept<Token>
{
/// @note forward declrations
__DEFAULT_FORWARD_VA(
Visitor,
Binary,
Grouping,
Literal,
Unary
);

/// @class Expr
/// @brief Base class for all expressions
/// @details Types of expression include
/// - Binary: An expression with two operands and an operator
/// - Example: 1 + 2
/// - Grouping: An expression with a single subexpression
/// - Example: (1 + 2)
/// - Literal: An expression with a single value
/// - Example: 1
/// - Unary: An expression with a single operator and a single operand
/// - Example: -1
template <typename T = void>
class Expr
{
public:
virtual T accept(const Visitor<T>& visitor) const = 0;
virtual ~Expr() = default;
};


/// @class Visitor
/// @brief This class is used to visit each type of expression
template <typename T>
class Visitor
{
public:
virtual T visit_binary(const Binary<T>& expr) const = 0;
virtual T visit_grouping(const Grouping<T>& expr) const = 0;
virtual T visit_literal(const Literal<T>& expr) const = 0;
virtual T visit_unary(const Unary<T>& expr) const = 0;
};

#pragma mark - Concrete Expressions

/// @class Binary
/// @param left The left operand
/// @param op The operator
/// @param right The right operand
template <typename T = void>
class Binary : public Expr<T>
{
public:
Binary(std::unique_ptr<Expr<T>> left, Token op, std::unique_ptr<Expr<T>> right): op(op), left(std::move(left)), right(std::move(right)) {};
Token op;
std::unique_ptr<Expr<T>> left;
std::unique_ptr<Expr<T>> right;

inline T accept(const Visitor<T>& visitor) const override { return visitor.visit_binary(*this); }
};

/// @class Grouping
/// @param expr The subexpression
template <typename T = void>
class Grouping : public Expr<T>
{
public:
Grouping(std::unique_ptr<Expr<T>> expr): expr(std::move(expr)) {};
std::unique_ptr<Expr<T>> expr;

inline T accept(const Visitor<T>& visitor) const override {return visitor.visit_grouping(*this);}
};

/// @class Literal
/// @param value The value of the literal
template <typename T = void>
class Literal: public Expr<T>
{
public:
Literal(Token value): value(value) {};
Token value;

inline T accept(const Visitor<T> &visitor) const override {return visitor.visit_literal(*this);}
};
public:
virtual Token accept(const Visitor& visitor) const override = 0;
virtual string accept_printer(const Visitor& visitor) const override = 0;
virtual ~Expr() = default;
};

#pragma mark - Concrete Expressions

/// @class Binary
/// @param left The left operand
/// @param op The operator
/// @param right The right operand
// template <typename T = Token, typename V = Token>
class Binary : public Expr
{
public:
Binary(std::unique_ptr<Expr> left, Token op, std::unique_ptr<Expr> right): op(op), left(std::move(left)), right(std::move(right)) {};
Token op;
std::unique_ptr<Expr> left;
std::unique_ptr<Expr> right;

inline Token accept(const Visitor& visitor) const override { return visitor.visit_binary(*this); }
inline string accept_printer(const Visitor& visitor) const override { return visitor.print_binary(*this); }
};

/// @class Grouping
/// @param expr The subexpression
class Grouping : public Expr
{
public:
Grouping(std::unique_ptr<Expr> expr): expr(std::move(expr)) {};
std::unique_ptr<Expr> expr;

/// @class Unary
/// @param op The operator
/// @param expr The operand
template <typename T = void>
class Unary : public Expr<T>
{
public:
Unary(Token op, std::unique_ptr<Expr<T>> expr): op(op), expr(std::move(expr)) {};
Token op;
std::unique_ptr<Expr<T>> expr;
inline Token accept(const Visitor& visitor) const override {return visitor.visit_grouping(*this);}
inline string accept_printer(const Visitor& visitor) const override {return visitor.print_grouping(*this);}
};

inline T accept(const Visitor<T>& visitor) const override {return visitor.visit_unary(*this);}
};
}
/// @class Literal
/// @param value The value of the literal
class Literal: public Expr
{
public:
Literal(Token value): value(value) {};
Token value;

inline Token accept(const Visitor &visitor) const override {return visitor.visit_literal(*this);}
inline string accept_printer(const Visitor& visitor) const override {return visitor.print_literal(*this);}
};

/// @class Unary
/// @param op The operator
/// @param expr The operand
class Unary : public Expr
{
public:
Unary(Token op, std::unique_ptr<Expr> expr): op(op), expr(std::move(expr)) {};
Token op;
std::unique_ptr<Expr> expr;

inline Token accept(const Visitor& visitor) const override {return visitor.visit_unary(*this);}
inline string accept_printer(const Visitor& visitor) const override {return visitor.print_unary(*this);}
};
}
};
Loading

0 comments on commit dd1a1f6

Please sign in to comment.