feat: nyash.toml SSOT + using AST統合完了(12時間の戦い)
- nyash.tomlを唯一の真実(SSOT)として依存管理確立 - dev/ci/prodプロファイルによる段階的厳格化実装 - AST結合で宣言/式の曖昧性を根本解決 - Fail-Fast原則をCLAUDE.md/AGENTS.mdに明文化 - VM fallbackでもASTベース using有効化(NYASH_USING_AST=1) - 静的メソッドの is_static=true 修正で解決安定化 - STATICブレークハック既定OFF化で堅牢性向上 🎉 usingシステム完全体への道筋確立!JSONライブラリ・Nyash VM開発が可能に Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@ -56,7 +56,7 @@ impl NyashRunner {
|
||||
let mut prelude_asts: Vec<nyash_rust::ast::ASTNode> = Vec::new();
|
||||
if crate::config::env::enable_using() {
|
||||
if use_ast {
|
||||
match crate::runner::modes::common_util::resolve::collect_using_and_strip(self, &code, filename) {
|
||||
match crate::runner::modes::common_util::resolve::resolve_prelude_paths_profiled(self, &code, filename) {
|
||||
Ok((clean, paths)) => {
|
||||
cleaned_code_owned = clean; code_ref = &cleaned_code_owned;
|
||||
// Parse each prelude file into AST and store
|
||||
@ -109,6 +109,30 @@ impl NyashRunner {
|
||||
ASTNode::Program { statements: combined, span: nyash_rust::ast::Span::unknown() }
|
||||
} else { main_ast };
|
||||
|
||||
// Optional: dump AST statement kinds for quick diagnostics
|
||||
if std::env::var("NYASH_AST_DUMP").ok().as_deref() == Some("1") {
|
||||
use nyash_rust::ast::ASTNode;
|
||||
eprintln!("[ast] dump start");
|
||||
if let ASTNode::Program { statements, .. } = &ast {
|
||||
for (i, st) in statements.iter().enumerate().take(50) {
|
||||
let kind = match st {
|
||||
ASTNode::BoxDeclaration { is_static, name, .. } => {
|
||||
if *is_static { format!("StaticBox({})", name) } else { format!("Box({})", name) }
|
||||
}
|
||||
ASTNode::FunctionDeclaration { name, .. } => format!("FuncDecl({})", name),
|
||||
ASTNode::FunctionCall { name, .. } => format!("FuncCall({})", name),
|
||||
ASTNode::MethodCall { method, .. } => format!("MethodCall({})", method),
|
||||
ASTNode::ScopeBox { .. } => "ScopeBox".to_string(),
|
||||
ASTNode::ImportStatement { path, .. } => format!("Import({})", path),
|
||||
ASTNode::UsingStatement { namespace_name, .. } => format!("Using({})", namespace_name),
|
||||
_ => format!("{:?}", st),
|
||||
};
|
||||
eprintln!("[ast] {}: {}", i, kind);
|
||||
}
|
||||
}
|
||||
eprintln!("[ast] dump end");
|
||||
}
|
||||
|
||||
// Stage-0: import loader (top-level only) — resolve path and register in modules registry
|
||||
if let nyash_rust::ast::ASTNode::Program { statements, .. } = &ast {
|
||||
for st in statements {
|
||||
|
||||
@ -8,5 +8,4 @@ pub mod strip;
|
||||
pub mod seam;
|
||||
|
||||
// Public re-exports to preserve existing call sites
|
||||
pub use strip::{strip_using_and_register, preexpand_at_local};
|
||||
|
||||
pub use strip::{strip_using_and_register, preexpand_at_local, collect_using_and_strip, resolve_prelude_paths_profiled};
|
||||
|
||||
@ -62,6 +62,10 @@ pub fn strip_using_and_register(
|
||||
if !crate::config::env::enable_using() {
|
||||
return Ok(code.to_string());
|
||||
}
|
||||
// Profile guard: legacy text inlining is not allowed under prod profile
|
||||
if crate::config::env::using_is_prod() {
|
||||
return Err("using: text inlining is disabled in prod profile. Enable NYASH_USING_AST=1 and declare dependencies in nyash.toml [using]".to_string());
|
||||
}
|
||||
// Optional external combiner (default OFF): NYASH_USING_COMBINER=1
|
||||
if std::env::var("NYASH_USING_COMBINER").ok().as_deref() == Some("1") {
|
||||
let fix_braces = crate::config::env::resolve_fix_braces();
|
||||
@ -435,6 +439,8 @@ pub fn collect_using_and_strip(
|
||||
return Ok((code.to_string(), Vec::new()));
|
||||
}
|
||||
let using_ctx = runner.init_using_context();
|
||||
let prod = crate::config::env::using_is_prod();
|
||||
let dev = crate::config::env::using_is_dev();
|
||||
let strict = std::env::var("NYASH_USING_STRICT").ok().as_deref() == Some("1");
|
||||
let verbose = crate::config::env::cli_verbose()
|
||||
|| std::env::var("NYASH_RESOLVE_TRACE").ok().as_deref() == Some("1");
|
||||
@ -454,6 +460,12 @@ pub fn collect_using_and_strip(
|
||||
} else { (rest0.to_string(), None) };
|
||||
let is_path = target.starts_with('"') || target.starts_with("./") || target.starts_with('/') || target.ends_with(".nyash");
|
||||
if is_path {
|
||||
if prod || !crate::config::env::allow_using_file() {
|
||||
return Err(format!(
|
||||
"using: file paths are disallowed in this profile. Add it to nyash.toml [using] (packages/aliases) and reference by name: {}",
|
||||
target
|
||||
));
|
||||
}
|
||||
let path = target.trim_matches('"').to_string();
|
||||
// Resolve relative to current file dir
|
||||
let mut p = std::path::PathBuf::from(&path);
|
||||
@ -464,29 +476,67 @@ pub fn collect_using_and_strip(
|
||||
continue;
|
||||
}
|
||||
// Resolve namespaces/packages
|
||||
match crate::runner::pipeline::resolve_using_target(
|
||||
&target,
|
||||
false,
|
||||
&using_ctx.pending_modules,
|
||||
&using_ctx.using_paths,
|
||||
&using_ctx.aliases,
|
||||
&using_ctx.packages,
|
||||
ctx_dir,
|
||||
strict,
|
||||
verbose,
|
||||
) {
|
||||
Ok(value) => {
|
||||
// Only file paths are candidates for AST prelude merge
|
||||
if value.ends_with(".nyash") || value.contains('/') || value.contains('\\') {
|
||||
// Resolve relative
|
||||
let mut p = std::path::PathBuf::from(&value);
|
||||
if p.is_relative() {
|
||||
if let Some(dir) = ctx_dir { let cand = dir.join(&p); if cand.exists() { p = cand; } }
|
||||
}
|
||||
prelude_paths.push(p.to_string_lossy().to_string());
|
||||
}
|
||||
if prod {
|
||||
// prod: only allow names present in aliases/packages (toml)
|
||||
let mut pkg_name: String = target.clone();
|
||||
if let Some(v) = using_ctx.aliases.get(&target) {
|
||||
pkg_name = v.clone();
|
||||
}
|
||||
if let Some(pkg) = using_ctx.packages.get(&pkg_name) {
|
||||
use crate::using::spec::PackageKind;
|
||||
match pkg.kind {
|
||||
PackageKind::Dylib => {
|
||||
// dylib: nothing to prelude-parse; runtime loader handles it.
|
||||
}
|
||||
PackageKind::Package => {
|
||||
let base = std::path::Path::new(&pkg.path);
|
||||
let out = if let Some(m) = &pkg.main {
|
||||
if base.extension().and_then(|s| s.to_str()) == Some("nyash") {
|
||||
pkg.path.clone()
|
||||
} else {
|
||||
base.join(m).to_string_lossy().to_string()
|
||||
}
|
||||
} else if base.extension().and_then(|s| s.to_str()) == Some("nyash") {
|
||||
pkg.path.clone()
|
||||
} else {
|
||||
let leaf = base.file_name().and_then(|s| s.to_str()).unwrap_or(&pkg_name);
|
||||
base.join(format!("{}.nyash", leaf)).to_string_lossy().to_string()
|
||||
};
|
||||
prelude_paths.push(out);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(format!(
|
||||
"using: '{}' not found in nyash.toml [using]. Define a package or alias and use its name (prod profile)",
|
||||
target
|
||||
));
|
||||
}
|
||||
} else {
|
||||
// dev/ci: allow broader resolution via resolver
|
||||
match crate::runner::pipeline::resolve_using_target(
|
||||
&target,
|
||||
false,
|
||||
&using_ctx.pending_modules,
|
||||
&using_ctx.using_paths,
|
||||
&using_ctx.aliases,
|
||||
&using_ctx.packages,
|
||||
ctx_dir,
|
||||
strict,
|
||||
verbose,
|
||||
) {
|
||||
Ok(value) => {
|
||||
// Only file paths are candidates for AST prelude merge
|
||||
if value.ends_with(".nyash") || value.contains('/') || value.contains('\\') {
|
||||
// Resolve relative
|
||||
let mut p = std::path::PathBuf::from(&value);
|
||||
if p.is_relative() {
|
||||
if let Some(dir) = ctx_dir { let cand = dir.join(&p); if cand.exists() { p = cand; } }
|
||||
}
|
||||
prelude_paths.push(p.to_string_lossy().to_string());
|
||||
}
|
||||
}
|
||||
Err(e) => return Err(format!("using: {}", e)),
|
||||
}
|
||||
Err(e) => return Err(format!("using: {}", e)),
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@ -503,6 +553,17 @@ pub fn collect_using_and_strip(
|
||||
Ok((out, prelude_paths))
|
||||
}
|
||||
|
||||
/// Profile-aware prelude resolution wrapper.
|
||||
/// Currently delegates to `collect_using_and_strip`, but provides a single
|
||||
/// entry point for callers (common and vm_fallback) to avoid logic drift.
|
||||
pub fn resolve_prelude_paths_profiled(
|
||||
runner: &NyashRunner,
|
||||
code: &str,
|
||||
filename: &str,
|
||||
) -> Result<(String, Vec<String>), String> {
|
||||
collect_using_and_strip(runner, code, filename)
|
||||
}
|
||||
|
||||
/// Pre-expand line-head `@name[: Type] = expr` into `local name[: Type] = expr`.
|
||||
/// Minimal, safe, no semantics change. Applies only at line head (after spaces/tabs).
|
||||
pub fn preexpand_at_local(src: &str) -> String {
|
||||
|
||||
@ -12,23 +12,79 @@ impl NyashRunner {
|
||||
Ok(s) => s,
|
||||
Err(e) => { eprintln!("❌ Error reading file {}: {}", filename, e); process::exit(1); }
|
||||
};
|
||||
// Using preprocessing (strip + autoload)
|
||||
// Using preprocessing (legacy inline or AST-prelude merge when NYASH_USING_AST=1)
|
||||
let mut code2 = code;
|
||||
let use_ast_prelude = crate::config::env::enable_using()
|
||||
&& std::env::var("NYASH_USING_AST").ok().as_deref() == Some("1");
|
||||
let mut prelude_asts: Vec<nyash_rust::ast::ASTNode> = Vec::new();
|
||||
if crate::config::env::enable_using() {
|
||||
match crate::runner::modes::common_util::resolve::strip_using_and_register(self, &code2, filename) {
|
||||
Ok(s) => { code2 = s; }
|
||||
Err(e) => { eprintln!("❌ {}", e); process::exit(1); }
|
||||
if use_ast_prelude {
|
||||
match crate::runner::modes::common_util::resolve::resolve_prelude_paths_profiled(self, &code2, filename) {
|
||||
Ok((clean, paths)) => {
|
||||
code2 = clean;
|
||||
for p in paths {
|
||||
match std::fs::read_to_string(&p) {
|
||||
Ok(src) => match NyashParser::parse_from_string(&src) {
|
||||
Ok(ast) => prelude_asts.push(ast),
|
||||
Err(e) => { eprintln!("❌ Parse error in using prelude {}: {}", p, e); process::exit(1); }
|
||||
},
|
||||
Err(e) => { eprintln!("❌ Error reading using prelude {}: {}", p, e); process::exit(1); }
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => { eprintln!("❌ {}", e); process::exit(1); }
|
||||
}
|
||||
} else {
|
||||
match crate::runner::modes::common_util::resolve::strip_using_and_register(self, &code2, filename) {
|
||||
Ok(s) => { code2 = s; }
|
||||
Err(e) => { eprintln!("❌ {}", e); process::exit(1); }
|
||||
}
|
||||
}
|
||||
}
|
||||
// Dev sugar pre-expand: @name = expr → local name = expr
|
||||
code2 = crate::runner::modes::common_util::resolve::preexpand_at_local(&code2);
|
||||
|
||||
// Parse -> expand macros -> compile MIR
|
||||
let ast = match NyashParser::parse_from_string(&code2) {
|
||||
// Parse main code
|
||||
let main_ast = match NyashParser::parse_from_string(&code2) {
|
||||
Ok(ast) => ast,
|
||||
Err(e) => { eprintln!("❌ Parse error: {}", e); process::exit(1); }
|
||||
};
|
||||
let ast = crate::r#macro::maybe_expand_and_dump(&ast, false);
|
||||
// When using AST prelude mode, combine prelude ASTs + main AST into one Program before macro expansion
|
||||
let ast_combined = if use_ast_prelude && !prelude_asts.is_empty() {
|
||||
use nyash_rust::ast::ASTNode;
|
||||
let mut combined: Vec<ASTNode> = Vec::new();
|
||||
for a in prelude_asts {
|
||||
if let ASTNode::Program { statements, .. } = a { combined.extend(statements); }
|
||||
}
|
||||
if let ASTNode::Program { statements, .. } = main_ast.clone() {
|
||||
combined.extend(statements);
|
||||
}
|
||||
ASTNode::Program { statements: combined, span: nyash_rust::ast::Span::unknown() }
|
||||
} else { main_ast };
|
||||
// Optional: dump AST statement kinds for quick diagnostics
|
||||
if std::env::var("NYASH_AST_DUMP").ok().as_deref() == Some("1") {
|
||||
use nyash_rust::ast::ASTNode;
|
||||
eprintln!("[ast] dump start (vm-fallback)");
|
||||
if let ASTNode::Program { statements, .. } = &ast_combined {
|
||||
for (i, st) in statements.iter().enumerate().take(50) {
|
||||
let kind = match st {
|
||||
ASTNode::BoxDeclaration { is_static, name, .. } => {
|
||||
if *is_static { format!("StaticBox({})", name) } else { format!("Box({})", name) }
|
||||
}
|
||||
ASTNode::FunctionDeclaration { name, .. } => format!("FuncDecl({})", name),
|
||||
ASTNode::FunctionCall { name, .. } => format!("FuncCall({})", name),
|
||||
ASTNode::MethodCall { method, .. } => format!("MethodCall({})", method),
|
||||
ASTNode::ScopeBox { .. } => "ScopeBox".to_string(),
|
||||
ASTNode::ImportStatement { path, .. } => format!("Import({})", path),
|
||||
ASTNode::UsingStatement { namespace_name, .. } => format!("Using({})", namespace_name),
|
||||
_ => format!("{:?}", st),
|
||||
};
|
||||
eprintln!("[ast] {}: {}", i, kind);
|
||||
}
|
||||
}
|
||||
eprintln!("[ast] dump end");
|
||||
}
|
||||
let ast = crate::r#macro::maybe_expand_and_dump(&ast_combined, false);
|
||||
let mut compiler = MirCompiler::with_options(!self.config.no_optimize);
|
||||
let compile = match compiler.compile(ast) {
|
||||
Ok(c) => c,
|
||||
@ -44,6 +100,12 @@ impl NyashRunner {
|
||||
|
||||
// Execute via MIR interpreter
|
||||
let mut vm = MirInterpreter::new();
|
||||
if std::env::var("NYASH_DUMP_FUNCS").ok().as_deref() == Some("1") {
|
||||
eprintln!("[vm] functions available:");
|
||||
for k in module_vm.functions.keys() {
|
||||
eprintln!(" - {}", k);
|
||||
}
|
||||
}
|
||||
match vm.execute_module(&module_vm) {
|
||||
Ok(_ret) => { /* interpreter already prints via println/console in program */ }
|
||||
Err(e) => {
|
||||
|
||||
Reference in New Issue
Block a user