-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintfLeading.idr
More file actions
71 lines (52 loc) · 2.33 KB
/
printfLeading.idr
File metadata and controls
71 lines (52 loc) · 2.33 KB
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
-- Author: Mukesh Tiwari
-- https://github.com/mukeshtiwari/Idris/blob/master/Printf.idr
import Data.String
%default total
data Format = FInt Format
| FLeadZero Nat Format
| FString Format
| FOther Char Format
| FEnd
parseNumWithoutSign : List Char -> Nat -> Maybe Nat
parseNumWithoutSign [] acc = Just acc
parseNumWithoutSign (c :: cs) acc =
if (c >= '0' && c <= '9')
then parseNumWithoutSign cs ((acc * 10) + (cast ((ord c) - (ord '0'))))
else Nothing
mutual
formatHelper : List Char -> List Char -> List Char -> Format
formatHelper s ('d' :: l) orig with (parseNumWithoutSign (s) 0)
| Just a = FLeadZero a (format l)
| Nothing = FOther '%' (FOther '0' (format orig))
formatHelper [] ('d' :: l) orig = FInt (format l)
formatHelper s (c :: l) orig with ('0' <= c && '9' >= c)
| True = formatHelper (s ++ (unpack $ singleton c)) l orig
| False = FOther '%' (FOther '0' (format orig))
formatHelper s [] orig = FOther '%' (FOther '0' (format orig))
format : List Char -> Format
format ('%' :: 'd' :: cs ) = FInt ( format cs )
format ('%' :: '0' :: s) = formatHelper (unpack "") s s
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 ( FLeadZero n f) = Int -> interpFormat f
interpFormat ( FString f ) = String -> interpFormat f
interpFormat ( FOther _ f ) = interpFormat f
interpFormat FEnd = String
formatString : String -> Format
formatString s = format ( unpack s )
genZeros : Nat -> String
genZeros Z = ""
genZeros (S n) = "0" ++ genZeros n
toFunction : ( fmt : Format ) -> String -> interpFormat fmt
toFunction ( FInt f ) a = \i => toFunction f ( a ++ show i )
toFunction ( FLeadZero n f) a = \i => toFunction f (a ++ genZeros (minus n (length (show i))) ++ (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
sprintf : ( s : String ) -> interpFormat ( formatString s )
sprintf s = toFunction ( formatString s ) ""
--main : IO ()
--main = putStrLn (sprintf "String parameter: %s, integer parameter: %d" "alpha" (10+23))