-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstPrinter.cpp
84 lines (69 loc) · 1.84 KB
/
AstPrinter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2020 <Copyright hulin>
#include <iostream>
#include <utility>
#include <string>
#include <vector>
#include <memory>
#include "./Expr.hpp"
#include "./AstPrinter.hpp"
#include "./Token.hpp"
using std::string;
using std::shared_ptr;
using std::cout;
using std::endl;
string AstPrinter::print(shared_ptr<Expr<string>> expr) {
return expr->accept(shared_from_this());
}
string AstPrinter::visitLiteralExpr(const Literal<string>& expr) {
if (expr.value == "") return "nil";
return expr.value;
}
string AstPrinter::visitAssignExpr(const Assign<string>& expr) {
return "(" +
expr.name.lexeme +
" " +
expr.value->accept(shared_from_this()) +
" )";
}
string AstPrinter::visitBinaryExpr(const Binary<string>& expr) {
return "(" +
expr.operation.lexeme +
" " +
expr.left->accept(shared_from_this()) +
" " +
expr.right->accept(shared_from_this()) +
")";
}
string AstPrinter::visitGroupingExpr(const Grouping<string>& expr) {
return "(group " +
expr.expression->accept(shared_from_this()) +
")";
}
string AstPrinter::visitUnaryExpr(const Unary<string>& expr) {
return "("+
expr.operation.lexeme +
" " +
expr.right->accept(shared_from_this()) +
")";
}
// string AstPrinter::visitCallExpr(const Call& expr) {
// return "";
// }
// string AstPrinter::visitGetExpr(const Get& expr) {
// return "";
// }
// string AstPrinter::visitLogicalExpr(const Logical& expr) {
// return "";
// }
// string AstPrinter::visitSetExpr(const Set& expr) {
// return "";
// }
// string AstPrinter::visitSuperExpr(const Super& expr) {
// return "";
// }
// string AstPrinter::visitThisExpr(const This& expr) {
// return "";
// }
// string AstPrinter::visitVariableExpr(const Variable& expr) {
// return "";
// }