Phase 25.1 完了成果: - ✅ LoopForm v2 テスト・ドキュメント・コメント完備 - 4ケース(A/B/C/D)完全テストカバレッジ - 最小再現ケース作成(SSAバグ調査用) - SSOT文書作成(loopform_ssot.md) - 全ソースに [LoopForm] コメントタグ追加 - ✅ Stage-1 CLI デバッグ環境構築 - stage1_cli.hako 実装 - stage1_bridge.rs ブリッジ実装 - デバッグツール作成(stage1_debug.sh/stage1_minimal.sh) - アーキテクチャ改善提案文書 - ✅ 環境変数削減計画策定 - 25変数の完全調査・分類 - 6段階削減ロードマップ(25→5、80%削減) - 即時削除可能変数特定(NYASH_CONFIG/NYASH_DEBUG) Phase 26-D からの累積変更: - PHI実装改善(ExitPhiBuilder/HeaderPhiBuilder等) - MIRビルダーリファクタリング - 型伝播・最適化パス改善 - その他約300ファイルの累積変更 🎯 技術的成果: - SSAバグ根本原因特定(条件分岐内loop変数変更) - Region+next_iパターン適用完了(UsingCollectorBox等) - LoopFormパターン文書化・テスト化完了 - セルフホスティング基盤強化 Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: ChatGPT <noreply@openai.com> Co-Authored-By: Task Assistant <task@anthropic.com>
249 lines
8.2 KiB
Rust
249 lines
8.2 KiB
Rust
/*!
|
|
* CLI Directives Scanner — early source comments to env plumbing
|
|
*
|
|
* Supports lightweight, file-scoped directives placed in the first lines
|
|
* of a Nyash source file. Current directives:
|
|
* - // @env KEY=VALUE → export KEY=VALUE into process env
|
|
* - // @plugin-builtins → NYASH_USE_PLUGIN_BUILTINS=1
|
|
* - // @jit-debug → enable common JIT debug flags (no-op if JIT unused)
|
|
* - // @jit-strict → strict JIT flags (no VM fallback) for experiments
|
|
*
|
|
* Also runs the "fields-at-top" lint delegated to pipeline::lint_fields_top.
|
|
*/
|
|
|
|
pub(super) fn apply_cli_directives_from_source(
|
|
code: &str,
|
|
strict_fields: bool,
|
|
verbose: bool,
|
|
) -> Result<(), String> {
|
|
// Scan only the header area (up to the first non-comment content line)
|
|
for (i, line) in code.lines().take(128).enumerate() {
|
|
let l = line.trim();
|
|
if !(l.starts_with("//") || l.starts_with("#!") || l.is_empty()) {
|
|
if i > 0 {
|
|
break;
|
|
}
|
|
}
|
|
if let Some(rest) = l.strip_prefix("//") {
|
|
let rest = rest.trim();
|
|
if let Some(dir) = rest.strip_prefix("@env ") {
|
|
if let Some((k, v)) = dir.split_once('=') {
|
|
let key = k.trim();
|
|
let val = v.trim();
|
|
if !key.is_empty() {
|
|
std::env::set_var(key, val);
|
|
}
|
|
}
|
|
} else if rest == "@plugin-builtins" {
|
|
std::env::set_var("NYASH_USE_PLUGIN_BUILTINS", "1");
|
|
} else if rest == "@jit-debug" {
|
|
// Safe even if JIT is disabled elsewhere; treated as no-op flags
|
|
std::env::set_var("NYASH_JIT_EXEC", "1");
|
|
std::env::set_var("NYASH_JIT_THRESHOLD", "1");
|
|
std::env::set_var("NYASH_JIT_EVENTS", "1");
|
|
std::env::set_var("NYASH_JIT_EVENTS_COMPILE", "1");
|
|
std::env::set_var("NYASH_JIT_EVENTS_RUNTIME", "1");
|
|
std::env::set_var("NYASH_JIT_SHIM_TRACE", "1");
|
|
} else if rest == "@jit-strict" {
|
|
std::env::set_var("NYASH_JIT_STRICT", "1");
|
|
std::env::set_var("NYASH_JIT_ARGS_HANDLE_ONLY", "1");
|
|
if std::env::var("NYASH_JIT_ONLY").ok().is_none() {
|
|
std::env::set_var("NYASH_JIT_ONLY", "1");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Lint: enforce fields at top-of-box (delegated)
|
|
super::pipeline::lint_fields_top(code, strict_fields, verbose)?;
|
|
|
|
// Dev-only guards (strict but opt-in via env)
|
|
// 1) ASI strict: disallow binary operator at end-of-line (line continuation)
|
|
if std::env::var("NYASH_ASI_STRICT").ok().as_deref() == Some("1") {
|
|
// operators to check (suffixes)
|
|
const OP2: [&str; 6] = ["==", "!=", "<=", ">=", "&&", "||"];
|
|
const OP1: [&str; 7] = ["+", "-", "*", "/", "%", "<", ">"];
|
|
for (i, line) in code.lines().enumerate() {
|
|
let l = line.trim_end();
|
|
if l.is_empty() {
|
|
continue;
|
|
}
|
|
let mut bad = false;
|
|
for op in OP2.iter() {
|
|
if l.ends_with(op) {
|
|
bad = true;
|
|
break;
|
|
}
|
|
}
|
|
if !bad {
|
|
for op in OP1.iter() {
|
|
if l.ends_with(op) {
|
|
bad = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if bad {
|
|
return Err(format!(
|
|
"Parse error: Strict ASI violation — line {} ends with operator",
|
|
i + 1
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
// 2) '+' mixed types (String and Number) error (opt-in)
|
|
if std::env::var("NYASH_PLUS_MIX_ERROR").ok().as_deref() == Some("1") {
|
|
for (i, line) in code.lines().enumerate() {
|
|
if let Some((ltok, rtok)) = find_plus_operands(line) {
|
|
let left_is_num = is_number_literal(ltok);
|
|
let right_is_str = is_string_literal(rtok);
|
|
let left_is_str = is_string_literal(ltok);
|
|
let right_is_num = is_number_literal(rtok);
|
|
if (left_is_num && right_is_str) || (left_is_str && right_is_num) {
|
|
return Err(format!("Type error: '+' mixed String and Number at line {} (use str()/explicit conversion)", i + 1));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3) '==' on likely Box variables: emit guidance to use equals() (opt-in)
|
|
if std::env::var("NYASH_BOX_EQ_GUIDE_ERROR").ok().as_deref() == Some("1") {
|
|
for (i, line) in code.lines().enumerate() {
|
|
let l = line;
|
|
let mut idx = 0usize;
|
|
while let Some(pos) = l[idx..].find("==") {
|
|
let at = idx + pos;
|
|
// find left token end and right token start
|
|
let (left_ok, right_ok) = (peek_ident_left(l, at), peek_ident_right(l, at + 2));
|
|
if left_ok && right_ok {
|
|
return Err(format!(
|
|
"Type error: '==' on boxes — use equals() (line {})",
|
|
i + 1
|
|
));
|
|
}
|
|
idx = at + 2;
|
|
}
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// ---- Helpers (very small, no external deps) ----
|
|
fn is_number_literal(s: &str) -> bool {
|
|
let t = s.trim();
|
|
!t.is_empty() && t.chars().all(|c| c.is_ascii_digit())
|
|
}
|
|
fn is_string_literal(s: &str) -> bool {
|
|
let t = s.trim();
|
|
t.starts_with('"')
|
|
}
|
|
|
|
fn find_plus_operands(line: &str) -> Option<(&str, &str)> {
|
|
let bytes = line.as_bytes();
|
|
let mut i = 0usize;
|
|
while i < bytes.len() {
|
|
if bytes[i] as char == '+' {
|
|
// extract left token
|
|
let mut l = i;
|
|
while l > 0 && bytes[l - 1].is_ascii_whitespace() {
|
|
l -= 1;
|
|
}
|
|
let mut lstart = l;
|
|
while lstart > 0 {
|
|
let c = bytes[lstart - 1] as char;
|
|
if c.is_ascii_alphanumeric() || c == '_' || c == '"' {
|
|
lstart -= 1;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
let left = &line[lstart..l];
|
|
// extract right token
|
|
let mut r = i + 1;
|
|
while r < bytes.len() && bytes[r].is_ascii_whitespace() {
|
|
r += 1;
|
|
}
|
|
let mut rend = r;
|
|
while rend < bytes.len() {
|
|
let c = bytes[rend] as char;
|
|
if c.is_ascii_alphanumeric() || c == '_' || c == '"' {
|
|
rend += 1;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
if r <= rend {
|
|
let right = &line[r..rend];
|
|
return Some((left, right));
|
|
}
|
|
return None;
|
|
}
|
|
i += 1;
|
|
}
|
|
None
|
|
}
|
|
|
|
fn peek_ident_left(s: &str, pos: usize) -> bool {
|
|
// scan left for first non-space token end, then back to token start
|
|
let bytes = s.as_bytes();
|
|
if pos == 0 {
|
|
return false;
|
|
}
|
|
let mut i = pos;
|
|
// skip spaces
|
|
while i > 0 && bytes[i - 1].is_ascii_whitespace() {
|
|
i -= 1;
|
|
}
|
|
if i == 0 {
|
|
return false;
|
|
}
|
|
// now consume identifier chars backwards
|
|
let mut j = i;
|
|
while j > 0 {
|
|
let c = bytes[j - 1] as char;
|
|
if c.is_ascii_alphanumeric() || c == '_' {
|
|
j -= 1;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
if j == i {
|
|
return false;
|
|
}
|
|
// ensure not starting with digit only (avoid numeric literal)
|
|
let tok = &s[j..i];
|
|
!tok.chars()
|
|
.next()
|
|
.map(|c| c.is_ascii_digit())
|
|
.unwrap_or(false)
|
|
}
|
|
fn peek_ident_right(s: &str, pos: usize) -> bool {
|
|
let bytes = s.as_bytes();
|
|
let mut i = pos;
|
|
while i < bytes.len() && bytes[i].is_ascii_whitespace() {
|
|
i += 1;
|
|
}
|
|
if i >= bytes.len() {
|
|
return false;
|
|
}
|
|
let mut j = i;
|
|
while j < bytes.len() {
|
|
let c = bytes[j] as char;
|
|
if c.is_ascii_alphanumeric() || c == '_' {
|
|
j += 1;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
if j == i {
|
|
return false;
|
|
}
|
|
let tok = &s[i..j];
|
|
!tok.chars()
|
|
.next()
|
|
.map(|c| c.is_ascii_digit())
|
|
.unwrap_or(false)
|
|
}
|