Skip to content

[Question][HW1] Parsing single line if-then-else statements. #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Lee-Janggun opened this issue Mar 19, 2022 · 1 comment
Closed

[Question][HW1] Parsing single line if-then-else statements. #350

Lee-Janggun opened this issue Mar 19, 2022 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@Lee-Janggun
Copy link
Member

Lee-Janggun commented Mar 19, 2022

Consider printing the following code

int main() {
  if (true) break;
  return 0;
}

Using my printer, I get the following result.

int main() {
  if (true) {
    break;
  }
  return (0);
}

I expect this to work, as the latter only has an extra set of curly brackets. However, the grading script fails with the following error.

thread 'test_examples_write_c' panicked at 'assertion failed: `(left.is_equiv(right))`
             left: `TranslationUnit([Node { node: FunctionDefinition(Node { node: FunctionDefinition { specifiers: [Node { node: TypeSpecifier(Node { node: Int, span: 173…176 }), span: 173…176 }], declarator: Node { node: Declarator { kind: Node { node: Identifier(Node { node: Identifier { name: "main" }, span: 177…181 }), span: 177…181 }, derived: [Node { node: KRFunction([]), span: 181…183 }], extensions: [] }, span: 177…183 }, declarations: [], statement: Node { node: Compound([Node { node: Statement(Node { node: If(Node { node: IfStatement { condition: Node { node: Identifier(Node { node: Identifier { name: "true" }, span: 192…196 }), span: 192…196 }, then_statement: Node { node: Break, span: 198…204 }, else_statement: None }, span: 188…207 }), span: 188…207 }), span: 188…207 }, Node { node: Statement(Node { node: Return(Some(Node { node: Constant(Node { node: Integer(Integer { base: Decimal, number: "0", suffix: IntegerSuffix { size: Int, unsigned: false, imaginary: false } }), span: 214…215 }), span: 214…215 })), span: 207…216 }), span: 207…216 }]), span: 184…218 } }, span: 173…218 }), span: 173…218 }])`,
            right: `TranslationUnit([Node { node: FunctionDefinition(Node { node: FunctionDefinition { specifiers: [Node { node: TypeSpecifier(Node { node: Int, span: 181…184 }), span: 181…184 }], declarator: Node { node: Declarator { kind: Node { node: Identifier(Node { node: Identifier { name: "main" }, span: 185…189 }), span: 185…189 }, derived: [Node { node: KRFunction([]), span: 189…191 }], extensions: [] }, span: 185…191 }, declarations: [], statement: Node { node: Compound([Node { node: Statement(Node { node: If(Node { node: IfStatement { condition: Node { node: Identifier(Node { node: Identifier { name: "true" }, span: 200…204 }), span: 200…204 }, then_statement: Node { node: Compound([Node { node: Statement(Node { node: Break, span: 212…218 }), span: 212…218 }]), span: 206…222 }, else_statement: None }, span: 196…225 }), span: 196…225 }), span: 196…225 }, Node { node: Statement(Node { node: Return(Some(Node { node: Constant(Node { node: Integer(Integer { base: Decimal, number: "0", suffix: IntegerSuffix { size: Int, unsigned: false, imaginary: false } }), span: 233…234 }), span: 232…235 })), span: 225…236 }), span: 225…236 }]), span: 192…238 } }, span: 181…238 }), span: 181…238 }])`',

How to fix this? It seems we do not have enough information to know if the then and else statements are single lined.

@Lee-Janggun Lee-Janggun added the question Further information is requested label Mar 19, 2022
@Lee-Janggun
Copy link
Member Author

I found out the problem while writing this question... one should check that the then_statement is a compound to see whether it is single line or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants