mirror of
https://github.com/imjasonh/nescript
synced 2026-07-08 08:55:38 +00:00
Parser: accept ; as optional statement separator
Newlines remain the primary separator, but \`;\` now lets short
statements share a line for readability:
on frame {
a += 1; b += 2
if button.a { a -= 1; b -= 1 }
}
Implementation: \`parse_block\` consumes any \`;\` tokens between
statements.
https://claude.ai/code/session_01W6eQFStA66EuMKHUFo2rx3
This commit is contained in:
parent
f0264b124a
commit
f95130346a
2 changed files with 28 additions and 0 deletions
|
|
@ -830,6 +830,13 @@ impl Parser {
|
|||
let mut statements = Vec::new();
|
||||
while *self.peek() != TokenKind::RBrace && *self.peek() != TokenKind::Eof {
|
||||
statements.push(self.parse_statement()?);
|
||||
// Allow optional `;` between statements for readability.
|
||||
// Newlines are still the primary separator, but `;` lets
|
||||
// users put short statements on the same line:
|
||||
// `x += 1; y += 1`
|
||||
while *self.peek() == TokenKind::Semicolon {
|
||||
self.advance();
|
||||
}
|
||||
}
|
||||
self.expect(&TokenKind::RBrace)?;
|
||||
|
||||
|
|
|
|||
|
|
@ -665,6 +665,27 @@ fn parse_mmc3_mapper() {
|
|||
assert_eq!(prog.game.mapper, Mapper::MMC3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_semicolon_separators() {
|
||||
// `;` should act as an optional statement separator so short
|
||||
// statements can share a line.
|
||||
let src = r#"
|
||||
game "Test" { mapper: NROM }
|
||||
var a: u8 = 0
|
||||
var b: u8 = 0
|
||||
on frame {
|
||||
a += 1; b += 2
|
||||
if button.a { a -= 1; b -= 1 }
|
||||
wait_frame
|
||||
}
|
||||
start Main
|
||||
"#;
|
||||
let prog = parse_ok(src);
|
||||
let frame = prog.states[0].on_frame.as_ref().unwrap();
|
||||
// Top-level statements: two assigns + if + wait_frame
|
||||
assert_eq!(frame.statements.len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_match_statement() {
|
||||
let src = r#"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue