git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Make JS `new` an operator
commit8fa5b0b658745a52db56113100ad4231a1cabfc6 authorGary Burgess <gary.burgess@gmail.com> dateWed 23 Jul 2014, 21:11 (12 years ago) parentb210603
3 files changed+9−18
src/Language/PureScript/CodeGen/JS.hs+3−3 src/Language/PureScript/CodeGen/JS/AST.hs+5−8 src/Language/PureScript/Pretty/JS.hs+1−7
src/Language/PureScript/CodeGen/JS.hs+3−3
@@ -120,7 +120,7 @@ declToJs _ mp (DataDeclaration _ _ ctors) e = do
120120 body = [ JSAssignment (JSAccessor arg (JSVar "this")) (JSVar arg) | arg <- args ]
121121 in JSFunction (Just ctorName) args (JSBlock body)
122122 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)
124124 go pn index n values =
125125 JSFunction Nothing ["value" ++ show index]
126126 (JSBlock [JSReturn (go pn (index + 1) (n - 1) (JSVar ("value" ++ show index) : values))])
@@ -192,7 +192,7 @@ valueToJs _ _ _ (BooleanLiteral b) = return $ JSBooleanLiteral b
192192valueToJs opts m e (ArrayLiteral xs) = JSArrayLiteral <$> mapM (valueToJs opts m e) xs
193193valueToJs opts m e (ObjectLiteral ps) = JSObjectLiteral <$> mapM (sndM (valueToJs opts m e)) ps
194194valueToJs 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)
196196valueToJs _ _ _ TypeClassDictionaryConstructorApp{} =
197197 error "TypeClassDictionaryConstructorApp did not contain object literal"
198198valueToJs opts m e (ObjectUpdate o ps) = do
@@ -302,7 +302,7 @@ bindersToJs opts m e binders vals = do
302302 jss <- forM binders $ \(CaseAlternative bs grd result) -> do
303303 ret <- valueToJs opts m e result
304304 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")]])))
306306 vals
307307 where
308308 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
3838 -- |
3939 -- Numeric unary \'plus\'
4040 --
41− | Positive deriving (Show, Eq, Data, Typeable)
41+ | Positive
42+ -- |
43+ -- Constructor
44+ --
45+ | JSNew deriving (Show, Eq, Data, Typeable)
4246
4347-- |
4448-- Built-in binary operators
@@ -170,10 +174,6 @@ data JS
170174 --
171175 | JSApp JS [JS]
172176 -- |
173− -- Constructor
174− --
175− | JSNew JS
176− -- |
177177 -- Variable
178178 --
179179 | JSVar String
@@ -270,7 +270,6 @@ everywhereOnJS f = go
270270 go (JSThrow js) = f (JSThrow (go js))
271271 go (JSTypeOf js) = f (JSTypeOf (go js))
272272 go (JSLabel name js) = f (JSLabel name (go js))
273− go (JSNew j) = f (JSNew (go j))
274273 go (JSInstanceOf j1 j2) = f (JSInstanceOf (go j1) (go j2))
275274 go other = f other
276275
@@ -298,7 +297,6 @@ everywhereOnJSTopDown f = go . f
298297 go (JSThrow j) = JSThrow (go (f j))
299298 go (JSTypeOf j) = JSTypeOf (go (f j))
300299 go (JSLabel name j) = JSLabel name (go (f j))
301− go (JSNew j) = JSNew (go (f j))
302300 go (JSInstanceOf j1 j2) = JSInstanceOf (go (f j1)) (go (f j2))
303301 go other = f other
304302
@@ -326,6 +324,5 @@ everythingOnJS (<>) f = go
326324 go j@(JSThrow j1) = f j <> go j1
327325 go j@(JSTypeOf j1) = f j <> go j1
328326 go j@(JSLabel _ j1) = f j <> go j1
329− go j@(JSNew j1) = f j <> go j1
330327 go j@(JSInstanceOf j1 j2) = f j <> go j1 <> go j2
331328 go other = f other
src/Language/PureScript/Pretty/JS.hs+1−7
@@ -180,12 +180,6 @@ instanceOf = mkPattern match
180180 match (JSInstanceOf val ty) = Just (val, ty)
181181 match _ = Nothing
182182
183−new :: Pattern PrinterState JS ((), JS)
184−new = mkPattern match
185− where
186− match (JSNew ctor) = Just ((), ctor)
187− match _ = Nothing
188−
189183unary :: UnaryOperator -> String -> Operator PrinterState JS String
190184unary op str = Wrap match (++)
191185 where
@@ -234,8 +228,8 @@ prettyPrintJS' = A.runKleisli $ runPattern matchValue
234228 operators =
235229 OperatorTable [ [ Wrap accessor $ \prop val -> val ++ "." ++ prop ]
236230 , [ Wrap indexer $ \index val -> val ++ "[" ++ index ++ "]" ]
237− , [ Wrap new $ \_ s -> "new " ++ s ]
238231 , [ Wrap app $ \args val -> val ++ "(" ++ args ++ ")" ]
232+ , [ unary JSNew "new " ]
239233 , [ Wrap lam $ \(name, args) ret -> "function "
240234 ++ fromMaybe "" name
241235 ++ "(" ++ intercalate ", " args ++ ") "