refactor: split optimizer/verifier/parser modules (mainline); add runner trace/directives; add LLVM terminator/select scaffolds; extract AST Span; update CURRENT_TASK with remaining plan

This commit is contained in:
Selfhosting Dev
2025-09-17 05:56:33 +09:00
parent 154778fc57
commit 9dc5c9afb9
39 changed files with 1327 additions and 739 deletions

View File

@ -0,0 +1,33 @@
use crate::parser::{NyashParser, ParseError};
use crate::parser::common::ParserUtils;
use crate::tokenizer::TokenType;
use crate::ast::{ASTNode, Span};
#[inline]
fn is_sugar_enabled() -> bool { crate::parser::sugar_gate::is_enabled() }
impl NyashParser {
pub(crate) fn expr_parse_coalesce(&mut self) -> Result<ASTNode, ParseError> {
let mut expr = self.expr_parse_or()?;
while self.match_token(&TokenType::QmarkQmark) {
if !is_sugar_enabled() {
let line = self.current_token().line;
return Err(ParseError::UnexpectedToken {
found: self.current_token().token_type.clone(),
expected: "enable NYASH_SYNTAX_SUGAR_LEVEL=basic|full for '??'".to_string(),
line,
});
}
self.advance();
let rhs = self.expr_parse_or()?;
let scr = expr;
expr = ASTNode::PeekExpr {
scrutinee: Box::new(scr.clone()),
arms: vec![(crate::ast::LiteralValue::Null, rhs)],
else_expr: Box::new(scr),
span: Span::unknown(),
};
}
Ok(expr)
}
}

36
src/parser/expr/logic.rs Normal file
View File

@ -0,0 +1,36 @@
use crate::parser::{NyashParser, ParseError};
use crate::parser::common::ParserUtils;
use crate::tokenizer::TokenType;
use crate::ast::{ASTNode, BinaryOperator, Span};
impl NyashParser {
pub(crate) fn expr_parse_or(&mut self) -> Result<ASTNode, ParseError> {
let mut expr = self.expr_parse_and()?;
while self.match_token(&TokenType::OR) {
let operator = BinaryOperator::Or;
self.advance();
let right = self.expr_parse_and()?;
if std::env::var("NYASH_GRAMMAR_DIFF").ok().as_deref() == Some("1") {
let ok = crate::grammar::engine::get().syntax_is_allowed_binop("or");
if !ok { eprintln!("[GRAMMAR-DIFF][Parser] binop 'or' not allowed by syntax rules"); }
}
expr = ASTNode::BinaryOp { operator, left: Box::new(expr), right: Box::new(right), span: Span::unknown() };
}
Ok(expr)
}
pub(crate) fn expr_parse_and(&mut self) -> Result<ASTNode, ParseError> {
let mut expr = self.parse_bit_or()?;
while self.match_token(&TokenType::AND) {
let operator = BinaryOperator::And;
self.advance();
let right = self.parse_equality()?;
if std::env::var("NYASH_GRAMMAR_DIFF").ok().as_deref() == Some("1") {
let ok = crate::grammar::engine::get().syntax_is_allowed_binop("and");
if !ok { eprintln!("[GRAMMAR-DIFF][Parser] binop 'and' not allowed by syntax rules"); }
}
expr = ASTNode::BinaryOp { operator, left: Box::new(expr), right: Box::new(right), span: Span::unknown() };
}
Ok(expr)
}
}

4
src/parser/expr/mod.rs Normal file
View File

@ -0,0 +1,4 @@
pub(crate) mod ternary;
pub(crate) mod coalesce;
pub(crate) mod logic;

View File

@ -0,0 +1,26 @@
use crate::parser::{NyashParser, ParseError};
use crate::parser::common::ParserUtils;
use crate::tokenizer::TokenType;
use crate::ast::{ASTNode, Span};
#[inline]
fn is_sugar_enabled() -> bool { crate::parser::sugar_gate::is_enabled() }
impl NyashParser {
pub(crate) fn expr_parse_ternary(&mut self) -> Result<ASTNode, ParseError> {
let cond = self.expr_parse_coalesce()?;
if self.match_token(&TokenType::QUESTION) {
self.advance();
let then_expr = self.parse_expression()?;
self.consume(TokenType::COLON)?;
let else_expr = self.parse_expression()?;
return Ok(ASTNode::If {
condition: Box::new(cond),
then_body: vec![then_expr],
else_body: Some(vec![else_expr]),
span: Span::unknown(),
});
}
Ok(cond)
}
}

View File

@ -85,99 +85,19 @@ impl NyashParser {
/// 三項演算子: cond ? then : else
/// Grammar (Phase 12.7): TernaryExpr = NullsafeExpr ( "?" Expr ":" Expr )?
/// 実装: coalesce の上に差し込み、`cond ? a : b` を If式に変換する。
fn parse_ternary(&mut self) -> Result<ASTNode, ParseError> {
let cond = self.parse_coalesce()?;
if self.match_token(&TokenType::QUESTION) {
// consume '?' and parse then/else expressions
self.advance();
let then_expr = self.parse_expression()?;
self.consume(TokenType::COLON)?; // ':'
let else_expr = self.parse_expression()?;
// Lower to If-expression AST (builder側でPhi化
return Ok(ASTNode::If {
condition: Box::new(cond),
then_body: vec![then_expr],
else_body: Some(vec![else_expr]),
span: Span::unknown(),
});
}
Ok(cond)
}
fn parse_ternary(&mut self) -> Result<ASTNode, ParseError> { self.expr_parse_ternary() }
/// デフォルト値(??: x ?? y => peek x { null => y, else => x }
fn parse_coalesce(&mut self) -> Result<ASTNode, ParseError> {
let mut expr = self.parse_or()?;
while self.match_token(&TokenType::QmarkQmark) {
if !is_sugar_enabled() {
let line = self.current_token().line;
return Err(ParseError::UnexpectedToken {
found: self.current_token().token_type.clone(),
expected: "enable NYASH_SYNTAX_SUGAR_LEVEL=basic|full for '??'".to_string(),
line,
});
}
self.advance(); // consume '??'
let rhs = self.parse_or()?;
let scr = expr;
expr = ASTNode::PeekExpr {
scrutinee: Box::new(scr.clone()),
arms: vec![(crate::ast::LiteralValue::Null, rhs)],
else_expr: Box::new(scr),
span: Span::unknown(),
};
}
Ok(expr)
}
fn parse_coalesce(&mut self) -> Result<ASTNode, ParseError> { self.expr_parse_coalesce() }
/// OR演算子をパース: ||
fn parse_or(&mut self) -> Result<ASTNode, ParseError> {
let mut expr = self.parse_and()?;
while self.match_token(&TokenType::OR) {
let operator = BinaryOperator::Or;
self.advance();
let right = self.parse_and()?;
// Non-invasive syntax diff: record binop
if std::env::var("NYASH_GRAMMAR_DIFF").ok().as_deref() == Some("1") {
let ok = crate::grammar::engine::get().syntax_is_allowed_binop("or");
if !ok { eprintln!("[GRAMMAR-DIFF][Parser] binop 'or' not allowed by syntax rules"); }
}
expr = ASTNode::BinaryOp {
operator,
left: Box::new(expr),
right: Box::new(right),
span: Span::unknown(),
};
}
Ok(expr)
}
fn parse_or(&mut self) -> Result<ASTNode, ParseError> { self.expr_parse_or() }
/// AND演算子をパース: &&
fn parse_and(&mut self) -> Result<ASTNode, ParseError> {
let mut expr = self.parse_bit_or()?;
while self.match_token(&TokenType::AND) {
let operator = BinaryOperator::And;
self.advance();
let right = self.parse_equality()?;
if std::env::var("NYASH_GRAMMAR_DIFF").ok().as_deref() == Some("1") {
let ok = crate::grammar::engine::get().syntax_is_allowed_binop("and");
if !ok { eprintln!("[GRAMMAR-DIFF][Parser] binop 'and' not allowed by syntax rules"); }
}
expr = ASTNode::BinaryOp {
operator,
left: Box::new(expr),
right: Box::new(right),
span: Span::unknown(),
};
}
Ok(expr)
}
fn parse_and(&mut self) -> Result<ASTNode, ParseError> { self.expr_parse_and() }
/// ビットOR: |
fn parse_bit_or(&mut self) -> Result<ASTNode, ParseError> {
pub(crate) fn parse_bit_or(&mut self) -> Result<ASTNode, ParseError> {
let mut expr = self.parse_bit_xor()?;
while self.match_token(&TokenType::BitOr) {
let operator = BinaryOperator::BitOr;
@ -213,7 +133,7 @@ impl NyashParser {
}
/// 等値演算子をパース: == !=
fn parse_equality(&mut self) -> Result<ASTNode, ParseError> {
pub(crate) fn parse_equality(&mut self) -> Result<ASTNode, ParseError> {
let mut expr = self.parse_comparison()?;
while self.match_token(&TokenType::EQUALS) || self.match_token(&TokenType::NotEquals) {

View File

@ -19,6 +19,7 @@
// サブモジュール宣言
mod common;
mod expressions;
mod expr;
mod statements;
mod declarations;
mod items;