git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Some simplifications and cleanup in runtime rep
9fd2ad0Andy Arvanitis
runtime/purescript.h+7−5
@@ -54,8 +54,11 @@ namespace purescript {
54 boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {}
54 boxed(const bool b) : std::shared_ptr<void>(std::make_shared<bool>(b)) {}
55 boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
55 boxed(const char s[]) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
56 boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {}
56 boxed(string&& s) : std::shared_ptr<void>(std::make_shared<string>(std::move(s))) {}
57+ boxed(const string& s) : std::shared_ptr<void>(std::make_shared<string>(s)) {}
57 boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {}
58 boxed(array_t&& l) : std::shared_ptr<void>(std::make_shared<array_t>(std::move(l))) {}
59+ boxed(const array_t& l) : std::shared_ptr<void>(std::make_shared<array_t>(l)) {}
58 boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {}
60 boxed(dict_t&& m) : std::shared_ptr<void>(std::make_shared<dict_t>(std::move(m))) {}
61+ boxed(const dict_t& m) : std::shared_ptr<void>(std::make_shared<dict_t>(m)) {}
59
62
60 template <typename T,
63 template <typename T,
61 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
64 typename = typename std::enable_if<!std::is_same<boxed,T>::value>::type>
@@ -128,7 +131,7 @@ namespace purescript {
128 }
131 }
129
132
130 template <typename T>
133 template <typename T>
131− constexpr inline auto unbox(const boxed& b) -> const T& {
134+ constexpr auto unbox(const boxed& b) -> const T& {
132 return *static_cast<const T*>(b.get());
135 return *static_cast<const T*>(b.get());
133 }
136 }
134
137
@@ -146,13 +149,12 @@ namespace purescript {
146 }
149 }
147
150
148 template <typename T>
151 template <typename T>
149− constexpr inline auto peek(boxed& b) -> T& {
152+ constexpr auto peek(boxed& b) -> T& {
150 return *static_cast<T*>(b.get());
153 return *static_cast<T*>(b.get());
151 }
154 }
152
155
153− template <typename T>
154− inline auto copy(const boxed& b) -> boxed {
156+ inline auto array_length(const boxed& a) -> boxed::array_t::size_type {
155− return box<T>(unbox<T>(b));
157+ return unbox<boxed::array_t>(a).size();
156 }
158 }
157
159
158 using fn_t = boxed::fn_t;
160 using fn_t = boxed::fn_t;
src/CodeGen/Cpp.hs+3−9
@@ -193,9 +193,9 @@ moduleToCpp (Module _ coms mn _ imps exports foreigns decls) _ =
193 updates <- mapM (sndM valueToCpp) ps
193 updates <- mapM (sndM valueToCpp) ps
194 obj' <- freshName
194 obj' <- freshName
195 let objVar = AST.Var Nothing obj'
195 let objVar = AST.Var Nothing obj'
196− copy = AST.VariableIntroduction Nothing obj' (Just $ makeCopy obj)
196+ copy = AST.VariableIntroduction Nothing obj' (Just $ AST.App Nothing (AST.Var Nothing (unbox dictType)) [obj])
197 assign (k, v) = AST.Assignment Nothing (accessorString k objVar) v
197 assign (k, v) = AST.Assignment Nothing (accessorString k objVar) v
198− sts = copy : (assign <$> updates) ++ [AST.Return Nothing $ makeCopy objVar]
198+ sts = copy : (assign <$> updates) ++ [AST.Return Nothing objVar]
199 return $ AST.App Nothing (AST.Function Nothing Nothing [] (AST.Block Nothing sts)) []
199 return $ AST.App Nothing (AST.Function Nothing Nothing [] (AST.Block Nothing sts)) []
200 valueToCpp (Abs _ arg val) = do
200 valueToCpp (Abs _ arg val) = do
201 ret <- valueToCpp val
201 ret <- valueToCpp val
@@ -387,11 +387,5 @@ moduleToCpp (Module _ coms mn _ imps exports foreigns decls) _ =
387emptyAnn :: Ann
387emptyAnn :: Ann
388emptyAnn = (SourceSpan "" (SourcePos 0 0) (SourcePos 0 0), [], Nothing, Nothing)
388emptyAnn = (SourceSpan "" (SourcePos 0 0) (SourcePos 0 0), [], Nothing, Nothing)
389
389
390−makeCopy :: AST -> AST
391−makeCopy obj = AST.App Nothing (AST.Var Nothing "copy<dict_t>") [obj]
392−
393arrayLength :: AST -> AST
390arrayLength :: AST -> AST
394−arrayLength array = applyMethod (AST.App Nothing (AST.Var Nothing "unbox<array_t>") [array]) "size"
391+arrayLength a = AST.App Nothing (AST.Var Nothing arrayLengthFn) [a]
395−
396−applyMethod :: AST -> Text -> AST
397−applyMethod obj method = (AST.Indexer Nothing (AST.ArrayLiteral Nothing [AST.Var Nothing method]) obj)
src/CodeGen/Cpp/Common.hs+24
@@ -192,5 +192,29 @@ cppLiterals =
192 , "unbox"
192 , "unbox"
193 ]
193 ]
194
194
195+dictType :: Text
196+dictType = "dict_t"
197+
198+arrayType :: Text
199+arrayType = "array_t"
200+
201+int :: Text
202+int = "int"
203+
204+float :: Text
205+float = "double"
206+
207+bool :: Text
208+bool = "bool"
209+
210+string :: Text
211+string = "string"
212+
213+unbox :: Text -> Text
214+unbox t = "unbox<" <> t <> ">"
215+
216+arrayLengthFn :: Text
217+arrayLengthFn = "array_length"
218+
195tcoLoop :: Text
219tcoLoop :: Text
196tcoLoop = "$TCO_loop$"
220tcoLoop = "$TCO_loop$"
src/CodeGen/Cpp/Printer.hs+13−43
@@ -106,9 +106,9 @@ literals = mkPattern' match'
106 , Just op <- renderOp fn
106 , Just op <- renderOp fn
107 , Just t <- unboxType dict
107 , Just t <- unboxType dict
108 = mconcat <$> sequence
108 = mconcat <$> sequence
109− [ return $ emit $ unbox t x
109+ [ return $ emit $ unbox' t x
110 , return $ emit op
110 , return $ emit op
111− , return $ emit $ unbox t y
111+ , return $ emit $ unbox' t y
112 ]
112 ]
113 match (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [n])
113 match (App _ (App _ (Indexer _ (Var _ fn) (Var _ fnMod)) [Indexer _ (Var _ dict) (Var _ dictMod)]) [n])
114 | fnMod == dictMod
114 | fnMod == dictMod
@@ -117,7 +117,7 @@ literals = mkPattern' match'
117 , Just t <- unboxType dict
117 , Just t <- unboxType dict
118 = mconcat <$> sequence
118 = mconcat <$> sequence
119 [ return $ emit "-"
119 [ return $ emit "-"
120− , return $ emit $ unbox t n
120+ , return $ emit $ unbox' t n
121 ]
121 ]
122 match (App _ val args) = mconcat <$> sequence
122 match (App _ val args) = mconcat <$> sequence
123 [ prettyPrintCpp' val
123 [ prettyPrintCpp' val
@@ -179,18 +179,6 @@ literals = mkPattern' match'
179 , prettyPrintCpp' prop
179 , prettyPrintCpp' prop
180 , return $ emit "()"
180 , return $ emit "()"
181 ]
181 ]
182− match (Indexer _ (ArrayLiteral _ [name]) val) = mconcat <$> sequence
183− [ prettyPrintCpp' val
184− , return $ emit "."
185− , prettyPrintCpp' name
186− , return $ emit "()"
187− ]
188− match (Indexer _ (NumericLiteral _ (Left index)) val) = mconcat <$> sequence
189− [ prettyPrintCpp' val
190− , return $ emit "["
191− , return . emit . T.pack $ show index
192− , return $ emit "]"
193− ]
194 match (Indexer _ prop val) = mconcat <$> sequence
182 match (Indexer _ prop val) = mconcat <$> sequence
195 [ prettyPrintCpp' val
183 [ prettyPrintCpp' val
196 , return $ emit "["
184 , return $ emit "["
@@ -198,7 +186,7 @@ literals = mkPattern' match'
198 , return $ emit "]"
186 , return $ emit "]"
199 ]
187 ]
200 match (InstanceOf _ val ty) = mconcat <$> sequence
188 match (InstanceOf _ val ty) = mconcat <$> sequence
201− [ return . emit $ unbox dictType val
189+ [ return . emit $ unbox' dictType val
202 , return $ emit ".contains("
190 , return $ emit ".contains("
203 , prettyPrintCpp' ty
191 , prettyPrintCpp' ty
204 , return $ emit ")"
192 , return $ emit ")"
@@ -225,21 +213,21 @@ literals = mkPattern' match'
225 ]
213 ]
226 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing True)) thens elses) = mconcat <$> sequence
214 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing True)) thens elses) = mconcat <$> sequence
227 [ return $ emit "if ("
215 [ return $ emit "if ("
228− , return $ emit $ unbox "bool" cond
216+ , return $ emit $ unbox' "bool" cond
229 , return $ emit ") "
217 , return $ emit ") "
230 , prettyPrintCpp' thens
218 , prettyPrintCpp' thens
231 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
219 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
232 ]
220 ]
233 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing False)) thens elses) = mconcat <$> sequence
221 match (IfElse _ (Binary _ EqualTo cond (BooleanLiteral Nothing False)) thens elses) = mconcat <$> sequence
234 [ return $ emit "if (!("
222 [ return $ emit "if (!("
235− , return $ emit $ unbox "bool" cond
223+ , return $ emit $ unbox' "bool" cond
236 , return $ emit ")) "
224 , return $ emit ")) "
237 , prettyPrintCpp' thens
225 , prettyPrintCpp' thens
238 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
226 , maybe (return mempty) (fmap (emit " else " <>) . prettyPrintCpp') elses
239 ]
227 ]
240 match (IfElse _ (Binary _ EqualTo x y@(NumericLiteral _ n)) thens elses) = mconcat <$> sequence
228 match (IfElse _ (Binary _ EqualTo x y@(NumericLiteral _ n)) thens elses) = mconcat <$> sequence
241 [ return $ emit "if ("
229 [ return $ emit "if ("
242− , return $ emit $ unbox t x
230+ , return $ emit $ unbox' t x
243 , return $ emit $ " == "
231 , return $ emit $ " == "
244 , prettyPrintCpp' y
232 , prettyPrintCpp' y
245 , return $ emit ") "
233 , return $ emit ") "
@@ -251,7 +239,7 @@ literals = mkPattern' match'
251 | Right _ <- n = float
239 | Right _ <- n = float
252 match (IfElse _ (Binary _ EqualTo a b@StringLiteral{}) thens elses) = mconcat <$> sequence
240 match (IfElse _ (Binary _ EqualTo a b@StringLiteral{}) thens elses) = mconcat <$> sequence
253 [ return $ emit "if ("
241 [ return $ emit "if ("
254− , return $ emit $ unbox string a
242+ , return $ emit $ unbox' string a
255 , return $ emit $ " == "
243 , return $ emit $ " == "
256 , prettyPrintCpp' b
244 , prettyPrintCpp' b
257 , return $ emit ") "
245 , return $ emit ") "
@@ -412,11 +400,11 @@ stringLiteral pss | Just s <- decodeString pss =
412 encodeChar c = T.singleton $ c
400 encodeChar c = T.singleton $ c
413stringLiteral _ = "\"\\uFFFD\""
401stringLiteral _ = "\"\\uFFFD\""
414
402
415−unbox :: Text -> AST -> Text
403+unbox' :: Text -> AST -> Text
416−unbox _ v@(NumericLiteral{}) = prettyPrintCpp1 v
404+unbox' _ v@(NumericLiteral{}) = prettyPrintCpp1 v
417−unbox _ v@(BooleanLiteral{}) = prettyPrintCpp1 v
405+unbox' _ v@(BooleanLiteral{}) = prettyPrintCpp1 v
418−unbox _ v@(StringLiteral{}) = prettyPrintCpp1 v
406+unbox' _ v@(StringLiteral{}) = prettyPrintCpp1 v
419−unbox t v = "unbox<" <> t <> ">(" <> prettyPrintCpp1 v <> ")"
407+unbox' t v = unbox t <> "(" <> prettyPrintCpp1 v <> ")"
420
408
421unboxType :: Text -> Maybe Text
409unboxType :: Text -> Maybe Text
422unboxType t
410unboxType t
@@ -490,24 +478,6 @@ implFooterSource mn foreigns =
490 \}\n\n\
478 \}\n\n\
491 \"
479 \"
492
480
493−dictType :: Text
494−dictType = "dict_t"
495−
496−arrayType :: Text
497−arrayType = "array_t"
498−
499−int :: Text
500−int = "int"
501−
502−float :: Text
503−float = "double"
504−
505−bool :: Text
506−bool = "bool"
507−
508−string :: Text
509−string = "string"
510−
511varDecl :: Text
481varDecl :: Text
512varDecl = "const boxed&"
482varDecl = "const boxed&"
513
483