-
Notifications
You must be signed in to change notification settings - Fork 2
/
Printf.idr
34 lines (25 loc) · 1.01 KB
/
Printf.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
module Printf
%default total
data Format = FInt Format
| FString Format
| FOther Char Format
| FEnd
format : List Char -> Format
format ('%' :: 'd' :: cs ) = FInt ( format cs )
format ('%' :: 's' :: cs ) = FString ( format cs )
format ( c :: cs ) = FOther c ( format cs )
format [] = FEnd
interpFormat : Format -> Type
interpFormat ( FInt f ) = Int -> interpFormat f
interpFormat ( FString f ) = String -> interpFormat f
interpFormat ( FOther _ f ) = interpFormat f
interpFormat End = String
formatString : String -> Format
formatString s = format ( unpack s )
toFunction : ( fmt : Format ) -> String -> interpFormat fmt
toFunction ( FInt f ) a = \i => toFunction f ( a ++ show i )
toFunction ( FString f ) a = \s => toFunction f ( a ++ s )
toFunction ( FOther c f ) a = toFunction f ( a ++ singleton c )
toFunction FEnd a = a
printf : ( s : String ) -> interpFormat ( formatString s )
printf s = toFunction ( formatString s ) ""