-
Notifications
You must be signed in to change notification settings - Fork 4
/
Main.hs
108 lines (102 loc) · 3.81 KB
/
Main.hs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{-
cli design
witc instance import xxx.wit
witc runtime export xxx.wit
witc check xxx.wit
witc check -- check all wit files in current directory
-}
module Main (main) where
import Control.Monad
import Data.Functor
import Data.List (isSuffixOf)
import Options.Applicative
import Prettyprinter
import Prettyprinter.Render.Terminal
import System.Directory
import Wit
main :: IO ()
main = do
join $
execParser
( info
(helper <*> versionOption <*> programOptions)
( fullDesc
<> progDesc "compiler for wit"
<> header
"witc - compiler for wit, a language for describing wasm interface types"
)
)
where
versionOption :: Parser (a -> a)
versionOption = infoOption "0.2.0" (long "version" <> help "Show version")
programOptions :: Parser (IO ())
programOptions =
subparser
( command
"check"
( info
(check <$> optional (strArgument (metavar "FILE" <> help "Name of the thing to create")))
(progDesc "Validate wit file")
)
<> command
"instance"
( info
( subparser
( command
"import"
( info
( codegen Import Instance
<$> strArgument (metavar "FILE" <> help "Wit file")
<*> strArgument (value "wasmedge" <> help "Name of import")
)
(progDesc "Generate import code for instance (wasm)")
)
<> command
"export"
( info
((\f -> codegen Export Instance f "wasmedge") <$> strArgument (metavar "FILE" <> help "Wit file"))
(progDesc "Generate export code for instance (wasm)")
)
)
)
(progDesc "Generate code for instance (wasm)")
)
<> command
"runtime"
( info
( subparser
( command
"import"
( info
( codegen Import Runtime
<$> strArgument (metavar "FILE" <> help "Wit file")
<*> strArgument (value "wasmedge" <> help "Name of import")
)
(progDesc "Generate import code for runtime (WasmEdge)")
)
<> command
"export"
( info
((\f -> codegen Export Runtime f "wasmedge") <$> strArgument (metavar "FILE" <> help "Wit file"))
(progDesc "Generate export code for runtime (WasmEdge)")
)
)
)
(progDesc "Generate code for runtime (WasmEdge)")
)
)
check :: Maybe FilePath -> IO ()
check (Just file) = checkFileWithDoneHint file
check Nothing = do
dir <- getCurrentDirectory
witFileList <- filter (".wit" `isSuffixOf`) <$> listDirectory dir
mapM_ checkFileWithDoneHint witFileList
checkFileWithDoneHint :: FilePath -> IO ()
checkFileWithDoneHint file = do
checkFile file $> ()
putDoc $ pretty file <+> annotate (color Green) (pretty "OK") <+> line
codegen :: Direction -> Side -> FilePath -> String -> IO ()
codegen d s file importName =
parseFile file
>>= eitherIO check0
>>= eitherIO (putDoc . prettyFile Config {language = Rust, direction = d, side = s} importName)