Skip to content

Commit

Permalink
Adding return type to type signature
Browse files Browse the repository at this point in the history
  • Loading branch information
flbulgarelli committed Jul 22, 2017
1 parent 2197c10 commit 75d5827
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 38 deletions.
2 changes: 1 addition & 1 deletion spec/InspectorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ spec = do

describe "declaresInterface" $ do
it "is True when present" $ do
declaresInterface (named "Optional") (Interface "Optional" [] (TypeSignature "get" ["A"])) `shouldBe` True
declaresInterface (named "Optional") (Interface "Optional" [] (TypeSignature "get" [] "A")) `shouldBe` True

it "is False when not present" $ do
declaresInterface (named "Bird") (Class "Bird" (Just "Animal") MuNull) `shouldBe` False
Expand Down
6 changes: 3 additions & 3 deletions spec/SignatureSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ spec = do

describe "TypedSignature" $ do
it "simple variable type declaration" $ do
signaturesOf (hs "foo :: Int") `shouldBe` [TypedSignature "foo" ["Int"]]
signaturesOf (hs "foo :: Int") `shouldBe` [TypedSignature "foo" [] "Int"]

it "simple function type declaration" $ do
signaturesOf (hs "foo :: Int -> Int") `shouldBe` [TypedSignature "foo" ["Int", "Int"]]
signaturesOf (hs "foo :: Int -> Int") `shouldBe` [TypedSignature "foo" ["Int"] "Int"]

it "simple function tuple declaration" $ do
signaturesOf (hs "foo :: b -> (Int, [a])") `shouldBe` [TypedSignature "foo" ["b", "(Int, [a])"]]
signaturesOf (hs "foo :: b -> (Int, [a])") `shouldBe` [TypedSignature "foo" ["b"] "(Int, [a])"]

describe "NamedSignature" $ do
it "empty expression" $ do
Expand Down
1 change: 1 addition & 0 deletions spec/SignaturesAnalyzerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ run language content style = analyse (signaturesAnalysis (CodeSample language co
spec = describe "SignturesAnalyzer" $ do
it "handles MulangStyle" $ do
(run Haskell "f x = x + 1" MulangStyle ) `shouldReturn` (result ["-- f(x)"])
(run Haskell "f :: Int -> String" MulangStyle ) `shouldReturn` (result ["-- f(Int): String"])

it "handles HaskellStyle" $ do
(run Haskell "f x = x + 1" HaskellStyle ) `shouldReturn` (result ["-- f x"])
Expand Down
6 changes: 3 additions & 3 deletions src/Language/Mulang/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ data Expression
| Record Identifier
-- ^ Imperative / Functional programming struct declaration.
-- Only the record name is parsed
| TypeSignature Identifier [Identifier]
-- ^ Generic type signature for a computation.
-- Only the target name of the computation is parsed
| TypeSignature Identifier [Identifier] Identifier
-- ^ Generic type signature for a computation,
-- composed by a name, parameter types and return type
| EntryPoint Identifier Expression
-- ^ Entry point with its body
| Function Identifier [Equation]
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Mulang/Explorer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extractReference _ = Nothing


extractDeclaration :: Expression -> Maybe (Binding, Expression)
extractDeclaration e@(TypeSignature n _) = Just (n, e)
extractDeclaration e@(TypeSignature n _ _)= Just (n, e)
extractDeclaration e@(TypeAlias n ) = Just (n, e)
extractDeclaration e@(Variable n _) = Just (n, e)
extractDeclaration e@(Subroutine n _) = Just (n, e)
Expand Down
8 changes: 4 additions & 4 deletions src/Language/Mulang/Inspector/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ usesIf = containsExpression f
-- | Inspection that tells whether a top level binding exists
declares :: BindedInspection
declares = containsDeclaration f
where f (TypeSignature _ _) = False
f _ = True
where f (TypeSignature _ _ _) = False
f _ = True

-- | Inspection that tells whether an expression is direct recursive
declaresRecursively :: BindedInspection
Expand Down Expand Up @@ -104,8 +104,8 @@ declaresTypeAlias = containsDeclaration f

declaresTypeSignature :: BindedInspection
declaresTypeSignature = containsDeclaration f
where f (TypeSignature _ _) = True
f _ = False
where f (TypeSignature _ _ _) = True
f _ = False


usesAnonymousVariable :: Inspection
Expand Down
9 changes: 6 additions & 3 deletions src/Language/Mulang/Parsers/Haskell.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mu (HsModule _ _ _ _ decls) = compact (concatMap muDecls decls)

muDecls (HsTypeDecl _ name _ _) = [TypeAlias (muName name)]
muDecls (HsDataDecl _ _ name _ _ _ ) = [Record (muName name)]
muDecls (HsTypeSig _ names (HsQualType _ t)) = map (\name -> TypeSignature (muName name) (muTopType t)) names
muDecls (HsTypeSig _ names (HsQualType _ t)) = map (muTypeSignature t) names
muDecls (HsFunBind equations) | (HsMatch _ name _ _ _) <- head equations =
[Function (muName name) (map muEquation equations)]
muDecls (HsPatBind _ (HsPVar name) (HsUnGuardedRhs exp) _) = [Variable (muName name) (muExp exp)]
Expand Down Expand Up @@ -118,13 +118,16 @@ mu (HsModule _ _ _ _ decls) = compact (concatMap muDecls decls)
muStmt (HsGenerator _ pat exp) = MuGenerator (muPat pat) (muExp exp)
muStmt (HsQualifier exp) = MuQualifier (muExp exp)

muTypeSignature t name = TypeSignature (muName name) (init topTypes) (last topTypes)
where topTypes = muTopTypes t

muTopType (HsTyFun i o) = muType i : muTopType o
muTopType t = [muType t]
muTopTypes (HsTyFun i o) = muType i : muTopTypes o
muTopTypes t = [muType t]

muType (HsTyFun i o) = muType i ++ " -> " ++ muType o
muType (HsTyCon name) = muQName name
muType (HsTyVar name) = muName name
muType (HsTyTuple ts) = "(" ++ (intercalate ", " . map muType $ ts) ++ ")"
muType (HsTyApp (HsTyCon (Special HsListCon)) t2) = "[" ++ muType t2 ++ "]"
muType (HsTyApp t1 t2) = muType t1 ++ " " ++ muType t2

44 changes: 22 additions & 22 deletions src/Language/Mulang/Signature.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@ import Data.Function (on)
type SignatureStyle = [Signature] -> [String]

data Signature = AritySignature Binding Int
| TypedSignature Binding [Binding]
| TypedSignature Binding [Binding] Binding
| NamedSignature Binding [Maybe Binding] deriving (Show, Eq)


arity :: Signature -> Int
arity (AritySignature _ a) = a
arity (TypedSignature _ ps) = length ps
arity (NamedSignature _ ps) = length ps
arity (AritySignature _ a) = a
arity (TypedSignature _ ps _) = length ps
arity (NamedSignature _ ps) = length ps

name :: Signature -> Binding
name (AritySignature n _) = n
name (TypedSignature n _) = n
name (NamedSignature n _) = n
name (AritySignature n _) = n
name (TypedSignature n _ _) = n
name (NamedSignature n _) = n

nameAndArity :: Signature -> (Binding, Int)
nameAndArity signature = (name signature, arity signature)

parameterNames :: Signature -> [Maybe Binding]
parameterNames (AritySignature _ arity) = replicate arity Nothing
parameterNames (TypedSignature _ types) = map (const Nothing) types
parameterNames (NamedSignature _ names) = names
parameterNames (AritySignature _ arity) = replicate arity Nothing
parameterNames (TypedSignature _ types _) = map (const Nothing) types
parameterNames (NamedSignature _ names) = names

signaturesOf :: Expression -> [Signature]
signaturesOf = nub . mapMaybe (signatureOf.snd) . declarationsOf mainExpressions

signatureOf :: Expression -> Maybe Signature
signatureOf (Subroutine name es) = Just $ NamedSignature name (parameterNamesOf es)
signatureOf (Clause name args _) = Just $ AritySignature name (length args)
signatureOf (TypeSignature name args) = Just $ TypedSignature name args
signatureOf (Variable name _) = Just $ AritySignature name 0
signatureOf _ = Nothing
signatureOf (Subroutine name es) = Just $ NamedSignature name (parameterNamesOf es)
signatureOf (Clause name args _) = Just $ AritySignature name (length args)
signatureOf (TypeSignature name args ret) = Just $ TypedSignature name args ret
signatureOf (Variable name _) = Just $ AritySignature name 0
signatureOf _ = Nothing

parameterNamesOf :: [Equation] -> [Maybe Binding]
parameterNamesOf = map msum . transpose . map (map parameterNameOf . equationParams)
Expand All @@ -77,9 +77,9 @@ styledCodeSignaturesOf style = style . signaturesOf
mulangStyle :: SignatureStyle
mulangStyle = makeLines "--" (return.s)
where s :: Signature -> String
s (AritySignature name arity) = name ++ "/" ++ show arity
s (NamedSignature name names) = name ++ "(" ++ (intercalate ", " . makeParamNames $ names) ++ ")"
s (TypedSignature name types) = name ++ " :: " ++ (intercalate " -> " types)
s (AritySignature name arity) = name ++ "/" ++ show arity
s (NamedSignature name names) = name ++ "(" ++ (intercalate ", " . makeParamNames $ names) ++ ")"
s (TypedSignature name types ret) = name ++ "(" ++ (intercalate ", " types) ++ "): " ++ ret

untypedCStyle :: SignatureStyle
untypedCStyle = makeLines "//" s
Expand All @@ -89,10 +89,10 @@ untypedCStyle = makeLines "//" s

haskellStyle :: SignatureStyle
haskellStyle = groupAndMakeLinesOn "--" name s
where s (NamedSignature name names) = Just $ name ++ " " ++ (intercalate " " . makeParamNames $ names)
s (TypedSignature name types) = Just $ name ++ " :: " ++ (intercalate " -> " types)
s (AritySignature name 0) = Just name
s _ = Nothing
where s (NamedSignature name names) = Just $ name ++ " " ++ (intercalate " " . makeParamNames $ names)
s (TypedSignature name types ret) = Just $ name ++ " :: " ++ (intercalate " -> " (types ++ [ret]))
s (AritySignature name 0) = Just name
s _ = Nothing

prologStyle :: SignatureStyle
prologStyle = groupAndMakeLinesOn "%%" nameAndArity s
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Mulang/Unfold.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mainExpressions o@(Object _ b) = o : mainExpressions b
mainExpressions c@(Class _ _ b) = c : mainExpressions b
mainExpressions c@(Interface _ _ b) = c : mainExpressions b
mainExpressions e@(EntryPoint _ b) = e : mainExpressions b
mainExpressions t@(TypeSignature _ _) = [t]
mainExpressions t@(TypeSignature _ _ _)= [t]
mainExpressions t@(TypeAlias _ ) = [t]
mainExpressions r@(Record _) = [r]
mainExpressions v@(Variable _ _) = [v]
Expand Down

0 comments on commit 75d5827

Please sign in to comment.