Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Ghul.Pipes | |
| 3 | ||
| 4 | // The operators in scope under an operator's name that take every | |
| 5 | // operand as an argument: global operators, the innates, and static | |
| 6 | // members brought in by `use`. | |
| 7 | // | |
| 8 | // A group holding only instance operators, as inside a type that | |
| 9 | // declares the operator as a member, does not hide one further out: | |
| 10 | // an instance operator takes its left operand as the receiver, so it | |
| 11 | // can never answer an operator whose operands are all arguments, and | |
| 12 | // is a candidate only through the operand's own type. | |
| 13 | // | |
| 14 | // The operator compiler gathers its candidates here, and the | |
| 15 | // store-free analysis bounds an operator expression by the same | |
| 16 | // candidates. Sharing the lookup keeps the analysis from charging | |
| 17 | // an operator with a member it cannot call. | |
| 18 | class SCOPE_OPERATOR_CANDIDATES is | |
| 19 | // Whether `function` takes the left operand as its receiver | |
| 20 | // rather than as an argument. | |
| 21 | takes_receiver(function: Semantic.Symbols.Function) -> bool static => | |
| 22 | function.is_instance \/ isa Semantic.Symbols.INNATE_METHOD(function) | |
| 23 | ||
| 24 | // The nearest group in scope under `name` holding an operator that | |
| 25 | // takes its operands as arguments, reduced to those, or null when | |
| 26 | // there is none. | |
| 27 | find(symbol_table: Semantic.SYMBOL_TABLE, location: Source.LOCATION, name: string) -> Semantic.Symbols.FUNCTION_GROUP? static is | |
| 28 | let found = | |
| 29 | symbol_table.find_enclosing_matching( | |
| 30 | name, | |
| 31 | symbol => | |
| 32 | if let group: Semantic.Symbols.FUNCTION_GROUP = symbol then | |
| 33 | group.functions |> any(f => !takes_receiver(f)) | |
| 34 | else | |
| 35 | false | |
| 36 | fi | |
| 37 | ) | |
| 38 | ||
| 39 | let group = cast Semantic.Symbols.FUNCTION_GROUP?(found) | |
| 40 | ||
| 41 | if !group? \/ group.functions |> all(f => !takes_receiver(f)) then | |
| 42 | return group | |
| 43 | fi | |
| 44 | ||
| 45 | let result = Semantic.Symbols.FUNCTION_GROUP(location, symbol_table.current_scope, name) | |
| 46 | ||
| 47 | for function in group.functions do | |
| 48 | if !takes_receiver(function) then | |
| 49 | result.add(function) | |
| 50 | fi | |
| 51 | od | |
| 52 | ||
| 53 | return result | |
| 54 | si | |
| 55 | si | |
| 56 | si |