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.

94 lignes · 1899 octetsBrut · Historique
1// Unboxed-int workers (ADR 0045, Revision 2): an Int worker computes on
2// raw int64s and bails to the boxed wrapper — which recomputes the call
3// with big-integer promotion — when a result leaves the 63-bit small
4// range. Both back ends must agree, whether the worker bails or not.
5
6import std/io
7
8fn fact :: n: Int -> Int
9{
10 if n <= 1
11 {
12 1
13 }
14 else
15 {
16 n * fact (n - 1)
17 }
18}
19
20fn power :: b: Int e: Int -> Int
21{
22 if e == 0
23 {
24 1
25 }
26 else
27 {
28 b * power b (e - 1)
29 }
30}
31
32// Tail-recursive: the same bail from a loop-shaped worker.
33fn muls :: n: Int acc: Int -> Int
34{
35 if n == 0
36 {
37 acc
38 }
39 else
40 {
41 muls (n - 1) (acc * 2 + 1)
42 }
43}
44
45// A big-integer argument takes the boxed call at the boundary.
46fn bump :: n: Int -> Int
47{
48 n + 1
49}
50
51// -2^62 / -1 leaves the small range: the worker's division bails and the
52// recompute promotes (and the promoted argument re-enters boxed).
53fn steps :: n: Int -> Int
54{
55 if n >= 0
56 {
57 n
58 }
59 else
60 {
61 steps (n / (0 - 1))
62 }
63}
64
65// Remainder and division in a worker, on a promoted (big) value too.
66fn digits :: n: Int -> Int
67{
68 if n < 10
69 {
70 n
71 }
72 else
73 {
74 n % 10 + digits (n / 10)
75 }
76}
77
78pub fn main :: -> () / <io>
79{
80 // fact(20) fits a small int (no bail); fact(25) promotes mid-recursion.
81 io.println (show (fact 20) <> " " <> show (fact 25))
82 io.println (show (power 2 63) <> " " <> show (power (0 - 2) 63))
83 io.println (show (muls 70 0) <> " " <> show (muls 40 0))
84 io.println (show (bump 41) <> " " <> show (bump (power 2 70)))
85 io.println
86 (show (steps 5) <> " " <> show (steps (0 - 4611686018427387903 - 1)))
87 io.println (show (digits (fact 30)) <> " " <> show (digits 998877))
88}
89//> 2432902008176640000 15511210043330985984000000
90//> 9223372036854775808 -9223372036854775808
91//> 1180591620717411303423 1099511627775
92//> 42 1180591620717411303425
93//> 5 4611686018427387904
94//> 117 48