Compare commits

..

2 commits

Author SHA1 Message Date
f203da328b [fine] Tests for conditional, semicolon optional at end of block
Just like rust, ';' means "discard this value".
2024-01-04 19:47:44 -08:00
ec5e59aed3 [fine] Clean up comments a bit 2024-01-04 19:44:03 -08:00
3 changed files with 41 additions and 7 deletions

View file

@ -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);
}

View file

@ -2,13 +2,13 @@ use fine::parser::concrete::Tree;
use pretty_assertions::assert_eq;
fn rebase_concrete(source_path: &str, dump: &str) {
// RE-BASE
let contents = std::fs::read_to_string(source_path)
.expect(&format!("unable to read input file {}", source_path));
let mut result = String::new();
let mut lines = contents.lines();
// Search for the "concrete:" section.
let mut found_concrete_section = false;
while let Some(line) = lines.next() {
result.push_str(line);
@ -26,6 +26,8 @@ fn rebase_concrete(source_path: &str, dump: &str) {
);
}
// We've found the section we care about, replace all the lines we care
// about with the actual lines.
let mut replaced_output = false;
while let Some(line) = lines.next() {
if line.starts_with("// | ") {

View 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 }