#! @file statements.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the parsing of blocks and the statements within them: `if`, `while`, #! `for`, `match`, jumps, contracts, local functions, local variables, and the #! expressions and assignments written as statements. module sakura::analyzer; #! `{ statements }`. #! @param this The parser. #! @returns The `block` node. def parser::block(&mut this): u32 { if !this.check(tk::lbrace) { this.fail(ec::token, "Expected `{` to begin a block."); return this.error_here(); } val token := this.index; val angled := this.angled; this.angled = false; this.advance(); val mark := this.mark; while !this.check(tk::rbrace) && !this.check(tk::eof) { this.push(this.statement()); if this.panic { this.synchronize(parser::scope::block); } } val statements := this.make_run(mark); this.expect(tk::rbrace, "Expected `}` to close the block."); this.angled = angled; return this.make_node(nk::block, token, statements); } def parser::statement(&mut this): u32 { return this.error_here() if !this.enter(); var made: u32 = 0; match this.kind { tk::kw_pub => { this.report(ec::token, "Only a declaration outside a function is `pub`, since nothing within one is reached by name from elsewhere."); this.advance(); made = this.statement(); } # The statement is kept with the attributes, within an `error` node. tk::attr => { val token := this.index; this.report(ec::token, "Attributes describe declarations outside a function, since nothing within one is reached by name from elsewhere."); val attrs := this.attributes(); val held := this.panic ? 0 : this.statement(); made = this.error(token, this.join_runs(attrs, this.run_of(held))); } tk::kw_val, tk::kw_var => { val lead := this.bare_lead(); made = this.variable(&lead, parser::place::local); } tk::kw_const => { # A function within a block lives in its frame, which no constant can reach. if this.peek(1) == tk::kw_def { this.report(ec::token, "A `const def` is declared outside any function, where constants can call it."); this.advance(); made = this.peek(1) == tk::name ? this.local_function() : this.simple(); } else { val lead := this.bare_lead(); made = this.variable(&lead, parser::place::local); } } tk::kw_if => made = this.if_stmt(); tk::kw_when => made = this.when_block(false); tk::kw_while => made = this.while_stmt(); tk::kw_for => made = this.for_stmt(); tk::kw_match => made = this.match_expr(false); tk::kw_ret => made = this.jump(nk::return_stmt); tk::kw_break => made = this.jump(nk::break_stmt); tk::kw_cont => made = this.jump(nk::continue_stmt); tk::lbrace => made = this.block(); tk::kw_def => made = this.peek(1) == tk::name ? this.local_function() : this.simple(); else => made = this.contract_word() ? this.contract() : this.simple(); } this.leave(); return made; } #! Whether the statement is a contract: `expects`, `asserts`, or `ensures` #! before `(`. Anywhere else, each is a name, as `t.expects(x)` calls a method. #! @param this The parser. #! @returns Whether it is. def parser::contract_word(&this): bool { return false if this.kind != tk::name || this.peek(1) != tk::lparen; val word := this.syntax.token_names[this.index]; return word == names::expects_id || word == names::asserts_id || word == names::ensures_id; } #! A contract, `expects(cond)`, `asserts(cond)`, or `ensures(cond)`, each of #! which may say what failed in place of the condition, as #! `expects(n != 0, "A count is not zero.")`, and an `ensures` may name what the #! function returns, as `ensures(r: r > 0)`, within which `old(e)` is what `e` #! was where it was reached. #! @param this The parser. #! @returns The `contract` node. def parser::contract(&mut this): u32 { val token := this.index; val ensures := this.syntax.token_names[token] == names::ensures_id; this.advance(); this.advance(); var bound: u32 = 0; if ensures && this.check(tk::name) && this.peek(1) == tk::colon { bound = this.name_node("Expected the name of what the function returns."); this.advance(); } val within := this.in_ensures; this.in_ensures = ensures; val condition := this.expression(); this.in_ensures = within; var text: u32 = 0; if this.accept(tk::comma) { text = this.expression(); } this.expect(tk::rparen, "Expected `)` to close the contract, which takes its condition and what it says failed."); this.expect(tk::semicolon, "Expected `;` after the statement."); return this.make_node(nk::contract, token, condition, this.make_record(bound, text)); } #! An expression followed by `;`. #! @param this The parser. #! @returns The `expr_stmt` node. def parser::simple(&mut this): u32 { val token := this.index; val made := this.expression(); this.expect(tk::semicolon, "Expected `;` after the statement."); return this.make_node(nk::expr_stmt, token, made); } #! `if cond { } else if cond { } else { }`, and `if const`. #! @param this The parser. #! @returns The `if_stmt` or `if_const` node. def parser::if_stmt(&mut this): u32 { return this.error_here() if !this.enter(); val token := this.index; this.advance(); val constant := this.accept(tk::kw_const); val condition := this.condition_expr(); val then := this.block(); var other: u32 = 0; if this.accept(tk::kw_else) { other = this.check(tk::kw_if) ? this.if_stmt() : this.block(); } this.leave(); return this.branch(constant ? nk::if_const : nk::if_stmt, token, condition, then, other); } def parser::while_stmt(&mut this): u32 { val token := this.index; this.advance(); val condition := this.condition_expr(); return this.make_node(nk::while_stmt, token, condition, this.block()); } #! `for x in xs`, `for i, x in xs`, `for &x in xs`, `for &mut x in xs`, #! `for i in a..b`, and `for const x in args`, which is unrolled over a pack #! while compiling. #! @param this The parser. #! @returns The `for_stmt` node. def parser::for_stmt(&mut this): u32 { val token := this.index; this.advance(); val constant := this.accept(tk::kw_const); var index: u32 = 0; if this.check(tk::name) && this.peek(1) == tk::comma { index = this.name_node("Expected a name for the index."); this.advance(); } val reference := this.accept(tk::bitand); val mutable := reference && this.accept(tk::kw_mut); val variable := this.name_node("Expected a name for the loop variable."); this.expect(tk::kw_in, "Expected `in` after the loop variable."); # A range is the loop's own, so `for i in 0..n` counts, rather than walking a # range made as a value. var iterated := this.condition_expr(); var end: u32 = 0; var inclusive := false; if this.syntax.node_kinds[iterated] == nk::range { inclusive = this.syntax.token_kinds[this.syntax.node_tokens[iterated]] == tk::cascadeeq; end = this.syntax.node_rights[iterated]; iterated = this.syntax.node_lefts[iterated]; } val flags := parser::bit(constant, nf::constant) | parser::bit(reference, nf::reference) | parser::bit(mutable, nf::mutable) | parser::bit(inclusive, nf::inclusive); val record := this.make_record(index, variable, iterated, end, flags); return this.make_node(nk::for_stmt, token, record, this.block()); } #! A match statement, or a match used as a value, whose arms are separated by #! commas. #! @param this The parser. #! @param value Whether it is used as a value. #! @returns The `match_stmt` or `match_value` node. def parser::match_expr(&mut this, value: bool): u32 { return this.error_here() if !this.enter(); val kind := value ? nk::match_value : nk::match_stmt; val token := this.index; this.advance(); val subject := this.condition_expr(); if !this.accept(tk::lbrace) { this.fail(ec::token, "Expected `{` to begin the arms of the match."); this.leave(); return this.make_node(kind, token, subject); } # A match used as a value separates its arms with commas, and one as a # statement ends each with the block or statement it does. val arms := value ? this.list(parser::item::value_arm, tk::rbrace, "Expected `,` between the arms, or `}` to close the match.") : this.arms(); this.leave(); return this.make_node(kind, token, subject, arms); } #! The arms of a match statement, through its `}`. #! @param this The parser. #! @returns The run of `match_arm` nodes. def parser::arms(&mut this): u32 { val mark := this.mark; while !this.check(tk::rbrace) && !this.check(tk::eof) { this.push(this.arm(false)); if this.panic { this.synchronize(parser::scope::block); } } val arms := this.make_run(mark); this.expect(tk::rbrace, "Expected `}` to close the match."); return arms; } #! An arm of a match: its patterns, or `else`, then what it does, which is a #! block or a statement, or in a match used as a value, an expression. A pattern #! is an expression: a value compared with the subject, or the name of the field #! of a union the subject holds, as `error` or `error(code)`. #! @param this The parser. #! @param value Whether the match is used as a value. #! @returns The `match_arm` node, or an `error` node. def parser::arm(&mut this, value: bool): u32 { val token := this.index; var patterns: u32 = 0; if !this.accept(tk::kw_else) { if this.check(tk::name) && this.peek(1) == tk::colon { this.fail(ec::token, "An arm names the field of a union it matches, as `field =>`, or `field(value) =>` to use its value."); } else { val mark := this.mark; while true { this.push(this.condition_expr()); break if !this.accept(tk::comma); } patterns = this.make_run(mark); } } this.expect(tk::arrow, "Expected `=>` followed by what the arm does."); var body: u32 = 0; if value && this.check(tk::kw_ret) { this.fail(ec::token, "Each arm of a match used as a value is the expression it gives, as `2 => x,`, so leave out `return` and `;`."); } else if value { body = this.expression(); } else if this.check(tk::lbrace) { body = this.block(); } else { # A single statement is the body, as a block of its own. val first := this.index; body = this.make_node(nk::block, first, this.run_of(this.statement())); } return this.make_node(nk::match_arm, token, patterns, body); } #! `return`, `break`, or `continue`, each of which may be followed by `if`. #! `return value if cond;` is the `if` it reads as, built here so that nothing #! below the parser tells the two apart. #! @param this The parser. #! @param kind `return_stmt`, `break_stmt`, or `continue_stmt`. #! @returns The node, or the `if_stmt` holding it. def parser::jump(&mut this, kind: nk): u32 { val token := this.index; this.advance(); var value: u32 = 0; if kind == nk::return_stmt && !this.check(tk::semicolon) && !this.check(tk::kw_if) { value = this.expression(); } var made := this.make_node(kind, token, value); if this.check(tk::kw_if) { val guard := this.index; this.advance(); val constant := this.accept(tk::kw_const); val condition := this.condition_expr(); val then := this.make_node(nk::block, token, this.run_of(made)); made = this.branch(constant ? nk::if_const : nk::if_stmt, guard, condition, then, 0); } this.expect(tk::semicolon, "Expected `;` after the statement."); return made; } #! A function declared within a block, `def name(params): type { body }`, which #! is a local holding the function as a `lambda`, named before its body so it #! can call itself. It gives the types of its parameters and its result, as any #! other named function does. #! @param this The parser. #! @returns The `variable` node, or an `error` node. def parser::local_function(&mut this): u32 { val token := this.index; this.advance(); val name := this.index; this.advance(); if this.check(tk::less) { this.fail(ec::token, "Functions declared within a block cannot be generic."); return this.error_here(); } val signature := this.params(false, false); val body := this.block(); val lambda := this.make_node(nk::lambda, token, signature.params, this.make_record(signature.result, body)); return this.make_node(nk::variable, name, this.make_record(0, 0, 0, lambda), nf::function as u32); }