Compare commits

..

No commits in common. "ae87a3b4a3b1fa4d473774756eeafbd56e1dd717" and "2f0c4be7bfdcadcb46ae347f91d8083628fbcb1a" have entirely different histories.

4 changed files with 39 additions and 63 deletions

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
cclox cclox
clox clox
*.dSYM

View File

@ -2,7 +2,6 @@
#include "error_handler.hh" #include "error_handler.hh"
#include "parser.hh" #include "parser.hh"
#include <iostream> #include <iostream>
#include <memory>
#include <sysexits.h> #include <sysexits.h>
ErrorHandler error_handler{}; ErrorHandler error_handler{};
@ -17,15 +16,11 @@ int main(int argc, char *argv[]) {
// //
// run_interpreter(argc, argv); // run_interpreter(argc, argv);
ExprPtr literal = std::make_shared<Expr>(ExprType::LITERAL, Literal{123}); Unary left {Token{TokenType::MINUS, "-", Object{}, 1}, Literal{123}};
ExprPtr left = std::make_shared<Expr>(ExprType::UNARY, Unary{Token{TokenType::MINUS, "-", Object{}, 1}, literal});
Token op{TokenType::STAR, "*", Object{}, 1}; Token op{TokenType::STAR, "*", Object{}, 1};
Grouping right{Literal{45.67}};
ExprPtr gliteral = std::make_shared<Expr>(ExprType::LITERAL, Literal{45.67}); Binary expr {left, op, right};
ExprPtr right = std::make_shared<Expr>(ExprType::GROUPING, gliteral);
Expr expr = {ExprType::BINARY, Binary{left, op, right}};
AstPrinter printer{}; AstPrinter printer{};
std::cout << printer.print(expr) << '\n'; std::cout << printer.print(expr) << '\n';

View File

@ -1,24 +1,20 @@
#include "expr.hh" #include "expr.hh"
#include "../tokenizer.hh" #include "../tokenizer.hh"
#include <cassert>
#include <string> #include <string>
#include <type_traits>
#include <variant>
#include <vector> #include <vector>
#include <sstream> #include <sstream>
std::string AstPrinter::print(const Expr &expr) { std::string AstPrinter::print(const Expression &expr) {
switch (expr.type) { return std::visit([this](auto &&node) -> std::string {
case ExprType::BINARY: { using T = std::decay_t<decltype(node)>;
Binary node = std::get<Binary>(expr.value);
assert(node.left != NULL && node.right != NULL); if constexpr(std::is_same_v<T, Binary>) {
return parenthesize(node.op.lexeme, {*node.left, *node.right}); return parenthesize(node.op.lexeme, {node.left, node.right});
} } else if constexpr(std::is_same_v<T, Grouping>) {
case ExprType::GROUPING: { return parenthesize("group", {node.expression});
Grouping node = std::get<Grouping>(expr.value); } else if constexpr(std::is_same_v<T, Literal>) {
assert(node.expression != NULL);
return parenthesize("group", {*node.expression});
}
case ExprType::LITERAL: {
Literal node = std::get<Literal>(expr.value);
switch (node.value.type) { switch (node.value.type) {
case ObjectType::NIL: case ObjectType::NIL:
return "nil"; return "nil";
@ -32,20 +28,22 @@ std::string AstPrinter::print(const Expr &expr) {
return std::to_string(value); return std::to_string(value);
} }
} }
} else if constexpr(std::is_same_v<T, Unary>) {
return parenthesize(node.op.lexeme, {node.right});
} }
case ExprType::UNARY: {
Unary node = std::get<Unary>(expr.value); return "";
assert(node.right != NULL); }, expr);
return parenthesize(node.op.lexeme, {*node.right});
}
}
} }
std::string AstPrinter::parenthesize(const std::string &name, std::vector<Expr> exprs) { std::string AstPrinter::parenthesize(const std::string &name, std::vector<Expression> exprs) {
std::stringstream ss{}; std::stringstream ss{};
ss << '(' << name; ss << '(' << name;
for (auto &expr : exprs) { for (auto &expr : exprs) {
ss << ' ' << print(expr); std::string value = std::visit([this](auto &&node) -> std::string {
return print(node);
}, expr);
ss << ' ' << value;
} }
ss << ')'; ss << ')';

View File

@ -2,61 +2,45 @@
#define EXPR_HH #define EXPR_HH
#include "../tokenizer.hh" #include "../tokenizer.hh"
#include <memory>
#include <string> #include <string>
#include <variant> #include <variant>
struct Expr; struct Expr {};
using ExprPtr = std::shared_ptr<Expr>; struct Binary : public Expr {
Binary(Expr left, Token op, Expr right) : left{left}, op{op}, right{right} {};
struct Binary { Expr left;
Binary(ExprPtr left, Token op, ExprPtr right) : left{left}, op{op}, right{right} {};
ExprPtr left;
Token op; Token op;
ExprPtr right; Expr right;
}; };
struct Grouping { struct Grouping : public Expr {
Grouping(ExprPtr expr) : expression{expr} {}; Grouping(Expr expr) : expression{expr} {};
ExprPtr expression; Expr expression;
}; };
struct Literal { struct Literal : public Expr {
Literal(Object value) : value{value} {}; Literal(Object value) : value{value} {};
Object value; Object value;
}; };
struct Unary { struct Unary : public Expr {
Unary(Token op, ExprPtr right) : op{op}, right{right} {}; Unary(Token op, Expr right) : op{op}, right{right} {};
Token op; Token op;
ExprPtr right; Expr right;
}; };
enum class ExprType { using Expression = std::variant<Expr, Binary, Grouping, Literal, Unary>;
BINARY,
GROUPING,
LITERAL,
UNARY,
};
using Expression = std::variant<Binary, Grouping, Literal, Unary>;
struct Expr {
Expr(ExprType type, Expression value) : type{type}, value{value} {}
ExprType type;
Expression value;
};
class AstPrinter { class AstPrinter {
public: public:
std::string print(const Expr &expr); std::string print(const Expression &expr);
private: private:
std::string parenthesize(const std::string &name, std::vector<Expr> exprs); std::string parenthesize(const std::string &name, std::vector<Expression> exprs);
}; };
#endif #endif