-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_ast_classes.rb
executable file
·167 lines (135 loc) · 3.9 KB
/
generate_ast_classes.rb
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
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env ruby
INDENT = ' ' * 4
VISIBILITY = 'internal'
class Type
attr_reader :class_name
attr_reader :fields
def initialize(class_name, *fields)
@class_name = class_name
@fields = fields.flatten
end
end
class Field
attr_reader :type
attr_reader :name
def initialize(type, name)
@type = type
@name = name
end
end
def define_visitor(base_name, types)
stream = StringIO.new
stream.puts "#{INDENT * 2}#{VISIBILITY} interface Visitor<R>"
stream.puts "#{INDENT * 2}{"
types.each { |t|
stream.puts "#{INDENT * 3}R Visit#{t.class_name}#{base_name}(#{t.class_name} #{base_name.downcase});"
}
stream.puts "#{INDENT * 2}}"
stream.string
end
def define_type(base_name, class_name, fields)
stream = StringIO.new
stream.puts "#{INDENT * 2}#{VISIBILITY} class #{class_name} : #{base_name}"
stream.puts "#{INDENT * 2}{"
# Fields.
fields.each { |f|
stream.puts("#{INDENT * 3}#{VISIBILITY} readonly #{f.type} #{f.name};");
}
stream.puts
# Constructor.
constructor_fields = fields.map { |f|
"#{f.type} #{f.name}"
}.join(', ')
stream.puts "#{INDENT * 3}#{VISIBILITY} #{class_name}(#{constructor_fields}) {"
# Store parameters in fields.
fields.each { |f|
stream.puts "#{INDENT * 4}this.#{f.name} = #{f.name};"
}
stream.puts "#{INDENT * 3}}";
stream.puts
# Visitor pattern implementation
stream.puts "#{INDENT * 3}#{VISIBILITY} override R Accept<R>(Visitor<R> visitor)"
stream.puts "#{INDENT * 3}{"
stream.puts "#{INDENT * 4}return visitor.Visit#{class_name}#{base_name}(this);"
stream.puts "#{INDENT * 3}}"
stream.puts "#{INDENT * 2}}"
stream
end
def define_ast(output_dir, base_name, types)
visitor_content = define_visitor(base_name, types)
inner_classes = types.map { |type|
define_type(base_name, type.class_name, type.fields)
}
inner_classes_content = inner_classes
.map(&:string)
.join("\n")
.rstrip
path = File.join(output_dir, base_name + ".cs");
File.write(path, <<~EOF)
using System.Collections.Generic;
namespace CSLox
{
#{INDENT}#{VISIBILITY} abstract class #{base_name}
#{INDENT}{
#{visitor_content}
#{inner_classes_content}
#{INDENT * 2}#{VISIBILITY} abstract R Accept<R>(Visitor<R> visitor);
#{INDENT}}
}
EOF
end
if ARGV.count != 1
puts "Usage: generate_ast <output directory>"
exit 1
end
output_dir = ARGV.pop
#
# Certain names like _params and _operator are prefixed, since they
# clash with C# reserved words.
#
define_ast(output_dir, "Expr", [
Type.new('Assign', Field.new('Token', 'name'), Field.new('Expr', 'value')),
Type.new('Binary', [
Field.new('Expr', 'left'),
Field.new('Token', '_operator'),
Field.new('Expr', 'right')
]),
Type.new('Call', [
Field.new('Expr', 'callee'),
Field.new('Token', 'paren'),
Field.new('List<Expr>', 'arguments')
]),
Type.new('Grouping', Field.new('Expr', 'expression')),
Type.new('Literal', Field.new('object', 'value')),
Type.new('Logical', [
Field.new('Expr', 'left'),
Field.new('Token', '_operator'),
Field.new('Expr', 'right')
]),
Type.new('Unary', [
Field.new('Token', '_operator'),
Field.new('Expr', 'right')
]),
Type.new('Variable', Field.new('Token', 'name'))
])
define_ast(output_dir, "Stmt", [
Type.new('Block', Field.new('List<Stmt>', 'statements')),
Type.new('Expression', Field.new('Expr', 'expression')),
Type.new('Function', [
Field.new('Token', 'name'),
Field.new('List<Token>', '_params'),
Field.new('List<Stmt>', 'body')
]),
Type.new('If', [
Field.new('Expr', 'condition'),
Field.new('Stmt', 'thenBranch'),
Field.new('Stmt', 'elseBranch')
]),
Type.new('Print', Field.new('Expr', 'expression')),
Type.new('Return', Field.new('Token', 'keyword'), Field.new('Expr', 'value')),
Type.new('Var', [
Field.new('Token', 'name'),
Field.new('Expr', 'initializer')
]),
Type.new('While', Field.new('Expr', 'condition'), Field.new('Stmt', 'body'))
])