Skip to content

Commit

Permalink
cleanup: Simplify pretty-printer.
Browse files Browse the repository at this point in the history
By recording semicolons in the AST, we don't need to go out of our way
to recover this information in the pretty-printer. Now the
pretty-printer is a nice simple function of `Ast -> Doc` for any given
node without context.
  • Loading branch information
iphydf committed Feb 5, 2022
1 parent d5989f4 commit 446530f
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 205 deletions.
2 changes: 2 additions & 0 deletions src/Language/Cimple/Ast.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ data NodeF lexeme a
| Case a a
| Default a
| Label lexeme a
| ExprStmt a
-- Variable declarations
| VLA a lexeme a
| VarDeclStmt a (Maybe a)
Expand Down Expand Up @@ -89,6 +90,7 @@ data NodeF lexeme a
| EnumConsts (Maybe lexeme) [a]
| EnumDecl lexeme [a] lexeme
| Enumerator lexeme (Maybe a)
| AggregateDecl a
| Typedef a lexeme
| TypedefFunction a
| Struct lexeme [a]
Expand Down
4 changes: 4 additions & 0 deletions src/Language/Cimple/MapAst.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ instance MapAst itext otext (Node (Lexeme itext)) where
Fix <$> (Default <$> recurse stmt)
Label label stmt ->
Fix <$> (Label <$> recurse label <*> recurse stmt)
ExprStmt expr ->
Fix <$> (ExprStmt <$> recurse expr)
VLA ty name size ->
Fix <$> (VLA <$> recurse ty <*> recurse name <*> recurse size)
VarDeclStmt decl ini ->
Expand Down Expand Up @@ -220,6 +222,8 @@ instance MapAst itext otext (Node (Lexeme itext)) where
Fix <$> (EnumDecl <$> recurse name <*> recurse members <*> recurse tyName)
Enumerator name value ->
Fix <$> (Enumerator <$> recurse name <*> recurse value)
AggregateDecl struct ->
Fix <$> (AggregateDecl <$> recurse struct)
Typedef ty name ->
Fix <$> (Typedef <$> recurse ty <*> recurse name)
TypedefFunction ty ->
Expand Down
10 changes: 5 additions & 5 deletions src/Language/Cimple/Parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,9 @@ Stmt
| ForStmt { $1 }
| WhileStmt { $1 }
| DoWhileStmt { $1 }
| AssignExpr ';' { $1 }
| ExprStmt ';' { $1 }
| FunctionCall ';' { $1 }
| AssignExpr ';' { Fix $ ExprStmt $1 }
| ExprStmt ';' { Fix $ ExprStmt $1 }
| FunctionCall ';' { Fix $ ExprStmt $1 }
| break ';' { Fix $ Break }
| goto ID_CONST ';' { Fix $ Goto $2 }
| ID_CONST ':' Stmt { Fix $ Label $1 $3 }
Expand All @@ -349,7 +349,7 @@ ForStmt

ForInit :: { StringNode }
ForInit
: AssignExpr ';' { $1 }
: AssignExpr ';' { Fix $ ExprStmt $1 }
| VarDeclStmt { $1 }

ForNext :: { StringNode }
Expand Down Expand Up @@ -572,7 +572,7 @@ EnumeratorName

AggregateDecl :: { StringNode }
AggregateDecl
: AggregateType ';' { $1 }
: AggregateType ';' { Fix $ AggregateDecl $1 }
| typedef AggregateType ID_SUE_TYPE ';' { Fix $ Typedef $2 $3 }

AggregateType :: { StringNode }
Expand Down
Loading

0 comments on commit 446530f

Please sign in to comment.