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:
33
src/parser/expr/coalesce.rs
Normal file
33
src/parser/expr/coalesce.rs
Normal 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
36
src/parser/expr/logic.rs
Normal 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
4
src/parser/expr/mod.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub(crate) mod ternary;
|
||||
pub(crate) mod coalesce;
|
||||
pub(crate) mod logic;
|
||||
|
||||
26
src/parser/expr/ternary.rs
Normal file
26
src/parser/expr/ternary.rs
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user