-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathCompiler.hs
42 lines (34 loc) · 1.05 KB
/
Compiler.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
{-# LANGUAGE DeriveFunctor #-}
module Compiler
( compile
, format
, typeCheck
, Result(..)
, formatJS
) where
import Data.List.NonEmpty (NonEmpty)
import Debug.Trace (trace)
import Data.Text (Text)
import qualified JavaScriptSyntax as JS
import HaskellSyntax
import TypeChecker
import Wasm
data Result a
= Success a
| ParseErr ParseError'
| CompileErr (NonEmpty CompileError)
deriving (Show, Functor)
typeCheck :: Text -> Result TypedModule
typeCheck code =
case parseModuleWithLineInformation code of
Left parseError -> ParseErr parseError
Right (forestModule, lineInformation) ->
case checkModuleWithLineInformation forestModule (Just lineInformation) of
Left compileError -> CompileErr compileError
Right typedModule -> Success typedModule
compile :: Text -> Result Text
compile code = printWasm . forestModuleToWasm <$> typeCheck code
format :: Text -> Either ParseError' Text
format s = printModule <$> parseModule s
formatJS :: Text -> Either ParseError' Text
formatJS s = JS.printModule <$> parseModule s