-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainass.hs
155 lines (139 loc) · 4.29 KB
/
brainass.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
module Main where
import System.IO (hPutStrLn, stderr)
import System.Environment
import System.Exit
data BFInstr
= Back Int
| Forward Int
| Inc Int
| Dec Int
| Loop
| LoopEnd
| Get
| Put
parseBFInstr :: Char -> [BFInstr]
parseBFInstr '<' = [Back 1]
parseBFInstr '>' = [Forward 1]
parseBFInstr '+' = [Inc 1]
parseBFInstr '-' = [Dec 1]
parseBFInstr '[' = [Loop]
parseBFInstr ']' = [LoopEnd]
parseBFInstr '.' = [Put]
parseBFInstr ',' = [Get]
parseBFInstr _ = []
parseBF :: String -> [BFInstr]
parseBF = concatMap parseBFInstr
optimize :: [BFInstr] -> [BFInstr]
optimize = id
compileHeader :: [[String]] -> [String]
compileHeader [[getP, get], [putP, put]] =
["(module"
,"(func $getc (import \"" ++ getP ++ "\" \"" ++ get ++ "\") (result i32))"
,"(func $putc (import \"" ++ putP ++ "\" \"" ++ put ++ "\") (param i32))"
,"(memory 1)"
,"(global $p (mut i32) (i32.const 0))"
,"(func $main"
]
compileHeader _ = error "expecting get/put and their packages"
compileFooter :: String -> [String]
compileFooter name =
[ ")"
, "(export \"" ++ name ++ "\" (func $main))"
, ")"
]
genInstr :: BFInstr -> [String]
genInstr (Back n) =
[ "get_global $p"
, "i32.const -" ++ show n
, "i32.add"
, "set_global $p"
]
genInstr (Forward n) =
[ "get_global $p"
, "i32.const " ++ show n
, "i32.add"
, "set_global $p"
]
genInstr (Inc n) =
[ "get_global $p"
, "get_global $p"
, "i32.load8_u"
, "i32.const " ++ show n
, "i32.add"
, "i32.store8"
]
genInstr (Dec n) =
[ "get_global $p"
, "get_global $p"
, "i32.load8_u"
, "i32.const -" ++ show n
, "i32.add"
, "i32.store8"
]
genInstr Loop =
[ "(block"
, "(loop"
, "get_global $p"
, "i32.load8_u"
, "i32.const 0"
, "i32.eq"
, "br_if 1"
]
genInstr LoopEnd =
[ "br 0"
, ")"
, ")"
]
genInstr Put =
[ "get_global $p"
, "i32.load8_u"
, "call $putc"
]
genInstr Get =
[ "get_global $p"
, "call $getc"
, "i32.store8"
]
compileBody :: String -> [String]
compileBody instrs = concatMap genInstr $ optimize $ parseBF instrs
compile :: [[String]] -> String -> String
compile imports source = unlines $ concat
[ compileHeader (take 2 imports)
, compileBody source
, compileFooter (head (imports !! 2))
]
wordsWhen :: (Char -> Bool) -> String -> [String]
wordsWhen p s = case dropWhile p s of
"" -> []
s' -> w : wordsWhen p s''
where (w, s'') = break p s'
parseImports :: [String] -> [[String]]
parseImports [input, output, name]
| length splitInput == 2 && length splitOutput == 2 = [splitInput, splitOutput, [name]]
| otherwise = []
where splitInput = wordsWhen (=='.') input
splitOutput = wordsWhen (=='.') output
parseImports _ = []
validateArgs :: [String] -> IO [[String]]
validateArgs args
| not $ null result = return result
| otherwise = usage >> exitSuccess
where result = parseImports args
usage :: IO ()
usage = hPutStrLn stderr $ unlines
[ "Usage: compile IMPORTED_INPUT IMPORTED_OUTPUT EXPORT "
, "where "
, " IMPORTED_INPUT is imported Javascript function for Brainfuck's , "
, " IMPORTED_OUTPUT is imported Javascript function for Brainfuck's . "
, " EXPORT is the exported Javascript function from the WASM module "
, " "
, "Brainfuck source is read fro STDIN and text wasm is written to STDOUT "
, "(similar to `cat`) "
, " "
, "Example: "
, " cat source.bf | compile bf.getchar bf.putchar bf > main.wat "
, " makes wasm module import bf.getchar as \",\" and bf.putchar as \".\""
, " and the wasm module exports a function named bf."
]
main :: IO ()
main = getArgs >>= validateArgs >>= \x -> interact $ compile x