Skip to content

Commit

Permalink
Ast Render no longer outputs erroneous semicolon
Browse files Browse the repository at this point in the history
closes #813
  • Loading branch information
Hejsil committed Mar 7, 2018
1 parent d96dd5b commit bb80daf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/ast_render.cpp
Expand Up @@ -490,7 +490,10 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
AstNode *statement = node->data.block.statements.at(i);
print_indent(ar);
render_node_grouped(ar, statement);
fprintf(ar->f, ";");

if (!statement_terminates_without_semicolon(statement))
fprintf(ar->f, ";");

fprintf(ar->f, "\n");
}
ar->indent -= ar->indent_size;
Expand Down
2 changes: 1 addition & 1 deletion src/parser.cpp
Expand Up @@ -2315,7 +2315,7 @@ static AstNode *ast_parse_expression(ParseContext *pc, size_t *token_index, bool
return nullptr;
}

static bool statement_terminates_without_semicolon(AstNode *node) {
bool statement_terminates_without_semicolon(AstNode *node) {
switch (node->type) {
case NodeTypeIfBoolExpr:
if (node->data.if_bool_expr.else_node)
Expand Down
2 changes: 2 additions & 0 deletions src/parser.hpp
Expand Up @@ -23,4 +23,6 @@ void ast_print(AstNode *node, int indent);

void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *context), void *context);

bool statement_terminates_without_semicolon(AstNode *node);

#endif
30 changes: 15 additions & 15 deletions test/translate_c.zig
Expand Up @@ -351,7 +351,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ var i: c_int = 0;
\\ while (a > c_uint(0)) {
\\ a >>= @import("std").math.Log2Int(c_uint)(1);
\\ };
\\ }
\\ return i;
\\}
);
Expand Down Expand Up @@ -498,7 +498,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ var i: c_int = 0;
\\ while (a > c_uint(0)) {
\\ a >>= u5(1);
\\ };
\\ }
\\ return i;
\\}
);
Expand Down Expand Up @@ -867,12 +867,12 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ while (true) {
\\ a -= 1;
\\ if (!(a != 0)) break;
\\ };
\\ }
\\ var b: c_int = 2;
\\ while (true) {
\\ b -= 1;
\\ if (!(b != 0)) break;
\\ };
\\ }
\\}
);

Expand Down Expand Up @@ -962,8 +962,8 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\pub fn foo() void {
\\ {
\\ var i: c_int = 0;
\\ while (i < 10) : (i += 1) {};
\\ };
\\ while (i < 10) : (i += 1) {}
\\ }
\\}
);

Expand All @@ -973,7 +973,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\}
,
\\pub fn foo() void {
\\ while (true) {};
\\ while (true) {}
\\}
);

Expand All @@ -987,7 +987,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\pub fn foo() void {
\\ while (true) {
\\ break;
\\ };
\\ }
\\}
);

Expand All @@ -1001,7 +1001,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\pub fn foo() void {
\\ while (true) {
\\ continue;
\\ };
\\ }
\\}
);

Expand Down Expand Up @@ -1058,7 +1058,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ {
\\ var x_0: c_int = 2;
\\ x_0 += 1;
\\ };
\\ }
\\ return x;
\\}
);
Expand Down Expand Up @@ -1147,7 +1147,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ return 0;
\\ } else {
\\ return 1;
\\ };
\\ }
\\}
);

Expand All @@ -1164,7 +1164,7 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ break :__to_bool_expr @bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp) != 0;
\\ }) {
\\ return 0;
\\ };
\\ }
\\}
);

Expand All @@ -1185,16 +1185,16 @@ pub fn addCases(cases: &tests.TranslateCContext) void {
\\ break :__to_bool_expr @bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp) != 0;
\\ }) {
\\ return 0;
\\ };
\\ }
\\ {
\\ var j: c_int = 4;
\\ while (__to_bool_expr: {
\\ const _tmp = j;
\\ break :__to_bool_expr @bitCast(@IntType(false, @sizeOf(@typeOf(_tmp)) * 8), _tmp) != 0;
\\ }) : (j -= 1) {
\\ return 0;
\\ };
\\ };
\\ }
\\ }
\\}
);
}

0 comments on commit bb80daf

Please sign in to comment.