-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_ast_code.rb
More file actions
155 lines (119 loc) · 2.65 KB
/
gen_ast_code.rb
File metadata and controls
155 lines (119 loc) · 2.65 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! /usr/bin/env ruby
# This script reads ast.h and generates:
#
# ast_visitor.h
# ast_visitor.cpp
# ast.cpp
def visit_function_name(tag)
raise "huh?" if !tag.start_with?('AST_')
tag = tag[4..-1]
return "visit_#{tag.downcase}"
end
def gen_ast_visitor_h(outf, ast_tags)
outf.print <<"EOF1"
#ifndef AST_VISITOR_H
#define AST_VISITOR_H
class Node;
class ASTVisitor {
public:
ASTVisitor();
virtual ~ASTVisitor();
virtual void visit(Node *n);
EOF1
ast_tags.each do |tag|
outf.puts " virtual void #{visit_function_name(tag)}(Node *n);"
end
outf.print <<"EOF2"
virtual void visit_children(Node *n);
virtual void visit_token(Node *n);
};
#endif // AST_VISITOR_H
EOF2
end
def gen_ast_visitor_cpp(outf, ast_tags)
outf.print <<"EOF3"
#include "node.h"
#include "exceptions.h"
#include "ast.h"
#include "ast_visitor.h"
ASTVisitor::ASTVisitor() {
}
ASTVisitor::~ASTVisitor() {
}
EOF3
ast_tags.each do |tag|
outf.print <<"EOF4"
void ASTVisitor::#{visit_function_name(tag)}(Node *n) {
visit_children(n);
}
EOF4
end
outf.print <<"EOF5"
void ASTVisitor::visit(Node *n) {
// assume that any node with a tag value less than 1000
// is a token
if (n->get_tag() < 1000) {
visit_token(n);
return;
}
switch (n->get_tag()) {
EOF5
ast_tags.each do |tag|
outf.puts " case #{tag}:"
outf.puts " #{visit_function_name(tag)}(n); break;"
end
outf.print <<"EOF6"
default:
RuntimeError::raise("Unknown AST node tag %d", n->get_tag());
}
}
void ASTVisitor::visit_children(Node *n) {
for (auto i = n->cbegin(); i != n->cend(); ++i) {
visit(*i);
}
}
void ASTVisitor::visit_token(Node *n) {
// default implementation does nothing
}
EOF6
end
def gen_ast_cpp(outf, ast_tags)
outf.print <<"EOF7"
#include <cassert>
#include "node.h"
#include "ast.h"
ASTTreePrint::ASTTreePrint() {
}
ASTTreePrint::~ASTTreePrint() {
}
std::string ASTTreePrint::node_tag_to_string(int tag) const {
switch (tag) {
EOF7
ast_tags.each do |tag|
outf.puts " case #{tag}: return \"#{tag}\";"
end
outf.print <<"EOF8"
// If the tag doesn't match any of the AST node tags,
// assume it's a parse tree node
default: return ParseTreePrint::node_tag_to_string(tag);
}
}
EOF8
end
ast_tags = []
STDIN.each_line do |line|
line.rstrip!
if m = /^\s+(AST_\S+)\s*(=\s*\d+\s*)?,(\s*\/\/.*)?$/.match(line)
ast_tags.push(m[1])
end
end
#puts "found #{ast_tags.length} AST tags"
File.open('ast_visitor.h', 'w') do |outf|
gen_ast_visitor_h(outf, ast_tags);
end
File.open('ast_visitor.cpp', 'w') do |outf|
gen_ast_visitor_cpp(outf, ast_tags)
end
File.open('ast.cpp', 'w') do |outf|
gen_ast_cpp(outf, ast_tags)
end