pyvm: split op handlers into ops_core/ops_box/ops_ctrl; add ops_flow + intrinsic; delegate vm.py without behavior change
net-plugin: modularize constants (consts.rs) and sockets (sockets.rs); remove legacy commented socket code; fix unused imports mir: move instruction unit tests to tests/mir_instruction_unit.rs (file lean-up); no semantic changes runner/pyvm: ensure using pre-strip; misc docs updates Build: cargo build ok; legacy cfg warnings remain as before
This commit is contained in:
@ -28,6 +28,9 @@ impl super::MirBuilder {
|
||||
// Look for the main() method
|
||||
let out = if let Some(main_method) = methods.get("main") {
|
||||
if let ASTNode::FunctionDeclaration { params, body, .. } = main_method {
|
||||
// Also materialize a callable function entry "BoxName.main/N" for harness/PyVM
|
||||
let func_name = format!("{}.{}", box_name, "main");
|
||||
let _ = self.lower_static_method_as_function(func_name, params.clone(), body.clone());
|
||||
// Convert the method body to a Program AST node and lower it
|
||||
let program_ast = ASTNode::Program {
|
||||
statements: body.clone(),
|
||||
|
||||
@ -241,6 +241,7 @@ impl super::MirBuilder {
|
||||
ASTNode::Include { filename, .. } => self.build_include_expression(filename.clone()),
|
||||
|
||||
ASTNode::Program { statements, .. } => self.cf_block(statements.clone()),
|
||||
ASTNode::ScopeBox { body, .. } => self.cf_block(body.clone()),
|
||||
|
||||
ASTNode::Print { expression, .. } => self.build_print_statement(*expression.clone()),
|
||||
|
||||
|
||||
@ -31,18 +31,24 @@ impl MirBuilder {
|
||||
// then
|
||||
self.current_block = Some(then_block);
|
||||
self.ensure_block_exists(then_block)?;
|
||||
// Scope enter for then-branch
|
||||
self.hint_scope_enter(0);
|
||||
let then_ast_for_analysis = then_branch.clone();
|
||||
self.variable_map = pre_if_var_map.clone();
|
||||
let then_value_raw = self.build_expression(then_branch)?;
|
||||
let then_exit_block = self.current_block()?;
|
||||
let then_var_map_end = self.variable_map.clone();
|
||||
if !self.is_current_block_terminated() {
|
||||
// Scope leave for then-branch
|
||||
self.hint_scope_leave(0);
|
||||
self.emit_instruction(MirInstruction::Jump { target: merge_block })?;
|
||||
}
|
||||
|
||||
// else
|
||||
self.current_block = Some(else_block);
|
||||
self.ensure_block_exists(else_block)?;
|
||||
// Scope enter for else-branch
|
||||
self.hint_scope_enter(0);
|
||||
let (else_value_raw, else_ast_for_analysis, else_var_map_end_opt) = if let Some(else_ast) = else_branch {
|
||||
self.variable_map = pre_if_var_map.clone();
|
||||
let val = self.build_expression(else_ast.clone())?;
|
||||
@ -54,6 +60,8 @@ impl MirBuilder {
|
||||
};
|
||||
let else_exit_block = self.current_block()?;
|
||||
if !self.is_current_block_terminated() {
|
||||
// Scope leave for else-branch
|
||||
self.hint_scope_leave(0);
|
||||
self.emit_instruction(MirInstruction::Jump { target: merge_block })?;
|
||||
}
|
||||
|
||||
@ -86,10 +94,18 @@ impl MirBuilder {
|
||||
pre_then_var_value,
|
||||
)?;
|
||||
|
||||
// Hint: join result variable if both branches assign to the same variable name
|
||||
// Hint: join result variable(s)
|
||||
// 1) Primary: if both branches assign to the same variable name, emit a hint for that name
|
||||
if let (Some(tn), Some(en)) = (assigned_then_pre.as_deref(), assigned_else_pre.as_deref()) {
|
||||
if tn == en {
|
||||
self.hint_join_result(tn);
|
||||
if tn == en { self.hint_join_result(tn); }
|
||||
}
|
||||
// 2) Secondary: if both branches assign multiple variables, hint全件(制限なし)
|
||||
if let Some(ref else_map_end) = else_var_map_end_opt {
|
||||
for name in then_var_map_end.keys() {
|
||||
if Some(name.as_str()) == assigned_then_pre.as_deref() { continue; }
|
||||
if else_map_end.contains_key(name) {
|
||||
self.hint_join_result(name.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -133,6 +133,9 @@ impl super::MirBuilder {
|
||||
|
||||
// Block: sequentially build statements and return last value or Void
|
||||
pub(super) fn build_block(&mut self, statements: Vec<ASTNode>) -> Result<ValueId, String> {
|
||||
// Scope hint for bare block (Program)
|
||||
let scope_id = self.current_block.map(|bb| bb.as_u32()).unwrap_or(0);
|
||||
self.hint_scope_enter(scope_id);
|
||||
let mut last_value = None;
|
||||
for statement in statements {
|
||||
last_value = Some(self.build_expression(statement)?);
|
||||
@ -142,7 +145,7 @@ impl super::MirBuilder {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(last_value.unwrap_or_else(|| {
|
||||
let out = last_value.unwrap_or_else(|| {
|
||||
let void_val = self.value_gen.next();
|
||||
self.emit_instruction(MirInstruction::Const {
|
||||
dst: void_val,
|
||||
@ -150,7 +153,12 @@ impl super::MirBuilder {
|
||||
})
|
||||
.unwrap();
|
||||
void_val
|
||||
}))
|
||||
});
|
||||
// Scope leave only if block not already terminated
|
||||
if !self.is_current_block_terminated() {
|
||||
self.hint_scope_leave(scope_id);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
// control-flow build_* moved to control_flow.rs (use cf_* instead)
|
||||
|
||||
Reference in New Issue
Block a user