Appearance
| 1 | namespace Syntax.Process is | |
| 2 | // Which type a fractional literal's trailing suffix selects. | |
| 3 | enum FloatLiteralKind is | |
| 4 | SINGLE, | |
| 5 | DOUBLE, | |
| 6 | DECIMAL, | |
| 7 | si | |
| 8 | ||
| 9 | // The type suffix of a fractional literal: `s` single, `d` double, | |
| 10 | // `m` decimal, and double where none is written. | |
| 11 | // | |
| 12 | // The lexer leaves the literal exactly as it was written, so an | |
| 13 | // absent suffix is absent here too and every reader has to apply | |
| 14 | // the default itself. Both of them resolve it through this class, | |
| 15 | // so the compile path and the analysis fast path cannot disagree | |
| 16 | // about what `1.5` is. | |
| 17 | // | |
| 18 | // The suffix is always the last character: an exponent's digits | |
| 19 | // follow `e`, so nothing else can end the literal. | |
| 20 | class FLOAT_LITERAL_SUFFIX is | |
| 21 | kind(value_string: string) -> FloatLiteralKind static is | |
| 22 | if value_string.length == 0 then | |
| 23 | return FloatLiteralKind.DOUBLE | |
| 24 | fi | |
| 25 | ||
| 26 | let last = value_string[value_string.length - 1] | |
| 27 | ||
| 28 | if last == 's' \/ last == 'S' then | |
| 29 | return FloatLiteralKind.SINGLE | |
| 30 | elif last == 'm' \/ last == 'M' then | |
| 31 | return FloatLiteralKind.DECIMAL | |
| 32 | fi | |
| 33 | ||
| 34 | return FloatLiteralKind.DOUBLE | |
| 35 | si | |
| 36 | ||
| 37 | has_suffix(value_string: string) -> bool static is | |
| 38 | if value_string.length == 0 then | |
| 39 | return false | |
| 40 | fi | |
| 41 | ||
| 42 | let last = value_string[value_string.length - 1] | |
| 43 | ||
| 44 | return | |
| 45 | last == 's' \/ last == 'S' \/ | |
| 46 | last == 'd' \/ last == 'D' \/ | |
| 47 | last == 'm' \/ last == 'M' | |
| 48 | si | |
| 49 | ||
| 50 | // The literal with its suffix removed, which is the value it | |
| 51 | // denotes and the string every back end should read. | |
| 52 | strip(value_string: string) -> string static => | |
| 53 | if has_suffix(value_string) then | |
| 54 | value_string.substring(0, value_string.length - 1) | |
| 55 | else | |
| 56 | value_string | |
| 57 | fi | |
| 58 | si | |
| 59 | si |