2025-09-17 07:53:05 +09:00
|
|
|
//! Lightweight wrapper structs around ASTNode variants (non-breaking).
|
|
|
|
|
//!
|
|
|
|
|
//! Purpose: provide a gentle path to work with structured nodes via
|
|
|
|
|
//! TryFrom/From without changing the canonical AST enum. This enables
|
|
|
|
|
//! gradual refactors in builders by converting once at the boundary and
|
|
|
|
|
//! then matching on small, typed wrappers.
|
|
|
|
|
|
|
|
|
|
use super::{ASTNode, Span};
|
|
|
|
|
|
|
|
|
|
// ----------------
|
|
|
|
|
// Statements
|
|
|
|
|
// ----------------
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct AssignStmt {
|
|
|
|
|
pub target: Box<ASTNode>,
|
|
|
|
|
pub value: Box<ASTNode>,
|
|
|
|
|
pub span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<ASTNode> for AssignStmt {
|
|
|
|
|
type Error = ASTNode;
|
|
|
|
|
fn try_from(node: ASTNode) -> Result<Self, Self::Error> {
|
|
|
|
|
match node {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::Assignment {
|
|
|
|
|
target,
|
|
|
|
|
value,
|
|
|
|
|
span,
|
|
|
|
|
} => Ok(AssignStmt {
|
|
|
|
|
target,
|
|
|
|
|
value,
|
|
|
|
|
span,
|
|
|
|
|
}),
|
2025-09-17 07:53:05 +09:00
|
|
|
other => Err(other),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<AssignStmt> for ASTNode {
|
|
|
|
|
fn from(s: AssignStmt) -> Self {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::Assignment {
|
|
|
|
|
target: s.target,
|
|
|
|
|
value: s.value,
|
|
|
|
|
span: s.span,
|
|
|
|
|
}
|
2025-09-17 07:53:05 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct ReturnStmt {
|
|
|
|
|
pub value: Option<Box<ASTNode>>,
|
|
|
|
|
pub span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<ASTNode> for ReturnStmt {
|
|
|
|
|
type Error = ASTNode;
|
|
|
|
|
fn try_from(node: ASTNode) -> Result<Self, Self::Error> {
|
|
|
|
|
match node {
|
|
|
|
|
ASTNode::Return { value, span } => Ok(ReturnStmt { value, span }),
|
|
|
|
|
other => Err(other),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<ReturnStmt> for ASTNode {
|
|
|
|
|
fn from(s: ReturnStmt) -> Self {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::Return {
|
|
|
|
|
value: s.value,
|
|
|
|
|
span: s.span,
|
|
|
|
|
}
|
2025-09-17 07:53:05 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct IfStmt {
|
|
|
|
|
pub condition: Box<ASTNode>,
|
|
|
|
|
pub then_body: Vec<ASTNode>,
|
|
|
|
|
pub else_body: Option<Vec<ASTNode>>,
|
|
|
|
|
pub span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<ASTNode> for IfStmt {
|
|
|
|
|
type Error = ASTNode;
|
|
|
|
|
fn try_from(node: ASTNode) -> Result<Self, Self::Error> {
|
|
|
|
|
match node {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::If {
|
|
|
|
|
condition,
|
|
|
|
|
then_body,
|
|
|
|
|
else_body,
|
|
|
|
|
span,
|
|
|
|
|
} => Ok(IfStmt {
|
|
|
|
|
condition,
|
|
|
|
|
then_body,
|
|
|
|
|
else_body,
|
|
|
|
|
span,
|
|
|
|
|
}),
|
2025-09-17 07:53:05 +09:00
|
|
|
other => Err(other),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<IfStmt> for ASTNode {
|
|
|
|
|
fn from(s: IfStmt) -> Self {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::If {
|
|
|
|
|
condition: s.condition,
|
|
|
|
|
then_body: s.then_body,
|
|
|
|
|
else_body: s.else_body,
|
|
|
|
|
span: s.span,
|
|
|
|
|
}
|
2025-09-17 07:53:05 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------
|
|
|
|
|
// Expressions
|
|
|
|
|
// ----------------
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct BinaryExpr {
|
|
|
|
|
pub operator: super::BinaryOperator,
|
|
|
|
|
pub left: Box<ASTNode>,
|
|
|
|
|
pub right: Box<ASTNode>,
|
|
|
|
|
pub span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<ASTNode> for BinaryExpr {
|
|
|
|
|
type Error = ASTNode;
|
|
|
|
|
fn try_from(node: ASTNode) -> Result<Self, Self::Error> {
|
|
|
|
|
match node {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::BinaryOp {
|
|
|
|
|
operator,
|
|
|
|
|
left,
|
|
|
|
|
right,
|
|
|
|
|
span,
|
|
|
|
|
} => Ok(BinaryExpr {
|
|
|
|
|
operator,
|
|
|
|
|
left,
|
|
|
|
|
right,
|
|
|
|
|
span,
|
|
|
|
|
}),
|
2025-09-17 07:53:05 +09:00
|
|
|
other => Err(other),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<BinaryExpr> for ASTNode {
|
|
|
|
|
fn from(e: BinaryExpr) -> Self {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::BinaryOp {
|
|
|
|
|
operator: e.operator,
|
|
|
|
|
left: e.left,
|
|
|
|
|
right: e.right,
|
|
|
|
|
span: e.span,
|
|
|
|
|
}
|
2025-09-17 07:53:05 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct CallExpr {
|
|
|
|
|
pub name: String,
|
|
|
|
|
pub arguments: Vec<ASTNode>,
|
|
|
|
|
pub span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<ASTNode> for CallExpr {
|
|
|
|
|
type Error = ASTNode;
|
|
|
|
|
fn try_from(node: ASTNode) -> Result<Self, Self::Error> {
|
|
|
|
|
match node {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::FunctionCall {
|
|
|
|
|
name,
|
|
|
|
|
arguments,
|
|
|
|
|
span,
|
|
|
|
|
} => Ok(CallExpr {
|
|
|
|
|
name,
|
|
|
|
|
arguments,
|
|
|
|
|
span,
|
|
|
|
|
}),
|
2025-09-17 07:53:05 +09:00
|
|
|
other => Err(other),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<CallExpr> for ASTNode {
|
|
|
|
|
fn from(c: CallExpr) -> Self {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::FunctionCall {
|
|
|
|
|
name: c.name,
|
|
|
|
|
arguments: c.arguments,
|
|
|
|
|
span: c.span,
|
|
|
|
|
}
|
2025-09-17 07:53:05 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-17 08:10:21 +09:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct MethodCallExpr {
|
|
|
|
|
pub object: Box<ASTNode>,
|
|
|
|
|
pub method: String,
|
|
|
|
|
pub arguments: Vec<ASTNode>,
|
|
|
|
|
pub span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<ASTNode> for MethodCallExpr {
|
|
|
|
|
type Error = ASTNode;
|
|
|
|
|
fn try_from(node: ASTNode) -> Result<Self, Self::Error> {
|
|
|
|
|
match node {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::MethodCall {
|
|
|
|
|
object,
|
|
|
|
|
method,
|
|
|
|
|
arguments,
|
|
|
|
|
span,
|
|
|
|
|
} => Ok(MethodCallExpr {
|
|
|
|
|
object,
|
|
|
|
|
method,
|
|
|
|
|
arguments,
|
|
|
|
|
span,
|
|
|
|
|
}),
|
2025-09-17 08:10:21 +09:00
|
|
|
other => Err(other),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<MethodCallExpr> for ASTNode {
|
|
|
|
|
fn from(m: MethodCallExpr) -> Self {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::MethodCall {
|
|
|
|
|
object: m.object,
|
|
|
|
|
method: m.method,
|
|
|
|
|
arguments: m.arguments,
|
|
|
|
|
span: m.span,
|
|
|
|
|
}
|
2025-09-17 08:10:21 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct FieldAccessExpr {
|
|
|
|
|
pub object: Box<ASTNode>,
|
|
|
|
|
pub field: String,
|
|
|
|
|
pub span: Span,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<ASTNode> for FieldAccessExpr {
|
|
|
|
|
type Error = ASTNode;
|
|
|
|
|
fn try_from(node: ASTNode) -> Result<Self, Self::Error> {
|
|
|
|
|
match node {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::FieldAccess {
|
|
|
|
|
object,
|
|
|
|
|
field,
|
|
|
|
|
span,
|
|
|
|
|
} => Ok(FieldAccessExpr {
|
|
|
|
|
object,
|
|
|
|
|
field,
|
|
|
|
|
span,
|
|
|
|
|
}),
|
2025-09-17 08:10:21 +09:00
|
|
|
other => Err(other),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<FieldAccessExpr> for ASTNode {
|
|
|
|
|
fn from(f: FieldAccessExpr) -> Self {
|
2025-11-21 06:25:17 +09:00
|
|
|
ASTNode::FieldAccess {
|
|
|
|
|
object: f.object,
|
|
|
|
|
field: f.field,
|
|
|
|
|
span: f.span,
|
|
|
|
|
}
|
2025-09-17 08:10:21 +09:00
|
|
|
}
|
|
|
|
|
}
|