git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Make JS `new` an operator
8fa5b0bGary Burgess
src/Language/PureScript/CodeGen/JS.hs+3−3
@@ -120,7 +120,7 @@ declToJs _ mp (DataDeclaration _ _ ctors) e = do
120 body = [ JSAssignment (JSAccessor arg (JSVar "this")) (JSVar arg) | arg <- args ]
120 body = [ JSAssignment (JSAccessor arg (JSVar "this")) (JSVar arg) | arg <- args ]
121 in JSFunction (Just ctorName) args (JSBlock body)
121 in JSFunction (Just ctorName) args (JSBlock body)
122 go :: ProperName -> Int -> Int -> [JS] -> JS
122 go :: ProperName -> Int -> Int -> [JS] -> JS
123− go pn _ 0 values = JSApp (JSNew (JSVar (runProperName pn))) (reverse values)
123+ go pn _ 0 values = JSUnary JSNew $ JSApp (JSVar $ runProperName pn) (reverse values)
124 go pn index n values =
124 go pn index n values =
125 JSFunction Nothing ["value" ++ show index]
125 JSFunction Nothing ["value" ++ show index]
126 (JSBlock [JSReturn (go pn (index + 1) (n - 1) (JSVar ("value" ++ show index) : values))])
126 (JSBlock [JSReturn (go pn (index + 1) (n - 1) (JSVar ("value" ++ show index) : values))])
@@ -192,7 +192,7 @@ valueToJs _ _ _ (BooleanLiteral b) = return $ JSBooleanLiteral b
192valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral <$> mapM (valueToJs opts m e) xs
192valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral <$> mapM (valueToJs opts m e) xs
193valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral <$> mapM (sndM (valueToJs opts m e)) ps
193valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral <$> mapM (sndM (valueToJs opts m e)) ps
194valueToJs opts m e (TypeClassDictionaryConstructorApp name (TypedValue _ (ObjectLiteral ps) _)) =
194valueToJs opts m e (TypeClassDictionaryConstructorApp name (TypedValue _ (ObjectLiteral ps) _)) =
195− JSApp (JSNew (qualifiedToJS m (Ident . runProperName) name)) <$> mapM (valueToJs opts m e . snd) (sortBy (compare `on` fst) ps)
195+ JSUnary JSNew . JSApp (qualifiedToJS m (Ident . runProperName) name) <$> mapM (valueToJs opts m e . snd) (sortBy (compare `on` fst) ps)
196valueToJs _ _ _ TypeClassDictionaryConstructorApp{} =
196valueToJs _ _ _ TypeClassDictionaryConstructorApp{} =
197 error "TypeClassDictionaryConstructorApp did not contain object literal"
197 error "TypeClassDictionaryConstructorApp did not contain object literal"
198valueToJs opts m e (ObjectUpdate o ps) = do
198valueToJs opts m e (ObjectUpdate o ps) = do
@@ -302,7 +302,7 @@ bindersToJs opts m e binders vals = do
302 jss <- forM binders $ \(CaseAlternative bs grd result) -> do
302 jss <- forM binders $ \(CaseAlternative bs grd result) -> do
303 ret <- valueToJs opts m e result
303 ret <- valueToJs opts m e result
304 go valNames [JSReturn ret] bs grd
304 go valNames [JSReturn ret] bs grd
305− return $ JSApp (JSFunction Nothing valNames (JSBlock (concat jss ++ [JSThrow $ JSApp (JSVar "Error") $ [(JSStringLiteral "Failed pattern match")]])))
305+ return $ JSApp (JSFunction Nothing valNames (JSBlock (concat jss ++ [JSThrow $ JSUnary JSNew $ JSApp (JSVar "Error") $ [(JSStringLiteral "Failed pattern match")]])))
306 vals
306 vals
307 where
307 where
308 go :: (Functor m, Applicative m, Monad m) => [String] -> [JS] -> [Binder] -> Maybe Guard -> SupplyT m [JS]
308 go :: (Functor m, Applicative m, Monad m) => [String] -> [JS] -> [Binder] -> Maybe Guard -> SupplyT m [JS]
src/Language/PureScript/CodeGen/JS/AST.hs+5−8
@@ -38,7 +38,11 @@ data UnaryOperator
38 -- |
38 -- |
39 -- Numeric unary \'plus\'
39 -- Numeric unary \'plus\'
40 --
40 --
41+ | Positive
42+ -- |
43+ -- Constructor
44+ --
41− | Positive deriving (Show, Eq, Data, Typeable)
45+ | JSNew deriving (Show, Eq, Data, Typeable)
42
46
43-- |
47-- |
44-- Built-in binary operators
48-- Built-in binary operators
@@ -170,10 +174,6 @@ data JS
170 --
174 --
171 | JSApp JS [JS]
175 | JSApp JS [JS]
172 -- |
176 -- |
173− -- Constructor
174− --
175− | JSNew JS
176− -- |
177 -- Variable
177 -- Variable
178 --
178 --
179 | JSVar String
179 | JSVar String
@@ -270,7 +270,6 @@ everywhereOnJS f = go
270 go (JSThrow js) = f (JSThrow (go js))
270 go (JSThrow js) = f (JSThrow (go js))
271 go (JSTypeOf js) = f (JSTypeOf (go js))
271 go (JSTypeOf js) = f (JSTypeOf (go js))
272 go (JSLabel name js) = f (JSLabel name (go js))
272 go (JSLabel name js) = f (JSLabel name (go js))
273− go (JSNew j) = f (JSNew (go j))
274 go (JSInstanceOf j1 j2) = f (JSInstanceOf (go j1) (go j2))
273 go (JSInstanceOf j1 j2) = f (JSInstanceOf (go j1) (go j2))
275 go other = f other
274 go other = f other
276
275
@@ -298,7 +297,6 @@ everywhereOnJSTopDown f = go . f
298 go (JSThrow j) = JSThrow (go (f j))
297 go (JSThrow j) = JSThrow (go (f j))
299 go (JSTypeOf j) = JSTypeOf (go (f j))
298 go (JSTypeOf j) = JSTypeOf (go (f j))
300 go (JSLabel name j) = JSLabel name (go (f j))
299 go (JSLabel name j) = JSLabel name (go (f j))
301− go (JSNew j) = JSNew (go (f j))
302 go (JSInstanceOf j1 j2) = JSInstanceOf (go (f j1)) (go (f j2))
300 go (JSInstanceOf j1 j2) = JSInstanceOf (go (f j1)) (go (f j2))
303 go other = f other
301 go other = f other
304
302
@@ -326,6 +324,5 @@ everythingOnJS (<>) f = go
326 go j@(JSThrow j1) = f j <> go j1
324 go j@(JSThrow j1) = f j <> go j1
327 go j@(JSTypeOf j1) = f j <> go j1
325 go j@(JSTypeOf j1) = f j <> go j1
328 go j@(JSLabel _ j1) = f j <> go j1
326 go j@(JSLabel _ j1) = f j <> go j1
329− go j@(JSNew j1) = f j <> go j1
330 go j@(JSInstanceOf j1 j2) = f j <> go j1 <> go j2
327 go j@(JSInstanceOf j1 j2) = f j <> go j1 <> go j2
331 go other = f other
328 go other = f other
src/Language/PureScript/Pretty/JS.hs+1−7
@@ -180,12 +180,6 @@ instanceOf = mkPattern match
180 match (JSInstanceOf val ty) = Just (val, ty)
180 match (JSInstanceOf val ty) = Just (val, ty)
181 match _ = Nothing
181 match _ = Nothing
182
182
183−new :: Pattern PrinterState JS ((), JS)
184−new = mkPattern match
185− where
186− match (JSNew ctor) = Just ((), ctor)
187− match _ = Nothing
188−
189unary :: UnaryOperator -> String -> Operator PrinterState JS String
183unary :: UnaryOperator -> String -> Operator PrinterState JS String
190unary op str = Wrap match (++)
184unary op str = Wrap match (++)
191 where
185 where
@@ -234,8 +228,8 @@ prettyPrintJS' = A.runKleisli $ runPattern matchValue
234 operators =
228 operators =
235 OperatorTable [ [ Wrap accessor $ \prop val -> val ++ "." ++ prop ]
229 OperatorTable [ [ Wrap accessor $ \prop val -> val ++ "." ++ prop ]
236 , [ Wrap indexer $ \index val -> val ++ "[" ++ index ++ "]" ]
230 , [ Wrap indexer $ \index val -> val ++ "[" ++ index ++ "]" ]
237− , [ Wrap new $ \_ s -> "new " ++ s ]
238 , [ Wrap app $ \args val -> val ++ "(" ++ args ++ ")" ]
231 , [ Wrap app $ \args val -> val ++ "(" ++ args ++ ")" ]
232+ , [ unary JSNew "new " ]
239 , [ Wrap lam $ \(name, args) ret -> "function "
233 , [ Wrap lam $ \(name, args) ret -> "function "
240 ++ fromMaybe "" name
234 ++ fromMaybe "" name
241 ++ "(" ++ intercalate ", " args ++ ") "
235 ++ "(" ++ intercalate ", " args ++ ") "