louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Integer comparisons compile to a direct compare-and-branch when the |
| 2 | // operands are provably small, with a fast path and the general code |
| 3 | // (big integers) behind it otherwise (issue #191): every operator, on |
| 4 | // small, negative, boundary and big values, agrees on both backends. |
| 5 | |
| 6 | import std/io |
| 7 | import std/list |
| 8 | |
| 9 | fn power :: b: Int e: Int -> Int |
| 10 | { |
| 11 | if e == 0 |
| 12 | { |
| 13 | 1 |
| 14 | } |
| 15 | else |
| 16 | { |
| 17 | b * power b (e - 1) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | // `n`'s callers pass small and big values alike: nothing is proven, so |
| 22 | // both the fast path and the general one run. |
| 23 | fn classify :: n: Int -> String |
| 24 | { |
| 25 | if n < 0 |
| 26 | { |
| 27 | "neg" |
| 28 | } |
| 29 | else |
| 30 | { |
| 31 | if n == 0 |
| 32 | { |
| 33 | "zero" |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | if n <= 4611686018427387903 |
| 38 | { |
| 39 | "small" |
| 40 | } |
| 41 | else |
| 42 | { |
| 43 | "big" |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn compare_all :: x: Int y: Int -> String |
| 50 | { |
| 51 | show (x < y) <> " " <> show (x <= y) <> " " <> show (x == y) <> " " |
| 52 | <> show (x != y) |
| 53 | <> " " |
| 54 | <> show (x >= y) |
| 55 | <> " " |
| 56 | <> show (x > y) |
| 57 | } |
| 58 | |
| 59 | pub fn main :: -> () / <io> |
| 60 | { |
| 61 | let max = 4611686018427387903 |
| 62 | let min = -4611686018427387904 |
| 63 | let big = power 2 70 |
| 64 | list.each |
| 65 | [0, 1, -1, 42, -42, max, min, max + 1, min - 1, big, -big] |
| 66 | fn x { io.println (classify x) } |
| 67 | // Boundary pairs: equal, one apart, straddling the small range. |
| 68 | list.each |
| 69 | [ (0, 0) |
| 70 | , (max, max) |
| 71 | , (max, max + 1) |
| 72 | , (max + 1, max) |
| 73 | , (min, min - 1) |
| 74 | , (min - 1, min) |
| 75 | , (big, big) |
| 76 | , (-big, big) |
| 77 | , (-1, 1) |
| 78 | ] |
| 79 | fn p { io.println (compare_all p.0 p.1) } |
| 80 | // A comparison's result matched on, and one in a loop's guard. |
| 81 | match big - big == 0 |
| 82 | { True -> io.println "difference is small", False -> io.println "no" } |
| 83 | io.println (show (count_by 0 10)) |
| 84 | } |
| 85 | |
| 86 | fn count_by :: i: Int n: Int -> Int |
| 87 | { |
| 88 | if i <= n |
| 89 | { |
| 90 | count_by (i + 3) n |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | i |
| 95 | } |
| 96 | } |
| 97 | //> zero |
| 98 | //> small |
| 99 | //> neg |
| 100 | //> small |
| 101 | //> neg |
| 102 | //> small |
| 103 | //> neg |
| 104 | //> big |
| 105 | //> neg |
| 106 | //> big |
| 107 | //> neg |
| 108 | //> False True True False True False |
| 109 | //> False True True False True False |
| 110 | //> True True False True False False |
| 111 | //> False False False True True True |
| 112 | //> False False False True True True |
| 113 | //> True True False True False False |
| 114 | //> False True True False True False |
| 115 | //> True True False True False False |
| 116 | //> True True False True False False |
| 117 | //> difference is small |
| 118 | //> 12 |