1
1
mirror of https://github.com/github/semantic.git synced 2025-01-02 04:10:29 +03:00

Split up BooleanOperator

This commit is contained in:
joshvera 2018-06-21 15:27:49 -04:00
parent cf709a7edb
commit 493399c74e
8 changed files with 78 additions and 31 deletions

View File

@ -77,40 +77,68 @@ instance Evaluatable Arithmetic where
go (FloorDivision a b) = liftNumeric2 liftedFloorDiv a b go (FloorDivision a b) = liftNumeric2 liftedFloorDiv a b
-- | Regex matching operators (Ruby's =~ and ~!) -- | Regex matching operators (Ruby's =~ and ~!)
data RegexMatch a data Matches a = Matches { lhs :: a, rhs :: a }
= Matches !a !a
| NotMatches !a !a
deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1)
instance Eq1 RegexMatch where liftEq = genericLiftEq instance Eq1 Matches where liftEq = genericLiftEq
instance Ord1 RegexMatch where liftCompare = genericLiftCompare instance Ord1 Matches where liftCompare = genericLiftCompare
instance Show1 RegexMatch where liftShowsPrec = genericLiftShowsPrec instance Show1 Matches where liftShowsPrec = genericLiftShowsPrec
instance Evaluatable Matches
-- TODO: Implement Eval instance for Match data NotMatches a = NotMatches { lhs :: a, rhs :: a }
instance Evaluatable RegexMatch
-- | Boolean operators.
data BooleanOperator a
= Or !a !a
| And !a !a
| Not !a
| XOr !a !a
deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1) deriving (Declarations1, Diffable, Eq, Foldable, FreeVariables1, Functor, Generic1, Hashable1, Mergeable, Ord, Show, ToJSONFields1, Traversable, Named1, Message1)
instance Eq1 BooleanOperator where liftEq = genericLiftEq instance Eq1 NotMatches where liftEq = genericLiftEq
instance Ord1 BooleanOperator where liftCompare = genericLiftCompare instance Ord1 NotMatches where liftCompare = genericLiftCompare
instance Show1 BooleanOperator where liftShowsPrec = genericLiftShowsPrec instance Show1 NotMatches where liftShowsPrec = genericLiftShowsPrec
instance Evaluatable NotMatches
instance Evaluatable BooleanOperator where data Or a = Or { lhs :: a, rhs :: a }
-- N.B. we have to use Monad rather than Applicative/Traversable on 'And' and 'Or' so that we don't evaluate both operands 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 eval t = rvalBox =<< go (fmap subtermValue t) where
go (And a b) = do go (And a b) = do
cond <- a cond <- a
ifthenelse cond b (pure cond) ifthenelse cond b (pure cond)
go (Or a b) = do
cond <- a data Not a = Not { term :: a }
ifthenelse cond (pure cond) b 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 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) go (XOr a b) = boolean <$> liftA2 (/=) (a >>= asBool) (b >>= asBool)
-- | Javascript delete operator -- | Javascript delete operator

View File

@ -17,6 +17,7 @@ import Data.JSON.Fields
import Data.Record import Data.Record
import Text.Show import Text.Show
import Proto3.Suite.Class import Proto3.Suite.Class
import Proto3.Suite.DotProto
import qualified Proto3.Wire.Encode as Encode import qualified Proto3.Wire.Encode as Encode
import qualified Proto3.Wire.Decode as Decode import qualified Proto3.Wire.Decode as Decode

View File

@ -41,14 +41,16 @@ type Syntax =
, Expression.RShift , Expression.RShift
, Expression.UnsignedRShift , Expression.UnsignedRShift
, Expression.Complement , Expression.Complement
, Expression.BooleanOperator
, Expression.Call , Expression.Call
, Expression.Comparison , Expression.Comparison
, Expression.Subscript , Expression.Subscript
, Statement.PostDecrement , Statement.PostDecrement
, Statement.PostIncrement , Statement.PostIncrement
, Expression.MemberAccess , Expression.MemberAccess
, Expression.BooleanOperator , Expression.And
, Expression.Not
, Expression.Or
, Expression.XOr
, Expression.Call , Expression.Call
, Expression.Comparison , Expression.Comparison
, Expression.Subscript , Expression.Subscript

View File

@ -44,7 +44,10 @@ type Syntax =
, Expression.RShift , Expression.RShift
, Expression.UnsignedRShift , Expression.UnsignedRShift
, Expression.Complement , Expression.Complement
, Expression.BooleanOperator , Expression.And
, Expression.Not
, Expression.Or
, Expression.XOr
, Expression.InstanceOf , Expression.InstanceOf
, Expression.MemberAccess , Expression.MemberAccess
, Expression.Subscript , Expression.Subscript

View File

@ -47,7 +47,10 @@ type Syntax = '[
, Expression.BXOr , Expression.BXOr
, Expression.LShift , Expression.LShift
, Expression.RShift , Expression.RShift
, Expression.BooleanOperator , Expression.And
, Expression.Not
, Expression.Or
, Expression.XOr
, Expression.Call , Expression.Call
, Expression.Cast , Expression.Cast
, Expression.Comparison , Expression.Comparison

View File

@ -49,7 +49,10 @@ type Syntax =
, Declaration.Function , Declaration.Function
, Declaration.Variable , Declaration.Variable
, Expression.Arithmetic , Expression.Arithmetic
, Expression.BooleanOperator , Expression.And
, Expression.Not
, Expression.Or
, Expression.XOr
, Expression.BAnd , Expression.BAnd
, Expression.BOr , Expression.BOr
, Expression.BXOr , Expression.BXOr

View File

@ -51,11 +51,15 @@ type Syntax = '[
, Expression.LShift , Expression.LShift
, Expression.RShift , Expression.RShift
, Expression.Complement , Expression.Complement
, Expression.BooleanOperator , Expression.And
, Expression.Not
, Expression.Or
, Expression.XOr
, Expression.Call , Expression.Call
, Expression.Comparison , Expression.Comparison
, Expression.Enumeration , Expression.Enumeration
, Expression.RegexMatch , Expression.Matches
, Expression.NotMatches
, Expression.MemberAccess , Expression.MemberAccess
, Expression.ScopeResolution , Expression.ScopeResolution
, Expression.Subscript , Expression.Subscript

View File

@ -55,7 +55,10 @@ type Syntax = '[
, Expression.RShift , Expression.RShift
, Expression.UnsignedRShift , Expression.UnsignedRShift
, Expression.Complement , Expression.Complement
, Expression.BooleanOperator , Expression.And
, Expression.Not
, Expression.Or
, Expression.XOr
, Expression.Call , Expression.Call
, Expression.Cast , Expression.Cast
, Expression.Comparison , Expression.Comparison