forked from crystal-lang/crystal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacros.cr
187 lines (181 loc) · 4.98 KB
/
macros.cr
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Defines a **`Struct`** with the given name and properties.
#
# The generated struct has a constructor with the given properties
# in the same order as declared. The struct only provides getters,
# not setters, making it immutable by default.
#
# The properties can be type declarations or assignments.
#
# You can pass a block to this macro, that will be inserted inside
# the struct definition.
#
# ```
# record Point, x : Int32, y : Int32
#
# Point.new 1, 2 # => #<Point(@x=1, @y=2)>
# ```
#
# An example with the block version:
#
# ```
# record Person, first_name : String, last_name : String do
# def full_name
# "#{first_name} #{last_name}"
# end
# end
#
# person = Person.new "John", "Doe"
# person.full_name # => "John Doe"
# ```
#
# An example with type declarations and default values:
#
# ```
# record Point, x : Int32 = 0, y : Int32 = 0
#
# Point.new # => #<Point(@x=0, @y=0)>
# Point.new y: 2 # => #<Point(@x=0, @y=2)>
# ```
#
# An example with assignments (in this case the compiler must be able to
# infer the types from the default values):
#
# ```
# record Point, x = 0, y = 0
#
# Point.new # => #<Point(@x=0, @y=0)>
# Point.new y: 2 # => #<Point(@x=0, @y=2)>
# ```
#
# This macro also provides a `copy_with` method which returns
# a copy of the record with the provided properties altered.
#
# ```
# record Point, x = 0, y = 0
#
# p = Point.new y: 2 # => #<Point(@x=0, @y=2)>
# p.copy_with x: 3 # => #<Point(@x=3, @y=2)>
# p # => #<Point(@x=0, @y=2)>
# ```
macro record(name, *properties)
struct {{name.id}}
{% for property in properties %}
{% if property.is_a?(Assign) %}
getter {{property.target.id}}
{% elsif property.is_a?(TypeDeclaration) %}
getter {{property}}
{% else %}
getter :{{property.id}}
{% end %}
{% end %}
def initialize({{
*properties.map do |field|
"@#{field.id}".id
end
}})
end
{{yield}}
def copy_with({{
*properties.map do |property|
if property.is_a?(Assign)
"#{property.target.id} _#{property.target.id} = @#{property.target.id}".id
elsif property.is_a?(TypeDeclaration)
"#{property.var.id} _#{property.var.id} = @#{property.var.id}".id
else
"#{property.id} _#{property.id} = @#{property.id}".id
end
end
}})
self.class.new({{
*properties.map do |property|
if property.is_a?(Assign)
"_#{property.target.id}".id
elsif property.is_a?(TypeDeclaration)
"_#{property.var.id}".id
else
"_#{property.id}".id
end
end
}})
end
def clone
self.class.new({{
*properties.map do |property|
if property.is_a?(Assign)
"@#{property.target.id}.clone".id
elsif property.is_a?(TypeDeclaration)
"@#{property.var.id}.clone".id
else
"@#{property.id}.clone".id
end
end
}})
end
end
end
# Prints a series of expressions together with their pretty printed values.
# Useful for print style debugging.
#
# ```
# a = 1
# pp! a # => "a # => 1"
#
# pp! [1, 2, 3].map(&.to_s) # => "[1, 2, 3].map(&.to_s) # => ["1", "2", "3"]"
# ```
#
# See also: `pp`, `Object#pretty_inspect`.
macro pp!(*exps)
{% if exps.size == 0 %}
# Nothing
{% elsif exps.size == 1 %}
{% exp = exps.first %}
%prefix = "#{{{ exp.stringify }}} # => "
::print %prefix
::pp({{exp}})
{% else %}
%names = { {{*exps.map(&.stringify)}} }
%max_size = %names.max_of &.size
{
{% for exp, i in exps %}
begin
%prefix = "#{%names[{{i}}].ljust(%max_size)} # => "
::print %prefix
::pp({{exp}})
end,
{% end %}
}
{% end %}
end
# Prints a series of expressions together with their inspected values.
# Useful for print style debugging.
#
# ```
# a = 1
# p! a # => "a # => 1"
#
# p! [1, 2, 3].map(&.to_s) # => "[1, 2, 3].map(&.to_s) # => ["1", "2", "3"]"
# ```
#
# See also: `p`, `Object#inspect`.
macro p!(*exps)
{% if exps.size == 0 %}
# Nothing
{% elsif exps.size == 1 %}
{% exp = exps.first %}
%prefix = "#{{{ exp.stringify }}} # => "
::print %prefix
::p({{exp}})
{% else %}
%names = { {{*exps.map(&.stringify)}} }
%max_size = %names.max_of &.size
{
{% for exp, i in exps %}
begin
%prefix = "#{%names[{{i}}].ljust(%max_size)} # => "
::print %prefix
::p({{exp}})
end,
{% end %}
}
{% end %}
end