From a6e129eb12a69607889599cff175eb0bc4ef16bb Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sun, 22 Jun 2025 12:39:47 +0100 Subject: [PATCH] Update to reduce copying of expressions --- cclox_src/parser/expr.cc | 19 ++++++++++--------- cclox_src/parser/expr.hh | 14 +++++++------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/cclox_src/parser/expr.cc b/cclox_src/parser/expr.cc index 79c0e88..b961da3 100644 --- a/cclox_src/parser/expr.cc +++ b/cclox_src/parser/expr.cc @@ -7,6 +7,7 @@ #include #include +// Copy constructor Expr::Expr(const Expr &other) : type{other.type} { switch(type) { case ExprType::BINARY: { @@ -36,7 +37,7 @@ Expr::Expr(const Expr &other) : type{other.type} { Expr::Expr(const Expr &&other) : type{other.type}, value{std::move(other.value)} {} // Binary expressoin -Expr::Expr(Expr left, Token op, Expr right) +Expr::Expr(const Expr &left, Token op, const Expr &right) : type{ExprType::BINARY}, value{std::make_shared(left, op, right)} {} // Literal expression @@ -44,12 +45,12 @@ Expr::Expr(Object value) : type{ExprType::LITERAL}, value{std::make_shared(value)} {} // Unary expressoin -Expr::Expr(Token op, Expr right) +Expr::Expr(Token op, const Expr &right) : type{ExprType::UNARY}, value{std::make_shared(op, right)} {} // Group expression // Has to take an extra parameter to avoid conflicting with copy constructor -Expr::Expr(Expr expr, void *ptr) +Expr::Expr(const Expr &expr, void *ptr) : type{ExprType::GROUPING}, value{std::make_shared(expr)} { (void)ptr; } std::string AstPrinter::print(const Expr &expr) { @@ -57,12 +58,12 @@ std::string AstPrinter::print(const Expr &expr) { case ExprType::BINARY: { std::shared_ptr binary = std::get>(expr.value); assert(binary != nullptr); - return parenthesize(binary->op.lexeme, {binary->left, binary->right}); + return parenthesize(binary->op.lexeme, {&binary->left, &binary->right}); } case ExprType::GROUPING: { std::shared_ptr group = std::get>(expr.value); assert(group != nullptr); - return parenthesize("group", {group->expr}); + return parenthesize("group", {&group->expr}); } case ExprType::LITERAL: { std::shared_ptr literal = std::get>(expr.value); @@ -84,16 +85,16 @@ std::string AstPrinter::print(const Expr &expr) { case ExprType::UNARY: { std::shared_ptr unary = std::get>(expr.value); assert(unary != nullptr); - return parenthesize(unary->op.lexeme, {unary->right}); + return parenthesize(unary->op.lexeme, {&unary->right}); } } } -std::string AstPrinter::parenthesize(const std::string &name, std::vector exprs) { +std::string AstPrinter::parenthesize(const std::string &name, std::vector exprs) { std::stringstream ss{}; ss << '(' << name; - for (auto &expr : exprs) { - ss << ' ' << print(expr); + for (const Expr *expr : exprs) { + ss << ' ' << print(*expr); } ss << ')'; diff --git a/cclox_src/parser/expr.hh b/cclox_src/parser/expr.hh index cc6ac9d..869eaac 100644 --- a/cclox_src/parser/expr.hh +++ b/cclox_src/parser/expr.hh @@ -34,31 +34,31 @@ struct Expr { Expr(const Expr &&other); // Binary expressoin - Expr(Expr left, Token op, Expr right); + Expr(const Expr &left, Token op, const Expr &right); // Literal expression Expr(Object value); // Unary expressoin - Expr(Token op, Expr right); + Expr(Token op, const Expr &right); // Group expression // Has to take an extra parameter to avoid conflicting with copy constructor - Expr(Expr expr, void *ptr); + Expr(const Expr &expr, void *ptr); ExprType type; ExprVariant value; }; struct Binary { - Binary(Expr left, Token op, Expr right) : left{left}, op{op}, right{right} {} + Binary(const Expr &left, Token op, const Expr &right) : left{left}, op{op}, right{right} {} Expr left; Token op; Expr right; }; struct Grouping { - Grouping(Expr expr) : expr{expr} {} + Grouping(const Expr &expr) : expr{expr} {} Expr expr; }; @@ -68,7 +68,7 @@ struct Literal { }; struct Unary { - Unary(Token op, Expr right) : op{op}, right{right} {} + Unary(Token op, const Expr &right) : op{op}, right{right} {} Token op; Expr right; }; @@ -77,7 +77,7 @@ class AstPrinter { public: std::string print(const Expr &expr); private: - std::string parenthesize(const std::string &name, std::vector exprs); + std::string parenthesize(const std::string &name, std::vector exprs); }; #endif