forked from amazon-ion/ion-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemMacro.kt
266 lines (246 loc) · 8.79 KB
/
SystemMacro.kt
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl.macro
import com.amazon.ion.impl.*
import com.amazon.ion.impl.SystemSymbols_1_1.*
import com.amazon.ion.impl.macro.ExpressionBuilderDsl.Companion.templateBody
import com.amazon.ion.impl.macro.ParameterFactory.exactlyOneTagged
import com.amazon.ion.impl.macro.ParameterFactory.zeroOrOneTagged
import com.amazon.ion.impl.macro.ParameterFactory.zeroToManyTagged
/**
* Macros that are built in, rather than being defined by a template.
*/
enum class SystemMacro(
val id: Byte,
val systemSymbol: SystemSymbols_1_1,
override val signature: List<Macro.Parameter>,
override val body: List<Expression.TemplateBodyExpression>? = null
) : Macro {
// Technically not system macros, but special forms. However, it's easier to model them as if they are macros in TDL.
// We give them an ID of -1 to distinguish that they are not addressable outside TDL.
IfNone(-1, IF_NONE, listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))),
IfSome(-1, IF_SOME, listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))),
IfSingle(-1, IF_SINGLE, listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))),
IfMulti(-1, IF_MULTI, listOf(zeroToManyTagged("stream"), zeroToManyTagged("true_branch"), zeroToManyTagged("false_branch"))),
// The real macros
None(0, NONE, emptyList()),
Values(1, VALUES, listOf(zeroToManyTagged("values"))),
Annotate(2, ANNOTATE, listOf(zeroToManyTagged("ann"), exactlyOneTagged("value"))),
MakeString(3, MAKE_STRING, listOf(zeroToManyTagged("text"))),
MakeSymbol(4, MAKE_SYMBOL, listOf(zeroToManyTagged("text"))),
MakeBlob(5, MAKE_BLOB, listOf(zeroToManyTagged("bytes"))),
MakeDecimal(6, MAKE_DECIMAL, listOf(exactlyOneTagged("coefficient"), exactlyOneTagged("exponent"))),
MakeTimestamp(
7, MAKE_TIMESTAMP,
listOf(
exactlyOneTagged("year"),
zeroOrOneTagged("month"),
zeroOrOneTagged("day"),
zeroOrOneTagged("hour"),
zeroOrOneTagged("minute"),
zeroOrOneTagged("second"),
zeroOrOneTagged("offset_minutes"),
)
),
// TODO: make_list
// TODO: make_sexp
// TODO: make_struct
/**
* ```ion
* (macro set_symbols (symbols*)
* $ion_encoding::(
* (symbol_table [(%symbols)])
* (macro_table $ion_encoding)
* ))
* ```
*/
SetSymbols(
11, SET_SYMBOLS, listOf(zeroToManyTagged("symbols")),
templateBody {
annotated(ION_ENCODING, ::sexp) {
sexp {
symbol(SYMBOL_TABLE)
list { variable(0) }
}
sexp {
symbol(MACRO_TABLE)
symbol(ION_ENCODING)
}
}
}
),
/**
* ```ion
* (macro add_symbols (symbols*)
* $ion_encoding::(
* (symbol_table $ion_encoding [(%symbols)])
* (macro_table $ion_encoding)
* ))
* ```
*/
AddSymbols(
12, ADD_SYMBOLS, listOf(zeroToManyTagged("symbols")),
templateBody {
annotated(ION_ENCODING, ::sexp) {
sexp {
symbol(SYMBOL_TABLE)
symbol(ION_ENCODING)
list { variable(0) }
}
sexp {
symbol(MACRO_TABLE)
symbol(ION_ENCODING)
}
}
}
),
/**
* ```ion
* (macro set_macros (macros*)
* $ion_encoding::(
* (symbol_table $ion_encoding)
* (macro_table (%macros))
* ))
* ```
*/
SetMacros(
13, SET_MACROS, listOf(zeroToManyTagged("macros")),
templateBody {
annotated(ION_ENCODING, ::sexp) {
sexp {
symbol(SYMBOL_TABLE)
symbol(ION_ENCODING)
}
sexp {
symbol(MACRO_TABLE)
variable(0)
}
}
}
),
/**
* ```ion
* (macro add_macros (macros*)
* $ion_encoding::(
* (symbol_table $ion_encoding)
* (macro_table $ion_encoding (%macros))
* ))
* ```
*/
AddMacros(
14, ADD_MACROS, listOf(zeroToManyTagged("macros")),
templateBody {
annotated(ION_ENCODING, ::sexp) {
sexp {
symbol(SYMBOL_TABLE)
symbol(ION_ENCODING)
}
sexp {
symbol(MACRO_TABLE)
symbol(ION_ENCODING)
variable(0)
}
}
}
),
/**
* ```ion
* (macro use (catalog_key version?)
* $ion_encoding::(
* (import the_module (%catalog_key) (.if_none (%version) 1 (%version)))
* (symbol_table $ion_encoding the_module)
* (macro_table $ion_encoding the_module)
* ))
* ```
*/
Use(
15, USE, listOf(exactlyOneTagged("catalog_key"), zeroOrOneTagged("version")),
templateBody {
val theModule = _Private_Utils.newSymbolToken("the_module")
annotated(ION_ENCODING, ::sexp) {
sexp {
symbol(IMPORT)
symbol(theModule)
variable(0)
macro(IfNone) {
variable(1)
int(1)
variable(1)
}
}
sexp {
symbol(SYMBOL_TABLE)
symbol(ION_ENCODING)
symbol(theModule)
}
sexp {
symbol(MACRO_TABLE)
symbol(ION_ENCODING)
symbol(theModule)
}
}
}
),
// TODO: parse_ion
Repeat(17, REPEAT, listOf(exactlyOneTagged("n"), zeroToManyTagged("value"))),
Delta(18, DELTA, listOf(zeroToManyTagged("deltas"))),
// TODO: flatten
Sum(20, SUM, listOf(exactlyOneTagged("a"), exactlyOneTagged("b"))),
Comment(21, META, listOf(zeroToManyTagged("values")), templateBody { macro(None) {} }),
MakeField(
22, MAKE_FIELD,
listOf(
Macro.Parameter("field_name", Macro.ParameterEncoding.FlexSym, Macro.ParameterCardinality.ExactlyOne), exactlyOneTagged("value")
)
),
Default(
23, DEFAULT, listOf(zeroToManyTagged("expr"), zeroToManyTagged("default_expr")),
templateBody {
macro(IfNone) {
variable(0)
variable(1)
variable(0)
}
}
),
;
val macroName: String get() = this.systemSymbol.text
override val dependencies: List<Macro>
get() = body
?.filterIsInstance<Expression.MacroInvocation>()
?.map(Expression.MacroInvocation::macro)
?.distinct()
?: emptyList()
companion object : MacroTable {
private val MACROS_BY_NAME: Map<String, SystemMacro> = SystemMacro.entries.associateBy { it.macroName }
// TODO: Once all of the macros are implemented, replace this with an array as in SystemSymbols_1_1
private val MACROS_BY_ID: Map<Byte, SystemMacro> = SystemMacro.entries
.filterNot { it.id < 0 }
.associateBy { it.id }
@JvmStatic
fun size() = MACROS_BY_ID.size
/** Gets a [SystemMacro] by its address in the system table */
@JvmStatic
operator fun get(id: Int): SystemMacro? = MACROS_BY_ID[id.toByte()]
/** Gets, by name, a [SystemMacro] with an address in the system table (i.e. that can be invoked as E-Expressions) */
@JvmStatic
operator fun get(name: String): SystemMacro? = MACROS_BY_NAME[name]?.takeUnless { it.id < 0 }
@JvmStatic
override operator fun get(address: MacroRef): SystemMacro? {
return when (address) {
is MacroRef.ById -> get(address.id)
is MacroRef.ByName -> get(address.name)
}
}
/** Gets a [SystemMacro] by name, including those which are not in the system table (i.e. special forms) */
@JvmStatic
fun getMacroOrSpecialForm(ref: MacroRef): SystemMacro? {
return when (ref) {
is MacroRef.ById -> get(ref.id)
is MacroRef.ByName -> MACROS_BY_NAME[ref.name]
}
}
@JvmStatic
val SYSTEM_MACRO_TABLE = this
}
}