[fine] Tests for conditional, semicolon optional at end of block
Just like rust, ';' means "discard this value".
This commit is contained in:
parent
ec5e59aed3
commit
f203da328b
2 changed files with 38 additions and 6 deletions
|
|
@ -422,7 +422,9 @@ fn statement_let(p: &mut CParser) {
|
|||
p.expect(TokenKind::Identifier, "expected a name for the variable");
|
||||
p.expect(TokenKind::Equal, "expected a '=' after the variable name");
|
||||
expression(p);
|
||||
p.expect(TokenKind::Semicolon, "expect ';' to end a let statement");
|
||||
if !p.at(TokenKind::RightBrace) {
|
||||
p.expect(TokenKind::Semicolon, "expect ';' to end a let statement");
|
||||
}
|
||||
|
||||
p.end(m, TreeKind::LetStatement);
|
||||
}
|
||||
|
|
@ -436,7 +438,9 @@ fn statement_return(p: &mut CParser) {
|
|||
"expect 'return' to start a return statement",
|
||||
);
|
||||
expression(p);
|
||||
p.expect(TokenKind::Semicolon, "expect ';' to end a return statement");
|
||||
if !p.at(TokenKind::RightBrace) {
|
||||
p.expect(TokenKind::Semicolon, "expect ';' to end a return statement");
|
||||
}
|
||||
|
||||
p.end(m, TreeKind::ReturnStatement);
|
||||
}
|
||||
|
|
@ -445,10 +449,12 @@ fn statement_expression(p: &mut CParser) {
|
|||
let m = p.start();
|
||||
|
||||
expression(p);
|
||||
p.expect(
|
||||
TokenKind::Semicolon,
|
||||
"expect ';' to end an expression statement",
|
||||
);
|
||||
if !p.at(TokenKind::RightBrace) {
|
||||
p.expect(
|
||||
TokenKind::Semicolon,
|
||||
"expect ';' to end an expression statement",
|
||||
);
|
||||
}
|
||||
|
||||
p.end(m, TreeKind::ExpressionStatement);
|
||||
}
|
||||
|
|
|
|||
26
fine/tests/expression/conditional.fine
Normal file
26
fine/tests/expression/conditional.fine
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
// concrete:
|
||||
// | File
|
||||
// | IfStatement
|
||||
// | ConditionalExpression
|
||||
// | If:'"if"'
|
||||
// | LiteralExpression
|
||||
// | True:'"true"'
|
||||
// | Block
|
||||
// | LeftBrace:'"{"'
|
||||
// | ExpressionStatement
|
||||
// | LiteralExpression
|
||||
// | String:'"\"discarded\""'
|
||||
// | Semicolon:'";"'
|
||||
// | ExpressionStatement
|
||||
// | LiteralExpression
|
||||
// | Number:'"23"'
|
||||
// | RightBrace:'"}"'
|
||||
// | Else:'"else"'
|
||||
// | Block
|
||||
// | LeftBrace:'"{"'
|
||||
// | ExpressionStatement
|
||||
// | LiteralExpression
|
||||
// | Number:'"45"'
|
||||
// | RightBrace:'"}"'
|
||||
//
|
||||
if true { "discarded"; 23 } else { 45 }
|
||||
Loading…
Add table
Add a link
Reference in a new issue