From d12a8d8e1b49cc8b79c84b822b8b87454bdd37ca Mon Sep 17 00:00:00 2001 From: Rick Winfrey Date: Wed, 12 Oct 2016 14:55:25 -0500 Subject: [PATCH] Recursively construct If Syntaxes for nested If Terms - Also adds Data.List (head) --- src/Language/JavaScript.hs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Language/JavaScript.hs b/src/Language/JavaScript.hs index 0d7207523..43f53dc9d 100644 --- a/src/Language/JavaScript.hs +++ b/src/Language/JavaScript.hs @@ -3,10 +3,11 @@ module Language.JavaScript where import Data.Record import Info -import Prologue +import Prologue hiding (head) import Source import qualified Syntax as S import Term +import Data.List (head) operators :: [Text] operators = [ "op", "bool_op", "math_op", "delete_op", "type_op", "void_op", "rel_op", "bitwise_op" ] @@ -159,7 +160,12 @@ toVarDecl :: (HasField fields Category) => Term (S.Syntax Text) (Record fields) toVarDecl child = cofree $ setCategory (extract child) VarDecl :< S.VarDecl child toElseIf :: (HasField fields Category) => Term (S.Syntax Text) (Record fields) -> Term (S.Syntax Text) (Record fields) -toElseIf child = cofree $ setCategory (extract child) If :< S.If child child [] +toElseIf child = case unwrap child of + S.If expr thenClause [] -> cofree $ setCategory (extract child) If :< S.If expr thenClause [] + S.If expr thenClause elseClause -> case unwrap . head $ elseClause of + S.If{} -> cofree $ setCategory (extract child) If :< S.If expr thenClause (toElseIf <$> elseClause) + _ -> cofree $ setCategory (extract child) If :< S.If expr thenClause [] + _ -> cofree $ setCategory (extract child) If :< S.If child child [] toTuple :: Term (S.Syntax Text) (Record fields) -> [Term (S.Syntax Text) (Record fields)] toTuple child | S.Indexed [key,value] <- unwrap child = [cofree (extract child :< S.Pair key value)]