ast: add MethodCallExpr/FieldAccessExpr wrappers; builder: adopt wrappers for method/field, and add loop helpers (create_loop_blocks/add_predecessor). No behavior change.
This commit is contained in:
@ -132,3 +132,51 @@ impl From<CallExpr> for ASTNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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 {
|
||||||
|
ASTNode::MethodCall { object, method, arguments, span } =>
|
||||||
|
Ok(MethodCallExpr { object, method, arguments, span }),
|
||||||
|
other => Err(other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<MethodCallExpr> for ASTNode {
|
||||||
|
fn from(m: MethodCallExpr) -> Self {
|
||||||
|
ASTNode::MethodCall { object: m.object, method: m.method, arguments: m.arguments, span: m.span }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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 {
|
||||||
|
ASTNode::FieldAccess { object, field, span } =>
|
||||||
|
Ok(FieldAccessExpr { object, field, span }),
|
||||||
|
other => Err(other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<FieldAccessExpr> for ASTNode {
|
||||||
|
fn from(f: FieldAccessExpr) -> Self {
|
||||||
|
ASTNode::FieldAccess { object: f.object, field: f.field, span: f.span }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
// Expression lowering split from builder.rs to keep files lean
|
// Expression lowering split from builder.rs to keep files lean
|
||||||
use super::{ConstValue, MirInstruction, ValueId};
|
use super::{ConstValue, MirInstruction, ValueId};
|
||||||
use crate::ast::{ASTNode, AssignStmt, ReturnStmt, BinaryExpr, CallExpr};
|
use crate::ast::{ASTNode, AssignStmt, ReturnStmt, BinaryExpr, CallExpr, MethodCallExpr, FieldAccessExpr};
|
||||||
|
|
||||||
impl super::MirBuilder {
|
impl super::MirBuilder {
|
||||||
// Main expression dispatcher
|
// Main expression dispatcher
|
||||||
@ -39,32 +39,19 @@ impl super::MirBuilder {
|
|||||||
|
|
||||||
ASTNode::Me { .. } => self.build_me_expression(),
|
ASTNode::Me { .. } => self.build_me_expression(),
|
||||||
|
|
||||||
ASTNode::MethodCall {
|
node @ ASTNode::MethodCall { .. } => {
|
||||||
object,
|
let m = MethodCallExpr::try_from(node).expect("ASTNode::MethodCall must convert");
|
||||||
method,
|
if (m.method == "is" || m.method == "as") && m.arguments.len() == 1 {
|
||||||
arguments,
|
if let Some(type_name) = Self::extract_string_literal(&m.arguments[0]) {
|
||||||
..
|
let obj_val = self.build_expression_impl(*m.object.clone())?;
|
||||||
} => {
|
|
||||||
if (method == "is" || method == "as") && arguments.len() == 1 {
|
|
||||||
if let Some(type_name) = Self::extract_string_literal(&arguments[0]) {
|
|
||||||
let obj_val = self.build_expression_impl(*object.clone())?;
|
|
||||||
let ty = Self::parse_type_name_to_mir(&type_name);
|
let ty = Self::parse_type_name_to_mir(&type_name);
|
||||||
let dst = self.value_gen.next();
|
let dst = self.value_gen.next();
|
||||||
let op = if method == "is" {
|
let op = if m.method == "is" { crate::mir::TypeOpKind::Check } else { crate::mir::TypeOpKind::Cast };
|
||||||
crate::mir::TypeOpKind::Check
|
self.emit_instruction(MirInstruction::TypeOp { dst, op, value: obj_val, ty })?;
|
||||||
} else {
|
|
||||||
crate::mir::TypeOpKind::Cast
|
|
||||||
};
|
|
||||||
self.emit_instruction(MirInstruction::TypeOp {
|
|
||||||
dst,
|
|
||||||
op,
|
|
||||||
value: obj_val,
|
|
||||||
ty,
|
|
||||||
})?;
|
|
||||||
return Ok(dst);
|
return Ok(dst);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.build_method_call(*object.clone(), method.clone(), arguments.clone())
|
self.build_method_call(*m.object.clone(), m.method.clone(), m.arguments.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode::FromCall {
|
ASTNode::FromCall {
|
||||||
@ -186,8 +173,9 @@ impl super::MirBuilder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode::FieldAccess { object, field, .. } => {
|
node @ ASTNode::FieldAccess { .. } => {
|
||||||
self.build_field_access(*object.clone(), field.clone())
|
let f = FieldAccessExpr::try_from(node).expect("ASTNode::FieldAccess must convert");
|
||||||
|
self.build_field_access(*f.object.clone(), f.field.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode::New {
|
ASTNode::New {
|
||||||
|
|||||||
Reference in New Issue
Block a user