1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 13:51:44 +03:00

Ensure that floats can be evaluated.

This commit is contained in:
Patrick Thomson 2018-03-07 14:38:02 -05:00
parent 86697b46d1
commit ad4e7e83f4
6 changed files with 20 additions and 6 deletions

View File

@ -10,6 +10,7 @@ import Data.Abstract.Environment
import Data.Abstract.FreeVariables
import Data.Abstract.Value as Value
import Data.Abstract.Type as Type
import Data.Scientific (Scientific)
import Prologue
import Prelude hiding (fail)
@ -29,6 +30,9 @@ class (MonadEvaluator t v m) => MonadValue t v m where
-- | Construct an abstract string value.
string :: ByteString -> m v
-- | Construct a floating-point value.
float :: Scientific -> m v
-- | Eliminate boolean values. TODO: s/boolean/truthy
ifthenelse :: v -> m v -> m v -> m v
@ -51,6 +55,7 @@ instance ( FreeVariables t
integer = pure . inj . Integer
boolean = pure . inj . Boolean
string = pure . inj . Value.String
float = pure . inj . Value.Float
ifthenelse cond if' else'
| Just (Boolean b) <- prj cond = if b then if' else else'
@ -83,6 +88,7 @@ instance (Alternative m, MonadEvaluator t Type m, MonadFresh m) => MonadValue t
integer _ = pure Int
boolean _ = pure Bool
string _ = pure Type.String
float _ = pure Type.Float
ifthenelse cond if' else' = unify cond Bool *> (if' <|> else')

View File

@ -13,6 +13,7 @@ data Type
| Bool -- ^ Primitive boolean type.
| String -- ^ Primitive string type.
| Unit -- ^ The unit type.
| Float -- ^ Floating-point type.
| Type :-> Type -- ^ Binary function types.
| Var TName -- ^ A type variable.
| Product [Type] -- ^ N-ary products.

View File

@ -8,8 +8,9 @@ import Data.Abstract.FreeVariables
import Data.Abstract.Live
import qualified Data.Abstract.Type as Type
import qualified Data.Set as Set
import Data.Scientific (Scientific)
import Prologue
import Prelude hiding (Integer, String, fail)
import Prelude hiding (Float, Integer, String, fail)
import qualified Prelude
type ValueConstructors location
@ -17,6 +18,7 @@ type ValueConstructors location
, Interface location
, Unit
, Boolean
, Float
, Integer
, String
]
@ -75,6 +77,14 @@ instance Eq1 String where liftEq = genericLiftEq
instance Ord1 String where liftCompare = genericLiftCompare
instance Show1 String where liftShowsPrec = genericLiftShowsPrec
-- | Float values.
newtype Float term = Float Scientific
deriving (Eq, Generic1, Ord, Show)
instance Eq1 Float where liftEq = genericLiftEq
instance Ord1 Float where liftCompare = genericLiftCompare
instance Show1 Float where liftShowsPrec = genericLiftShowsPrec
-- | The environment for an abstract value type.
type EnvironmentFor v = Environment (LocationFor v) v

View File

@ -6,7 +6,6 @@ import Data.ByteString.Char8 (readInteger, unpack)
import qualified Data.ByteString.Char8 as B
import Data.Monoid (Endo (..), appEndo)
import Data.Scientific (Scientific)
import Data.Word (Word8)
import Diffing.Algorithm
import Prelude hiding (Float, fail)
import Prologue hiding (Set)
@ -87,8 +86,8 @@ buildFloat preds val =
Nothing -> fail ("Invalid floating-point value: " <> show val)
Just s -> pure (Float s val)
-- TODO: Implement Eval instance for Float
instance Evaluatable Data.Syntax.Literal.Float
instance Evaluatable Data.Syntax.Literal.Float where
eval (Float s _) = float s
-- Rational literals e.g. `2/3r`
newtype Rational a = Rational ByteString

View File

@ -17,7 +17,6 @@ import GHC.Stack
import Language.Python.Grammar as Grammar
import Language.Python.Syntax as Python.Syntax
import qualified Assigning.Assignment as Assignment
import qualified Data.ByteString as B
import qualified Data.Syntax as Syntax
import qualified Data.Syntax.Comment as Comment
import qualified Data.Syntax.Declaration as Declaration

View File

@ -12,7 +12,6 @@ import Data.Record
import Data.List (elem)
import Data.Syntax (contextualize, postContextualize, emptyTerm, parseError, handleError, infixContext, makeTerm, makeTerm', makeTerm'', makeTerm1)
import qualified Assigning.Assignment as Assignment
import qualified Data.ByteString as B
import qualified Data.Syntax as Syntax
import qualified Data.Syntax.Comment as Comment
import qualified Data.Syntax.Declaration as Declaration