Skip to content

Commit

Permalink
Merge pull request #358 from mumuki/feature-generalize-declares-compu…
Browse files Browse the repository at this point in the history
…tation

Feature generalize declares computation
  • Loading branch information
flbulgarelli committed Mar 21, 2023
2 parents 3208666 + 6246354 commit be71808
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
8 changes: 4 additions & 4 deletions gem/lib/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ en:
DeclaresClass_like: '%{binding} %{must} declare a class similar to %{target}'
DeclaresClass_named: '%{binding} %{must} declare a class %{target}'
DeclaresClass: '%{binding} %{must} declare classes'
DeclaresComputation_except: '%{binding} %{must} declare computations distinct from %{target}'
DeclaresComputation_like: '%{binding} %{must} declare a computation similar to %{target}'
DeclaresComputation_named: '%{binding} %{must} declare a computation %{target}'
DeclaresComputation: '%{binding} %{must} declare computations'
DeclaresComputation_except: '%{binding} %{must} declare computations distinct from %{target}%{matching}'
DeclaresComputation_like: '%{binding} %{must} declare a computation similar to %{target}%{matching}'
DeclaresComputation_named: '%{binding} %{must} declare a computation %{target}%{matching}'
DeclaresComputation: '%{binding} %{must} declare computations%{matching}'
DeclaresComputationWithArity0_named: '%{target} %{must} have zero parameters'
DeclaresComputationWithArity1_named: '%{target} %{must} have one parameter'
DeclaresComputationWithArity2_named: '%{target} %{must} have 2 parameters'
Expand Down
8 changes: 4 additions & 4 deletions gem/lib/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ es:
DeclaresClass_like: '%{binding} %{must} declarar una clase parecida a %{target}'
DeclaresClass_named: '%{binding} %{must} declarar una clase %{target}'
DeclaresClass: '%{binding} %{must} declarar clases'
DeclaresComputation_except: '%{binding} %{must} declarar computaciones diferentes a %{target}'
DeclaresComputation_like: '%{binding} %{must} declarar una computación parecida a %{target}'
DeclaresComputation_named: '%{binding} %{must} declarar una computación %{target}'
DeclaresComputation: '%{binding} %{must} declarar computaciones'
DeclaresComputation_except: '%{binding} %{must} declarar computaciones diferentes a %{target}%{matching}'
DeclaresComputation_like: '%{binding} %{must} declarar una computación parecida a %{target}%{matching}'
DeclaresComputation_named: '%{binding} %{must} declarar una computación %{target}%{matching}'
DeclaresComputation: '%{binding} %{must} declarar computaciones%{matching}'
DeclaresComputationWithArity0_named: '%{target} %{must} declarar cero parametros'
DeclaresComputationWithArity1_named: '%{target} %{must} tener un parámetro'
DeclaresComputationWithArity2_named: '%{target} %{must} tener dos parámetros'
Expand Down
8 changes: 4 additions & 4 deletions gem/lib/locales/pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ pt:
DeclaresClass_like: '%{binding} %{must} declarar uma classe semelhante a %{target}'
DeclaresClass_named: '%{binding} %{must} declarar uma classe %{target}'
DeclaresClass: '%{binding} %{must} declarar classes'
DeclaresComputation_except: '%{binding} %{must} declarar cálculos diferentes de %{target}'
DeclaresComputation_like: '%{binding} %{must} declarar uma computação semelhante a %{target}'
DeclaresComputation_named: '%{binding} %{must} declarar uma computação %{target}'
DeclaresComputation: '%{binding} %{must} declarar cálculos'
DeclaresComputation_except: '%{binding} %{must} declarar cálculos diferentes de %{target}%{matching}'
DeclaresComputation_like: '%{binding} %{must} declarar uma computação semelhante a %{target}%{matching}'
DeclaresComputation_named: '%{binding} %{must} declarar uma computação %{target}%{matching}'
DeclaresComputation: '%{binding} %{must} declarar cálculos%{matching}'
DeclaresComputationWithArity0_named: '%{target} %{must} declarar parâmetros zero'
DeclaresComputationWithArity1_named: '%{target} %{must} ter um parâmetro'
DeclaresComputationWithArity2_named: '%{target} %{must} ter dois parâmetros'
Expand Down
26 changes: 26 additions & 0 deletions spec/GenericSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,32 @@ spec = do
it "is True when exists" $ do
declaresComputation (named "x") (hs "x _ = True") `shouldBe` True

describe "with matchers" $ do
it "is True when using a matcher that matches" $ do
(declaresComputationMatching (with isSelf) anyone) (js "function f(x) { this; return 0; }") `shouldBe` True
(declaresComputationMatching (with isSelf) anyone) (js "function f(x) { this; }") `shouldBe` True

it "is False when using a matcher that does not match" $ do
(declaresComputationMatching (with isSelf) anyone) (js "function f(x) { return 0; }") `shouldBe` False
(declaresComputationMatching (with isSelf) anyone) (js "function f(x) { }") `shouldBe` False

it "is True when using a non literal returns matcher that matches" $ do
(declaresComputationMatching (with (returnsMatching (with (isNumber 2)))) anyone) (js "function f() { return 2; }") `shouldBe` True

it "is False when using a non literal returns matcher that doesn't match" $ do
(declaresComputationMatching (with (returnsMatching (with (isNumber 2)))) anyone) (js "function f() { return 3; }") `shouldBe` False

it "is True when using a non literal assigns matcher that matches" $ do
(declaresComputationMatching (with (assignsMatching (with (isNumber 2)) anyone)) anyone) (js "function f() { window.aGlobal = 2; }") `shouldBe` True

it "is False when using a non literal assigns matcher that doesn't match" $ do
(declaresComputationMatching (with (assignsMatching (with (isNumber 2)) anyone)) anyone) (js "function f() { window.aGlobal = 3; }") `shouldBe` False

it "is False when using a literal matcher and it does not match literally" $ do
(declaresComputationMatching (with . isNumber $ 2) anyone) (js "function f() { return 2; }") `shouldBe` False
(declaresComputationMatching (with . isNumber $ 2) anyone) (js "function f() { window.aGlobal = 2 }") `shouldBe` False


describe "declares" $ do
describe "with constants" $ do
it "is True when exists" $ do
Expand Down
14 changes: 7 additions & 7 deletions src/Language/Mulang/Analyzer/EdlQueryCompiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ compileInspection = f
f "Declares" E.Unmatching = bound declares
f "DeclaresAttribute" m = boundMatching declaresAttributeMatching m
f "DeclaresClass" m = boundMatching declaresClassMatching m
f "DeclaresComputation" E.Unmatching = bound declaresComputation
f "DeclaresComputationWithArity0" E.Unmatching = bound (declaresComputationWithArity 0)
f "DeclaresComputationWithArity1" E.Unmatching = bound (declaresComputationWithArity 1)
f "DeclaresComputationWithArity2" E.Unmatching = bound (declaresComputationWithArity 2)
f "DeclaresComputationWithArity3" E.Unmatching = bound (declaresComputationWithArity 3)
f "DeclaresComputationWithArity4" E.Unmatching = bound (declaresComputationWithArity 4)
f "DeclaresComputationWithArity5" E.Unmatching = bound (declaresComputationWithArity 5)
f "DeclaresComputation" m = boundMatching declaresComputationMatching m
f "DeclaresComputationWithArity0" m = boundMatching (declaresComputationWithArityMatching 0) m
f "DeclaresComputationWithArity1" m = boundMatching (declaresComputationWithArityMatching 1) m
f "DeclaresComputationWithArity2" m = boundMatching (declaresComputationWithArityMatching 2) m
f "DeclaresComputationWithArity3" m = boundMatching (declaresComputationWithArityMatching 3) m
f "DeclaresComputationWithArity4" m = boundMatching (declaresComputationWithArityMatching 4) m
f "DeclaresComputationWithArity5" m = boundMatching (declaresComputationWithArityMatching 5) m
f "DeclaresEntryPoint" m = boundMatching declaresEntryPointMatching m
f "DeclaresEnumeration" E.Unmatching = bound declaresEnumeration
f "DeclaresFact" E.Unmatching = bound declaresFact
Expand Down
23 changes: 15 additions & 8 deletions src/Language/Mulang/Inspector/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module Language.Mulang.Inspector.Generic (
declares,
declaresComputation,
declaresComputationWithArity,
declaresComputationWithArity',
declaresComputationMatching,
declaresComputationWithArityMatching,
declaresEntryPoint,
declaresEntryPointMatching,
declaresFunction,
Expand Down Expand Up @@ -187,16 +188,22 @@ declaresEntryPointMatching matcher = containsBoundDeclaration f
f _ = False

-- | Inspection that tells whether a top level computation declaration exists
declaresComputation :: BoundInspection
declaresComputation = declaresComputationWithArity' (const True)
declaresComputation ::BoundInspection
declaresComputation = unmatching declaresComputationMatching

declaresComputationMatching :: Matcher -> BoundInspection
declaresComputationMatching = declaresComputationWithArityMatching' (const True)

declaresComputationWithArity :: Int -> BoundInspection
declaresComputationWithArity arity = declaresComputationWithArity' (== arity)
declaresComputationWithArity arity = unmatching (declaresComputationWithArityMatching arity)

declaresComputationWithArityMatching :: Int -> Matcher -> BoundInspection
declaresComputationWithArityMatching arity = declaresComputationWithArityMatching' (== arity)

declaresComputationWithArity' :: (Int -> Bool) -> BoundInspection
declaresComputationWithArity' arityPredicate = containsBoundDeclaration f
where f (Subroutine _ es) = any equationArityIs es
f (Clause _ args _) = argsHaveArity args
declaresComputationWithArityMatching' :: (Int -> Bool) -> Matcher -> BoundInspection
declaresComputationWithArityMatching' arityPredicate matcher = containsBoundDeclaration f
where f (Subroutine _ eqs) = any equationArityIs eqs && matches matcher equationsExpandedExpressions eqs
f (Clause _ args es) = argsHaveArity args && matcher es
f _ = False

equationArityIs (Equation args _) = argsHaveArity args
Expand Down

0 comments on commit be71808

Please sign in to comment.