@@ -15,7 +15,6 @@ import Control.Arrow ((<+>))
15import Control.Monad (forM, mzero)
15import Control.Monad (forM, mzero)
16import Control.Monad.State (StateT, evalStateT)
16import Control.Monad.State (StateT, evalStateT)
17import Control.PatternArrows
17import Control.PatternArrows
18−import Data.Char (isAscii)
19import Data.List (filter, (\\))
18import Data.List (filter, (\\))
20import qualified Control.Arrow as A
19import qualified Control.Arrow as A
@@ -49,26 +48,23 @@ literals = mkPattern' match'
49 match (BooleanLiteral _ True) = return $ emit "true"
48 match (BooleanLiteral _ True) = return $ emit "true"
50 match (BooleanLiteral _ False) = return $ emit "false"
49 match (BooleanLiteral _ False) = return $ emit "false"
51 match (ArrayLiteral _ xs) = mconcat <$> sequence
50 match (ArrayLiteral _ xs) = mconcat <$> sequence
52− [ return . emit $ arrayType <> "{ "
51+ [ return . emit $ arrayType <> "{"
53 , intercalate (emit ", ") <$> forM xs prettyPrintIL'
52 , intercalate (emit ", ") <$> forM xs prettyPrintIL'
54− , return $ emit " }"
53+ , return $ emit "}"
56 -- match (ObjectLiteral _ []) = return $ emit "std::initializer_list<std::pair<const string, boxed>>{}"
55 -- match (ObjectLiteral _ []) = return $ emit "std::initializer_list<std::pair<const string, boxed>>{}"
57 match (ObjectLiteral _ ps) = mconcat <$> sequence
56 match (ObjectLiteral _ ps) = mconcat <$> sequence
58− [ return . emit $ dictType <> "{\n"
57+ [ return . emit $ dictType <> "{"
59 , withIndent $ do
58 , withIndent $ do
60 ils <- forM ps $ \(key, value) -> do
59 ils <- forM ps $ \(key, value) -> do
61 value' <- prettyPrintIL' value
60 value' <- prettyPrintIL' value
62− return $ emit "{ " <> objectPropertyToString key <> value' <> emit " }"
61+ return $ objectPropertyToString key <> value'
63− indentString <- currentIndent
64− return $ intercalate (emit ",\n") $ map (indentString <>) ils
62+ return $ intercalate (emit " ") $ map (<> emit ",") ils
67 , return $ emit "}"
63 , return $ emit "}"
70 objectPropertyToString :: (Emit gen) => PSString -> gen
66 objectPropertyToString :: (Emit gen) => PSString -> gen
71− objectPropertyToString s = emit $ stringLiteral s <> ", "
67+ objectPropertyToString s = emit $ stringLiteral s <> ": "
72 match (Block _ sts) = mconcat <$> sequence
68 match (Block _ sts) = mconcat <$> sequence
73 [ return $ emit "{\n"
69 [ return $ emit "{\n"
74 , withIndent $ prettyStatements sts
70 , withIndent $ prettyStatements sts
@@ -78,7 +74,7 @@ literals = mkPattern' match'
79 match (Var _ ident) = return $ emit ident
75 match (Var _ ident) = return $ emit ident
80 match (VariableIntroduction _ ident value) = mconcat <$> sequence
76 match (VariableIntroduction _ ident value) = mconcat <$> sequence
81− [ return $ emit $ "boxed " <> ident
77+ [ return . emit $ varDecl <> " " <> ident <> " " <> anyType
82 , maybe (return mempty) (fmap (emit " = " <>) . prettyPrintIL') value
78 , maybe (return mempty) (fmap (emit " = " <>) . prettyPrintIL') value
84 match (Assignment _ target value) = mconcat <$> sequence
80 match (Assignment _ target value) = mconcat <$> sequence
@@ -87,13 +83,20 @@ literals = mkPattern' match'
87 , prettyPrintIL' value
83 , prettyPrintIL' value
89 match (App _ val []) = mconcat <$> sequence
85 match (App _ val []) = mconcat <$> sequence
86+ [ return $ emit "EffApply("
90− [ prettyPrintIL' val
87+ , prettyPrintIL' val
91− , return $ emit "()"
88+ , return $ emit ")"
90+ match (App _ (Var _ fn) [arg]) | fn == arrayLengthFn = mconcat <$> sequence
91+ [ return $ emit arrayLengthFn
93− match (App ss (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [x]) [y])
96+ match (App ss (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod), x, y])
94 | fnMod == dictMod
97 | fnMod == dictMod
95 , dictMod == C.dataEq || dictMod == C.dataSemiring || dictMod == C.dataRing ||
98 , dictMod == C.dataEq || dictMod == C.dataSemiring || dictMod == C.dataRing ||
96− dictMod == C.dataEuclideanRing || dictMod == C.dataHeytingAlgebra
99+ dictMod == C.dataEuclideanRing || dictMod == C.dataHeytingAlgebra || dictMod == C.dataOrd
97 , Just op <- renderOp fn
100 , Just op <- renderOp fn
98 , Just t <- unboxType dict
101 , Just t <- unboxType dict
99 = mconcat <$> sequence
102 = mconcat <$> sequence
@@ -101,130 +104,151 @@ literals = mkPattern' match'
101 , return $ emit op
104 , return $ emit op
102 , return $ emit $ unbox' t y
105 , return $ emit $ unbox' t y
104− match (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [n])
107+ match (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod), n])
105 | fnMod == dictMod
108 | fnMod == dictMod
106 , dictMod == C.dataRing
109 , dictMod == C.dataRing
107 , fn == C.negate
110 , fn == C.negate
108 , Just t <- unboxType dict
111 , Just t <- unboxType dict
109 = mconcat <$> sequence
112 = mconcat <$> sequence
110− [ return $ emit "-"
113+ [ return $ emit "-("
111 , return $ emit $ unbox' t n
114 , return $ emit $ unbox' t n
113− match (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [n])
117+ match (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod), n])
114 | fnMod == dictMod
118 | fnMod == dictMod
115 , dictMod == C.dataHeytingAlgebra
119 , dictMod == C.dataHeytingAlgebra
116 , fn == properToIL C.not
120 , fn == properToIL C.not
117 , Just t <- unboxType dict
121 , Just t <- unboxType dict
118 = mconcat <$> sequence
122 = mconcat <$> sequence
119− [ return $ emit "!"
123+ [ return $ emit "!("
120 , return $ emit $ unbox' t n
124 , return $ emit $ unbox' t n
122 match (App _ val args) = mconcat <$> sequence
127 match (App _ val args) = mconcat <$> sequence
128+ [ return $ emit "Apply("
123− [ prettyPrintIL' val
129+ , prettyPrintIL' val
124− , return $ emit "("
130+ , return $ emit ", "
125 , intercalate (emit ", ") <$> forM args prettyPrintIL'
131 , intercalate (emit ", ") <$> forM args prettyPrintIL'
126 , return $ emit ")"
132 , return $ emit ")"
128 match (Function _ (Just name) [] (Block _ [Return _ ret]))
134 match (Function _ (Just name) [] (Block _ [Return _ ret]))
129− | isLiteral ret = mconcat <$> sequence
135+ | isComplexLiteral ret = mconcat <$> sequence
130− [ return $ emit "auto "
136+ [ return $ emit "\n"
137+ , return . emit $ varDecl <> " " <> initName name <> " Once\n"
138+ , prettyPrintIL' $ VariableIntroduction Nothing (valueName name) Nothing
139+ , return $ emit "\n\n"
131− , return $ emit name
140+ , return $ emit "func "
141+ , return . emit $ withPrefix name
132− , return $ emit "() -> const boxed& "
142+ , return . emit $ "() " <> anyType <> " "
133 , return $ emit "{\n"
143 , return $ emit "{\n"
134 , withIndent $ do
144 , withIndent $ do
135− indentString <- currentIndent
145+ indentString <- currentIndent
136− return $ indentString <> emit staticValueBegin
146+ return $ indentString <> (emit $ initName name <> ".Do(func() {\n") <>
147+ indentString <> indentString <> (emit $ valueName name <> " = ")
137 , withIndent $ do
148 , withIndent $ do
138 case ret of
149 case ret of
139 Function _ Nothing args body -> mconcat <$> sequence
150 Function _ Nothing args body -> mconcat <$> sequence
140− [ return $ emit "[]("
151+ [ return $ emit "func("
141− , return $ emit $ intercalate ", " (renderArg <$> args)
152+ , return . emit $ intercalate ", " (renderArg anyType <$> args)
142− , return $ emit ") -> boxed "
153+ , return . emit $ ") " <> anyType <> " "
143 , prettyPrintIL' body
154 , prettyPrintIL' body
146 prettyPrintIL' ret
157 prettyPrintIL' ret
147− , return $ emit ";\n"
158+ , return $ emit "\n"
148 , withIndent $ do
159 , withIndent $ do
149− indentString <- currentIndent
160+ indentString <- currentIndent
150− return $ indentString <> emit staticValueEnd
161+ return $ indentString <> (emit $ "})\n" <> "return " <> valueName name <>"\n")
151− , return $ emit "\n}"
162+ , return $ emit "}\n\n"
153 match (Function _ (Just name) [] ret) = mconcat <$> sequence
164 match (Function _ (Just name) [] ret) = mconcat <$> sequence
154− [ return $ emit "auto "
165+ [ return $ emit "func "
155− , return $ emit name
166+ , return . emit $ withPrefix name
156− , return $ emit "() -> boxed "
167+ , return . emit $ "() " <> anyType <> " "
157 , prettyPrintIL' ret
168 , prettyPrintIL' ret
159− match (Function _ name args ret) = mconcat <$> sequence
170+ match (Function _ _ args ret) = mconcat <$> sequence
160− [ return $ emit captures
171+ [ return $ emit "func("
162− , return $ emit $ intercalate ", " (render <$> args)
172+ , return . emit $ intercalate ", " (renderArg anyType <$> args)
163− , return $ emit ") -> boxed "
173+ , return . emit $ ") " <> anyType <> " "
164 , prettyPrintIL' ret
174 , prettyPrintIL' ret
166− where
176+ match (Indexer _ (Var _ name) (Var _ "")) = mconcat <$> sequence
167− (captures, render)
177+ [ prettyPrintIL' (Var Nothing $ withPrefix name)
168− | name == Just tcoLoop = ("[&]", renderArgByVal)
178+ , return $ emit "()"
169− | otherwise = ("[=]", renderArg)
179+ ]
170− match (Indexer _ prop@(Var _ name) val) = mconcat <$> sequence
180+ match (Indexer _ (Var _ name) val) = mconcat <$> sequence
171 [ prettyPrintIL' val
181 [ prettyPrintIL' val
172− , return $ emit "::"
182+ , return $ emit "."
173− , prettyPrintIL' prop
183+ , prettyPrintIL' (Var Nothing $ withPrefix name)
174 , return $ emit "()"
184 , return $ emit "()"
176 match (Indexer _ prop val) = mconcat <$> sequence
186 match (Indexer _ prop val) = mconcat <$> sequence
187+ [ return $ emit "Get("
177− [ prettyPrintIL' val
188+ , prettyPrintIL' val
178− , return $ emit "["
189+ , return $ emit ", "
179 , prettyPrintIL' prop
190 , prettyPrintIL' prop
180− , return $ emit "]"
191+ , return $ emit ")"
182 match (InstanceOf _ val ty) = mconcat <$> sequence
193 match (InstanceOf _ val ty) = mconcat <$> sequence
183− [ return . emit $ unbox' dictType val
194+ [ return $ emit "Contains("
195+ , prettyPrintIL' val
184− , return $ emit ".contains("
196+ , return $ emit ", "
185 , prettyPrintIL' ty
197 , prettyPrintIL' ty
186 , return $ emit ")"
198 , return $ emit ")"
188 match (While _ cond sts) = mconcat <$> sequence
200 match (While _ cond sts) = mconcat <$> sequence
189− [ return $ emit "while ("
201+ [ return $ emit "for "
190 , prettyPrintIL' cond
202 , prettyPrintIL' cond
191− , return $ emit ") "
203+ , return $ emit " "
192 , prettyPrintIL' sts
204 , prettyPrintIL' sts
194 match (For _ ident start end sts) = mconcat <$> sequence
206 match (For _ ident start end sts) = mconcat <$> sequence
195− [ return $ emit $ "for (boxed " <> ident <> " = "
207+ [ return $ emit $ "for var " <> ident <> " = "
196 , prettyPrintIL' start
208 , prettyPrintIL' start
197 , return $ emit $ "; " <> ident <> " < "
209 , return $ emit $ "; " <> ident <> " < "
198 , prettyPrintIL' end
210 , prettyPrintIL' end
199− , return $ emit $ "; " <> ident <> "++) "
211+ , return $ emit $ "; " <> ident <> "++ "
200 , prettyPrintIL' sts
212 , prettyPrintIL' sts
202 match (ForIn _ ident obj sts) = mconcat <$> sequence
214 match (ForIn _ ident obj sts) = mconcat <$> sequence
203− [ return $ emit $ "for (boxed " <> ident <> " in "
215+ [ return $ emit $ "for var " <> ident <> " in "
204 , prettyPrintIL' obj
216 , prettyPrintIL' obj
205− , return $ emit ") "
217+ , return $ emit " "
206 , prettyPrintIL' sts
218 , prettyPrintIL' sts
220+ match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing True)) thens elses)
221+ | (App ss (Indexer _ (Var _ _) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod), x, y]) <- cond
223+ , dictMod == C.dataEq || dictMod == C.dataOrd
224+ , Just _ <- unboxTypeInt dict
225+ = mconcat <$> sequence
226+ [ return $ emit "if "
227+ , prettyPrintIL' cond
229+ , prettyPrintIL' thens
230+ , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
208 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing True)) thens elses) = mconcat <$> sequence
232 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing True)) thens elses) = mconcat <$> sequence
209− [ return $ emit "if ("
233+ [ return $ emit "if "
210 , return $ emit $ unbox' "bool" cond
234 , return $ emit $ unbox' "bool" cond
211− , return $ emit ") "
235+ , return $ emit " "
212 , prettyPrintIL' thens
236 , prettyPrintIL' thens
213 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
237 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
215 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing False)) thens elses) = mconcat <$> sequence
239 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing False)) thens elses) = mconcat <$> sequence
216− [ return $ emit "if (!("
240+ [ return $ emit "if !("
217 , return $ emit $ unbox' "bool" cond
241 , return $ emit $ unbox' "bool" cond
218− , return $ emit ")) "
242+ , return $ emit ") "
219 , prettyPrintIL' thens
243 , prettyPrintIL' thens
220 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
244 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
222 match (IfElse _ (Binary _ EqualTo x y@(NumericLiteral _ n)) thens elses) = mconcat <$> sequence
246 match (IfElse _ (Binary _ EqualTo x y@(NumericLiteral _ n)) thens elses) = mconcat <$> sequence
223− [ return $ emit "if ("
247+ [ return $ emit "if "
224 , return $ emit $ unbox' t x
248 , return $ emit $ unbox' t x
225 , return $ emit $ " == "
249 , return $ emit $ " == "
226 , prettyPrintIL' y
250 , prettyPrintIL' y
227− , return $ emit ") "
251+ , return $ emit " "
228 , prettyPrintIL' thens
252 , prettyPrintIL' thens
229 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
253 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
@@ -232,18 +256,18 @@ literals = mkPattern' match'
232 t | Left _ <- n = int
256 t | Left _ <- n = int
233 | Right _ <- n = float
257 | Right _ <- n = float
234 match (IfElse _ (Binary _ EqualTo a b@StringLiteral{}) thens elses) = mconcat <$> sequence
258 match (IfElse _ (Binary _ EqualTo a b@StringLiteral{}) thens elses) = mconcat <$> sequence
235− [ return $ emit "if ("
259+ [ return $ emit "if "
236 , return $ emit $ unbox' string a
260 , return $ emit $ unbox' string a
237 , return $ emit $ " == "
261 , return $ emit $ " == "
238 , prettyPrintIL' b
262 , prettyPrintIL' b
239− , return $ emit ") "
263+ , return $ emit " "
240 , prettyPrintIL' thens
264 , prettyPrintIL' thens
241 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
265 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
243 match (IfElse _ cond thens elses) = mconcat <$> sequence
267 match (IfElse _ cond thens elses) = mconcat <$> sequence
244− [ return $ emit "if ("
268+ [ return $ emit "if "
245 , prettyPrintIL' cond
269 , prettyPrintIL' cond
246− , return $ emit ") "
270+ , return $ emit " "
247 , prettyPrintIL' thens
271 , prettyPrintIL' thens
248 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
272 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintIL') elses
@@ -251,11 +275,12 @@ literals = mkPattern' match'
251 [ return $ emit "return "
275 [ return $ emit "return "
252 , prettyPrintIL' value
276 , prettyPrintIL' value
254− match (ReturnNoResult _) = return $ emit "return undefined"
278+ match (ReturnNoResult _) = return . emit $ "return " <> undefinedName
255 -- match (Throw _ _) = return mempty
279 -- match (Throw _ _) = return mempty
256 match (Throw _ value) = mconcat <$> sequence
280 match (Throw _ value) = mconcat <$> sequence
257− [ return $ emit "THROW_("
281+ [ return $ emit "panic("
258 , return $ emit "\"PatternMatchFailure: \""
282 , return $ emit "\"PatternMatchFailure: \""
283+ , return $ emit " + "
259 , prettyPrintIL' value
284 , prettyPrintIL' value
260 , return $ emit ")"
285 , return $ emit ")"
@@ -330,11 +355,17 @@ isLiteral ObjectLiteral{} = True
330isLiteral ArrayLiteral{} = True
355isLiteral ArrayLiteral{} = True
331isLiteral _ = False
356isLiteral _ = False
358+isComplexLiteral :: AST -> Bool
359+-- isComplexLiteral Function{} = True
360+isComplexLiteral ObjectLiteral{} = True
361+isComplexLiteral ArrayLiteral{} = True
362+isComplexLiteral _ = False
333prettyStatements :: (Emit gen) => [AST] -> StateT PrinterState Maybe gen
364prettyStatements :: (Emit gen) => [AST] -> StateT PrinterState Maybe gen
334prettyStatements sts = do
365prettyStatements sts = do
335 ils <- forM sts prettyPrintIL'
366 ils <- forM sts prettyPrintIL'
336 indentString <- currentIndent
367 indentString <- currentIndent
337− return $ intercalate (emit "\n") $ map ((<> emit ";") . (indentString <>)) ils
368+ return $ intercalate (emit "\n") $ map (indentString <>) ils
339-- | Generate a pretty-printed string representing a collection of C++ expressions at the same indentation level
370-- | Generate a pretty-printed string representing a collection of C++ expressions at the same indentation level
340prettyPrintILWithSourceMaps :: [AST] -> (Text, [SMap])
371prettyPrintILWithSourceMaps :: [AST] -> (Text, [SMap])
@@ -383,15 +414,12 @@ prettyPrintIL1 :: AST -> Text
383prettyPrintIL1 = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyPrintIL'
414prettyPrintIL1 = maybe (internalError "Incomplete pattern") runPlainString . flip evalStateT (PrinterState 0) . prettyPrintIL'
385stringLiteral :: PSString -> Text
416stringLiteral :: PSString -> Text
386−stringLiteral pss | Just s <- decodeString pss =
417+stringLiteral pss | Just s <- decodeString pss = stringLiteral' s
389− else ("u8")) <> stringLiteral' s
391 stringLiteral' :: Text -> Text
419 stringLiteral' :: Text -> Text
392 stringLiteral' s = "\"" <> T.concatMap encodeChar s <> "\""
420 stringLiteral' s = "\"" <> T.concatMap encodeChar s <> "\""
393 encodeChar :: Char -> Text
421 encodeChar :: Char -> Text
394− encodeChar '\0' = "\\0"
422+ encodeChar '\0' = "\\x00"
395 encodeChar '\b' = "\\b"
423 encodeChar '\b' = "\\b"
396 encodeChar '\t' = "\\t"
424 encodeChar '\t' = "\\t"
397 encodeChar '\n' = "\\n"
425 encodeChar '\n' = "\\n"
@@ -407,13 +435,14 @@ unbox' :: Text -> AST -> Text
407unbox' _ v@(NumericLiteral{}) = prettyPrintIL1 v
435unbox' _ v@(NumericLiteral{}) = prettyPrintIL1 v
408unbox' _ v@(BooleanLiteral{}) = prettyPrintIL1 v
436unbox' _ v@(BooleanLiteral{}) = prettyPrintIL1 v
409unbox' _ v@(StringLiteral{}) = prettyPrintIL1 v
437unbox' _ v@(StringLiteral{}) = prettyPrintIL1 v
410−unbox' t v = unbox t <> "(" <> prettyPrintIL1 v <> ")"
438+unbox' t v = "(" <> prettyPrintIL1 v <> ")" <> unbox t -- unbox t <> "(" <> prettyPrintIL1 v <> ")"
412unboxType :: Text -> Maybe Text
440unboxType :: Text -> Maybe Text
413unboxType t
441unboxType t
414 | t == C.eqInt ||
442 | t == C.eqInt ||
415 t == C.semiringInt ||
443 t == C.semiringInt ||
416− t == C.ringInt
444+ t == C.ringInt ||
417 = Just int
446 = Just int
418 | t == C.semiringNumber ||
447 | t == C.semiringNumber ||
419 t == C.ringNumber ||
448 t == C.ringNumber ||
@@ -423,10 +452,23 @@ unboxType t
423 = Just bool
452 = Just bool
424 | otherwise = Nothing
453 | otherwise = Nothing
455+unboxTypeInt :: Text -> Maybe Text
458+ t == C.semiringInt ||
462+ | otherwise = Nothing
426renderOp :: Text -> Maybe Text
464renderOp :: Text -> Maybe Text
427renderOp op
465renderOp op
428 | op == C.eq = Just " == "
466 | op == C.eq = Just " == "
429 | op == C.notEq = Just " != "
467 | op == C.notEq = Just " != "
468+ | op == C.lessThan = Just " < "
469+ | op == C.lessThanOrEq = Just " <= "
470+ | op == C.greaterThan = Just " > "
471+ | op == C.greaterThanOrEq = Just " >= "
430 | op == C.add = Just " + "
472 | op == C.add = Just " + "
431 | op == C.sub = Just " - "
473 | op == C.sub = Just " - "
432 | op == C.mul = Just " * "
474 | op == C.mul = Just " * "
@@ -436,29 +478,22 @@ renderOp op
436 | otherwise = Nothing
478 | otherwise = Nothing
438interfaceSource :: Text -> [(Text,Bool)] -> [Ident] -> Text
480interfaceSource :: Text -> [(Text,Bool)] -> [Ident] -> Text
439−interfaceSource mn values foreigns =
481+interfaceSource _ _ _ = ""
440− let foreigns' = identToIL <$> foreigns
441− valueNames = fst <$> values
442− values' = filter ((`notElem` foreigns') . fst) values ++
443− zip (foreigns' \\ valueNames) (repeat True)
445− "// Generated by psil compiler\n\n" <>
446− "#ifndef " <> mn <> "_H\n" <>
447− "#define " <> mn <> "_H\n\n" <>
448− "#include \"purescript.h\"\n\n" <>
449− "namespace " <> mn <> " {\n\n" <>
450− "using namespace purescript;\n\n" <>
451− (T.concat $ (\(export, static) ->
452− let rty = if static then "const boxed&" else "boxed" in
453− "auto " <> export <> "() -> " <> rty <> ";\n") <$> values') <> "\n" <>
454− "} // end namespace " <> mn <> "\n\n" <>
455− "#endif // " <> mn <> "_H\n\n"
457implHeaderSource :: Text -> [Text] -> Text -> Text
483implHeaderSource :: Text -> [Text] -> Text -> Text
458−implHeaderSource mn imports interfaceImport =
484+implHeaderSource mn imports _ =
485+ "// Code generated by psgo. DO NOT EDIT.\n\n" <>
486+ "package " <> (if mn == "Main" then "main" else mn) <> "\n\n" <>
487+ "import . \"purescript\"\n" <>
489+ then "import \"purescript_ffi\"\n"
459− T.concat imports <> "\n"<>
491+ T.concat imports <> "\n" <>
460− interfaceImport <> "\n\n" <>
492+ "type IGNORE_UNUSED_IMPORTS = bool\n" <>
461− "namespace " <> mn <> " {\n\n"
493+ "type _ = IGNORE_UNUSED_RUNTIME\n" <>
495+ then "\nconst _ = purescript_ffi.Loader\n\n"
463implFooterSource :: Text -> [Ident] -> Text
498implFooterSource :: Text -> [Ident] -> Text
464implFooterSource mn foreigns =
499implFooterSource mn foreigns =
@@ -466,50 +501,41 @@ implFooterSource mn foreigns =
466 (if null foreigns
501 (if null foreigns
468 else ("// Foreign values\n\n" <>
503 else ("// Foreign values\n\n" <>
469− "DEFINE_FOREIGN_DICTIONARY_AND_ACCESSOR()\n\n" <>
504+ "var " <> foreignDict <> " = " <> dictType <> "{}\n\n" <>
470− (T.concat $ (\foreign' -> "auto " <>
505+ (T.concat $ (\foreign' ->
471− identToIL foreign' <>
506+ let name = identToIL foreign' in
472− "() -> const boxed& { " <>
507+ varDecl <> " " <> initName name <> " Once\n" <>
473− staticValueBegin <>
508+ varDecl <> " " <> valueName name <> " " <> anyType <> "\n\n" <>
474− foreignDict <> "().at(" <>
509+ "func " <>
512+ " " <> initName name <> ".Do(func() {\n" <>
513+ " " <> valueName name <> " = " <>
514+ "SafeGet(" <> foreignDict <> ", " <>
475− (stringLiteral . mkString $ runIdent foreign') <> "); " <>
515+ (stringLiteral $ mkString name) <> ")\n" <>
476− staticValueEnd <>
516+ " })\n" <>
517+ " return " <> valueName name <> "\n" <>
477− " };\n") <$> foreigns) <>
518+ "}\n\n") <$> foreigns))) <>
479− "} // end namespace " <> mn <>
481 if mn == "Main" then mainSource else "\n"
519 if mn == "Main" then mainSource else "\n"
483 mainSource :: Text
521 mainSource :: Text
484 mainSource = "\
522 mainSource = "\
485− \int main(int argc, const char * argv[]) {\n\
523+ \func main() {\n\
486− \ Main::main()();\n\
524+ \ EffApply(PS__main())\n\
491varDecl :: Text
528varDecl :: Text
492−varDecl = "const boxed"
529+varDecl = "var"
495−varRefDecl = varDecl <> "&"
497−renderArg' :: Text -> Text -> Text
499− | arg == unusedName = decl
500− | otherwise = decl <> " " <> arg
502−renderArg :: Text -> Text
503−renderArg = renderArg' varRefDecl
505−renderArgByVal :: Text -> Text
531+renderArg :: Text -> Text -> Text
506−renderArgByVal = renderArg' varDecl
532+renderArg decl arg = arg <> " " <> decl
508foreignDict :: Text
534foreignDict :: Text
509−foreignDict = "foreign"
535+foreignDict = "Foreign"
511−staticValueBegin :: Text
537+initName :: Text -> Text
512−staticValueBegin = "static const boxed _ = "
538+initName s = "_" <> s <> "_init_"
514−staticValueEnd :: Text
540+valueName :: Text -> Text
515−staticValueEnd = "return _;"
541+valueName s = "_" <> s <> "_value_"