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#115 · main · 21 min 41 s
0Étoiles
0Forks
40Issues
| 1 | // Functions passed to C (ADR 0015). |
| 2 | |
| 3 | import std/io |
| 4 | |
| 5 | type Compare = fn(Int, Int) -> Int / <ffi> |
| 6 | |
| 7 | // Accepted: total or `<ffi>` callbacks, also through an alias. |
| 8 | @extern c "ok_sort" |
| 9 | fn sort :: |
| 10 | xs: List<Int> |
| 11 | compare: fn(Int, Int) -> Int / <ffi> |
| 12 | -> List<Int> / <ffi> |
| 13 | |
| 14 | @extern c "ok_apply" |
| 15 | fn apply :: f: fn(Int) -> Int x: Int -> Int |
| 16 | |
| 17 | @extern c "ok_alias" |
| 18 | fn sort_with :: xs: List<Int> compare: Compare -> List<Int> / <ffi> |
| 19 | |
| 20 | // A callback runs inside the C call: it cannot wait for anything. |
| 21 | @extern c "bad_io" |
| 22 | fn each :: |
| 23 | xs: List<Int> |
| 24 | f: fn(Int) -> () / <io> //~ ERROR E0113 may perform `<ffi>` at most |
| 25 | -> () / <io> |
| 26 | |
| 27 | @extern c "bad_open" |
| 28 | fn each_open :: |
| 29 | xs: List<Int> |
| 30 | f: fn(Int) -> () / <ffi | e> //~ ERROR E0113 may perform `<ffi>` at most |
| 31 | -> () / <ffi> |
| 32 | |
| 33 | // A blocking call runs on another thread. |
| 34 | @extern c "bad_blocking" blocking |
| 35 | fn slow_sort :: |
| 36 | xs: List<Int> |
| 37 | compare: fn(Int, Int) -> Int //~ ERROR E0113 cannot take a function |
| 38 | -> List<Int> / <ffi> |
| 39 | |
| 40 | // The extern performs what its callback performs. |
| 41 | @extern c "bad_row" |
| 42 | fn sort_total :: |
| 43 | xs: List<Int> |
| 44 | compare: fn(Int, Int) -> Int / <ffi> //~ ERROR E0113 performs `ffi` too |
| 45 | -> List<Int> |
| 46 | |
| 47 | // Functions reach C only as parameters. |
| 48 | @extern c "bad_nested" |
| 49 | fn first :: |
| 50 | fs: List<fn(Int) -> Int> //~ ERROR E0113 only a parameter itself |
| 51 | -> Int |
| 52 | |
| 53 | pub fn main :: -> () / <io, ffi> |
| 54 | { |
| 55 | io.println (show (sort [2, 1] fn a b { a - b })) |
| 56 | io.println (show (apply fn x { x + 1 } 1)) |
| 57 | io.println (show (sort_with [2, 1] fn a b { a - b })) |
| 58 | } |