From 6f0294bcf6240a9a8d95906804ad423dc180d4e8 Mon Sep 17 00:00:00 2001 From: Franco Bulgarelli Date: Thu, 4 Jan 2018 10:21:26 -0300 Subject: [PATCH] Fixing JS not bug --- spec/InspectorSpec.hs | 6 ++++++ spec/JavaScriptSpec.hs | 8 +++++++- src/Language/Mulang/Parsers/JavaScript.hs | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/spec/InspectorSpec.hs b/spec/InspectorSpec.hs index 9c6af2433..4372b0311 100644 --- a/spec/InspectorSpec.hs +++ b/spec/InspectorSpec.hs @@ -419,6 +419,12 @@ spec = do it "is True on direct usage in function" $ do uses (named "m") (js "function f(x) { m }") `shouldBe` True + it "is True on direct call in function" $ do + uses (named "m") (js "function f(x) { m() }") `shouldBe` True + + it "is True on negated call in function" $ do + uses (named "m") (js "function f(x) { !m() }") `shouldBe` True + it "is True on direct usage of something like it in function" $ do uses (like "m") (js "function f(x) { m2 }") `shouldBe` True diff --git a/spec/JavaScriptSpec.hs b/spec/JavaScriptSpec.hs index c7d83e1b3..2982fc0c7 100644 --- a/spec/JavaScriptSpec.hs +++ b/spec/JavaScriptSpec.hs @@ -38,7 +38,7 @@ spec = do js "var m = function(x) { return 1 }" `shouldBe` hs "m = \\x -> 1" it "simple function declaration" $ do - js "function f(x) { return 1 }" `shouldBe` hs "f x = 1" + js "function f(x) { return 1 }" `shouldBe` hs "f x = 1" it "simple procedure declaration" $ do js "function f(x) { console.log('fruit') }" `shouldBe` ( @@ -83,6 +83,12 @@ spec = do it "handles booleans" $ do js "true" `shouldBe` MuTrue + it "handles negation" $ do + js "!true" `shouldBe` (Application (Reference "!") [MuTrue]) + + it "handles boolean binary operations" $ do + js "true || false " `shouldBe` (Application (Reference "||") [MuTrue, MuFalse]) + it "handles lambdas" $ do js "(function(x, y) { 1 })" `shouldBe` (Lambda [VariablePattern "x", VariablePattern "y"] (MuNumber 1)) diff --git a/src/Language/Mulang/Parsers/JavaScript.hs b/src/Language/Mulang/Parsers/JavaScript.hs index 43a700cad..8a017fe3a 100644 --- a/src/Language/Mulang/Parsers/JavaScript.hs +++ b/src/Language/Mulang/Parsers/JavaScript.hs @@ -111,6 +111,7 @@ muJSExpression (JSMemberNew _ (JSIdentifier _ name) _ args _) = New name ( --muJSExpression (JSMemberSquare JSExpression _ JSExpression _) -- ^firstpart, lb, expr, rb muJSExpression (JSNewExpression _ (JSIdentifier _ name)) = New name [] muJSExpression (JSObjectLiteral _ propertyList _) = MuObject (compactMap id.map muJSObjectProperty.muJSCommaTrailingList $ propertyList) +muJSExpression (JSUnaryExpression (JSUnaryOpNot _) e) = Application (Reference "!") [muJSExpression e] muJSExpression (JSUnaryExpression op (JSIdentifier _ name)) = Assignment name (muJSUnaryOp op name) muJSExpression (JSVarInitExpression (JSIdentifier _ name) initial) = Variable name (muJSVarInitializer initial) muJSExpression _ = Other @@ -147,7 +148,6 @@ muJSUnaryOp (JSUnaryOpDecr _) r = (Application (Reference "-") [Reference r, MuN --muJSUnaryOp (JSUnaryOpDelete _) muJSUnaryOp (JSUnaryOpIncr _) r = (Application (Reference "+") [Reference r, MuNumber 1]) --muJSUnaryOp (JSUnaryOpMinus _) ---muJSUnaryOp (JSUnaryOpNot _) --muJSUnaryOp (JSUnaryOpPlus _) --muJSUnaryOp (JSUnaryOpTilde _) --muJSUnaryOp (JSUnaryOpTypeof _)