Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use IR.Values.Value | |
| 3 | use Semantic.Types.Type | |
| 4 | ||
| 5 | // How many elements a source will produce, known before any of them | |
| 6 | // is read: an array's or list's count, a range's `to - from` (plus one | |
| 7 | // when inclusive), or the count `repeat(value, count)` was given where | |
| 8 | // that is a literal or a variable read. The count is built over | |
| 9 | // `source_load`, the source already evaluated into a local, so reading | |
| 10 | // it evaluates nothing twice - except `repeat`'s count, which is why | |
| 11 | // only a count with no effect qualifies. | |
| 12 | // | |
| 13 | // A range or `repeat` count can be negative, meaning nothing is | |
| 14 | // produced, so it is clamped at zero here. A collection's own count | |
| 15 | // cannot be negative and is not. | |
| 16 | class KNOWN_COUNT_SOURCE is | |
| 17 | _innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup | |
| 18 | _function_caller: Semantic.FUNCTION_CALLER | |
| 19 | _zero_argument_function: (Type, string) -> Semantic.Symbols.Function? | |
| 20 | _pipes_function_name: (Trees.Expressions.CALL) -> string? | |
| 21 | ||
| 22 | init( | |
| 23 | innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup, | |
| 24 | function_caller: Semantic.FUNCTION_CALLER, | |
| 25 | zero_argument_function: (Type, string) -> Semantic.Symbols.Function?, | |
| 26 | pipes_function_name: (Trees.Expressions.CALL) -> string? | |
| 27 | ) is | |
| 28 | _innate_symbol_lookup = innate_symbol_lookup | |
| 29 | _function_caller = function_caller | |
| 30 | _zero_argument_function = zero_argument_function | |
| 31 | _pipes_function_name = pipes_function_name | |
| 32 | si | |
| 33 | ||
| 34 | count_of( | |
| 35 | source: Trees.Expressions.Expression, | |
| 36 | source_load: Value, | |
| 37 | source_type: Type, | |
| 38 | location: Source.LOCATION | |
| 39 | ) -> Value? is | |
| 40 | let int_type = _innate_symbol_lookup.get_int_type() | |
| 41 | let no_arguments = Collections.LIST[Value](0) | |
| 42 | ||
| 43 | if isa Semantic.Types.ARRAY(source_type) \/ | |
| 44 | is_same_generic(source_type, _innate_symbol_lookup.get_unspecialized_mutable_list_type()) \/ | |
| 45 | is_same_generic(source_type, _innate_symbol_lookup.get_unspecialized_list_type()) | |
| 46 | then | |
| 47 | if let count = _zero_argument_function(source_type, "$get_count") then | |
| 48 | return count.call(location, source_load, no_arguments, null, _function_caller) | |
| 49 | fi | |
| 50 | ||
| 51 | return null | |
| 52 | fi | |
| 53 | ||
| 54 | // The range types resolve only as reflected ghul-runtime types, | |
| 55 | // and are absent while compiling the runtime itself, where they | |
| 56 | // are source symbols in the project. Sizing is an optimisation | |
| 57 | // and nothing may throw out of it, so it asks for them in the | |
| 58 | // form that can decline. | |
| 59 | let ranges = _innate_symbol_lookup.get_int_range_types() | |
| 60 | ||
| 61 | if !ranges? then | |
| 62 | return null | |
| 63 | fi | |
| 64 | ||
| 65 | let is_range = is_same_generic(source_type, ranges.exclusive) | |
| 66 | let is_inclusive = is_same_generic(source_type, ranges.inclusive) | |
| 67 | ||
| 68 | if is_range \/ is_inclusive then | |
| 69 | let to = _zero_argument_function(source_type, "$get_to") | |
| 70 | let from = _zero_argument_function(source_type, "$get_from") | |
| 71 | ||
| 72 | if !to? \/ !from? then | |
| 73 | return null | |
| 74 | fi | |
| 75 | ||
| 76 | let span = | |
| 77 | cast Value( | |
| 78 | IR.Values.ARITHMETIC( | |
| 79 | IR.ArithmeticOperation.SUBTRACT, | |
| 80 | to.call(location, source_load, no_arguments, null, _function_caller), | |
| 81 | from.call(location, source_load, no_arguments, null, _function_caller), | |
| 82 | int_type).unmarked()) | |
| 83 | ||
| 84 | let extent = | |
| 85 | if is_inclusive then | |
| 86 | cast Value( | |
| 87 | IR.Values.ARITHMETIC( | |
| 88 | IR.ArithmeticOperation.ADD, | |
| 89 | span, | |
| 90 | IR.Values.Literal.NUMBER(1, int_type), | |
| 91 | int_type).unmarked()) | |
| 92 | else | |
| 93 | span | |
| 94 | fi | |
| 95 | ||
| 96 | return IR.Values.INT_MAX_ZERO(extent, int_type) | |
| 97 | fi | |
| 98 | ||
| 99 | if let source_call = cast Trees.Expressions.CALL?(source) then | |
| 100 | if let name = _pipes_function_name(source_call) then | |
| 101 | let arguments = source_call.arguments.expressions | |
| 102 | ||
| 103 | if name =~ "repeat" /\ arguments.count == 2 then | |
| 104 | let given = arguments[1].value | |
| 105 | ||
| 106 | if isa IR.Values.Literal.NUMBER(given) \/ isa IR.Values.Load.SYMBOL(given) then | |
| 107 | return IR.Values.INT_MAX_ZERO(given, int_type) | |
| 108 | fi | |
| 109 | fi | |
| 110 | fi | |
| 111 | fi | |
| 112 | ||
| 113 | return null | |
| 114 | si | |
| 115 | ||
| 116 | // Whether `type` is an instantiation of the generic `generic` | |
| 117 | // stands for, by the identity of the Classy each is over. | |
| 118 | is_same_generic(type: Type, generic: Type?) -> bool is | |
| 119 | if !generic? then | |
| 120 | return false | |
| 121 | fi | |
| 122 | ||
| 123 | let a = _classy_of(type) | |
| 124 | let b = _classy_of(generic) | |
| 125 | ||
| 126 | return a? /\ b? /\ a == b | |
| 127 | si | |
| 128 | ||
| 129 | _classy_of(type: Type) -> Semantic.Symbols.Classy? is | |
| 130 | if !isa Semantic.Types.NAMED(type) then | |
| 131 | return null | |
| 132 | fi | |
| 133 | ||
| 134 | let symbol mut = (cast Semantic.Types.NAMED(type)).symbol | |
| 135 | ||
| 136 | if isa Semantic.Symbols.GENERIC(symbol) then | |
| 137 | symbol = (cast Semantic.Symbols.GENERIC(symbol)).symbol | |
| 138 | fi | |
| 139 | ||
| 140 | if isa Semantic.Symbols.Classy(symbol) then | |
| 141 | return cast Semantic.Symbols.Classy(symbol) | |
| 142 | fi | |
| 143 | ||
| 144 | return null | |
| 145 | si | |
| 146 | si | |
| 147 | si |