tobogganSe connecter

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.

184 lignes · 4847 octetsBrut · Historique
1// `Int` outside 63 bits becomes a big integer instead of overflowing
2// (ADR 0014). Arithmetic, comparisons, `show`, `hash`, parsing and
3// sending agree on both backends, and results that fit are small again.
4
5import std/array
6import std/bytes
7import std/io
8import std/list
9import std/process.{type Subject, type proc}
10import std/string
11
12fn factorial :: n: Int -> Int
13{
14 if n <= 1
15 {
16 1
17 }
18 else
19 {
20 n * factorial (n - 1)
21 }
22}
23
24fn power :: b: Int e: Int -> Int
25{
26 if e == 0
27 {
28 1
29 }
30 else
31 {
32 b * power b (e - 1)
33 }
34}
35
36pub fn main :: -> () / <io, proc>
37{
38 boundaries ()
39 big_values ()
40 comparisons ()
41 conversions ()
42 sending ()
43}
44
45fn boundaries :: -> () / <io>
46{
47 let max = 4611686018427387903
48 let min = -4611686018427387904
49 io.println
50 ( show (max + 1) <> " " <> show (min - 1) <> " " <> show (max * 2) <> " "
51 <> show (min * -1)
52 <> " "
53 <> show (min / -1)
54 <> " "
55 <> show (-min)
56 )
57 io.println
58 ( show (max + 1 - 1 == max) <> " " <> show (min - 1 + 1 == min) <> " "
59 <> show (max * 2 / 2)
60 <> " "
61 <> show (max + max - max)
62 )
63 // Back in range, a result is small again: it matches a literal.
64 match max * 4 / 4
65 { 4611686018427387903 -> io.println "small again"
66 , _ -> io.println "still big"
67 }
68}
69
70fn big_values :: -> () / <io>
71{
72 io.println (show (factorial 100))
73 let p = power 2 100
74 io.println
75 ( show p <> " " <> show (p % 1000) <> " "
76 <> show (factorial 100 / factorial 98)
77 )
78 io.println
79 ( show (-p / 7) <> " " <> show (-p % 7) <> " " <> show (p * p - p * p + 5)
80 <> " "
81 <> show (p + 3 - p)
82 <> " "
83 <> show (p / p)
84 )
85 io.println
86 ( show (power 2 64) <> " " <> show (power 2 64 - 1) <> " "
87 <> show (-power 2 64)
88 )
89 io.println (show (list.fold (list.range 1 10) 0 fn acc i { acc + i * p }))
90}
91
92fn comparisons :: -> () / <io>
93{
94 let p = power 2 70
95 let q = power 2 35 * power 2 35
96 io.println
97 ( show (p == q) <> " " <> show (p == q + 1) <> " " <> show (p != 5) <> " "
98 <> show (hash p == hash q)
99 <> " "
100 <> show (hash (-p) == hash p)
101 )
102 io.println
103 ( show (p > 5) <> " " <> show (-p < -4611686018427387904) <> " "
104 <> show (p < p + 1)
105 <> " "
106 <> show (-p - 1 < -q)
107 <> " "
108 <> show (p >= q)
109 )
110 io.println (debug [p, -p, 1] <> " " <> show (list.map [p, 7] fn x { x % 10 }))
111}
112
113fn conversions :: -> () / <io>
114{
115 io.println
116 ( show (string.to_int "123456789012345678901234567890") <> " "
117 <> show (string.to_int "-99999999999999999999")
118 <> " "
119 <> show (string.to_int "+5")
120 <> " "
121 <> show (string.to_int "1_000")
122 <> " "
123 <> show (string.to_int "")
124 )
125 // Indices and bounds that are big: out of range, or clamped.
126 let huge = power 2 70
127 let a = array.from_list [1, 2, 3]
128 io.println
129 ( show (array.get a huge) <> " " <> show (array.get a (-huge)) <> " "
130 <> show (array.slice a (-huge) huge)
131 <> " "
132 <> show (bytes.get (bytes.from_string "ab") huge)
133 )
134}
135
136fn doubler :: inbox: Subject<(Int, Subject<Int>)> left: Int -> () / <proc>
137{
138 if left > 0
139 {
140 let (n, reply) = process.receive_forever inbox
141 process.send reply (n * 2)
142 doubler inbox (left - 1)
143 }
144 else
145 {
146 ()
147 }
148}
149
150// Big integers are values like any other: sent, kept, and sent back.
151fn sending :: -> () / <io, proc>
152{
153 let inbox = process.subject ()
154 let reply = process.subject ()
155 let _ =
156 process.spawn
157 fn
158 {
159 let mine = process.subject ()
160 process.send inbox mine
161 doubler mine 2
162 }
163 let worker = process.receive_forever inbox
164 let p = power 3 50
165 process.send worker (p, reply)
166 io.println (show (process.receive_forever reply) <> " " <> show p)
167 process.send worker (power 2 61, reply)
168 io.println (show (process.receive_forever reply))
169}
170//> 4611686018427387904 -4611686018427387905 9223372036854775806 4611686018427387904 4611686018427387904 4611686018427387904
171//> True True 4611686018427387903 4611686018427387903
172//> small again
173//> 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
174//> 1267650600228229401496703205376 376 9900
175//> -181092942889747057356671886482 -2 5 3 1
176//> 18446744073709551616 18446744073709551615 -18446744073709551616
177//> 69720783012552617082318676295680
178//> True False True True False
179//> True True True True True
180//> [1180591620717411303424, -1180591620717411303424, 1] [4, 7]
181//> Ok(123456789012345678901234567890) Ok(-99999999999999999999) Ok(5) Error(not an integer: "1_000") Error(not an integer: "")
182//> None None [1, 2, 3] None
183//> 1435795975383705177540498 717897987691852588770249
184//> 4611686018427387904