Skip to content

Commit

Permalink
feat: Support extra braces in compound literal initialisers.
Browse files Browse the repository at this point in the history
This is needed by some C parsers, so we'll have to do it :(.
  • Loading branch information
iphydf committed Feb 17, 2022
1 parent 2258a2a commit b976618
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 27 deletions.
3 changes: 2 additions & 1 deletion src/Language/Cimple/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ data NodeF lexeme a
| AssignExpr a AssignOp a
| ParenExpr a
| CastExpr a a
| CompoundExpr a a
| CompoundExpr a a -- DEPRECATED
| CompoundLiteral a a
| SizeofExpr a
| SizeofType a
| LiteralExpr LiteralType lexeme
Expand Down
4 changes: 3 additions & 1 deletion src/Language/Cimple/MapAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,10 @@ instance MapAst itext otext (Node (Lexeme itext)) where
Fix <$> (ParenExpr <$> recurse expr)
CastExpr ty expr ->
Fix <$> (CastExpr <$> recurse ty <*> recurse expr)
CompoundExpr ty expr ->
CompoundExpr ty expr -> -- DEPRECATED
Fix <$> (CompoundExpr <$> recurse ty <*> recurse expr)
CompoundLiteral ty expr ->
Fix <$> (CompoundLiteral <$> recurse ty <*> recurse expr)
SizeofExpr expr ->
Fix <$> (SizeofExpr <$> recurse expr)
SizeofType ty ->
Expand Down
17 changes: 13 additions & 4 deletions src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,22 @@ Expr
: LhsExpr { $1 }
| ExprStmt { $1 }
| FunctionCall { $1 }
| CompoundExpr { $1 }
| CompoundLiteral { $1 }
| PureExpr(Expr) { $1 }

-- Allow `(Type){0}` to set struct values to all-zero.
CompoundExpr :: { StringNode }
CompoundExpr
: '(' QualType ')' '{' Expr '}' { Fix $ CompoundExpr $2 $5 }
CompoundLiteral :: { StringNode }
CompoundLiteral
: '(' QualType ')' '{' ZeroInitExpr '}' { Fix $ CompoundLiteral $2 $5 }

ZeroInitExpr :: { StringNode }
ZeroInitExpr
: ID_VAR { Fix $ VarExpr $1 }
| LIT_CHAR { Fix $ LiteralExpr Char $1 }
| LIT_INTEGER { Fix $ LiteralExpr Int $1 }
| LIT_FALSE { Fix $ LiteralExpr Bool $1 }
| ID_CONST { Fix $ LiteralExpr ConstId $1 }
| '{' ZeroInitExpr '}' { Fix $ InitialiserList [$2] }

AssignExpr :: { StringNode }
AssignExpr
Expand Down
39 changes: 20 additions & 19 deletions src/Language/Cimple/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,26 @@ ppNode = foldFix go
Commented c d ->
c <$> d

VarExpr var -> ppLexeme var
LiteralExpr _ l -> dullred $ ppLexeme l
SizeofExpr arg -> kwSizeof <> parens arg
SizeofType arg -> kwSizeof <> parens arg
BinaryExpr l o r -> l <+> ppBinaryOp o <+> r
AssignExpr l o r -> l <+> ppAssignOp o <+> r
TernaryExpr c t e -> ppTernaryExpr c t e
UnaryExpr o e -> ppUnaryOp o <> e
ParenExpr e -> parens e
FunctionCall c a -> ppFunctionCall c a
ArrayAccess e i -> e <> char '[' <> i <> char ']'
CastExpr ty e -> parens ty <> e
CompoundExpr ty e -> parens ty <+> lbrace <> e <> rbrace
PreprocDefined n -> text "defined(" <> ppLexeme n <> char ')'
InitialiserList l -> ppInitialiserList l
PointerAccess e m -> e <> text "->" <> ppLexeme m
MemberAccess e m -> e <> text "." <> ppLexeme m
CommentExpr c e -> c <+> e
Ellipsis -> text "..."
VarExpr var -> ppLexeme var
LiteralExpr _ l -> dullred $ ppLexeme l
SizeofExpr arg -> kwSizeof <> parens arg
SizeofType arg -> kwSizeof <> parens arg
BinaryExpr l o r -> l <+> ppBinaryOp o <+> r
AssignExpr l o r -> l <+> ppAssignOp o <+> r
TernaryExpr c t e -> ppTernaryExpr c t e
UnaryExpr o e -> ppUnaryOp o <> e
ParenExpr e -> parens e
FunctionCall c a -> ppFunctionCall c a
ArrayAccess e i -> e <> char '[' <> i <> char ']'
CastExpr ty e -> parens ty <> e
CompoundExpr ty e -> parens ty <+> lbrace <> e <> rbrace -- DEPRECATED
CompoundLiteral ty e -> parens ty <+> lbrace <> e <> rbrace
PreprocDefined n -> text "defined(" <> ppLexeme n <> char ')'
InitialiserList l -> ppInitialiserList l
PointerAccess e m -> e <> text "->" <> ppLexeme m
MemberAccess e m -> e <> text "." <> ppLexeme m
CommentExpr c e -> c <+> e
Ellipsis -> text "..."

VarDecl ty name arrs -> ty <+> ppLexeme name <> hcat arrs
DeclSpecArray Nothing -> text "[]"
Expand Down
6 changes: 5 additions & 1 deletion src/Language/Cimple/TraverseAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ instance TraverseAst text (Node (Lexeme text)) where
_ <- recurse ty
_ <- recurse expr
pure ()
CompoundExpr ty expr -> do
CompoundExpr ty expr -> do -- DEPRECATED
_ <- recurse ty
_ <- recurse expr
pure ()
CompoundLiteral ty expr -> do
_ <- recurse ty
_ <- recurse expr
pure ()
Expand Down
2 changes: 1 addition & 1 deletion src/Language/Cimple/TreeParser.y
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ import Language.Cimple.Lexer (Lexeme)
assignExpr { Fix (AssignExpr{}) }
parenExpr { Fix (ParenExpr{}) }
castExpr { Fix (CastExpr{}) }
compoundExpr { Fix (CompoundExpr{}) }
compoundLiteral { Fix (CompoundLiteral{}) }
sizeofExpr { Fix (SizeofExpr{}) }
sizeofType { Fix (SizeofType{}) }
literalExpr { Fix (LiteralExpr{}) }
Expand Down

0 comments on commit b976618

Please sign in to comment.