Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src / ASTPrinter.cpp
blob: 571726416597efb9e572bc49c923765460c2c5ea (plain) (blame)
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
85
#include "ASTPrinter.h"
#include "Expression.h"

ASTPrinter::ASTPrinter(std::ostream& os)
 : os(os)
{}

void Print(std::ostream& os, ASTPrinter &printer, const char* name, const ExpressionPtr& expr)
{
	os << name;
	expr->Accept(printer);
}

void Print(std::ostream& os, ASTPrinter &printer, const char* name, const IPLVector<ExpressionPtr>& list)
{
	os << name << "[ \n";
	for (auto& expr : list)
	{
		expr->Accept(printer);
		os << ",";
	}
	os << name << "] \n";
}

void Print(std::ostream& os, ASTPrinter &printer, const char* name, const IPLVector<IPLString>& list)
{
	os << name << "[ \n";
	for (auto& name : list)
	{
		os << name << ",";
	}
	os << name << "] \n";
}


template<typename T>
void Print(std::ostream& os, ASTPrinter &printer, const char* name, T member)
{
	os << name << " " << member << "\n";
}

IPLString AsString(LiteralType type)
{
	switch (type)
	{
	case LiteralType::Number:
		return "Number";
	case LiteralType::String:
		return "String";
	case LiteralType::Boolean:
		return "Boolean";
	case LiteralType::Null:
		return "Null";
	case LiteralType::Undefined:
		return "Undefined";
	default:
		break;
	}
	return "Undefined";
}

void ASTPrinter::Visit(LiteralExpression* e)
{
	os << "{\n" << "LiteralExpression" << '\n';
	Print(os, *this, "BooleanValue", e->GetBooleanValue());
	Print(os, *this, "LiteralType", AsString(e->GetLiteralType()));
	Print(os, *this, "NumValue", e->GetNumValue());
	Print(os, *this, "StringValue", e->GetStringValue());
	os << "}\n";
}



#define VISIT_MEMBER(type, name, def) Print(os, *this, #name, e->Get##name());

#define GENERATE_AST_PRINTER_FUNCTION(ClassName, Base, MEMBERS_ITERATOR)\
	void ASTPrinter::Visit(ClassName* e)                        \
	{                                                           \
		os << "{\n" << #ClassName << '\n';                      \
		MEMBERS_ITERATOR(VISIT_MEMBER);                         \
		os << "}\n";                                            \
	}


EXPRESSION_DEFINITION_ITERATOR(GENERATE_AST_PRINTER_FUNCTION);