Skip to content

Commit

Permalink
Fix some parser tests by enabling extra parens in the printer
Browse files Browse the repository at this point in the history
  • Loading branch information
UplinkCoder committed Jan 16, 2024
1 parent 8bdc41b commit dda5bf5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
8 changes: 6 additions & 2 deletions parser/metac_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2493,8 +2493,11 @@ void TestParseExprssion(void)

MetaCPrinter_Init(&printer,
&LPP.Parser.IdentifierTable,
&LPP.Parser.StringTable
&LPP.Parser.StringTable,
0
);
MetaCPrinter_ExtraParens(&printer, true);

metac_expr_t* expr;
#ifdef OLD_PARSER
# define MetaCLPP_ParseExpr2FromString(LPP, STR) \
Expand Down Expand Up @@ -2538,7 +2541,8 @@ void TestParseDecl(void)

MetaCPrinter_Init(&printer,
&LPP.Parser.IdentifierTable,
&LPP.Parser.StringTable
&LPP.Parser.StringTable,
0
);
metac_expr_t* expr;

Expand Down
41 changes: 24 additions & 17 deletions printer/metac_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1065,12 +1065,12 @@ static inline void PrintExpr(metac_printer_t* self, metac_expr_t* expr)
{
if (expr->Kind == expr_paren)
{
if (!IsBinaryExp(expr->E1->Kind))
if (self->ExtraParens || !IsBinaryExp(expr->E1->Kind))
PrintChar(self, '(');

PrintExpr(self, expr->E1);

if (!IsBinaryExp(expr->E1->Kind))
if (self->ExtraParens || !IsBinaryExp(expr->E1->Kind))
PrintChar(self, ')');
}
else if (expr->Kind == expr_ternary)
Expand Down Expand Up @@ -1170,8 +1170,8 @@ static inline void PrintExpr(metac_printer_t* self, metac_expr_t* expr)
else if (IsBinaryExp(expr->Kind))
{
const char* op = BinExpTypeToChars((metac_binary_expr_kind_t)expr->Kind);

//PrintChar(self, '(');
if (self->ExtraParens)
PrintChar(self, '(');
PrintExpr(self, expr->E1);

if (expr->Kind != expr_dot)
Expand All @@ -1183,7 +1183,8 @@ static inline void PrintExpr(metac_printer_t* self, metac_expr_t* expr)
PrintSpace(self);

PrintExpr(self, expr->E2);
//PrintChar(self, ')');
if (self->ExtraParens)
PrintChar(self, ')');
}
else if (expr->Kind == expr_cast)
{
Expand Down Expand Up @@ -1239,15 +1240,15 @@ static inline void PrintExpr(metac_printer_t* self, metac_expr_t* expr)

PrintString(self, op, (uint32_t)strlen(op));
}
/*
if (!IsBinaryExp(expr->E1->Kind))

if (self->ExtraParens)
PrintChar(self, '(');
*/

PrintExpr(self, expr->E1);
/*
if (!IsBinaryExp(expr->E1->Kind))

if (self->ExtraParens)
PrintChar(self, ')');
*/

}
else if (expr->Kind == expr_outer)
{
Expand All @@ -1272,15 +1273,15 @@ static inline void PrintExpr(metac_printer_t* self, metac_expr_t* expr)
assert(op);

PrintString(self, op, (uint32_t)strlen(op));
/*
if (!IsBinaryExp(expr->E1->Kind))

if (self->ExtraParens)
PrintChar(self, '(');
*/

PrintExpr(self, expr->E1);
/*
if (!IsBinaryExp(expr->E1->Kind))

if (self->ExtraParens)
PrintChar(self, ')');
*/

}
else if (expr->Kind == expr_post_increment || expr->Kind == expr_post_decrement)
{
Expand Down Expand Up @@ -2262,13 +2263,19 @@ void MetaCPrinter_InitSz(metac_printer_t* self,
self->SuppressNewlineAfterDecl = false;
self->AsType = false;
self->ForTypedef = false;
self->ExtraParens = false;
self->ForAnonymousField = 0;
MetaCPrinter_Reset(self);

self->IdentifierTable = identifierTable;
self->StringTable = stringTable;
}

void MetaCPrinter_ExtraParens(metac_printer_t* self, bool value)
{
self->ExtraParens = value;
}

void MetaCPrinter_Free(metac_printer_t* self)
{
if (self->StringMemory)
Expand Down
3 changes: 3 additions & 0 deletions printer/metac_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ typedef struct metac_printer_t
bool SuppressNewlineAfterDecl;
bool AsType;
bool ForTypedef;
bool ExtraParens;
int32_t ForAnonymousField;

metac_alloc_t Allocator;
Expand All @@ -42,6 +43,8 @@ void MetaCPrinter_Init(metac_printer_t* self,
metac_identifier_table_t* stringTable,
metac_alloc_t* alloc);

void MetaCPrinter_ExtraParens(metac_printer_t* self, bool value);

void MetaCPRinter_Free(metac_printer_t* self);

void MetaCPrinter_InitSz(metac_printer_t* self,
Expand Down

0 comments on commit dda5bf5

Please sign in to comment.