#! @file when.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the parsing of `when` blocks, which choose the declarations or #! statements a program has while compiling, and of the conditions they test. module sakura::analyzer; #! `when condition { ... }`, holding declarations at the top level of a file and #! statements within a function, followed by `else when` and `else`. #! @param this The parser. #! @param declarations Whether it holds declarations, at the top level of a #! file, rather than statements. #! @returns The `when_block` node. def parser::when_block(&mut this, declarations: bool): u32 { return this.error_here() if !this.enter(); val token := this.index; this.advance(); val condition := this.condition(); val then := declarations ? this.decl_block() : this.block(); var other: u32 = 0; if this.accept(tk::kw_else) { other = this.check(tk::kw_when) ? this.when_block(declarations) : declarations ? this.decl_block() : this.block(); } this.leave(); return this.branch(nk::when_block, token, condition, then, other); } #! The declarations of a `when` at the top level, as a `block`, which cannot #! import or begin a module. #! @param this The parser. #! @returns The `block` node. def parser::decl_block(&mut this): u32 { if !this.check(tk::lbrace) { this.fail(ec::token, "Expected `{` to begin the declarations."); return this.error_here(); } val token := this.index; this.advance(); val mark := this.mark; while !this.check(tk::rbrace) && !this.check(tk::eof) { if this.check(tk::kw_imp) || this.check(tk::kw_mod) || this.check(tk::kw_extern) { this.fail(ec::decl, "Imports and modules cannot be within `when`; a whole section of a file can be declared `module path when condition;`."); } else { this.push(this.declaration()); } if this.panic { this.synchronize(parser::scope::declarations); } } val declarations := this.make_run(mark); this.expect(tk::rbrace, "Expected `}` to close the declarations."); return this.make_node(nk::block, token, declarations); } #! A condition on the target, where `&&` binds tighter than `||`. #! @param this The parser. #! @returns The condition's node. def parser::condition(&mut this): u32 { var left := this.condition_and(); while this.check(tk::logor) { val token := this.index; this.advance(); left = this.make_node(nk::binary, token, left, this.condition_and()); } return left; } def parser::condition_and(&mut this): u32 { var left := this.condition_operand(); while this.check(tk::logand) { val token := this.index; this.advance(); left = this.make_node(nk::binary, token, left, this.condition_operand()); } return left; } #! A target key compared with a value, as `os == linux`, a negation, or a #! condition in parentheses. #! @param this The parser. #! @returns The operand's node, or an `error` node. def parser::condition_operand(&mut this): u32 { return this.error_here() if !this.enter(); val token := this.index; var made: u32 = 0; if this.accept(tk::negate) { made = this.make_node(nk::unary, token, this.condition_operand()); } else if this.accept(tk::lparen) { made = this.condition(); this.expect(tk::rparen, "Expected `)` to close the condition."); } else if !this.check(tk::name) { this.fail(ec::ident, "Expected a target key: `os`, `arch`, or `backend`."); made = this.error_here(); } else { made = this.comparison(); } this.leave(); return made; } #! `key == value` or `key != value`, from the key. #! @param this The parser. #! @returns The `binary` node. def parser::comparison(&mut this): u32 { val key := this.name_node("Expected a target key: `os`, `arch`, or `backend`."); if !this.check(tk::equals) && !this.check(tk::noteq) { this.fail(ec::token, "Expected `==` or `!=` after the target key."); return this.error_here(); } val token := this.index; this.advance(); val value := this.name_node("Expected the value the key is compared with, as `linux`."); return this.make_node(nk::binary, token, key, value); }