-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathPrint.hs
More file actions
60 lines (51 loc) · 1.54 KB
/
Print.hs
File metadata and controls
60 lines (51 loc) · 1.54 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
{-# LANGUAGE OverloadedStrings #-}
module Print where
import Data.Char
import Data.Text (Text)
import qualified Data.Text as T
import Text.Printf
import Types
import Parse
camelize :: Text -> Text
camelize = T.concat . map go . T.splitOn "_"
where
go "af" = "AF"
go xs = T.cons (toUpper c) cs
where
c = T.head xs
cs = T.tail xs
isPtr (Type _ x) = x > 0
genBinding :: AST -> Text
genBinding (AST type' name params) =
header <> dumpBody <> dumpOutput
where
dumpOutput | isPtr type' = "IO (" <> printType type' <> ")"
| otherwise = "IO " <> printType type'
header = T.pack $ printf "foreign import ccall unsafe \"%s\"\n %s :: " name name
dumpBody = printTypes params
printTypes :: [Type] -> Text
printTypes [] = mempty
printTypes [x] = printType x <> " -> "
printTypes (x:xs) =
mconcat [
printType x
, " -> "
, printTypes xs
]
printType (Type (Name x) 0) = showType x
printType (Type (Name x) 1) = "Ptr " <> showType x
printType (Type t n) = "Ptr (" <> printType (Type t (n-1)) <> ")"
-- | Additional mappings, very important for CodeGen
showType :: Text -> Text
showType "char" = "CChar"
showType "void" = "()"
showType "unsigned" = "CUInt"
showType "dim_t" = "DimT"
showType "af_someenum_t" = "AFSomeEnum"
showType "size_t" = "CSize"
showType "uintl" = "UIntL"
showType "intl" = "IntL"
showType "af_index_t" = "AFIndex"
showType "af_cspace_t" = "AFCSpace"
showType "afcl_platform" = "AFCLPlatform"
showType x = camelize x