Appearance
| 1 | namespace Syntax.Process is | |
| 2 | use Source.LOCATION | |
| 3 | ||
| 4 | // A call's arguments as the callee's formals see them. | |
| 5 | // | |
| 6 | // Written one expression per formal, the two lists agree and every | |
| 7 | // slot is the expression standing in it. Written with an argument | |
| 8 | // pack spread out - `apply(f, "a", "b")` against `v: T..` - the | |
| 9 | // trailing expressions fill one formal between them. That slot has a | |
| 10 | // location and, once walked, a value, but no expression of its own: | |
| 11 | // the tuple is built as a value over the elements' values, and the | |
| 12 | // call keeps the argument list it was written with. | |
| 13 | class ARGUMENT_SLOTS is | |
| 14 | written: Collections.List[Trees.Expressions.Expression] | |
| 15 | ||
| 16 | // The formal the trailing expressions fill, and negative where | |
| 17 | // every formal has an expression of its own. | |
| 18 | spread_index: int | |
| 19 | ||
| 20 | init(written: Collections.List[Trees.Expressions.Expression], spread_index: int) is | |
| 21 | self.written = written | |
| 22 | self.spread_index = spread_index | |
| 23 | si | |
| 24 | ||
| 25 | plain(written: Collections.List[Trees.Expressions.Expression]) -> ARGUMENT_SLOTS static => | |
| 26 | ARGUMENT_SLOTS(written, -1) | |
| 27 | ||
| 28 | has_spread: bool => spread_index >= 0 | |
| 29 | ||
| 30 | count: int => | |
| 31 | if has_spread then spread_index + 1 else written.count fi | |
| 32 | ||
| 33 | is_spread(index: int) -> bool => | |
| 34 | has_spread /\ index == spread_index | |
| 35 | ||
| 36 | // The expression standing in the slot, and absent for the one the | |
| 37 | // spread fills. | |
| 38 | expression(index: int) -> Trees.Expressions.Expression? => | |
| 39 | if is_spread(index) then null else written[index] fi | |
| 40 | ||
| 41 | // One entry per formal. | |
| 42 | expressions: Collections.List[Trees.Expressions.Expression?] is | |
| 43 | let result = Collections.LIST[Trees.Expressions.Expression?]() | |
| 44 | ||
| 45 | for i in 0..count do | |
| 46 | result.add(expression(i)) | |
| 47 | od | |
| 48 | ||
| 49 | return result | |
| 50 | si | |
| 51 | ||
| 52 | // The expressions the spread collects, in order. | |
| 53 | elements: Collections.List[Trees.Expressions.Expression] is | |
| 54 | let result = Collections.LIST[Trees.Expressions.Expression]() | |
| 55 | ||
| 56 | if has_spread then | |
| 57 | for i in spread_index..written.count do | |
| 58 | result.add(written[i]) | |
| 59 | od | |
| 60 | fi | |
| 61 | ||
| 62 | return result | |
| 63 | si | |
| 64 | ||
| 65 | // Where the slot is written: its expression, or the span from the | |
| 66 | // first spread element to the last. | |
| 67 | location(index: int) -> LOCATION is | |
| 68 | if let expression = expression(index) then | |
| 69 | return expression.location | |
| 70 | fi | |
| 71 | ||
| 72 | let first = written[spread_index].location | |
| 73 | let last = written[written.count - 1].location | |
| 74 | ||
| 75 | return LOCATION(first.file_name, first.start_line, first.start_column, last.end_line, last.end_column) | |
| 76 | si | |
| 77 | si | |
| 78 | si |