Skip to content

Commit

Permalink
Parser for tuples in macros
Browse files Browse the repository at this point in the history
This is a first step towards #262.
  • Loading branch information
edsko committed Nov 16, 2024
1 parent 447145c commit 74987ed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
4 changes: 4 additions & 0 deletions hs-bindgen/examples/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#define LONG_INT_TOKEN3 1844'6744'0737'0955'0592uLL
#define LONG_INT_TOKEN4 184467'440737'0'95505'92LLU

#define TUPLE1 ( 1 , 2 )
#define TUPLE2 3 , 4
#define TUPLE3 5, 6

// ---
// https://en.cppreference.com/w/cpp/language/floating_literal
// (1)
Expand Down
3 changes: 3 additions & 0 deletions hs-bindgen/src/HsBindgen/C/AST/Macro.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Data.String
import Data.Text qualified as Text
import Data.Type.Equality
( type (:~:)(..) )
import Data.Type.Nat (SNatI)
import GHC.Generics (Generic)
import System.FilePath (takeBaseName)
import Text.Show.Pretty (PrettyVal(..))
Expand Down Expand Up @@ -117,6 +118,8 @@ data MFun arity where
MLogicalAnd :: MFun ( S ( S Z ) )
-- | @||@
MLogicalOr :: MFun ( S ( S Z ) )
-- | Tuples
MTuple :: SNatI n => MFun ( S ( S n ) )

deriving stock instance Show ( MFun arity )
deriving stock instance Eq ( MFun arity )
Expand Down
13 changes: 11 additions & 2 deletions hs-bindgen/src/HsBindgen/C/Reparse/Macro.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module HsBindgen.C.Reparse.Macro (
reparseMacro
) where

import Data.Type.Nat
import Data.Vec.Lazy
import Text.Parsec
import Text.Parsec.Expr
Expand All @@ -18,7 +19,6 @@ import HsBindgen.C.Reparse.Type
import HsBindgen.Clang.HighLevel.Types
import HsBindgen.Clang.LowLevel.Core


{-------------------------------------------------------------------------------
Top-level
Expand Down Expand Up @@ -113,7 +113,7 @@ actualArgs = parens $ mExpr `sepBy` comma

mExpr :: Reparse MExpr
mExpr =
buildExpressionParser ops term <?> "expression"
(tup <$> buildExpressionParser ops term `sepBy1` comma) <?> "expression"
where
term :: Reparse MExpr
term = choice [
Expand Down Expand Up @@ -169,5 +169,14 @@ mExpr =
, [ Infix (ap2 MLogicalOr <$ punctuation "||") AssocLeft ]
]

ap1 :: MFun (S Z) -> MExpr -> MExpr
ap1 op arg = MApp op ( arg ::: VNil )

ap2 :: MFun (S (S Z)) -> MExpr -> MExpr -> MExpr
ap2 op arg1 arg2 = MApp op ( arg1 ::: arg2 ::: VNil )

tup :: [MExpr] -> MExpr
tup [] = error "apSN: empty list" -- sepBy1 should give us @NonEmpty@
tup [e] = e
tup (e1 : e2 : es) = reifyList es $ \es' -> MApp MTuple (e1 ::: e2 ::: es')

1 change: 1 addition & 0 deletions hs-bindgen/src/HsBindgen/C/Tc/Macro.hs
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,7 @@ inferMFun = \case
MBitwiseOr -> q1 $ \ a -> QuantTyBody [Bits a] ( funTy [a,a] a )
MLogicalAnd -> q0 $ QuantTyBody [] ( funTy [Bool, Bool] Bool )
MLogicalOr -> q0 $ QuantTyBody [] ( funTy [Bool, Bool] Bool )
_otherwise -> QuantTy @Z $ \VNil -> QuantTyBody [] Empty -- TODO
where
q0 body = QuantTy @Z $ \ VNil -> body
q1 body = QuantTy @( S Z ) $ \ (a ::: VNil) -> body a
Expand Down

0 comments on commit 74987ed

Please sign in to comment.