2025-08-26 04:34:14 +09:00
|
|
|
use super::super::NyashRunner;
|
2025-09-17 07:43:07 +09:00
|
|
|
use nyash_rust::{
|
|
|
|
|
ast::ASTNode,
|
|
|
|
|
backend::VM,
|
|
|
|
|
box_factory::user_defined::UserDefinedBoxFactory,
|
|
|
|
|
core::model::BoxDeclaration as CoreBoxDecl,
|
|
|
|
|
interpreter::SharedState,
|
|
|
|
|
mir::MirCompiler,
|
|
|
|
|
parser::NyashParser,
|
|
|
|
|
runtime::{NyashRuntime, NyashRuntimeBuilder},
|
|
|
|
|
};
|
2025-08-26 04:34:14 +09:00
|
|
|
use std::sync::Arc;
|
2025-09-17 07:43:07 +09:00
|
|
|
use std::{fs, process};
|
2025-08-26 04:34:14 +09:00
|
|
|
|
|
|
|
|
impl NyashRunner {
|
|
|
|
|
/// Execute VM mode (split)
|
|
|
|
|
pub(crate) fn execute_vm_mode(&self, filename: &str) {
|
2025-09-15 18:44:49 +09:00
|
|
|
// Quiet mode for child pipelines (e.g., selfhost compiler JSON emit)
|
|
|
|
|
let quiet_pipe = std::env::var("NYASH_JSON_ONLY").ok().as_deref() == Some("1");
|
2025-09-08 01:08:59 +09:00
|
|
|
// Enforce plugin-first policy for VM on this branch (deterministic):
|
|
|
|
|
// - Initialize plugin host if not yet loaded
|
|
|
|
|
// - Prefer plugin implementations for core boxes
|
|
|
|
|
// - Optionally fail fast when plugins are missing (NYASH_VM_PLUGIN_STRICT=1)
|
|
|
|
|
{
|
|
|
|
|
// Initialize unified registry globals (idempotent)
|
|
|
|
|
nyash_rust::runtime::init_global_unified_registry();
|
|
|
|
|
// Init plugin host from nyash.toml if not yet loaded
|
|
|
|
|
let need_init = {
|
|
|
|
|
let host = nyash_rust::runtime::get_global_plugin_host();
|
2025-09-17 07:43:07 +09:00
|
|
|
host.read()
|
|
|
|
|
.map(|h| h.config_ref().is_none())
|
|
|
|
|
.unwrap_or(true)
|
2025-09-08 01:08:59 +09:00
|
|
|
};
|
|
|
|
|
if need_init {
|
|
|
|
|
let _ = nyash_rust::runtime::init_global_plugin_host("nyash.toml");
|
|
|
|
|
crate::runner_plugin_init::init_bid_plugins();
|
|
|
|
|
}
|
|
|
|
|
// Prefer plugin-builtins for core types unless explicitly disabled
|
|
|
|
|
if std::env::var("NYASH_USE_PLUGIN_BUILTINS").ok().is_none() {
|
|
|
|
|
std::env::set_var("NYASH_USE_PLUGIN_BUILTINS", "1");
|
|
|
|
|
}
|
|
|
|
|
// Build stable override list
|
2025-09-17 07:43:07 +09:00
|
|
|
let mut override_types: Vec<String> =
|
|
|
|
|
if let Ok(list) = std::env::var("NYASH_PLUGIN_OVERRIDE_TYPES") {
|
|
|
|
|
list.split(',')
|
|
|
|
|
.map(|s| s.trim().to_string())
|
|
|
|
|
.filter(|s| !s.is_empty())
|
|
|
|
|
.collect()
|
|
|
|
|
} else {
|
|
|
|
|
vec![]
|
|
|
|
|
};
|
2025-09-08 01:08:59 +09:00
|
|
|
for t in [
|
2025-09-17 07:43:07 +09:00
|
|
|
"FileBox",
|
|
|
|
|
"TOMLBox", // IO/config
|
|
|
|
|
"ConsoleBox",
|
|
|
|
|
"StringBox",
|
|
|
|
|
"IntegerBox", // core value-ish
|
|
|
|
|
"ArrayBox",
|
|
|
|
|
"MapBox", // collections
|
|
|
|
|
"MathBox",
|
|
|
|
|
"TimeBox", // math/time helpers
|
2025-09-08 01:08:59 +09:00
|
|
|
] {
|
2025-09-17 07:43:07 +09:00
|
|
|
if !override_types.iter().any(|x| x == t) {
|
|
|
|
|
override_types.push(t.to_string());
|
|
|
|
|
}
|
2025-09-08 01:08:59 +09:00
|
|
|
}
|
|
|
|
|
std::env::set_var("NYASH_PLUGIN_OVERRIDE_TYPES", override_types.join(","));
|
|
|
|
|
|
|
|
|
|
// Strict mode: verify providers exist for override types
|
|
|
|
|
if std::env::var("NYASH_VM_PLUGIN_STRICT").ok().as_deref() == Some("1") {
|
|
|
|
|
let v2 = nyash_rust::runtime::get_global_registry();
|
|
|
|
|
let mut missing: Vec<String> = Vec::new();
|
2025-09-17 07:43:07 +09:00
|
|
|
for t in [
|
|
|
|
|
"FileBox",
|
|
|
|
|
"ConsoleBox",
|
|
|
|
|
"ArrayBox",
|
|
|
|
|
"MapBox",
|
|
|
|
|
"StringBox",
|
|
|
|
|
"IntegerBox",
|
|
|
|
|
] {
|
|
|
|
|
if v2.get_provider(t).is_none() {
|
|
|
|
|
missing.push(t.to_string());
|
|
|
|
|
}
|
2025-09-08 01:08:59 +09:00
|
|
|
}
|
|
|
|
|
if !missing.is_empty() {
|
2025-09-17 07:43:07 +09:00
|
|
|
eprintln!(
|
|
|
|
|
"❌ VM plugin-first strict: missing providers for: {:?}",
|
|
|
|
|
missing
|
|
|
|
|
);
|
2025-09-08 01:08:59 +09:00
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-26 04:34:14 +09:00
|
|
|
// Read the file
|
|
|
|
|
let code = match fs::read_to_string(filename) {
|
|
|
|
|
Ok(content) => content,
|
2025-09-17 07:43:07 +09:00
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("❌ Error reading file {}: {}", filename, e);
|
|
|
|
|
process::exit(1);
|
|
|
|
|
}
|
2025-08-26 04:34:14 +09:00
|
|
|
};
|
|
|
|
|
|
2025-09-19 02:07:38 +09:00
|
|
|
// Optional Phase-15: strip `using` lines and register aliases/modules
|
|
|
|
|
let code = if crate::config::env::enable_using() {
|
|
|
|
|
match crate::runner::modes::common::resolve::strip_using_and_register(self, &code, filename) {
|
|
|
|
|
Ok(s) => s,
|
|
|
|
|
Err(e) => { eprintln!("❌ {}", e); process::exit(1); }
|
2025-09-18 06:35:49 +09:00
|
|
|
}
|
|
|
|
|
} else { code };
|
|
|
|
|
|
2025-08-26 04:34:14 +09:00
|
|
|
// Parse to AST
|
|
|
|
|
let ast = match NyashParser::parse_from_string(&code) {
|
|
|
|
|
Ok(ast) => ast,
|
2025-09-17 07:43:07 +09:00
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("❌ Parse error: {}", e);
|
|
|
|
|
process::exit(1);
|
|
|
|
|
}
|
2025-08-26 04:34:14 +09:00
|
|
|
};
|
2025-09-19 22:27:59 +09:00
|
|
|
let ast = crate::r#macro::maybe_expand_and_dump(&ast, false);
|
2025-08-26 04:34:14 +09:00
|
|
|
|
|
|
|
|
// Prepare runtime and collect Box declarations for VM user-defined types
|
|
|
|
|
let runtime = {
|
2025-08-30 08:54:15 +09:00
|
|
|
let mut builder = NyashRuntimeBuilder::new();
|
2025-08-27 17:06:46 +09:00
|
|
|
if std::env::var("NYASH_GC_COUNTING").ok().as_deref() == Some("1") {
|
|
|
|
|
builder = builder.with_counting_gc();
|
|
|
|
|
}
|
|
|
|
|
let rt = builder.build();
|
2025-08-26 04:34:14 +09:00
|
|
|
self.collect_box_declarations(&ast, &rt);
|
|
|
|
|
// Register UserDefinedBoxFactory backed by the same declarations
|
|
|
|
|
let mut shared = SharedState::new();
|
|
|
|
|
shared.box_declarations = rt.box_declarations.clone();
|
|
|
|
|
let udf = Arc::new(UserDefinedBoxFactory::new(shared));
|
2025-09-17 07:43:07 +09:00
|
|
|
if let Ok(mut reg) = rt.box_registry.lock() {
|
|
|
|
|
reg.register(udf);
|
|
|
|
|
}
|
2025-08-26 04:34:14 +09:00
|
|
|
rt
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Compile to MIR (opt passes configurable)
|
|
|
|
|
let mut mir_compiler = MirCompiler::with_options(!self.config.no_optimize);
|
|
|
|
|
let compile_result = match mir_compiler.compile(ast) {
|
|
|
|
|
Ok(result) => result,
|
2025-09-17 07:43:07 +09:00
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("❌ MIR compilation error: {}", e);
|
|
|
|
|
process::exit(1);
|
|
|
|
|
}
|
2025-08-26 04:34:14 +09:00
|
|
|
};
|
|
|
|
|
|
2025-08-27 17:06:46 +09:00
|
|
|
// Optional: demo scheduling hook
|
|
|
|
|
if std::env::var("NYASH_SCHED_DEMO").ok().as_deref() == Some("1") {
|
|
|
|
|
if let Some(s) = &runtime.scheduler {
|
|
|
|
|
// Immediate task
|
2025-09-17 07:43:07 +09:00
|
|
|
s.spawn(
|
|
|
|
|
"demo-immediate",
|
|
|
|
|
Box::new(|| {
|
|
|
|
|
println!("[SCHED] immediate task ran at safepoint");
|
|
|
|
|
}),
|
|
|
|
|
);
|
2025-08-27 17:06:46 +09:00
|
|
|
// Delayed task
|
2025-09-17 07:43:07 +09:00
|
|
|
s.spawn_after(
|
|
|
|
|
0,
|
|
|
|
|
"demo-delayed",
|
|
|
|
|
Box::new(|| {
|
|
|
|
|
println!("[SCHED] delayed task ran at safepoint");
|
|
|
|
|
}),
|
|
|
|
|
);
|
2025-08-27 17:06:46 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-06 06:24:08 +09:00
|
|
|
// Optional: dump MIR for diagnostics
|
|
|
|
|
if std::env::var("NYASH_VM_DUMP_MIR").ok().as_deref() == Some("1") {
|
2025-09-16 03:54:44 +09:00
|
|
|
let p = nyash_rust::mir::MirPrinter::new();
|
2025-09-06 06:24:08 +09:00
|
|
|
eprintln!("{}", p.print_module(&compile_result.module));
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 03:03:04 +09:00
|
|
|
// Optional: VM-only escape analysis to elide barriers before execution
|
|
|
|
|
let mut module_vm = compile_result.module.clone();
|
|
|
|
|
if std::env::var("NYASH_VM_ESCAPE_ANALYSIS").ok().as_deref() == Some("1") {
|
|
|
|
|
let removed = nyash_rust::mir::passes::escape::escape_elide_barriers_vm(&mut module_vm);
|
2025-09-19 02:07:38 +09:00
|
|
|
if removed > 0 { crate::cli_v!("[VM] escape_elide_barriers: removed {} barriers", removed); }
|
2025-08-31 03:03:04 +09:00
|
|
|
}
|
|
|
|
|
|
2025-09-14 04:51:33 +09:00
|
|
|
// Optional: PyVM path. When NYASH_VM_USE_PY=1, emit MIR(JSON) and delegate execution to tools/pyvm_runner.py
|
|
|
|
|
if std::env::var("NYASH_VM_USE_PY").ok().as_deref() == Some("1") {
|
2025-09-19 02:07:38 +09:00
|
|
|
match super::common_util::pyvm::run_pyvm_harness_lib(&module_vm, "vm") {
|
|
|
|
|
Ok(code) => { process::exit(code); }
|
|
|
|
|
Err(e) => { eprintln!("❌ PyVM error: {}", e); process::exit(1); }
|
2025-09-14 04:51:33 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-01 23:44:34 +09:00
|
|
|
// Expose GC/scheduler hooks globally for JIT externs (checkpoint/await, etc.)
|
|
|
|
|
nyash_rust::runtime::global_hooks::set_from_runtime(&runtime);
|
|
|
|
|
|
2025-08-26 04:34:14 +09:00
|
|
|
// Execute with VM using prepared runtime
|
|
|
|
|
let mut vm = VM::with_runtime(runtime);
|
2025-08-31 03:03:04 +09:00
|
|
|
match vm.execute_module(&module_vm) {
|
2025-08-26 04:34:14 +09:00
|
|
|
Ok(result) => {
|
2025-09-17 07:43:07 +09:00
|
|
|
if !quiet_pipe {
|
|
|
|
|
println!("✅ VM execution completed successfully!");
|
|
|
|
|
}
|
2025-09-08 01:08:59 +09:00
|
|
|
// Pretty-print with coercions for plugin-backed values
|
|
|
|
|
// Prefer MIR signature when available, but fall back to runtime coercions to keep VM/JIT consistent.
|
|
|
|
|
let (ety, sval) = if let Some(func) = compile_result.module.functions.get("main") {
|
2025-09-17 07:43:07 +09:00
|
|
|
use nyash_rust::box_trait::{BoolBox, IntegerBox, StringBox};
|
2025-08-28 22:31:51 +09:00
|
|
|
use nyash_rust::boxes::FloatBox;
|
2025-09-17 07:43:07 +09:00
|
|
|
use nyash_rust::mir::MirType;
|
2025-09-08 01:08:59 +09:00
|
|
|
match &func.signature.return_type {
|
2025-08-28 22:31:51 +09:00
|
|
|
MirType::Float => {
|
|
|
|
|
if let Some(fb) = result.as_any().downcast_ref::<FloatBox>() {
|
|
|
|
|
("Float", format!("{}", fb.value))
|
|
|
|
|
} else if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
|
|
|
|
|
("Float", format!("{}", ib.value as f64))
|
2025-09-17 07:43:07 +09:00
|
|
|
} else if let Some(s) =
|
|
|
|
|
nyash_rust::runtime::semantics::coerce_to_string(result.as_ref())
|
|
|
|
|
{
|
2025-09-08 01:08:59 +09:00
|
|
|
("String", s)
|
|
|
|
|
} else {
|
|
|
|
|
(result.type_name(), result.to_string_box().value)
|
|
|
|
|
}
|
2025-08-28 22:31:51 +09:00
|
|
|
}
|
|
|
|
|
MirType::Integer => {
|
|
|
|
|
if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
|
|
|
|
|
("Integer", ib.value.to_string())
|
2025-09-17 07:43:07 +09:00
|
|
|
} else if let Some(i) =
|
|
|
|
|
nyash_rust::runtime::semantics::coerce_to_i64(result.as_ref())
|
|
|
|
|
{
|
2025-09-08 01:08:59 +09:00
|
|
|
("Integer", i.to_string())
|
|
|
|
|
} else {
|
|
|
|
|
(result.type_name(), result.to_string_box().value)
|
|
|
|
|
}
|
2025-08-28 22:31:51 +09:00
|
|
|
}
|
|
|
|
|
MirType::Bool => {
|
|
|
|
|
if let Some(bb) = result.as_any().downcast_ref::<BoolBox>() {
|
|
|
|
|
("Bool", bb.value.to_string())
|
|
|
|
|
} else if let Some(ib) = result.as_any().downcast_ref::<IntegerBox>() {
|
|
|
|
|
("Bool", (ib.value != 0).to_string())
|
2025-09-08 01:08:59 +09:00
|
|
|
} else {
|
|
|
|
|
(result.type_name(), result.to_string_box().value)
|
|
|
|
|
}
|
2025-08-28 22:31:51 +09:00
|
|
|
}
|
|
|
|
|
MirType::String => {
|
|
|
|
|
if let Some(sb) = result.as_any().downcast_ref::<StringBox>() {
|
|
|
|
|
("String", sb.value.clone())
|
2025-09-17 07:43:07 +09:00
|
|
|
} else if let Some(s) =
|
|
|
|
|
nyash_rust::runtime::semantics::coerce_to_string(result.as_ref())
|
|
|
|
|
{
|
2025-09-08 01:08:59 +09:00
|
|
|
("String", s)
|
|
|
|
|
} else {
|
|
|
|
|
(result.type_name(), result.to_string_box().value)
|
|
|
|
|
}
|
2025-08-28 22:31:51 +09:00
|
|
|
}
|
2025-09-08 01:08:59 +09:00
|
|
|
_ => {
|
2025-09-17 07:43:07 +09:00
|
|
|
if let Some(i) =
|
|
|
|
|
nyash_rust::runtime::semantics::coerce_to_i64(result.as_ref())
|
|
|
|
|
{
|
2025-09-08 01:08:59 +09:00
|
|
|
("Integer", i.to_string())
|
2025-09-17 07:43:07 +09:00
|
|
|
} else if let Some(s) =
|
|
|
|
|
nyash_rust::runtime::semantics::coerce_to_string(result.as_ref())
|
|
|
|
|
{
|
2025-09-08 01:08:59 +09:00
|
|
|
("String", s)
|
2025-09-17 07:43:07 +09:00
|
|
|
} else {
|
|
|
|
|
(result.type_name(), result.to_string_box().value)
|
|
|
|
|
}
|
2025-09-08 01:08:59 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-28 22:31:51 +09:00
|
|
|
} else {
|
2025-09-17 07:43:07 +09:00
|
|
|
if let Some(i) = nyash_rust::runtime::semantics::coerce_to_i64(result.as_ref())
|
|
|
|
|
{
|
2025-09-08 01:08:59 +09:00
|
|
|
("Integer", i.to_string())
|
2025-09-17 07:43:07 +09:00
|
|
|
} else if let Some(s) =
|
|
|
|
|
nyash_rust::runtime::semantics::coerce_to_string(result.as_ref())
|
|
|
|
|
{
|
2025-09-08 01:08:59 +09:00
|
|
|
("String", s)
|
2025-09-17 07:43:07 +09:00
|
|
|
} else {
|
|
|
|
|
(result.type_name(), result.to_string_box().value)
|
|
|
|
|
}
|
2025-09-08 01:08:59 +09:00
|
|
|
};
|
2025-09-15 18:44:49 +09:00
|
|
|
if !quiet_pipe {
|
|
|
|
|
println!("ResultType(MIR): {}", ety);
|
|
|
|
|
println!("Result: {}", sval);
|
|
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
}
|
|
|
|
|
Err(e) => {
|
|
|
|
|
eprintln!("❌ VM execution error: {}", e);
|
|
|
|
|
process::exit(1);
|
|
|
|
|
}
|
2025-08-26 04:34:14 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Collect Box declarations from AST and register into runtime
|
|
|
|
|
pub(crate) fn collect_box_declarations(&self, ast: &ASTNode, runtime: &NyashRuntime) {
|
2025-08-30 22:52:16 +09:00
|
|
|
fn resolve_include_path(filename: &str) -> String {
|
2025-09-17 07:43:07 +09:00
|
|
|
if filename.starts_with("./") || filename.starts_with("../") {
|
|
|
|
|
return filename.to_string();
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
let parts: Vec<&str> = filename.splitn(2, '/').collect();
|
|
|
|
|
if parts.len() == 2 {
|
2025-09-17 07:43:07 +09:00
|
|
|
let root = parts[0];
|
|
|
|
|
let rest = parts[1];
|
2025-08-30 22:52:16 +09:00
|
|
|
let cfg_path = "nyash.toml";
|
|
|
|
|
if let Ok(toml_str) = std::fs::read_to_string(cfg_path) {
|
|
|
|
|
if let Ok(toml_val) = toml::from_str::<toml::Value>(&toml_str) {
|
|
|
|
|
if let Some(include) = toml_val.get("include") {
|
|
|
|
|
if let Some(roots) = include.get("roots").and_then(|v| v.as_table()) {
|
|
|
|
|
if let Some(base) = roots.get(root).and_then(|v| v.as_str()) {
|
2025-09-17 07:43:07 +09:00
|
|
|
let mut b = base.to_string();
|
|
|
|
|
if !b.ends_with('/') && !b.ends_with('\\') {
|
|
|
|
|
b.push('/');
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
return format!("{}{}", b, rest);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
format!("./{}", filename)
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 06:13:44 +09:00
|
|
|
use std::collections::HashSet;
|
2025-08-30 23:47:08 +09:00
|
|
|
|
2025-09-17 07:43:07 +09:00
|
|
|
fn walk_with_state(
|
|
|
|
|
node: &ASTNode,
|
|
|
|
|
runtime: &NyashRuntime,
|
|
|
|
|
stack: &mut Vec<String>,
|
|
|
|
|
visited: &mut HashSet<String>,
|
|
|
|
|
) {
|
2025-08-26 04:34:14 +09:00
|
|
|
match node {
|
2025-09-17 07:43:07 +09:00
|
|
|
ASTNode::Program { statements, .. } => {
|
|
|
|
|
for st in statements {
|
|
|
|
|
walk_with_state(st, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ASTNode::FunctionDeclaration { body, .. } => {
|
|
|
|
|
for st in body {
|
|
|
|
|
walk_with_state(st, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
ASTNode::Include { filename, .. } => {
|
|
|
|
|
let mut path = resolve_include_path(filename);
|
|
|
|
|
if std::path::Path::new(&path).is_dir() {
|
|
|
|
|
path = format!("{}/index.nyash", path.trim_end_matches('/'));
|
|
|
|
|
} else if std::path::Path::new(&path).extension().is_none() {
|
|
|
|
|
path.push_str(".nyash");
|
|
|
|
|
}
|
2025-08-30 23:47:08 +09:00
|
|
|
// Cycle detection using stack
|
|
|
|
|
if let Some(pos) = stack.iter().position(|p| p == &path) {
|
|
|
|
|
let mut chain = stack[pos..].to_vec();
|
|
|
|
|
chain.push(path.clone());
|
|
|
|
|
eprintln!("include cycle detected (collector): {}", chain.join(" -> "));
|
|
|
|
|
return; // Skip to avoid infinite recursion
|
|
|
|
|
}
|
|
|
|
|
if visited.contains(&path) {
|
|
|
|
|
return; // Already processed
|
|
|
|
|
}
|
|
|
|
|
stack.push(path.clone());
|
2025-08-30 22:52:16 +09:00
|
|
|
if let Ok(content) = std::fs::read_to_string(&path) {
|
|
|
|
|
if let Ok(inc_ast) = NyashParser::parse_from_string(&content) {
|
2025-08-30 23:47:08 +09:00
|
|
|
walk_with_state(&inc_ast, runtime, stack, visited);
|
|
|
|
|
visited.insert(path);
|
2025-08-30 22:52:16 +09:00
|
|
|
}
|
|
|
|
|
}
|
2025-08-30 23:47:08 +09:00
|
|
|
stack.pop();
|
2025-08-30 22:52:16 +09:00
|
|
|
}
|
|
|
|
|
ASTNode::Assignment { target, value, .. } => {
|
2025-09-17 07:43:07 +09:00
|
|
|
walk_with_state(target, runtime, stack, visited);
|
|
|
|
|
walk_with_state(value, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
ASTNode::Return { value, .. } => {
|
|
|
|
|
if let Some(v) = value {
|
|
|
|
|
walk_with_state(v, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ASTNode::Print { expression, .. } => {
|
|
|
|
|
walk_with_state(expression, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
ASTNode::If {
|
|
|
|
|
condition,
|
|
|
|
|
then_body,
|
|
|
|
|
else_body,
|
|
|
|
|
..
|
|
|
|
|
} => {
|
|
|
|
|
walk_with_state(condition, runtime, stack, visited);
|
|
|
|
|
for st in then_body {
|
|
|
|
|
walk_with_state(st, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
if let Some(eb) = else_body {
|
|
|
|
|
for st in eb {
|
|
|
|
|
walk_with_state(st, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
ASTNode::Loop {
|
|
|
|
|
condition, body, ..
|
|
|
|
|
} => {
|
2025-08-30 23:47:08 +09:00
|
|
|
walk_with_state(condition, runtime, stack, visited);
|
2025-09-17 07:43:07 +09:00
|
|
|
for st in body {
|
|
|
|
|
walk_with_state(st, runtime, stack, visited);
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
ASTNode::TryCatch {
|
|
|
|
|
try_body,
|
|
|
|
|
catch_clauses,
|
|
|
|
|
finally_body,
|
|
|
|
|
..
|
|
|
|
|
} => {
|
|
|
|
|
for st in try_body {
|
|
|
|
|
walk_with_state(st, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
for cc in catch_clauses {
|
|
|
|
|
for st in &cc.body {
|
|
|
|
|
walk_with_state(st, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if let Some(fb) = finally_body {
|
|
|
|
|
for st in fb {
|
|
|
|
|
walk_with_state(st, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
ASTNode::Throw { expression, .. } => {
|
|
|
|
|
walk_with_state(expression, runtime, stack, visited);
|
2025-08-30 22:52:16 +09:00
|
|
|
}
|
|
|
|
|
ASTNode::Local { initial_values, .. } => {
|
2025-09-17 07:43:07 +09:00
|
|
|
for iv in initial_values {
|
|
|
|
|
if let Some(v) = iv {
|
|
|
|
|
walk_with_state(v, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
}
|
|
|
|
|
ASTNode::Outbox { initial_values, .. } => {
|
2025-09-17 07:43:07 +09:00
|
|
|
for iv in initial_values {
|
|
|
|
|
if let Some(v) = iv {
|
|
|
|
|
walk_with_state(v, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ASTNode::FunctionCall { arguments, .. } => {
|
|
|
|
|
for a in arguments {
|
|
|
|
|
walk_with_state(a, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ASTNode::MethodCall {
|
|
|
|
|
object, arguments, ..
|
|
|
|
|
} => {
|
|
|
|
|
walk_with_state(object, runtime, stack, visited);
|
|
|
|
|
for a in arguments {
|
|
|
|
|
walk_with_state(a, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ASTNode::FieldAccess { object, .. } => {
|
|
|
|
|
walk_with_state(object, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
ASTNode::New { arguments, .. } => {
|
|
|
|
|
for a in arguments {
|
|
|
|
|
walk_with_state(a, runtime, stack, visited);
|
|
|
|
|
}
|
2025-08-30 22:52:16 +09:00
|
|
|
}
|
2025-09-17 07:43:07 +09:00
|
|
|
ASTNode::BinaryOp { left, right, .. } => {
|
|
|
|
|
walk_with_state(left, runtime, stack, visited);
|
|
|
|
|
walk_with_state(right, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
ASTNode::UnaryOp { operand, .. } => {
|
|
|
|
|
walk_with_state(operand, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
ASTNode::AwaitExpression { expression, .. } => {
|
|
|
|
|
walk_with_state(expression, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
ASTNode::Arrow {
|
|
|
|
|
sender, receiver, ..
|
|
|
|
|
} => {
|
|
|
|
|
walk_with_state(sender, runtime, stack, visited);
|
|
|
|
|
walk_with_state(receiver, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
ASTNode::Nowait { expression, .. } => {
|
|
|
|
|
walk_with_state(expression, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
ASTNode::BoxDeclaration {
|
|
|
|
|
name,
|
|
|
|
|
fields,
|
|
|
|
|
public_fields,
|
|
|
|
|
private_fields,
|
|
|
|
|
methods,
|
|
|
|
|
constructors,
|
|
|
|
|
init_fields,
|
|
|
|
|
weak_fields,
|
|
|
|
|
is_interface,
|
|
|
|
|
extends,
|
|
|
|
|
implements,
|
|
|
|
|
type_parameters,
|
|
|
|
|
..
|
|
|
|
|
} => {
|
|
|
|
|
for (_mname, mnode) in methods {
|
|
|
|
|
walk_with_state(mnode, runtime, stack, visited);
|
|
|
|
|
}
|
|
|
|
|
for (_ckey, cnode) in constructors {
|
|
|
|
|
walk_with_state(cnode, runtime, stack, visited);
|
|
|
|
|
}
|
2025-08-26 04:34:14 +09:00
|
|
|
let decl = CoreBoxDecl {
|
|
|
|
|
name: name.clone(),
|
|
|
|
|
fields: fields.clone(),
|
|
|
|
|
public_fields: public_fields.clone(),
|
|
|
|
|
private_fields: private_fields.clone(),
|
|
|
|
|
methods: methods.clone(),
|
|
|
|
|
constructors: constructors.clone(),
|
|
|
|
|
init_fields: init_fields.clone(),
|
|
|
|
|
weak_fields: weak_fields.clone(),
|
|
|
|
|
is_interface: *is_interface,
|
|
|
|
|
extends: extends.clone(),
|
|
|
|
|
implements: implements.clone(),
|
|
|
|
|
type_parameters: type_parameters.clone(),
|
|
|
|
|
};
|
2025-09-17 07:43:07 +09:00
|
|
|
if let Ok(mut map) = runtime.box_declarations.write() {
|
|
|
|
|
map.insert(name.clone(), decl);
|
|
|
|
|
}
|
2025-08-26 04:34:14 +09:00
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-30 23:47:08 +09:00
|
|
|
let mut stack: Vec<String> = Vec::new();
|
|
|
|
|
let mut visited: HashSet<String> = HashSet::new();
|
|
|
|
|
walk_with_state(ast, runtime, &mut stack, &mut visited);
|
2025-08-26 04:34:14 +09:00
|
|
|
}
|
|
|
|
|
}
|