Skip to content

Commit 824778a

Browse files
committed
Update javascript printer for changed to Language
1 parent fefd99a commit 824778a

File tree

3 files changed

+69
-30
lines changed

3 files changed

+69
-30
lines changed

app/Main.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ main = do
7171
Left err ->
7272
(TIO.hPutStrLn stderr $ reportParseError filename err) >>
7373
exitWith (ExitFailure 1)
74-
["format", filename] -> do
74+
["format-js", filename] -> do
7575
contents <- TIO.readFile filename
7676
case formatJS contents of
7777
Right formattedCode ->

src/Compiler.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ compile code = printWasm . forestModuleToWasm <$> typeCheck code
3838
format :: Text -> Either ParseError' Text
3939
format s = printModule <$> parseModule s
4040

41-
formatJS :: String -> Either ParseError' String
41+
formatJS :: Text -> Either ParseError' Text
4242
formatJS s = JS.printModule <$> parseModule s

src/JavaScriptSyntax.hs

+67-28
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,101 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
13
module JavaScriptSyntax
24
( printModule
35
) where
46

57
import Data.List (intercalate)
8+
import qualified Data.List.NonEmpty as NE
69
import Data.List.NonEmpty (toList)
10+
import Data.Semigroup
11+
import Data.Text (Text)
12+
import qualified Data.Text as T
713
import Language
814

9-
indent :: Int -> String -> String
15+
showT :: Show a => a -> Text
16+
showT = T.pack . show
17+
18+
indent :: Int -> Text -> Text
1019
indent level str =
11-
intercalate "\n" $ map (\line -> replicate level ' ' ++ line) (lines str)
20+
T.intercalate "\n" $
21+
map (\line -> T.replicate level " " <> line) (T.lines str)
1222

13-
indent2 :: String -> String
23+
indent2 :: Text -> Text
1424
indent2 = indent 2
1525

16-
printModule :: Module -> String
26+
printModule :: Module -> Text
1727
printModule (Module topLevels) =
18-
(intercalate "\n\n" $ map printTopLevel topLevels) ++ "\n"
28+
(T.intercalate "\n\n" $ map printTopLevel topLevels) <> "\n"
1929

20-
printTopLevel :: TopLevel -> String
30+
printTopLevel :: TopLevel -> Text
2131
printTopLevel topLevel =
2232
case topLevel of
2333
Function declaration -> printDeclaration declaration
24-
DataType _ -> undefined
34+
DataType (ADT name generics ctors) ->
35+
"type " <> s name <> printedGenerics <> " =" <>
36+
indent 2 printedCtors
37+
where printedGenerics =
38+
case generics of
39+
[] -> ""
40+
_ -> "<" <> T.intercalate ", " (s <$> generics) <> ">"
41+
printedCtors = T.intercalate " | " (printCtor <$> (NE.toList ctors))
42+
printCtor (Constructor name maybeType) =
43+
s name <> " " <> maybe "" printConstructorType maybeType
44+
printConstructorType ctorType =
45+
case ctorType of
46+
CTConcrete i -> s i
47+
CTApplied a b ->
48+
printConstructorType a <> " " <> printConstructorType b
49+
CTParenthesized ct -> parens (printConstructorType ct)
2550

26-
printDeclaration :: Declaration -> String
51+
printDeclaration :: Declaration -> Text
2752
printDeclaration (Declaration _ name args expression) =
28-
"function " ++
29-
s name ++
30-
printedArgs ++ " {\n return " ++ printExpression expression ++ "\n}"
53+
"function " <> s name <> printedArgs <> " {\n return " <>
54+
printExpression expression <>
55+
"\n}"
3156
where
32-
printedArgs = parens $ intercalate ", " $ map s args
57+
printedArgs = parens $ T.intercalate ", " $ map printArgument args
3358

34-
printExpression :: Expression -> String
59+
printExpression :: Expression -> Text
3560
printExpression expression =
3661
case expression of
37-
Number number -> show number
62+
Number number -> showT number
63+
Float f -> showT f
3864
Identifier identifier -> s identifier
3965
Infix operator a b ->
40-
intercalate
66+
T.intercalate
4167
" "
4268
[printExpression a, printOperator operator, printExpression b]
43-
String' string -> show string
44-
Call name args ->
45-
s name ++ parens (intercalate ", " (map printExpression args))
69+
String' string -> showT string
70+
Apply a b -> printExpression a <> parens (printExpression b)
4671
BetweenParens expression -> parens $ printExpression expression
4772
Case expression branches ->
48-
"switch " ++
49-
parens (printExpression expression) ++
50-
" {\n" ++ indent 4 (printBranches branches) ++ "\n }"
51-
_ -> error $ "not implemented " ++ show expression
73+
"switch " <> parens (printExpression expression) <> " {\n" <>
74+
indent 4 (printBranches branches) <>
75+
"\n }"
76+
Let declarations expr ->
77+
indent
78+
2
79+
(T.intercalate
80+
"\n"
81+
((printDeclaration <$>
82+
(NE.toList declarations)) <> [printExpression expr]))
5283
where
5384
printBranches branches =
54-
intercalate "\n" $ toList $ fmap printBranch branches
85+
T.intercalate "\n" $ toList $ fmap printBranch branches
5586
printBranch (condition, body) =
56-
"case " ++
57-
printExpression condition ++ ":\n" ++ indent2 (printExpression body)
87+
"case " <> printArgument condition <> ":\n" <>
88+
indent2 (printExpression body)
89+
90+
printArgument :: Argument -> Text
91+
printArgument a =
92+
case a of
93+
AIdentifier n -> s n
94+
ADeconstruction name args ->
95+
s name <> parens (T.intercalate ", " (printArgument <$> args))
96+
ANumberLiteral i -> showT i
5897

59-
printOperator :: OperatorExpr -> String
98+
printOperator :: OperatorExpr -> Text
6099
printOperator operator =
61100
case operator of
62101
Add -> "+"
@@ -65,5 +104,5 @@ printOperator operator =
65104
Multiply -> "*"
66105
StringAdd -> "++"
67106

68-
parens :: String -> String
69-
parens s = "(" ++ s ++ ")"
107+
parens :: Text -> Text
108+
parens s = "(" <> s <> ")"

0 commit comments

Comments
 (0)