2025-09-17 07:43:07 +09:00
|
|
|
use super::{ConstValue, Effect, EffectMask, MirInstruction, ValueId};
|
2025-09-17 07:54:45 +09:00
|
|
|
use crate::ast::{ASTNode, CallExpr};
|
2025-09-17 07:43:07 +09:00
|
|
|
use crate::mir::TypeOpKind;
|
2025-09-03 01:37:38 +09:00
|
|
|
|
|
|
|
|
impl super::MirBuilder {
|
2025-09-03 05:04:56 +09:00
|
|
|
// Print statement: env.console.log(value) with early TypeOp handling
|
2025-09-03 01:37:38 +09:00
|
|
|
pub(super) fn build_print_statement(&mut self, expression: ASTNode) -> Result<ValueId, String> {
|
2025-09-03 05:04:56 +09:00
|
|
|
super::utils::builder_debug_log("enter build_print_statement");
|
2025-09-17 07:54:45 +09:00
|
|
|
// Prefer wrapper for simple function-call pattern (non-breaking refactor)
|
|
|
|
|
if let Ok(call) = CallExpr::try_from(expression.clone()) {
|
|
|
|
|
if (call.name == "isType" || call.name == "asType") && call.arguments.len() == 2 {
|
|
|
|
|
super::utils::builder_debug_log("pattern: print(FunctionCall isType|asType) [via wrapper]");
|
|
|
|
|
if let Some(type_name) = super::MirBuilder::extract_string_literal(&call.arguments[1]) {
|
|
|
|
|
super::utils::builder_debug_log(&format!("extract_string_literal OK: {}", type_name));
|
|
|
|
|
let val = self.build_expression(call.arguments[0].clone())?;
|
|
|
|
|
let ty = super::MirBuilder::parse_type_name_to_mir(&type_name);
|
|
|
|
|
let dst = self.value_gen.next();
|
|
|
|
|
let op = if call.name == "isType" { TypeOpKind::Check } else { TypeOpKind::Cast };
|
|
|
|
|
super::utils::builder_debug_log(&format!("emit TypeOp {:?} value={} dst= {}", op, val, dst));
|
|
|
|
|
self.emit_instruction(MirInstruction::TypeOp { dst, op, value: val, ty })?;
|
|
|
|
|
self.emit_instruction(MirInstruction::ExternCall {
|
|
|
|
|
dst: None,
|
|
|
|
|
iface_name: "env.console".to_string(),
|
|
|
|
|
method_name: "log".to_string(),
|
|
|
|
|
args: vec![dst],
|
|
|
|
|
effects: EffectMask::PURE.add(Effect::Io),
|
|
|
|
|
})?;
|
|
|
|
|
return Ok(dst);
|
|
|
|
|
} else {
|
|
|
|
|
super::utils::builder_debug_log("extract_string_literal FAIL [via wrapper]");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
match &expression {
|
|
|
|
|
// print(isType(val, "Type")) / print(asType(...))
|
2025-09-17 07:43:07 +09:00
|
|
|
ASTNode::FunctionCall {
|
|
|
|
|
name, arguments, ..
|
|
|
|
|
} if (name == "isType" || name == "asType") && arguments.len() == 2 => {
|
2025-09-03 05:04:56 +09:00
|
|
|
super::utils::builder_debug_log("pattern: print(FunctionCall isType|asType)");
|
|
|
|
|
if let Some(type_name) = super::MirBuilder::extract_string_literal(&arguments[1]) {
|
2025-09-17 07:43:07 +09:00
|
|
|
super::utils::builder_debug_log(&format!(
|
|
|
|
|
"extract_string_literal OK: {}",
|
|
|
|
|
type_name
|
|
|
|
|
));
|
2025-09-03 05:04:56 +09:00
|
|
|
let val = self.build_expression(arguments[0].clone())?;
|
|
|
|
|
let ty = super::MirBuilder::parse_type_name_to_mir(&type_name);
|
|
|
|
|
let dst = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
let op = if name == "isType" {
|
|
|
|
|
TypeOpKind::Check
|
|
|
|
|
} else {
|
|
|
|
|
TypeOpKind::Cast
|
|
|
|
|
};
|
|
|
|
|
super::utils::builder_debug_log(&format!(
|
|
|
|
|
"emit TypeOp {:?} value={} dst= {}",
|
|
|
|
|
op, val, dst
|
|
|
|
|
));
|
|
|
|
|
self.emit_instruction(MirInstruction::TypeOp {
|
|
|
|
|
dst,
|
|
|
|
|
op,
|
|
|
|
|
value: val,
|
|
|
|
|
ty,
|
|
|
|
|
})?;
|
2025-09-03 05:04:56 +09:00
|
|
|
self.emit_instruction(MirInstruction::ExternCall {
|
|
|
|
|
dst: None,
|
|
|
|
|
iface_name: "env.console".to_string(),
|
|
|
|
|
method_name: "log".to_string(),
|
|
|
|
|
args: vec![dst],
|
|
|
|
|
effects: EffectMask::PURE.add(Effect::Io),
|
|
|
|
|
})?;
|
|
|
|
|
return Ok(dst);
|
|
|
|
|
} else {
|
|
|
|
|
super::utils::builder_debug_log("extract_string_literal FAIL");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// print(obj.is("Type")) / print(obj.as("Type"))
|
2025-09-17 07:43:07 +09:00
|
|
|
ASTNode::MethodCall {
|
|
|
|
|
object,
|
|
|
|
|
method,
|
|
|
|
|
arguments,
|
|
|
|
|
..
|
|
|
|
|
} if (method == "is" || method == "as") && arguments.len() == 1 => {
|
2025-09-03 05:04:56 +09:00
|
|
|
super::utils::builder_debug_log("pattern: print(MethodCall is|as)");
|
|
|
|
|
if let Some(type_name) = super::MirBuilder::extract_string_literal(&arguments[0]) {
|
2025-09-17 07:43:07 +09:00
|
|
|
super::utils::builder_debug_log(&format!(
|
|
|
|
|
"extract_string_literal OK: {}",
|
|
|
|
|
type_name
|
|
|
|
|
));
|
2025-09-03 05:04:56 +09:00
|
|
|
let obj_val = self.build_expression(*object.clone())?;
|
|
|
|
|
let ty = super::MirBuilder::parse_type_name_to_mir(&type_name);
|
|
|
|
|
let dst = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
let op = if method == "is" {
|
|
|
|
|
TypeOpKind::Check
|
|
|
|
|
} else {
|
|
|
|
|
TypeOpKind::Cast
|
|
|
|
|
};
|
|
|
|
|
super::utils::builder_debug_log(&format!(
|
|
|
|
|
"emit TypeOp {:?} obj={} dst= {}",
|
|
|
|
|
op, obj_val, dst
|
|
|
|
|
));
|
|
|
|
|
self.emit_instruction(MirInstruction::TypeOp {
|
|
|
|
|
dst,
|
|
|
|
|
op,
|
|
|
|
|
value: obj_val,
|
|
|
|
|
ty,
|
|
|
|
|
})?;
|
2025-09-03 05:04:56 +09:00
|
|
|
self.emit_instruction(MirInstruction::ExternCall {
|
|
|
|
|
dst: None,
|
|
|
|
|
iface_name: "env.console".to_string(),
|
|
|
|
|
method_name: "log".to_string(),
|
|
|
|
|
args: vec![dst],
|
|
|
|
|
effects: EffectMask::PURE.add(Effect::Io),
|
|
|
|
|
})?;
|
|
|
|
|
return Ok(dst);
|
|
|
|
|
} else {
|
|
|
|
|
super::utils::builder_debug_log("extract_string_literal FAIL");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let value = self.build_expression(expression)?;
|
|
|
|
|
super::utils::builder_debug_log(&format!("fallback print value={}", value));
|
|
|
|
|
self.emit_instruction(MirInstruction::ExternCall {
|
|
|
|
|
dst: None,
|
|
|
|
|
iface_name: "env.console".to_string(),
|
|
|
|
|
method_name: "log".to_string(),
|
|
|
|
|
args: vec![value],
|
|
|
|
|
effects: EffectMask::PURE.add(Effect::Io),
|
|
|
|
|
})?;
|
|
|
|
|
Ok(value)
|
2025-09-03 01:37:38 +09:00
|
|
|
}
|
2025-09-03 05:04:56 +09:00
|
|
|
|
|
|
|
|
// Block: sequentially build statements and return last value or Void
|
2025-09-03 01:37:38 +09:00
|
|
|
pub(super) fn build_block(&mut self, statements: Vec<ASTNode>) -> Result<ValueId, String> {
|
2025-09-21 08:53:00 +09:00
|
|
|
// 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);
|
2025-09-03 05:04:56 +09:00
|
|
|
let mut last_value = None;
|
2025-09-06 06:24:08 +09:00
|
|
|
for statement in statements {
|
|
|
|
|
last_value = Some(self.build_expression(statement)?);
|
|
|
|
|
// If the current block was terminated by this statement (e.g., return/throw),
|
|
|
|
|
// do not emit any further instructions for this block.
|
|
|
|
|
if self.is_current_block_terminated() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-21 08:53:00 +09:00
|
|
|
let out = last_value.unwrap_or_else(|| {
|
2025-09-03 05:04:56 +09:00
|
|
|
let void_val = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
self.emit_instruction(MirInstruction::Const {
|
|
|
|
|
dst: void_val,
|
|
|
|
|
value: ConstValue::Void,
|
|
|
|
|
})
|
|
|
|
|
.unwrap();
|
2025-09-03 05:04:56 +09:00
|
|
|
void_val
|
2025-09-21 08:53:00 +09:00
|
|
|
});
|
|
|
|
|
// Scope leave only if block not already terminated
|
|
|
|
|
if !self.is_current_block_terminated() {
|
|
|
|
|
self.hint_scope_leave(scope_id);
|
|
|
|
|
}
|
|
|
|
|
Ok(out)
|
2025-09-03 01:37:38 +09:00
|
|
|
}
|
2025-09-03 05:04:56 +09:00
|
|
|
|
2025-09-19 08:34:29 +09:00
|
|
|
// control-flow build_* moved to control_flow.rs (use cf_* instead)
|
2025-09-03 05:04:56 +09:00
|
|
|
|
|
|
|
|
// Local declarations with optional initializers
|
|
|
|
|
pub(super) fn build_local_statement(
|
|
|
|
|
&mut self,
|
|
|
|
|
variables: Vec<String>,
|
|
|
|
|
initial_values: Vec<Option<Box<ASTNode>>>,
|
|
|
|
|
) -> Result<ValueId, String> {
|
|
|
|
|
let mut last_value = None;
|
|
|
|
|
for (i, var_name) in variables.iter().enumerate() {
|
|
|
|
|
let value_id = if i < initial_values.len() && initial_values[i].is_some() {
|
|
|
|
|
let init_expr = initial_values[i].as_ref().unwrap();
|
|
|
|
|
self.build_expression(*init_expr.clone())?
|
|
|
|
|
} else {
|
|
|
|
|
self.value_gen.next()
|
|
|
|
|
};
|
|
|
|
|
self.variable_map.insert(var_name.clone(), value_id);
|
|
|
|
|
last_value = Some(value_id);
|
|
|
|
|
}
|
|
|
|
|
Ok(last_value.unwrap_or_else(|| self.value_gen.next()))
|
2025-09-03 01:37:38 +09:00
|
|
|
}
|
2025-09-03 05:04:56 +09:00
|
|
|
|
|
|
|
|
// Return statement
|
2025-09-17 07:43:07 +09:00
|
|
|
pub(super) fn build_return_statement(
|
|
|
|
|
&mut self,
|
|
|
|
|
value: Option<Box<ASTNode>>,
|
|
|
|
|
) -> Result<ValueId, String> {
|
2025-09-19 02:07:38 +09:00
|
|
|
// Enforce cleanup policy
|
|
|
|
|
if self.in_cleanup_block && !self.cleanup_allow_return {
|
|
|
|
|
return Err("return is not allowed inside cleanup block (enable NYASH_CLEANUP_ALLOW_RETURN=1 to permit)".to_string());
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-03 05:04:56 +09:00
|
|
|
let return_value = if let Some(expr) = value {
|
|
|
|
|
self.build_expression(*expr)?
|
|
|
|
|
} else {
|
|
|
|
|
let void_dst = self.value_gen.next();
|
2025-09-19 02:07:38 +09:00
|
|
|
self.emit_instruction(MirInstruction::Const { dst: void_dst, value: ConstValue::Void })?;
|
2025-09-03 05:04:56 +09:00
|
|
|
void_dst
|
|
|
|
|
};
|
2025-09-19 02:07:38 +09:00
|
|
|
|
|
|
|
|
if self.return_defer_active {
|
|
|
|
|
// Defer: copy into slot and jump to target
|
|
|
|
|
if let (Some(slot), Some(target)) = (self.return_defer_slot, self.return_defer_target) {
|
|
|
|
|
self.return_deferred_emitted = true;
|
|
|
|
|
self.emit_instruction(MirInstruction::Copy { dst: slot, src: return_value })?;
|
|
|
|
|
if !self.is_current_block_terminated() {
|
|
|
|
|
self.emit_instruction(MirInstruction::Jump { target })?;
|
|
|
|
|
}
|
|
|
|
|
Ok(return_value)
|
|
|
|
|
} else {
|
|
|
|
|
// Fallback: no configured slot/target; emit a real return
|
|
|
|
|
self.emit_instruction(MirInstruction::Return { value: Some(return_value) })?;
|
|
|
|
|
Ok(return_value)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Normal return
|
|
|
|
|
self.emit_instruction(MirInstruction::Return { value: Some(return_value) })?;
|
|
|
|
|
Ok(return_value)
|
|
|
|
|
}
|
2025-09-03 01:37:38 +09:00
|
|
|
}
|
2025-09-03 05:04:56 +09:00
|
|
|
|
|
|
|
|
// Nowait: prefer env.future.spawn_instance if method call; else FutureNew
|
2025-09-17 07:43:07 +09:00
|
|
|
pub(super) fn build_nowait_statement(
|
|
|
|
|
&mut self,
|
|
|
|
|
variable: String,
|
|
|
|
|
expression: ASTNode,
|
|
|
|
|
) -> Result<ValueId, String> {
|
|
|
|
|
if let ASTNode::MethodCall {
|
|
|
|
|
object,
|
|
|
|
|
method,
|
|
|
|
|
arguments,
|
|
|
|
|
..
|
|
|
|
|
} = expression.clone()
|
|
|
|
|
{
|
2025-09-03 05:04:56 +09:00
|
|
|
let recv_val = self.build_expression(*object)?;
|
|
|
|
|
let mname_id = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
self.emit_instruction(MirInstruction::Const {
|
|
|
|
|
dst: mname_id,
|
|
|
|
|
value: super::ConstValue::String(method.clone()),
|
|
|
|
|
})?;
|
2025-09-03 05:04:56 +09:00
|
|
|
let mut arg_vals: Vec<ValueId> = Vec::with_capacity(2 + arguments.len());
|
|
|
|
|
arg_vals.push(recv_val);
|
|
|
|
|
arg_vals.push(mname_id);
|
2025-09-17 07:43:07 +09:00
|
|
|
for a in arguments.into_iter() {
|
|
|
|
|
arg_vals.push(self.build_expression(a)?);
|
|
|
|
|
}
|
2025-09-03 05:04:56 +09:00
|
|
|
let future_id = self.value_gen.next();
|
|
|
|
|
self.emit_instruction(MirInstruction::ExternCall {
|
|
|
|
|
dst: Some(future_id),
|
|
|
|
|
iface_name: "env.future".to_string(),
|
|
|
|
|
method_name: "spawn_instance".to_string(),
|
|
|
|
|
args: arg_vals,
|
|
|
|
|
effects: crate::mir::effect::EffectMask::PURE.add(crate::mir::effect::Effect::Io),
|
|
|
|
|
})?;
|
|
|
|
|
self.variable_map.insert(variable.clone(), future_id);
|
|
|
|
|
return Ok(future_id);
|
|
|
|
|
}
|
|
|
|
|
let expression_value = self.build_expression(expression)?;
|
|
|
|
|
let future_id = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
self.emit_instruction(MirInstruction::FutureNew {
|
|
|
|
|
dst: future_id,
|
|
|
|
|
value: expression_value,
|
|
|
|
|
})?;
|
2025-09-03 05:04:56 +09:00
|
|
|
self.variable_map.insert(variable.clone(), future_id);
|
|
|
|
|
Ok(future_id)
|
2025-09-03 01:37:38 +09:00
|
|
|
}
|
2025-09-03 05:04:56 +09:00
|
|
|
|
|
|
|
|
// Await: insert Safepoint before/after and emit Await
|
2025-09-17 07:43:07 +09:00
|
|
|
pub(super) fn build_await_expression(
|
|
|
|
|
&mut self,
|
|
|
|
|
expression: ASTNode,
|
|
|
|
|
) -> Result<ValueId, String> {
|
2025-09-03 05:04:56 +09:00
|
|
|
let future_value = self.build_expression(expression)?;
|
|
|
|
|
self.emit_instruction(MirInstruction::Safepoint)?;
|
|
|
|
|
let result_id = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
self.emit_instruction(MirInstruction::Await {
|
|
|
|
|
dst: result_id,
|
|
|
|
|
future: future_value,
|
|
|
|
|
})?;
|
2025-09-03 05:04:56 +09:00
|
|
|
self.emit_instruction(MirInstruction::Safepoint)?;
|
|
|
|
|
Ok(result_id)
|
2025-09-03 01:37:38 +09:00
|
|
|
}
|
2025-09-03 05:04:56 +09:00
|
|
|
|
|
|
|
|
// me: resolve to param if present; else symbolic const (stable mapping)
|
2025-09-03 01:37:38 +09:00
|
|
|
pub(super) fn build_me_expression(&mut self) -> Result<ValueId, String> {
|
2025-09-17 07:43:07 +09:00
|
|
|
if let Some(id) = self.variable_map.get("me").cloned() {
|
|
|
|
|
return Ok(id);
|
|
|
|
|
}
|
2025-09-03 05:04:56 +09:00
|
|
|
let me_value = self.value_gen.next();
|
2025-09-17 07:43:07 +09:00
|
|
|
let me_tag = if let Some(ref cls) = self.current_static_box {
|
|
|
|
|
cls.clone()
|
|
|
|
|
} else {
|
|
|
|
|
"__me__".to_string()
|
|
|
|
|
};
|
|
|
|
|
self.emit_instruction(MirInstruction::Const {
|
|
|
|
|
dst: me_value,
|
|
|
|
|
value: ConstValue::String(me_tag),
|
|
|
|
|
})?;
|
2025-09-03 05:04:56 +09:00
|
|
|
self.variable_map.insert("me".to_string(), me_value);
|
|
|
|
|
Ok(me_value)
|
2025-09-03 01:37:38 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-09-19 02:07:38 +09:00
|
|
|
// use crate::mir::loop_api::LoopBuilderApi; // no longer needed here
|