1
1
mirror of https://github.com/github/semantic.git synced 2024-12-01 09:15:01 +03:00

Recursively construct If Syntaxes for nested If Terms

- Also adds Data.List (head)
This commit is contained in:
Rick Winfrey 2016-10-12 14:55:25 -05:00
parent e4ac39547b
commit d12a8d8e1b

View File

@ -3,10 +3,11 @@ module Language.JavaScript where
import Data.Record import Data.Record
import Info import Info
import Prologue import Prologue hiding (head)
import Source import Source
import qualified Syntax as S import qualified Syntax as S
import Term import Term
import Data.List (head)
operators :: [Text] operators :: [Text]
operators = [ "op", "bool_op", "math_op", "delete_op", "type_op", "void_op", "rel_op", "bitwise_op" ] 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 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 :: (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 :: 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)] toTuple child | S.Indexed [key,value] <- unwrap child = [cofree (extract child :< S.Pair key value)]