-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ffi.idr
76 lines (58 loc) · 1.95 KB
/
test_ffi.idr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public export
record UInt where
-- Then we should instance some type class for
-- UInt or other custom primitive types.
-- Note!!! The impl for those primitive types
-- could use foreign functions, which makes it
-- super fast!
constructor MkUInt
i : Int
public export
unsigned : Int -> UInt
unsigned = MkUInt
mutual
public export
data Com_Fn t = MkComFn t
public export
data Com_Raw t = MkComRaw t
public export
data Com_FunTypes : Type -> Type where
Com_Fun : Com s -> Com_FunTypes t -> Com_FunTypes (s -> t)
Com_FnIO : Com t -> Com_FunTypes (CamIO t)
Com_FnBase : Com t -> Com_FunTypes t
public export
data Com : Type -> Type where
ComUnit : Com ()
ComStr : Com String
ComDouble : Com Double -- bit -> type
ComInt : Com Int -- bit -> type
ComUInt : Com UInt -- an example of extensive primitive type impl
ComPtr : Com Ptr
ComFun : Com_FunTypes a -> Com (Com_Fn a)
ComChar : Com Char
ComRaw : Com (Com_Raw a)
public export
data ForeignName
= Builtin String
| Library String String
public export
FFICam : FFI
FFICam = MkFFI Com ForeignName String
public export
CamIO : Type -> Type
CamIO = IO' FFICam
%inline
camCall: (ty : Type) -> (fname : ForeignName) -> {auto fty : FTy FFICam [] ty} -> ty
camCall ty fname = foreign FFICam fname ty
println : String -> CamIO ()
println s = camCall (String -> CamIO ()) (Builtin "println") s
data FileHandler;
openFile : String -> CamIO (Com_Raw FileHandler)
openFile filename = camCall (String -> CamIO (Com_Raw FileHandler)) (Builtin "simple_open") filename
readFile : Com_Raw FileHandler -> CamIO String
readFile handle = camCall (Com_Raw FileHandler -> CamIO String) (Builtin "simple_read") handle
main : CamIO ()
main = do
fh <- openFile "./text.txt"
s <- readFile fh
println s