-
Notifications
You must be signed in to change notification settings - Fork 0
/
AstPrinter.h
66 lines (55 loc) · 1.73 KB
/
AstPrinter.h
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
#pragma once
#include <iostream>
#include <string>
#include "Expr.h"
namespace AstPrinter
{
std::string visit(Expr *expr);
std::string parenthesize(std::string name, Expr *expr1 = 0, Expr *expr2 = 0, Expr *expr3 = 0, Expr *expr4 = 0)
{
std::stringstream ss;
ss << "(" << name;
if (expr1) ss << " " << visit(expr1);
if (expr2) ss << " " << visit(expr2);
if (expr3) ss << " " << visit(expr3);
if (expr4) ss << " " << visit(expr4);
ss << ")";
return ss.str();
}
std::string visitTernary(Ternary *expr)
{
return parenthesize(expr->oper1->lexeme + expr->oper2->lexeme, expr->first, expr->second, expr->third);
}
std::string visitBinary(Binary *expr)
{
return parenthesize(expr->oper->lexeme, expr->left, expr->right);
}
std::string visitGrouping(Grouping *expr)
{
return parenthesize("group", expr->expression);
}
std::string visitLiteral(Literal *expr)
{
return expr->value->str();
}
std::string visitUnary(Unary *expr)
{
return parenthesize(expr->oper->lexeme, expr->right);
}
std::string visit(Expr *expr)
{
switch (expr->type)
{
case ExprType_Ternary: return visitTernary((Ternary *)expr);
case ExprType_Binary: return visitBinary((Binary *)expr);
case ExprType_Grouping: return visitGrouping((Grouping *)expr);
case ExprType_Literal: return visitLiteral((Literal *)expr);
case ExprType_Unary: return visitUnary((Unary *)expr);
}
return "<<<INVALID EXPR TYPE>>>";
}
void print(Expr *expr)
{
std::cout << "::AstPrinter:: " << visit(expr) << std::endl;
}
};