From 493399c74ead7ee3e6ec8d40d176823143d291e7 Mon Sep 17 00:00:00 2001 From: joshvera Date: Thu, 21 Jun 2018 15:27:49 -0400 Subject: [PATCH] Split up BooleanOperator --- src/Data/Syntax/Expression.hs | 74 ++++++++++++++++++--------- src/Data/Term.hs | 1 + src/Language/Go/Assignment.hs | 6 ++- src/Language/Java/Assignment.hs | 5 +- src/Language/PHP/Assignment.hs | 5 +- src/Language/Python/Assignment.hs | 5 +- src/Language/Ruby/Assignment.hs | 8 ++- src/Language/TypeScript/Assignment.hs | 5 +- 8 files changed, 78 insertions(+), 31 deletions(-) diff --git a/src/Data/Syntax/Expression.hs b/src/Data/Syntax/Expression.hs index 1a7c75105..d0ac70c3b 100644 --- a/src/Data/Syntax/Expression.hs +++ b/src/Data/Syntax/Expression.hs @@ -77,40 +77,68 @@ instance Evaluatable Arithmetic where go (FloorDivision a b) = liftNumeric2 liftedFloorDiv a b -- | Regex matching operators (Ruby's =~ and ~!) -data RegexMatch a - = Matches !a !a - | NotMatches !a !a +data Matches a = Matches { lhs :: a, rhs :: a } deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) -instance Eq1 RegexMatch where liftEq = genericLiftEq -instance Ord1 RegexMatch where liftCompare = genericLiftCompare -instance Show1 RegexMatch where liftShowsPrec = genericLiftShowsPrec +instance Eq1 Matches where liftEq = genericLiftEq +instance Ord1 Matches where liftCompare = genericLiftCompare +instance Show1 Matches where liftShowsPrec = genericLiftShowsPrec +instance Evaluatable Matches --- TODO: Implement Eval instance for Match -instance Evaluatable RegexMatch - --- | Boolean operators. -data BooleanOperator a - = Or !a !a - | And !a !a - | Not !a - | XOr !a !a +data NotMatches a = NotMatches { lhs :: a, rhs :: a } deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) -instance Eq1 BooleanOperator where liftEq = genericLiftEq -instance Ord1 BooleanOperator where liftCompare = genericLiftCompare -instance Show1 BooleanOperator where liftShowsPrec = genericLiftShowsPrec +instance Eq1 NotMatches where liftEq = genericLiftEq +instance Ord1 NotMatches where liftCompare = genericLiftCompare +instance Show1 NotMatches where liftShowsPrec = genericLiftShowsPrec +instance Evaluatable NotMatches -instance Evaluatable BooleanOperator where - -- N.B. we have to use Monad rather than Applicative/Traversable on 'And' and 'Or' so that we don't evaluate both operands +data Or a = Or { lhs :: a, rhs :: a } + deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) + +instance Eq1 Or where liftEq = genericLiftEq +instance Ord1 Or where liftCompare = genericLiftCompare +instance Show1 Or where liftShowsPrec = genericLiftShowsPrec + +instance Evaluatable Or where + eval t = rvalBox =<< go (fmap subtermValue t) where + go (Or a b) = do + cond <- a + ifthenelse cond (pure cond) b + +data And a = And { lhs :: a, rhs :: a } + deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) + +instance Eq1 And where liftEq = genericLiftEq +instance Ord1 And where liftCompare = genericLiftCompare +instance Show1 And where liftShowsPrec = genericLiftShowsPrec +instance Evaluatable And where eval t = rvalBox =<< go (fmap subtermValue t) where go (And a b) = do cond <- a ifthenelse cond b (pure cond) - go (Or a b) = do - cond <- a - ifthenelse cond (pure cond) b + +data Not a = Not { term :: a } + deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) + +instance Eq1 Not where liftEq = genericLiftEq +instance Ord1 Not where liftCompare = genericLiftCompare +instance Show1 Not where liftShowsPrec = genericLiftShowsPrec + +instance Evaluatable Not where + eval t = rvalBox =<< go (fmap subtermValue t) where go (Not a) = a >>= fmap (boolean . not) . asBool + +data XOr a = XOr { lhs :: a, rhs :: a } + deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) + +instance Eq1 XOr where liftEq = genericLiftEq +instance Ord1 XOr where liftCompare = genericLiftCompare +instance Show1 XOr where liftShowsPrec = genericLiftShowsPrec + +instance Evaluatable XOr where + -- N.B. we have to use Monad rather than Applicative/Traversable on 'And' and 'Or' so that we don't evaluate both operands + eval t = rvalBox =<< go (fmap subtermValue t) where go (XOr a b) = boolean <$> liftA2 (/=) (a >>= asBool) (b >>= asBool) -- | Javascript delete operator diff --git a/src/Data/Term.hs b/src/Data/Term.hs index ded401444..35004d985 100644 --- a/src/Data/Term.hs +++ b/src/Data/Term.hs @@ -17,6 +17,7 @@ import Data.JSON.Fields import Data.Record import Text.Show import Proto3.Suite.Class +import Proto3.Suite.DotProto import qualified Proto3.Wire.Encode as Encode import qualified Proto3.Wire.Decode as Decode diff --git a/src/Language/Go/Assignment.hs b/src/Language/Go/Assignment.hs index 924a89aeb..c975d9396 100644 --- a/src/Language/Go/Assignment.hs +++ b/src/Language/Go/Assignment.hs @@ -41,14 +41,16 @@ type Syntax = , Expression.RShift , Expression.UnsignedRShift , Expression.Complement - , Expression.BooleanOperator , Expression.Call , Expression.Comparison , Expression.Subscript , Statement.PostDecrement , Statement.PostIncrement , Expression.MemberAccess - , Expression.BooleanOperator + , Expression.And + , Expression.Not + , Expression.Or + , Expression.XOr , Expression.Call , Expression.Comparison , Expression.Subscript diff --git a/src/Language/Java/Assignment.hs b/src/Language/Java/Assignment.hs index 5c0259178..a5959d3e3 100644 --- a/src/Language/Java/Assignment.hs +++ b/src/Language/Java/Assignment.hs @@ -44,7 +44,10 @@ type Syntax = , Expression.RShift , Expression.UnsignedRShift , Expression.Complement - , Expression.BooleanOperator + , Expression.And + , Expression.Not + , Expression.Or + , Expression.XOr , Expression.InstanceOf , Expression.MemberAccess , Expression.Subscript diff --git a/src/Language/PHP/Assignment.hs b/src/Language/PHP/Assignment.hs index 6220c217d..945cee9b5 100644 --- a/src/Language/PHP/Assignment.hs +++ b/src/Language/PHP/Assignment.hs @@ -47,7 +47,10 @@ type Syntax = '[ , Expression.BXOr , Expression.LShift , Expression.RShift - , Expression.BooleanOperator + , Expression.And + , Expression.Not + , Expression.Or + , Expression.XOr , Expression.Call , Expression.Cast , Expression.Comparison diff --git a/src/Language/Python/Assignment.hs b/src/Language/Python/Assignment.hs index 1c70083dc..fc49cd68c 100644 --- a/src/Language/Python/Assignment.hs +++ b/src/Language/Python/Assignment.hs @@ -49,7 +49,10 @@ type Syntax = , Declaration.Function , Declaration.Variable , Expression.Arithmetic - , Expression.BooleanOperator + , Expression.And + , Expression.Not + , Expression.Or + , Expression.XOr , Expression.BAnd , Expression.BOr , Expression.BXOr diff --git a/src/Language/Ruby/Assignment.hs b/src/Language/Ruby/Assignment.hs index b0ebec210..84c18cc59 100644 --- a/src/Language/Ruby/Assignment.hs +++ b/src/Language/Ruby/Assignment.hs @@ -51,11 +51,15 @@ type Syntax = '[ , Expression.LShift , Expression.RShift , Expression.Complement - , Expression.BooleanOperator + , Expression.And + , Expression.Not + , Expression.Or + , Expression.XOr , Expression.Call , Expression.Comparison , Expression.Enumeration - , Expression.RegexMatch + , Expression.Matches + , Expression.NotMatches , Expression.MemberAccess , Expression.ScopeResolution , Expression.Subscript diff --git a/src/Language/TypeScript/Assignment.hs b/src/Language/TypeScript/Assignment.hs index 234f8ce6b..9b187929a 100644 --- a/src/Language/TypeScript/Assignment.hs +++ b/src/Language/TypeScript/Assignment.hs @@ -55,7 +55,10 @@ type Syntax = '[ , Expression.RShift , Expression.UnsignedRShift , Expression.Complement - , Expression.BooleanOperator + , Expression.And + , Expression.Not + , Expression.Or + , Expression.XOr , Expression.Call , Expression.Cast , Expression.Comparison