Skip to content

Commit

Permalink
Advancement
Browse files Browse the repository at this point in the history
  • Loading branch information
amanuel2 committed Aug 4, 2024
1 parent 8dd7287 commit b6cab5f
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 114 deletions.
9 changes: 6 additions & 3 deletions include/ast/decl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#pragma once

#include <ast/expr.hh>
#include <ast/stmt.hh>
#include <ast/grmr.hh>
#include <ast/env.hh>

Expand All @@ -34,9 +35,11 @@ namespace rift
class DeclVisitor
{
public:
virtual T visit_decl_stmt(const DeclStmt<T>& decl) const;
virtual T visit_decl_var(const DeclVar<T>& decl) const;
virtual T visit_decl_func(const DeclFunc<T>& decl) const;
DeclVisitor() = default;
virtual ~DeclVisitor() = default;
virtual T visit_decl_stmt(const DeclStmt<T>& decl) const = 0;
virtual T visit_decl_var(const DeclVar<T>& decl) const = 0;
virtual T visit_decl_func(const DeclFunc<T>& decl) const = 0;
};

/// @class Decl
Expand Down
14 changes: 7 additions & 7 deletions include/ast/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace rift
{
namespace ast
{
class Eval : public ExprVisitor<Token>, StmtVisitor<Token>,
class Eval : public ExprVisitor<Token>, StmtVisitor<void>,
DeclVisitor<Token>, ProgramVisitor<Tokens>
{
public:
Expand All @@ -48,12 +48,12 @@ namespace rift
Token visit_call(const Call<Token>& expr) const override;

// statements
Token visit_expr_stmt(const StmtExpr<Token>& stmt) const override;
Token visit_print_stmt(const StmtPrint<Token>& stmt) const override;
Token visit_if_stmt(const StmtIf<Token>& stmt) const override;
Token visit_return_stmt(const StmtReturn<Token>& stmt) const override;
Token visit_block_stmt(const Block<Token>& block) const override;
Token visit_for_stmt(const For<Token>& decl) const override;
void visit_expr_stmt(const StmtExpr<void>& stmt) const override;
void visit_print_stmt(const StmtPrint<void>& stmt) const override;
void visit_if_stmt(const StmtIf<void>& stmt) const override;
void visit_return_stmt(const StmtReturn<void>& stmt) const override;
void visit_block_stmt(const Block<void>& block) const override;
void visit_for_stmt(const For<void>& decl) const override;

// declarations
Token visit_decl_stmt(const DeclStmt<Token>& decl) const override;
Expand Down
18 changes: 10 additions & 8 deletions include/ast/expr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,24 @@ namespace rift
class ExprVisitor
{
public:
ExprVisitor() = default;
virtual ~ExprVisitor() = default;
/// @example x = 1 + 2;
virtual T visit_assign(const Assign<T>& expr) const;
virtual T visit_assign(const Assign<T>& expr) const = 0;
/// @example 1 + 2
virtual T visit_binary(const Binary<T>& expr) const;
virtual T visit_binary(const Binary<T>& expr) const = 0;
/// @example (1 + 2)
virtual T visit_grouping(const Grouping<T>& expr) const;
virtual T visit_grouping(const Grouping<T>& expr) const = 0;
/// @example 1
virtual T visit_literal(const Literal<T>& expr) const;
virtual T visit_literal(const Literal<T>& expr) const = 0;
/// @example x
virtual T visit_var_expr(const VarExpr<T>& expr) const;
virtual T visit_var_expr(const VarExpr<T>& expr) const = 0;
/// @example -1
virtual T visit_unary(const Unary<T>& expr) const;
virtual T visit_unary(const Unary<T>& expr) const = 0;
/// @example (true) ? 1 : 2
virtual T visit_ternary(const Ternary<T>& expr) const;
virtual T visit_ternary(const Ternary<T>& expr) const = 0;
/// @example x = foo(1, 2)
virtual T visit_call(const Call<T>& expr) const;
virtual T visit_call(const Call<T>& expr) const = 0;
};

/// @class Expr
Expand Down
62 changes: 31 additions & 31 deletions include/ast/grmr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,41 +53,41 @@ namespace rift
/// call → primary ( "(" arguments? ")" )* ";"
/// primary → NUMBER | STRING | "true" | "false" | "nil" | "(" expression ")" ";"

__DEFAULT_FORWARD_VA(
Assign,
Binary,
Grouping,
Literal,
VarExpr,
Unary
)
// __DEFAULT_FORWARD_VA(
// Assign,
// Binary,
// Grouping,
// Literal,
// VarExpr,
// Unary
// )

__DEFAULT_FORWARD_VA(
Printer,
Ternary,
For,
Call
)
// __DEFAULT_FORWARD_VA(
// Printer,
// Ternary,
// For,
// Call
// )

__DEFAULT_FORWARD_VA(
Stmt,
StmtPrint,
StmtExpr,
StmtIf,
StmtReturn
)
// __DEFAULT_FORWARD_VA(
// Stmt,
// StmtPrint,
// StmtExpr,
// StmtIf,
// StmtReturn
// )

__DEFAULT_FORWARD_VA(
Decl,
DeclStmt,
DeclVar,
DeclFunc
)
// __DEFAULT_FORWARD_VA(
// Decl,
// DeclStmt,
// DeclVar,
// DeclFunc
// )

__DEFAULT_FORWARD_VA(
Program,
Block
)
// __DEFAULT_FORWARD_VA(
// Program,
// Block
// )

/// @class Visitor
/// @brief implementation of the statements and expressions
Expand Down
8 changes: 6 additions & 2 deletions include/ast/prgm.hh
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ namespace rift
template <typename T>
class ProgramVisitor
{
virtual T visit_program(const Program<T>& prgm) const;
public:
ProgramVisitor() = default;
virtual ~ProgramVisitor() = default;
virtual T visit_program(const Program<T>& prgm) const = 0;
};

template <typename T>
class Program
{
public:
using vec_t = std::vector<std::unique_ptr<Decl<Token>>>;
Program(vec_t decls) : decls(std::move(decls)) {}
// Program(vec_t decls) : decls(std::move(decls)) {}
Program(vec_t&& decls): decls(std::move(decls)) {}
virtual ~Program() = default;
friend class Eval;

Expand Down
2 changes: 1 addition & 1 deletion include/ast/printer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace rift
{
public:
using vec = std::vector<Expr<string>*>;
Printer();
Printer() = default;
~Printer() = default;

/// Prints the given expression string.
Expand Down
17 changes: 10 additions & 7 deletions include/ast/stmt.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <ast/expr.hh>
#include <ast/grmr.hh>
#include <ast/parser.hh>
#include <utils/macros.hh>

namespace rift
Expand All @@ -37,24 +36,28 @@ namespace rift
For
);

__DEFAULT_FORWARD_VA(Decl);

/// @class Visitor
/// @brief Visitor pattern for expressions
template <typename T>
class StmtVisitor
{
public:
StmtVisitor() = default;
virtual ~StmtVisitor() = default;
/// @example [start, end] = [end, start];
virtual T visit_expr_stmt(const StmtExpr<T>& stmt) const;
virtual T visit_expr_stmt(const StmtExpr<T>& stmt) const = 0;
/// @example print(x);
virtual T visit_print_stmt(const StmtPrint<T>& stmt) const;
virtual T visit_print_stmt(const StmtPrint<T>& stmt) const = 0;
/// @example if (cond) {stmt}
virtual T visit_if_stmt(const StmtIf<T>& stmt) const;
virtual T visit_if_stmt(const StmtIf<T>& stmt) const = 0;
/// @example return x;
virtual T visit_return_stmt(const StmtReturn<T>& stmt) const;
virtual T visit_return_stmt(const StmtReturn<T>& stmt) const = 0;
/// @example { x = 3; }
virtual T visit_block_stmt(const Block<T>& block) const;
virtual T visit_block_stmt(const Block<T>& block) const = 0;
/// @example for i in arr {}
virtual T visit_for_stmt(const For<T>& decl) const;
virtual T visit_for_stmt(const For<T>& decl) const = 0;
};

/// @class Stmt
Expand Down
3 changes: 2 additions & 1 deletion include/scanner/tokens.hh
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace rift
this->l_type = other.l_type;
}

~Token() { }
virtual ~Token() = default;

/// @brief Converts a TokenType to a string
static std::string convertTypeString(TokenType type);
Expand All @@ -135,6 +135,7 @@ namespace rift
return os;
}
bool operator==(const Token &token) const;
bool operator!=(const Token &token) const;

virtual Token& operator=(const Token& other) = default;

Expand Down
2 changes: 1 addition & 1 deletion include/utils/macros.hh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
class class_name; \

#define __DEFAULT_FORWARD_MULTI(class_name) \
template <typename T, typename V> \
template <typename T, typename E> \
class class_name; \

#define __DEFAULT_FORWARD_EACH(...) \
Expand Down
27 changes: 12 additions & 15 deletions lib/ast/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace rift
return curr_env->at(depth)->getEnv<Token>(key);
} else {
// return curr_env->getEnv<Token>(key);
Environment::getInstance(false).getEnv<Token>(key);
return Environment::getInstance(false).getEnv<Token>(key);
}
}

Expand Down Expand Up @@ -329,22 +329,22 @@ namespace rift

#pragma mark - Stmt Visitors

Token Eval::visit_expr_stmt(const StmtExpr<Token>& stmt) const
void Eval::visit_expr_stmt(const StmtExpr<void>& stmt) const
{
return stmt.expr->accept(*this);
stmt.expr->accept(*this);
}

Token Eval::visit_print_stmt(const StmtPrint<Token>& stmt) const
void Eval::visit_print_stmt(const StmtPrint<void>& stmt) const
{
Token val = stmt.expr->accept(*this);
std::string res = castAnyString(val);
if (res.at(0) == '"' && res.at(res.size()-1) == '"')
res = res.substr(1, res.size()-2);
std::cout << res << std::endl;
return val;
// return val;
}

Token Eval::visit_if_stmt(const StmtIf<Token>& stmt) const
void Eval::visit_if_stmt(const StmtIf<void>& stmt) const
{
auto if_stmt = stmt.if_stmt;
// if stmt
Expand All @@ -356,7 +356,6 @@ namespace rift
if (if_stmt->blk != nullptr) if_stmt->blk->accept(*this);
else if (if_stmt->stmt != nullptr) if_stmt->stmt->accept(*this);
else rift::error::runTimeError("If statement should have a statement or block");
return Token();
}

// elif stmt
Expand All @@ -367,7 +366,6 @@ namespace rift
if (elif_stmt->blk != nullptr) elif_stmt->blk->accept(*this);
else if (elif_stmt->stmt != nullptr) elif_stmt->stmt->accept(*this);
else rift::error::runTimeError("Elif statement should have a statement or block");
return Token();
}
}

Expand All @@ -378,18 +376,17 @@ namespace rift
else if (else_stmt->stmt != nullptr) else_stmt->stmt->accept(*this);
else rift::error::runTimeError("Else statement should have a statement or block");
}
return Token();
}

Token Eval::visit_return_stmt(const StmtReturn<Token>& stmt) const
void Eval::visit_return_stmt(const StmtReturn<void>& stmt) const
{
return_token = stmt.expr->accept(*this);
return return_token;
// return return_token;
}

#pragma mark - Program / Block Visitor

Token Eval::visit_block_stmt(const Block<Token>& block) const
void Eval::visit_block_stmt(const Block<void>& block) const
{
Tokens toks = {};

Expand All @@ -400,10 +397,10 @@ namespace rift
}
curr_env->removeChild(); // remove scope

return return_token;
// return return_token;
}

Token Eval::visit_for_stmt(const For<Token>& decl) const
void Eval::visit_for_stmt(const For<void>& decl) const
{
Tokens toks = {};
if (decl.decl != nullptr) decl.decl->accept(*this);
Expand All @@ -416,7 +413,7 @@ namespace rift

if (decl.stmt_r != nullptr) decl.stmt_r->accept(*this);
}
return return_token;
// return return_token;
}

#pragma mark - Decl Visitors
Expand Down
Loading

0 comments on commit b6cab5f

Please sign in to comment.