Appearance
| 1 | namespace IR is | |
| 2 | use Collections.LIST | |
| 3 | ||
| 4 | use Semantic.Types.Type | |
| 5 | use Semantic.Symbols.Function | |
| 6 | ||
| 7 | use Values.Value | |
| 8 | ||
| 9 | /// An interpolation's builder operations as calls on the core library's | |
| 10 | /// builder, for a target whose core library is compiled from source: the | |
| 11 | /// builder is constructed, takes each piece of text through `append`, and | |
| 12 | /// answers the whole through `to_string`. A value's text comes from its | |
| 13 | /// own `to_string`, which dispatches as any other call does. Each answer | |
| 14 | /// is IR the target lowers as it lowers any other. | |
| 15 | class INTERPOLATION_CALLS is | |
| 16 | _report: string -> void | |
| 17 | ||
| 18 | /// - report: called with a description of what an interpolation needs | |
| 19 | /// that the core library does not provide | |
| 20 | init(report: string -> void) is | |
| 21 | _report = report | |
| 22 | si | |
| 23 | ||
| 24 | /// The construction of the builder `type` with room for the text | |
| 25 | /// `hints` predicts, the literal text's length and the number of | |
| 26 | /// values, as .NET's handler is given them. Where the hints are not | |
| 27 | /// both constant, or predict nothing, or the builder has no | |
| 28 | /// constructor taking a capacity, it is constructed with nothing. | |
| 29 | new_builder(type: Type?, hints: Collections.List[Value]) -> Value? is | |
| 30 | if let capacity = _capacity(hints) /\ capacity > 0 then | |
| 31 | let int_type = IoC.CONTAINER.instance.innate_symbol_lookup.get_int_type() | |
| 32 | ||
| 33 | if let group: Semantic.Symbols.FUNCTION_GROUP = type?.find_member("init") then | |
| 34 | for function in group.functions do | |
| 35 | if function.arguments.count == 1 /\ function.arguments[0].symbol == int_type.symbol then | |
| 36 | return Values.NEW(type!, function, LIST[Value]([Values.Literal.NUMBER(capacity, int_type)])) | |
| 37 | fi | |
| 38 | od | |
| 39 | fi | |
| 40 | fi | |
| 41 | ||
| 42 | return new_builder(type) | |
| 43 | si | |
| 44 | ||
| 45 | // The number of code units an interpolation's text is predicted to | |
| 46 | // take: its literal text, and eleven for each value, which is what | |
| 47 | // an `int` can take. | |
| 48 | _capacity(hints: Collections.List[Value]) -> int? static is | |
| 49 | if hints.count != 2 then | |
| 50 | return null | |
| 51 | fi | |
| 52 | ||
| 53 | if let literal: Values.Literal.NUMBER = hints[0], values: Values.Literal.NUMBER = hints[1] then | |
| 54 | if let l: Values.Literal.CONSTANT.I4 = literal.constant, v: Values.Literal.CONSTANT.I4 = values.constant then | |
| 55 | return l.value + 11 * v.value | |
| 56 | fi | |
| 57 | fi | |
| 58 | ||
| 59 | return null | |
| 60 | si | |
| 61 | ||
| 62 | /// The construction of the builder `type`, or null where it has no | |
| 63 | /// constructor taking nothing. | |
| 64 | new_builder(type: Type?) -> Value? is | |
| 65 | if let constructor = method(type, "init", 0) then | |
| 66 | return Values.NEW(type!, constructor, LIST[Value]()) | |
| 67 | fi | |
| 68 | ||
| 69 | _report("an interpolation, whose builder has no constructor") | |
| 70 | ||
| 71 | return null | |
| 72 | si | |
| 73 | ||
| 74 | /// The call appending `text` to `builder` through the builder's | |
| 75 | /// `append` taking a string, or null where it has none. A builder may | |
| 76 | /// overload `append` and return itself from it, as .NET's does, so | |
| 77 | /// the overload is chosen by its argument and the target drops any | |
| 78 | /// result. | |
| 79 | append(builder: Value, text: Value) -> Value? is | |
| 80 | if let append = string_append(builder.type) then | |
| 81 | return Values.Call.INSTANCE(builder, append, LIST[Value]([text]), null) | |
| 82 | fi | |
| 83 | ||
| 84 | _report("an interpolation, whose builder has no append") | |
| 85 | ||
| 86 | return null | |
| 87 | si | |
| 88 | ||
| 89 | /// The call appending `text` to `builder` padded to `alignment`, or | |
| 90 | /// null where the builder cannot. | |
| 91 | append_aligned(builder: Value, text: Value, alignment: Value) -> Value? is | |
| 92 | if let append = method(builder.type, "_append_aligned", 2) then | |
| 93 | return Values.Call.INSTANCE(builder, append, LIST[Value]([text, alignment]), null) | |
| 94 | fi | |
| 95 | ||
| 96 | _report("an interpolated value with an alignment") | |
| 97 | ||
| 98 | return null | |
| 99 | si | |
| 100 | ||
| 101 | /// The call answering everything appended to `builder`, or null | |
| 102 | /// where it has no `to_string`. | |
| 103 | build(builder: Value) -> Value? is | |
| 104 | if let build = method(builder.type, "to_string", 0) then | |
| 105 | return Values.Call.INSTANCE(builder, build, LIST[Value](), null) | |
| 106 | fi | |
| 107 | ||
| 108 | _report("an interpolation, whose builder has no to_string") | |
| 109 | ||
| 110 | return null | |
| 111 | si | |
| 112 | ||
| 113 | /// The text of `value`, whose type is `type` once any type parameter | |
| 114 | /// is replaced: a string as itself, anything else through its | |
| 115 | /// `to_string`. A value whose type has no `to_string` of its own is | |
| 116 | /// displayed through the core library's `$`, so it reaches here only | |
| 117 | /// where the library declares none, and is reported rather than | |
| 118 | /// given different text. | |
| 119 | text_of(value: Value, type: Type?) -> Value is | |
| 120 | if _is_string(type) then | |
| 121 | return value | |
| 122 | elif let text = enum_text(value, type) then | |
| 123 | return text | |
| 124 | elif let to_string = method(type, "to_string", 0) then | |
| 125 | if let owner: Semantic.Symbols.Classy = to_string.owner /\ owner.is_object then | |
| 126 | _report("an interpolated value whose type has no to_string of its own") | |
| 127 | ||
| 128 | return value | |
| 129 | fi | |
| 130 | ||
| 131 | return Values.Call.INSTANCE(value, to_string, LIST[Value](), null) | |
| 132 | fi | |
| 133 | ||
| 134 | _report("an interpolated value of type {value.type}") | |
| 135 | ||
| 136 | return value | |
| 137 | si | |
| 138 | ||
| 139 | /// The text of `value`, whose type is `type` once any type parameter | |
| 140 | /// is replaced, under the format string `format`. A numeric value is | |
| 141 | /// written by its type's `to_string(format)`. .NET ignores the format | |
| 142 | /// of a value that is not formattable, so a string, a bool, a char or | |
| 143 | /// an object of a class is written as it is without one. Enums and | |
| 144 | /// decimals are formattable on .NET and have no formatter in the core | |
| 145 | /// library, so they are reported rather than given different text. | |
| 146 | formatted_text_of(value: Value, format: Value, type: Type?) -> Value is | |
| 147 | if type?.symbol?.symbol_kind =~ Semantic.Symbols.SymbolKind.ENUM then | |
| 148 | _report("an interpolated enum with a format") | |
| 149 | ||
| 150 | return value | |
| 151 | fi | |
| 152 | ||
| 153 | let lookup = IoC.CONTAINER.instance.innate_symbol_lookup | |
| 154 | ||
| 155 | if type? /\ type.symbol == lookup.get_decimal_type().symbol then | |
| 156 | _report("an interpolated decimal with a format") | |
| 157 | ||
| 158 | return value | |
| 159 | fi | |
| 160 | ||
| 161 | let scalar = IoC.CONTAINER.instance.value_converter.scalar(type) | |
| 162 | let is_bigint = type? /\ !type.is_optional /\ type.symbol == lookup.get_bigint_type().symbol | |
| 163 | ||
| 164 | if !is_bigint /\ !scalar? then | |
| 165 | if let formatted = _formattable_text(value, format, type) then | |
| 166 | return formatted | |
| 167 | fi | |
| 168 | ||
| 169 | return text_of(value, type) | |
| 170 | fi | |
| 171 | ||
| 172 | if !is_bigint /\ (scalar =~ Scalar.BOOL \/ scalar =~ Scalar.CHAR) then | |
| 173 | return text_of(value, type) | |
| 174 | fi | |
| 175 | ||
| 176 | if let literal: Values.Literal.STRING = format then | |
| 177 | let integer = is_bigint \/ !(scalar =~ Scalar.FLOAT32 \/ scalar =~ Scalar.FLOAT64) | |
| 178 | ||
| 179 | if !CORE_NUMERIC_FORMAT.is_covered(literal.value, integer) then | |
| 180 | _report("the numeric format {literal.value} for {type}") | |
| 181 | ||
| 182 | return value | |
| 183 | fi | |
| 184 | fi | |
| 185 | ||
| 186 | if let to_string = method(type, "to_string", 1) then | |
| 187 | return Values.Call.INSTANCE(value, to_string, LIST[Value]([format]), null) | |
| 188 | fi | |
| 189 | ||
| 190 | _report("an interpolated value of type {type} with a format") | |
| 191 | ||
| 192 | return value | |
| 193 | si | |
| 194 | ||
| 195 | /// The builder's one-argument `append` whose argument is a string. | |
| 196 | string_append(type: Type?) -> Function? is | |
| 197 | if let group: Semantic.Symbols.FUNCTION_GROUP = type?.find_member("append") then | |
| 198 | for function in group.functions do | |
| 199 | if function.arguments.count == 1 /\ _is_string(function.arguments[0]) then | |
| 200 | return function | |
| 201 | fi | |
| 202 | od | |
| 203 | elif let function: Function = type?.find_member("append") /\ | |
| 204 | function.arguments.count == 1 /\ _is_string(function.arguments[0]) | |
| 205 | then | |
| 206 | return function | |
| 207 | fi | |
| 208 | ||
| 209 | return null | |
| 210 | si | |
| 211 | ||
| 212 | // `value` written in `format` by its `to_string(format, provider)`, | |
| 213 | // where its type implements the core library's `System.IFormattable`, | |
| 214 | // as .NET formats only a value that does. | |
| 215 | _formattable_text(value: Value, format: Value, type: Type?) -> Value? static is | |
| 216 | let formattable = cast Semantic.Symbols.Classy?(IoC.CONTAINER.instance.innate_symbol_lookup.get_formattable_symbol()) | |
| 217 | ||
| 218 | if !formattable? \/ !type? \/ !Semantic.Types.NAMED(formattable).is_assignable_from(type) then | |
| 219 | return null | |
| 220 | fi | |
| 221 | ||
| 222 | if let to_string = method(type, "to_string", 2) then | |
| 223 | return Values.Call.INSTANCE(value, to_string, LIST[Value]([format, Values.NULL(to_string.arguments[1])]), null) | |
| 224 | fi | |
| 225 | ||
| 226 | return null | |
| 227 | si | |
| 228 | ||
| 229 | /// The method of `type` named `name` that takes `arity` arguments. | |
| 230 | method(type: Type?, name: string, arity: int) -> Function? static is | |
| 231 | let found = type?.find_member(name) | |
| 232 | ||
| 233 | if let group: Semantic.Symbols.FUNCTION_GROUP = found then | |
| 234 | for function in group.functions do | |
| 235 | if function.arguments.count == arity then | |
| 236 | return function | |
| 237 | fi | |
| 238 | od | |
| 239 | elif let function: Function = found /\ function.arguments.count == arity then | |
| 240 | return function | |
| 241 | fi | |
| 242 | ||
| 243 | return null | |
| 244 | si | |
| 245 | ||
| 246 | /// The text of `value`, a value of the enum `type`, as .NET gives | |
| 247 | /// it: its member's name, the names a flags value combines, or the | |
| 248 | /// number. A value is held as its integer, so the core library is | |
| 249 | /// handed the enum's members. Null where `type` is not an enum the | |
| 250 | /// core library can name. | |
| 251 | enum_text(value: Value, type: Type?) -> Value? static is | |
| 252 | // A value whose text is asked for through `object` arrives boxed. | |
| 253 | let inner = if let box: Values.BOX = value then box.value else value fi | |
| 254 | let `enum = cast Semantic.Symbols.ENUM_STRUCT?((if inner == value then type else inner.type fi)?.symbol) | |
| 255 | let lookup = IoC.CONTAINER.instance.innate_symbol_lookup | |
| 256 | let functions = lookup.get_enum_text_functions() | |
| 257 | ||
| 258 | if !`enum? \/ !functions? \/ `enum.is_wider_than_int32(lookup) then | |
| 259 | return null | |
| 260 | fi | |
| 261 | ||
| 262 | let function = _method_of(functions, 4) | |
| 263 | ||
| 264 | if !function? then | |
| 265 | return null | |
| 266 | fi | |
| 267 | ||
| 268 | let members = LIST[(name: string, value: int)]() | |
| 269 | ||
| 270 | for symbol in `enum.symbols do | |
| 271 | if let member: Semantic.Symbols.ENUM_STRUCT_MEMBER = symbol, number = member.numeric_value then | |
| 272 | members.add((name = member.name, value = number)) | |
| 273 | fi | |
| 274 | od | |
| 275 | ||
| 276 | members.sort((a, b) => if a.value < b.value then -1 elif a.value > b.value then 1 else a.name.compare_to(b.name) fi) | |
| 277 | ||
| 278 | let int_type = lookup.get_int_type() | |
| 279 | let string_type = lookup.get_string_type() | |
| 280 | ||
| 281 | let names = LIST[Value]() | |
| 282 | let values = LIST[Value]() | |
| 283 | ||
| 284 | for (name, number) in members do | |
| 285 | names.add(Values.Literal.STRING(name, string_type)) | |
| 286 | values.add(Values.Literal.NUMBER(number, int_type)) | |
| 287 | od | |
| 288 | ||
| 289 | let arguments = LIST[Value]([ | |
| 290 | inner, | |
| 291 | Values.SEQUENCE(lookup.get_array_type(string_type), string_type, names), | |
| 292 | Values.SEQUENCE(lookup.get_array_type(int_type), int_type, values), | |
| 293 | Values.Literal.NUMBER(if `enum.is_flags then 1 else 0 fi, lookup.get_bool_type()) | |
| 294 | ]) | |
| 295 | ||
| 296 | return Values.Call.STATIC(function, arguments, string_type, null) | |
| 297 | si | |
| 298 | ||
| 299 | _method_of(group: Semantic.Symbols.FUNCTION_GROUP, arity: int) -> Function? static is | |
| 300 | for function in group.functions do | |
| 301 | if function.arguments.count == arity then | |
| 302 | return function | |
| 303 | fi | |
| 304 | od | |
| 305 | ||
| 306 | return null | |
| 307 | si | |
| 308 | ||
| 309 | _is_string(type: Type?) -> bool => | |
| 310 | type? /\ type.symbol == IoC.CONTAINER.instance.innate_symbol_lookup.get_string_type().symbol | |
| 311 | si | |
| 312 | si |