Appearance
| 1 | namespace IR.Values is | |
| 2 | ||
| 3 | use Semantic.Types.Type | |
| 4 | ||
| 5 | use Ghul.Pipes | |
| 6 | ||
| 7 | // Every one of a list of boolean values, short-circuited: the | |
| 8 | // first that answers false is the answer, and the rest are never | |
| 9 | // evaluated. | |
| 10 | // | |
| 11 | // A synthesized `=~` is a guard followed by one comparison per | |
| 12 | // member followed by whatever the base contributes, and each of | |
| 13 | // those is built as a value of its own. This is the conjunction | |
| 14 | // around them, so that nothing has to build a tree of `/\` | |
| 15 | // expressions to say `and` in. | |
| 16 | class ALL_OF: Composite is | |
| 17 | operands: Collections.LIST[Value] | |
| 18 | _result_type: Type | |
| 19 | ||
| 20 | type: Type => _result_type | |
| 21 | is_lightweight_pure: bool => false | |
| 22 | ||
| 23 | init(operands: Collections.LIST[Value], result_type: Type) is | |
| 24 | super.init() | |
| 25 | ||
| 26 | self.operands = operands | |
| 27 | self._result_type = result_type | |
| 28 | si | |
| 29 | ||
| 30 | // The first operand to answer false leaves the conjunction false, | |
| 31 | // and the rest are never evaluated. | |
| 32 | expand() -> BLOCK is | |
| 33 | let expansion = IR.EXPANSION(type) | |
| 34 | ||
| 35 | // Nothing to be false: an empty conjunction is true, which | |
| 36 | // is what a type with no members to compare answers once | |
| 37 | // its guard has passed. | |
| 38 | if operands.count == 0 then | |
| 39 | expansion.add(Literal.NUMBER(1, type)) | |
| 40 | ||
| 41 | return expansion.result | |
| 42 | fi | |
| 43 | ||
| 44 | let result = expansion.temp("all_of", type) | |
| 45 | ||
| 46 | let done = expansion.region() | |
| 47 | let failed = expansion.region() | |
| 48 | ||
| 49 | for operand in operands do | |
| 50 | expansion.exit_if(failed, IR.BRANCH.Z, operand) | |
| 51 | od | |
| 52 | ||
| 53 | expansion.store(result, Literal.NUMBER(1, type)) | |
| 54 | expansion.exit(done) | |
| 55 | expansion.close(failed) | |
| 56 | ||
| 57 | expansion.store(result, Literal.NUMBER(0, type)) | |
| 58 | expansion.close(done) | |
| 59 | ||
| 60 | expansion.add(result.load()) | |
| 61 | ||
| 62 | return expansion.result | |
| 63 | si | |
| 64 | ||
| 65 | to_string() -> string => | |
| 66 | "all-of:[{type}]({operands |> map(o => "{o}") |> join(",")})" | |
| 67 | si | |
| 68 | si |