Code Monkey home page Code Monkey logo

language-c99-util's People

Contributors

fdedden avatar ryanglscott avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

language-c99-util's Issues

`litfloat` produces a `double` literal rather than a `float` literal

Intuitively, I would expect the litfloat function to return a floating-point literal suffixed with F or f, as laid out in section 6.4.4.2 of the C99 spec. However, this doesn't happen in practice:

λ> import Language.C99.Pretty (pretty)
λ> import Language.C99.Util (litfloat, litdouble)
λ> pretty (litfloat 42)
42.0
λ> pretty (litdouble 42)
42.0

Both litfloat 42 and litdouble 42 produce double literals, which require a cast to be used as a float. A more direct approach would be to make litfloat 42 be printed as 42.0f. That is, do something like this:

diff --git a/src/Language/C99/Util/Expr.hs b/src/Language/C99/Util/Expr.hs
index c9a858d..0de1054 100644
--- a/src/Language/C99/Util/Expr.hs
+++ b/src/Language/C99/Util/Expr.hs
@@ -91,9 +91,9 @@ litint i | i == 0 = UnaryPostfix $ PostfixPrim $ constzero
          | i >  0 = UnaryPostfix $ PostfixPrim $ constint i
          | i <  0 = UnaryOp UOMin (CastUnary $ litint (abs i))
 
-litdouble :: Double -> UnaryExpr
-litdouble = parse . lex where
-  lex :: Double -> (String, String, String)
+litfp :: (RealFloat a, Show a) => Maybe FloatSuffix -> a -> UnaryExpr
+litfp mbFS = parse . lex where
+  lex :: (RealFloat a, Show a) => a -> (String, String, String)
   lex d | isInfinite d = error "Can't translate an infinite floating point number:"
         | otherwise    = (nat, dec, exp) where
     ds = show d
@@ -105,7 +105,7 @@ litdouble = parse . lex where
       _ -> tail e
 
   parse :: (String, String, String) -> UnaryExpr
-  parse (nat, dec, exp) = op $ PostfixPrim $ PrimConst $ ConstFloat $ FloatDec $ DecFloatFrac (FracZero (Just nat') dec') exp' Nothing where
+  parse (nat, dec, exp) = op $ PostfixPrim $ PrimConst $ ConstFloat $ FloatDec $ DecFloatFrac (FracZero (Just nat') dec') exp' mbFS where
     op = case head nat of
       '-' -> UnaryOp UOMin . CastUnary . UnaryPostfix
       _   -> UnaryPostfix
@@ -119,8 +119,11 @@ litdouble = parse . lex where
         '-' -> Just $ E (Just SMinus) (digitseq $ digitsc es)
         _   -> Just $ E Nothing (digitseq $ digitsc (e:es))
 
+litdouble :: Double -> UnaryExpr
+litdouble = litfp Nothing
+
 litfloat :: Float -> UnaryExpr
-litfloat = litdouble . realToFrac
+litfloat = litfp (Just FF)
 
 litstring :: String -> UnaryExpr
 litstring ss = wrap $ PrimString $ StringLit $ (sl ss) where

Build failure with GHC 9.2.1

src/Language/C99/Util/IsList.hs:25:10: warning: [-Wmissing-methods]
    • No explicit implementation for
        ‘toList’
    • In the instance declaration for ‘IsList ArgExprList’
   |
25 | instance IsList ArgExprList where
   |          ^^^^^^^^^^^^^^^^^^

src/Language/C99/Util/IsList.hs:30:10: warning: [-Wmissing-methods]
    • No explicit implementation for
        ‘toList’
    • In the instance declaration for ‘IsList InitList’
   |
30 | instance IsList InitList where
   |          ^^^^^^^^^^^^^^^

src/Language/C99/Util/IsList.hs:37:10: warning: [-Wmissing-methods]
    • No explicit implementation for
        ‘toList’
    • In the instance declaration for ‘IsList BlockItemList’
   |
37 | instance IsList BlockItemList where
   |          ^^^^^^^^^^^^^^^^^^^^

src/Language/C99/Util/IsList.hs:42:10: warning: [-Wmissing-methods]
    • No explicit implementation for
        ‘toList’
    • In the instance declaration for ‘IsList TransUnit’
   |
42 | instance IsList TransUnit where
   |          ^^^^^^^^^^^^^^^^

src/Language/C99/Util/IsList.hs:47:10: warning: [-Wmissing-methods]
    • No explicit implementation for
        ‘toList’
    • In the instance declaration for ‘IsList DeclnList’
   |
47 | instance IsList DeclnList where
   |          ^^^^^^^^^^^^^^^^
[2 of 4] Compiling Language.C99.Util.Wrap ( src/Language/C99/Util/Wrap.hs, dist/build/Language/C99/Util/Wrap.o, dist/build/Language/C99/Util/Wrap.dyn_o )

src/Language/C99/Util/Wrap.hs:78:31: error:
    • Non type-variable argument in the constraint: Wrap b PrimExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a PrimExpr’
   |
78 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b PrimExpr)
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:82:31: error:
    • Non type-variable argument in the constraint: Wrap b PostfixExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a PostfixExpr’
   |
82 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b PostfixExpr)
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:86:31: error:
    • Non type-variable argument in the constraint: Wrap b UnaryExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a UnaryExpr’
   |
86 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b UnaryExpr)
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:90:31: error:
    • Non type-variable argument in the constraint: Wrap b CastExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a CastExpr’
   |
90 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b CastExpr)
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:94:31: error:
    • Non type-variable argument in the constraint: Wrap b MultExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a MultExpr’
   |
94 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b MultExpr)
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:98:31: error:
    • Non type-variable argument in the constraint: Wrap b AddExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a AddExpr’
   |
98 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b AddExpr)
   |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:102:31: error:
    • Non type-variable argument in the constraint: Wrap b ShiftExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a ShiftExpr’
    |
102 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b ShiftExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:106:31: error:
    • Non type-variable argument in the constraint: Wrap b RelExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a RelExpr’
    |
106 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b RelExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:110:31: error:
    • Non type-variable argument in the constraint: Wrap b EqExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a EqExpr’
    |
110 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b EqExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:114:31: error:
    • Non type-variable argument in the constraint: Wrap b AndExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a AndExpr’
    |
114 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b AndExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:118:31: error:
    • Non type-variable argument in the constraint: Wrap b OrExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a OrExpr’
    |
118 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b OrExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:122:31: error:
    • Non type-variable argument in the constraint: Wrap b XOrExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a XOrExpr’
    |
122 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b XOrExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:126:31: error:
    • Non type-variable argument in the constraint: Wrap b LAndExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a LAndExpr’
    |
126 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b LAndExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:130:31: error:
    • Non type-variable argument in the constraint: Wrap b LOrExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a LOrExpr’
    |
130 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b LOrExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:134:31: error:
    • Non type-variable argument in the constraint: Wrap b CondExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a CondExpr’
    |
134 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b CondExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:138:31: error:
    • Non type-variable argument in the constraint: Wrap b AssignExpr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a AssignExpr’
    |
138 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b AssignExpr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

src/Language/C99/Util/Wrap.hs:142:31: error:
    • Non type-variable argument in the constraint: Wrap b Expr
      (Use FlexibleContexts to permit this)
    • In the instance declaration for ‘Wrap a Expr’
    |
142 | instance {-# OVERLAPPABLE #-} (WrapStep a b, Wrap b Expr)
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^...
cabal: Failed to build language-c99-util-0.1.1.

I think the error is related to this: https://gitlab.haskell.org/ghc/ghc/-/wikis/migration/9.2#undecidableinstances-no-longer-implies-flexiblecontexts-in-instance-declarations

v0.1.0.0 produces a similar error.

`litdouble` fails on `INFINITY` and `NAN` values

$ cabal repl -b ieee754 -b language-c99-util
<snip>
λ> import Language.C99.Pretty (pretty)
λ> import Language.C99.Util (litdouble)
λ> import Numeric.IEEE (IEEE(..))
λ> pretty (litdouble infinity)
*** Exception: Can't translate an infinite floating point number:
CallStack (from HasCallStack):
  error, called at src/Language/C99/Util/Expr.hs:97:26 in language-c99-util-0.1.1-5c88c936679457490f9da31103e55d54f5b3e7222fb03697d5ac08b8412de52c:Language.C99.Util.Expr
λ> pretty (litdouble nan)
*** Exception: Prelude.tail: empty list

It seems like we could make litdouble a total function by having it generate the INFINITY/NAN macros as appropriate.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.