phase15: implement Phase S root treatment for break control flow
🔧 **Phase S (Immediate Stabilization) Implementation** - Create control flow utilities module (src/mir/utils/) - Refactor loop_builder.rs duplicated code to utilities - Fix PHI incoming predecessor capture per ChatGPT Pro analysis 📊 **AI Collaborative Analysis Complete** - Task agent: Root cause identification - Gemini: Strategic 3-phase approach - codex: Advanced type inference solution (archived) - ChatGPT Pro: Definitive staged treatment strategy 🗂️ **Documentation & Archive** - Strategy document: docs/development/strategies/break-control-flow-strategy.md - codex solutions: archive/codex-solutions/ (100+ lines changes) - Update CLAUDE.md with 2025-09-23 progress ⚡ **Expected Impact** - Resolve collect_prints null return issue - Eliminate code duplication (4 locations unified) - Foundation for Phase M (PHI unification) and Phase L (BuildOutcome) 🎯 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
43
archive/codex-solutions/README.md
Normal file
43
archive/codex-solutions/README.md
Normal file
@ -0,0 +1,43 @@
|
||||
# Codex Solutions Archive
|
||||
|
||||
## break文問題の解決策コレクション
|
||||
|
||||
### 🤖 Codex: Nested Returns Detection Solution
|
||||
**ファイル**: `codex-nested-returns-solution.patch`
|
||||
**日付**: 2025-09-23
|
||||
**ブランチ**: `codex/investigate-collect_prints-abnormal-termination-czqapj`
|
||||
|
||||
#### 戦略
|
||||
1. **短期修正**: break → return out に変更
|
||||
2. **根本修正**: contains_value_return()でネストしたreturn文を検出
|
||||
3. **型推論改善**: 戻り値型の自動推論
|
||||
|
||||
#### 変更ファイル
|
||||
- `apps/selfhost/vm/boxes/mini_vm_core.nyash`: collect_prints修正
|
||||
- `src/mir/builder/builder_calls.rs`: 型推論システム強化(100行以上)
|
||||
|
||||
#### 特徴
|
||||
- ✅ 根本的なアーキテクチャ修正
|
||||
- ✅ ネストした制御構造への対応
|
||||
- ❌ ビルド失敗(複雑性が原因?)
|
||||
|
||||
### 📝 使用方法
|
||||
```bash
|
||||
# パッチ適用(テスト用)
|
||||
git apply archive/codex-solutions/codex-nested-returns-solution.patch
|
||||
|
||||
# 元に戻す
|
||||
git checkout -- apps/selfhost/vm/boxes/mini_vm_core.nyash src/mir/builder/builder_calls.rs
|
||||
```
|
||||
|
||||
### 🔄 他の解決策との比較
|
||||
- **task先生**: 根本原因分析
|
||||
- **Gemini**: 短期(案A)+ 長期(案B)戦略
|
||||
- **codex**: 実装重視の根本修正
|
||||
- **ChatGPT Pro**: 分析中...
|
||||
|
||||
### 📊 評価
|
||||
- **技術的難易度**: ⭐⭐⭐⭐⭐
|
||||
- **実装リスク**: ⭐⭐⭐⭐
|
||||
- **根本解決度**: ⭐⭐⭐⭐⭐
|
||||
- **Phase 15適合**: ⭐⭐⭐
|
||||
541
archive/codex-solutions/builder_calls-codex-solution.rs
Normal file
541
archive/codex-solutions/builder_calls-codex-solution.rs
Normal file
@ -0,0 +1,541 @@
|
||||
// Extracted call-related builders from builder.rs to keep files lean
|
||||
use super::{Effect, EffectMask, FunctionSignature, MirInstruction, MirType, ValueId};
|
||||
use crate::ast::{ASTNode, LiteralValue, MethodCallExpr};
|
||||
|
||||
fn contains_value_return(nodes: &[ASTNode]) -> bool {
|
||||
fn node_has_value_return(node: &ASTNode) -> bool {
|
||||
match node {
|
||||
ASTNode::Return { value: Some(_), .. } => true,
|
||||
ASTNode::If { then_body, else_body, .. } => {
|
||||
contains_value_return(then_body)
|
||||
|| else_body
|
||||
.as_ref()
|
||||
.map_or(false, |body| contains_value_return(body))
|
||||
}
|
||||
ASTNode::Loop { body, .. } => contains_value_return(body),
|
||||
ASTNode::TryCatch {
|
||||
try_body,
|
||||
catch_clauses,
|
||||
finally_body,
|
||||
..
|
||||
} => {
|
||||
contains_value_return(try_body)
|
||||
|| catch_clauses
|
||||
.iter()
|
||||
.any(|clause| contains_value_return(&clause.body))
|
||||
|| finally_body
|
||||
.as_ref()
|
||||
.map_or(false, |body| contains_value_return(body))
|
||||
}
|
||||
ASTNode::Program { statements, .. } => contains_value_return(statements),
|
||||
ASTNode::ScopeBox { body, .. } => contains_value_return(body),
|
||||
ASTNode::FunctionDeclaration { body, .. } => contains_value_return(body),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
nodes.iter().any(node_has_value_return)
|
||||
}
|
||||
use crate::mir::{slot_registry, TypeOpKind};
|
||||
|
||||
impl super::MirBuilder {
|
||||
/// Try handle math.* function in function-style (sin/cos/abs/min/max).
|
||||
/// Returns Some(result) if handled, otherwise None.
|
||||
fn try_handle_math_function(
|
||||
&mut self,
|
||||
name: &str,
|
||||
raw_args: Vec<ASTNode>,
|
||||
) -> Option<Result<ValueId, String>> {
|
||||
let is_math_func = matches!(name, "sin" | "cos" | "abs" | "min" | "max");
|
||||
if !is_math_func {
|
||||
return None;
|
||||
}
|
||||
// Build numeric args directly for math.* to preserve f64 typing
|
||||
let mut math_args: Vec<ValueId> = Vec::new();
|
||||
for a in raw_args.into_iter() {
|
||||
match a {
|
||||
ASTNode::New { class, arguments, .. } if class == "FloatBox" && arguments.len() == 1 => {
|
||||
match self.build_expression(arguments[0].clone()) { v @ Ok(_) => math_args.push(v.unwrap()), err @ Err(_) => return Some(err), }
|
||||
}
|
||||
ASTNode::New { class, arguments, .. } if class == "IntegerBox" && arguments.len() == 1 => {
|
||||
let iv = match self.build_expression(arguments[0].clone()) { Ok(v) => v, Err(e) => return Some(Err(e)) };
|
||||
let fv = self.value_gen.next();
|
||||
if let Err(e) = self.emit_instruction(MirInstruction::TypeOp { dst: fv, op: TypeOpKind::Cast, value: iv, ty: MirType::Float }) { return Some(Err(e)); }
|
||||
math_args.push(fv);
|
||||
}
|
||||
ASTNode::Literal { value: LiteralValue::Float(_), .. } => {
|
||||
match self.build_expression(a) { v @ Ok(_) => math_args.push(v.unwrap()), err @ Err(_) => return Some(err), }
|
||||
}
|
||||
other => {
|
||||
match self.build_expression(other) { v @ Ok(_) => math_args.push(v.unwrap()), err @ Err(_) => return Some(err), }
|
||||
}
|
||||
}
|
||||
}
|
||||
// new MathBox()
|
||||
let math_recv = self.value_gen.next();
|
||||
if let Err(e) = self.emit_instruction(MirInstruction::NewBox { dst: math_recv, box_type: "MathBox".to_string(), args: vec![] }) { return Some(Err(e)); }
|
||||
self.value_origin_newbox.insert(math_recv, "MathBox".to_string());
|
||||
// birth()
|
||||
let birt_mid = slot_registry::resolve_slot_by_type_name("MathBox", "birth");
|
||||
if let Err(e) = self.emit_box_or_plugin_call(None, math_recv, "birth".to_string(), birt_mid, vec![], EffectMask::READ) { return Some(Err(e)); }
|
||||
// call method
|
||||
let dst = self.value_gen.next();
|
||||
if let Err(e) = self.emit_box_or_plugin_call(Some(dst), math_recv, name.to_string(), None, math_args, EffectMask::READ) { return Some(Err(e)); }
|
||||
Some(Ok(dst))
|
||||
}
|
||||
|
||||
/// Try handle env.* extern methods like env.console.log via FieldAccess(object, field).
|
||||
fn try_handle_env_method(
|
||||
&mut self,
|
||||
object: &ASTNode,
|
||||
method: &str,
|
||||
arguments: &Vec<ASTNode>,
|
||||
) -> Option<Result<ValueId, String>> {
|
||||
let ASTNode::FieldAccess { object: env_obj, field: env_field, .. } = object else { return None; };
|
||||
if let ASTNode::Variable { name: env_name, .. } = env_obj.as_ref() {
|
||||
if env_name != "env" { return None; }
|
||||
// Build arguments once
|
||||
let mut arg_values = Vec::new();
|
||||
for arg in arguments {
|
||||
match self.build_expression(arg.clone()) { Ok(v) => arg_values.push(v), Err(e) => return Some(Err(e)) }
|
||||
}
|
||||
let iface = env_field.as_str();
|
||||
let m = method;
|
||||
let mut extern_call = |iface_name: &str, method_name: &str, effects: EffectMask, returns: bool| -> Result<ValueId, String> {
|
||||
let result_id = self.value_gen.next();
|
||||
self.emit_instruction(MirInstruction::ExternCall { dst: if returns { Some(result_id) } else { None }, iface_name: iface_name.to_string(), method_name: method_name.to_string(), args: arg_values.clone(), effects })?;
|
||||
if returns {
|
||||
Ok(result_id)
|
||||
} else {
|
||||
let void_id = self.value_gen.next();
|
||||
self.emit_instruction(MirInstruction::Const { dst: void_id, value: super::ConstValue::Void })?;
|
||||
Ok(void_id)
|
||||
}
|
||||
};
|
||||
if let Some((iface_name, method_name, effects, returns)) =
|
||||
Self::get_env_method_spec(iface, m)
|
||||
{
|
||||
return Some(extern_call(&iface_name, &method_name, effects, returns));
|
||||
}
|
||||
return None;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Table-like spec for env.* methods. Returns iface_name, method_name, effects, returns.
|
||||
fn get_env_method_spec(
|
||||
iface: &str,
|
||||
method: &str,
|
||||
) -> Option<(String, String, EffectMask, bool)> {
|
||||
// This match is the table. Keep it small and explicit.
|
||||
match (iface, method) {
|
||||
("future", "delay") => Some(("env.future".to_string(), "delay".to_string(), EffectMask::READ.add(Effect::Io), true)),
|
||||
("task", "currentToken") => Some(("env.task".to_string(), "currentToken".to_string(), EffectMask::READ, true)),
|
||||
("task", "cancelCurrent") => Some(("env.task".to_string(), "cancelCurrent".to_string(), EffectMask::IO, false)),
|
||||
("console", "log") => Some(("env.console".to_string(), "log".to_string(), EffectMask::IO, false)),
|
||||
("console", "readLine") => Some(("env.console".to_string(), "readLine".to_string(), EffectMask::IO, true)),
|
||||
("canvas", m) if matches!(m, "fillRect" | "fillText") => Some(("env.canvas".to_string(), method.to_string(), EffectMask::IO, false)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Try direct static call for `me` in static box
|
||||
fn try_handle_me_direct_call(
|
||||
&mut self,
|
||||
method: &str,
|
||||
arguments: &Vec<ASTNode>,
|
||||
) -> Option<Result<ValueId, String>> {
|
||||
let Some(cls_name) = self.current_static_box.clone() else { return None; };
|
||||
// Build args
|
||||
let mut arg_values = Vec::new();
|
||||
for a in arguments {
|
||||
match self.build_expression(a.clone()) { Ok(v) => arg_values.push(v), Err(e) => return Some(Err(e)) }
|
||||
}
|
||||
let result_id = self.value_gen.next();
|
||||
let fun_name = format!("{}.{}{}", cls_name, method, format!("/{}", arg_values.len()));
|
||||
let fun_val = self.value_gen.next();
|
||||
if let Err(e) = self.emit_instruction(MirInstruction::Const { dst: fun_val, value: super::ConstValue::String(fun_name) }) { return Some(Err(e)); }
|
||||
if let Err(e) = self.emit_instruction(MirInstruction::Call { dst: Some(result_id), func: fun_val, args: arg_values, effects: EffectMask::READ.add(Effect::ReadHeap) }) { return Some(Err(e)); }
|
||||
Some(Ok(result_id))
|
||||
}
|
||||
// Build function call: name(args)
|
||||
pub(super) fn build_function_call(
|
||||
&mut self,
|
||||
name: String,
|
||||
args: Vec<ASTNode>,
|
||||
) -> Result<ValueId, String> {
|
||||
// Minimal TypeOp wiring via function-style: isType(value, "Type"), asType(value, "Type")
|
||||
if (name == "isType" || name == "asType") && args.len() == 2 {
|
||||
if let Some(type_name) = Self::extract_string_literal(&args[1]) {
|
||||
let val = self.build_expression(args[0].clone())?;
|
||||
let ty = Self::parse_type_name_to_mir(&type_name);
|
||||
let dst = self.value_gen.next();
|
||||
let op = if name == "isType" {
|
||||
TypeOpKind::Check
|
||||
} else {
|
||||
TypeOpKind::Cast
|
||||
};
|
||||
self.emit_instruction(MirInstruction::TypeOp {
|
||||
dst,
|
||||
op,
|
||||
value: val,
|
||||
ty,
|
||||
})?;
|
||||
return Ok(dst);
|
||||
}
|
||||
}
|
||||
// Keep original args for special handling (math.*)
|
||||
let raw_args = args.clone();
|
||||
|
||||
if let Some(res) = self.try_handle_math_function(&name, raw_args) { return res; }
|
||||
|
||||
let dst = self.value_gen.next();
|
||||
|
||||
// Default: call via fully-qualified function name string
|
||||
let mut arg_values = Vec::new();
|
||||
for a in args {
|
||||
arg_values.push(self.build_expression(a)?);
|
||||
}
|
||||
let fun_val = self.value_gen.next();
|
||||
self.emit_instruction(MirInstruction::Const {
|
||||
dst: fun_val,
|
||||
value: super::ConstValue::String(name),
|
||||
})?;
|
||||
self.emit_instruction(MirInstruction::Call {
|
||||
dst: Some(dst),
|
||||
func: fun_val,
|
||||
args: arg_values,
|
||||
effects: EffectMask::READ.add(Effect::ReadHeap),
|
||||
})?;
|
||||
Ok(dst)
|
||||
}
|
||||
|
||||
// Build method call: object.method(arguments)
|
||||
pub(super) fn build_method_call(
|
||||
&mut self,
|
||||
object: ASTNode,
|
||||
method: String,
|
||||
arguments: Vec<ASTNode>,
|
||||
) -> Result<ValueId, String> {
|
||||
// Minimal TypeOp wiring via method-style syntax: value.is("Type") / value.as("Type")
|
||||
if (method == "is" || method == "as") && arguments.len() == 1 {
|
||||
if let Some(type_name) = Self::extract_string_literal(&arguments[0]) {
|
||||
let object_value = self.build_expression(object.clone())?;
|
||||
let mir_ty = Self::parse_type_name_to_mir(&type_name);
|
||||
let dst = self.value_gen.next();
|
||||
let op = if method == "is" {
|
||||
TypeOpKind::Check
|
||||
} else {
|
||||
TypeOpKind::Cast
|
||||
};
|
||||
self.emit_instruction(MirInstruction::TypeOp {
|
||||
dst,
|
||||
op,
|
||||
value: object_value,
|
||||
ty: mir_ty,
|
||||
})?;
|
||||
return Ok(dst);
|
||||
}
|
||||
}
|
||||
if let Some(res) = self.try_handle_env_method(&object, &method, &arguments) { return res; }
|
||||
// If object is `me` within a static box, lower to direct Call: BoxName.method/N
|
||||
if let ASTNode::Me { .. } = object {
|
||||
if let Some(res) = self.try_handle_me_direct_call(&method, &arguments) { return res; }
|
||||
}
|
||||
// Build the object expression (wrapper allows simple access if needed in future)
|
||||
let _mc = MethodCallExpr { object: Box::new(object.clone()), method: method.clone(), arguments: arguments.clone(), span: crate::ast::Span::unknown() };
|
||||
let object_value = self.build_expression(object.clone())?;
|
||||
// Secondary interception for is/as
|
||||
if (method == "is" || method == "as") && arguments.len() == 1 {
|
||||
if let Some(type_name) = Self::extract_string_literal(&arguments[0]) {
|
||||
let mir_ty = Self::parse_type_name_to_mir(&type_name);
|
||||
let dst = self.value_gen.next();
|
||||
let op = if method == "is" {
|
||||
TypeOpKind::Check
|
||||
} else {
|
||||
TypeOpKind::Cast
|
||||
};
|
||||
self.emit_instruction(MirInstruction::TypeOp {
|
||||
dst,
|
||||
op,
|
||||
value: object_value,
|
||||
ty: mir_ty,
|
||||
})?;
|
||||
return Ok(dst);
|
||||
}
|
||||
}
|
||||
// Fallback: generic plugin invoke
|
||||
let mut arg_values: Vec<ValueId> = Vec::new();
|
||||
for a in &arguments {
|
||||
arg_values.push(self.build_expression(a.clone())?);
|
||||
}
|
||||
let result_id = self.value_gen.next();
|
||||
self.emit_box_or_plugin_call(
|
||||
Some(result_id),
|
||||
object_value,
|
||||
method,
|
||||
None,
|
||||
arg_values,
|
||||
EffectMask::READ.add(Effect::ReadHeap),
|
||||
)?;
|
||||
Ok(result_id)
|
||||
}
|
||||
|
||||
// Map a user-facing type name to MIR type
|
||||
pub(super) fn parse_type_name_to_mir(name: &str) -> super::MirType {
|
||||
match name {
|
||||
// Primitive families
|
||||
"Integer" | "Int" | "I64" | "IntegerBox" | "IntBox" => super::MirType::Integer,
|
||||
"Float" | "F64" | "FloatBox" => super::MirType::Float,
|
||||
"Bool" | "Boolean" | "BoolBox" => super::MirType::Bool,
|
||||
"String" | "StringBox" => super::MirType::String,
|
||||
"Void" | "Unit" => super::MirType::Void,
|
||||
// Fallback: treat as user box type
|
||||
other => super::MirType::Box(other.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
// Extract string literal from AST node if possible
|
||||
pub(super) fn extract_string_literal(node: &ASTNode) -> Option<String> {
|
||||
let mut cur = node;
|
||||
loop {
|
||||
match cur {
|
||||
ASTNode::Literal {
|
||||
value: LiteralValue::String(s),
|
||||
..
|
||||
} => return Some(s.clone()),
|
||||
ASTNode::New {
|
||||
class, arguments, ..
|
||||
} if class == "StringBox" && arguments.len() == 1 => {
|
||||
cur = &arguments[0];
|
||||
continue;
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Build from expression: from Parent.method(arguments)
|
||||
pub(super) fn build_from_expression(
|
||||
&mut self,
|
||||
parent: String,
|
||||
method: String,
|
||||
arguments: Vec<ASTNode>,
|
||||
) -> Result<ValueId, String> {
|
||||
let mut arg_values = Vec::new();
|
||||
for arg in arguments {
|
||||
arg_values.push(self.build_expression(arg)?);
|
||||
}
|
||||
let parent_value = self.value_gen.next();
|
||||
self.emit_instruction(MirInstruction::Const {
|
||||
dst: parent_value,
|
||||
value: super::ConstValue::String(parent),
|
||||
})?;
|
||||
let result_id = self.value_gen.next();
|
||||
self.emit_box_or_plugin_call(
|
||||
Some(result_id),
|
||||
parent_value,
|
||||
method,
|
||||
None,
|
||||
arg_values,
|
||||
EffectMask::READ.add(Effect::ReadHeap),
|
||||
)?;
|
||||
Ok(result_id)
|
||||
}
|
||||
|
||||
// Lower a box method into a standalone MIR function (with `me` parameter)
|
||||
pub(super) fn lower_method_as_function(
|
||||
&mut self,
|
||||
func_name: String,
|
||||
box_name: String,
|
||||
params: Vec<String>,
|
||||
body: Vec<ASTNode>,
|
||||
) -> Result<(), String> {
|
||||
let mut param_types = Vec::new();
|
||||
param_types.push(MirType::Box(box_name.clone()));
|
||||
for _ in ¶ms {
|
||||
param_types.push(MirType::Unknown);
|
||||
}
|
||||
let returns_value = contains_value_return(&body);
|
||||
let ret_ty = if returns_value {
|
||||
MirType::Unknown
|
||||
} else {
|
||||
MirType::Void
|
||||
};
|
||||
let signature = FunctionSignature {
|
||||
name: func_name,
|
||||
params: param_types,
|
||||
return_type: ret_ty,
|
||||
effects: EffectMask::READ.add(Effect::ReadHeap),
|
||||
};
|
||||
let entry = self.block_gen.next();
|
||||
let function = super::MirFunction::new(signature, entry);
|
||||
let saved_function = self.current_function.take();
|
||||
let saved_block = self.current_block.take();
|
||||
let saved_var_map = std::mem::take(&mut self.variable_map);
|
||||
let saved_value_gen = self.value_gen.clone();
|
||||
self.value_gen.reset();
|
||||
self.current_function = Some(function);
|
||||
self.current_block = Some(entry);
|
||||
self.ensure_block_exists(entry)?;
|
||||
if let Some(ref mut f) = self.current_function {
|
||||
let me_id = self.value_gen.next();
|
||||
f.params.push(me_id);
|
||||
self.variable_map.insert("me".to_string(), me_id);
|
||||
self.value_origin_newbox.insert(me_id, box_name.clone());
|
||||
for p in ¶ms {
|
||||
let pid = self.value_gen.next();
|
||||
f.params.push(pid);
|
||||
self.variable_map.insert(p.clone(), pid);
|
||||
}
|
||||
}
|
||||
let program_ast = ASTNode::Program {
|
||||
statements: body,
|
||||
span: crate::ast::Span::unknown(),
|
||||
};
|
||||
let _last = self.build_expression(program_ast)?;
|
||||
if !returns_value && !self.is_current_block_terminated() {
|
||||
let void_val = self.value_gen.next();
|
||||
self.emit_instruction(MirInstruction::Const {
|
||||
dst: void_val,
|
||||
value: super::ConstValue::Void,
|
||||
})?;
|
||||
self.emit_instruction(MirInstruction::Return {
|
||||
value: Some(void_val),
|
||||
})?;
|
||||
}
|
||||
if let Some(ref mut f) = self.current_function {
|
||||
if returns_value
|
||||
&& matches!(f.signature.return_type, MirType::Void | MirType::Unknown)
|
||||
{
|
||||
let mut inferred: Option<MirType> = None;
|
||||
'search: for (_bid, bb) in f.blocks.iter() {
|
||||
for inst in bb.instructions.iter() {
|
||||
if let MirInstruction::Return { value: Some(v) } = inst {
|
||||
if let Some(mt) = self.value_types.get(v).cloned() {
|
||||
inferred = Some(mt);
|
||||
break 'search;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(MirInstruction::Return { value: Some(v) }) = &bb.terminator {
|
||||
if let Some(mt) = self.value_types.get(v).cloned() {
|
||||
inferred = Some(mt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(mt) = inferred {
|
||||
f.signature.return_type = mt;
|
||||
}
|
||||
}
|
||||
}
|
||||
let finalized_function = self.current_function.take().unwrap();
|
||||
if let Some(ref mut module) = self.current_module {
|
||||
module.add_function(finalized_function);
|
||||
}
|
||||
self.current_function = saved_function;
|
||||
self.current_block = saved_block;
|
||||
self.variable_map = saved_var_map;
|
||||
self.value_gen = saved_value_gen;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Lower a static method body into a standalone MIR function (no `me` parameter)
|
||||
pub(super) fn lower_static_method_as_function(
|
||||
&mut self,
|
||||
func_name: String,
|
||||
params: Vec<String>,
|
||||
body: Vec<ASTNode>,
|
||||
) -> Result<(), String> {
|
||||
let mut param_types = Vec::new();
|
||||
for _ in ¶ms {
|
||||
param_types.push(MirType::Unknown);
|
||||
}
|
||||
let returns_value = contains_value_return(&body);
|
||||
let ret_ty = if returns_value {
|
||||
MirType::Unknown
|
||||
} else {
|
||||
MirType::Void
|
||||
};
|
||||
let signature = FunctionSignature {
|
||||
name: func_name,
|
||||
params: param_types,
|
||||
return_type: ret_ty,
|
||||
effects: EffectMask::READ.add(Effect::ReadHeap),
|
||||
};
|
||||
let entry = self.block_gen.next();
|
||||
let function = super::MirFunction::new(signature, entry);
|
||||
let saved_function = self.current_function.take();
|
||||
let saved_block = self.current_block.take();
|
||||
let saved_var_map = std::mem::take(&mut self.variable_map);
|
||||
let saved_value_gen = self.value_gen.clone();
|
||||
self.value_gen.reset();
|
||||
self.current_function = Some(function);
|
||||
self.current_block = Some(entry);
|
||||
self.ensure_block_exists(entry)?;
|
||||
if let Some(ref mut f) = self.current_function {
|
||||
for p in ¶ms {
|
||||
let pid = self.value_gen.next();
|
||||
f.params.push(pid);
|
||||
self.variable_map.insert(p.clone(), pid);
|
||||
}
|
||||
}
|
||||
let program_ast = ASTNode::Program {
|
||||
statements: body,
|
||||
span: crate::ast::Span::unknown(),
|
||||
};
|
||||
let _last = self.build_expression(program_ast)?;
|
||||
if !returns_value {
|
||||
if let Some(ref mut f) = self.current_function {
|
||||
if let Some(block) = f.get_block(self.current_block.unwrap()) {
|
||||
if !block.is_terminated() {
|
||||
let void_val = self.value_gen.next();
|
||||
self.emit_instruction(MirInstruction::Const {
|
||||
dst: void_val,
|
||||
value: super::ConstValue::Void,
|
||||
})?;
|
||||
self.emit_instruction(MirInstruction::Return {
|
||||
value: Some(void_val),
|
||||
})?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(ref mut f) = self.current_function {
|
||||
if returns_value
|
||||
&& matches!(f.signature.return_type, MirType::Void | MirType::Unknown)
|
||||
{
|
||||
let mut inferred: Option<MirType> = None;
|
||||
'search: for (_bid, bb) in f.blocks.iter() {
|
||||
for inst in bb.instructions.iter() {
|
||||
if let MirInstruction::Return { value: Some(v) } = inst {
|
||||
if let Some(mt) = self.value_types.get(v).cloned() {
|
||||
inferred = Some(mt);
|
||||
break 'search;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(MirInstruction::Return { value: Some(v) }) = &bb.terminator {
|
||||
if let Some(mt) = self.value_types.get(v).cloned() {
|
||||
inferred = Some(mt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(mt) = inferred {
|
||||
f.signature.return_type = mt;
|
||||
}
|
||||
}
|
||||
}
|
||||
let finalized = self.current_function.take().unwrap();
|
||||
if let Some(ref mut module) = self.current_module {
|
||||
module.add_function(finalized);
|
||||
}
|
||||
self.current_function = saved_function;
|
||||
self.current_block = saved_block;
|
||||
self.variable_map = saved_var_map;
|
||||
self.value_gen = saved_value_gen;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
215
archive/codex-solutions/codex-nested-returns-solution.patch
Normal file
215
archive/codex-solutions/codex-nested-returns-solution.patch
Normal file
@ -0,0 +1,215 @@
|
||||
commit 8b0abf84292e85a3c09eba92d8bd0da79770ec8f
|
||||
Author: moe-charm <moecharm.dev@gmail.com>
|
||||
Date: Tue Sep 23 06:41:07 2025 +0900
|
||||
|
||||
mir: detect nested returns in static lowering
|
||||
|
||||
diff --git a/apps/selfhost/vm/boxes/mini_vm_core.nyash b/apps/selfhost/vm/boxes/mini_vm_core.nyash
|
||||
index bac66c4..184cd2f 100644
|
||||
--- a/apps/selfhost/vm/boxes/mini_vm_core.nyash
|
||||
+++ b/apps/selfhost/vm/boxes/mini_vm_core.nyash
|
||||
@@ -541,10 +541,20 @@ static box MiniVm {
|
||||
local k_print = "\"kind\":\"Print\""
|
||||
loop (true) {
|
||||
guard = guard + 1
|
||||
- if guard > 200 { if trace == 1 { print("[collect][guard_break] guard="+guard) } break }
|
||||
+ if guard > 200 {
|
||||
+ if trace == 1 { print("[collect][guard_break] guard="+guard) }
|
||||
+ if trace == 1 { print("[collect][loop_exit] guard="+guard+" out.size="+out.size()) }
|
||||
+ if trace == 1 { print("[collect][return] out.size="+out.size()) }
|
||||
+ return out
|
||||
+ }
|
||||
local p = index_of_from(json, k_print, pos)
|
||||
if trace == 1 { print("[collect][loop] pos="+pos+" p="+p+" guard="+guard) }
|
||||
- if p < 0 { if trace == 1 { print("[collect][p_break] p="+p) } break }
|
||||
+ if p < 0 {
|
||||
+ if trace == 1 { print("[collect][p_break] p="+p) }
|
||||
+ if trace == 1 { print("[collect][loop_exit] guard="+guard+" out.size="+out.size()) }
|
||||
+ if trace == 1 { print("[collect][return] out.size="+out.size()) }
|
||||
+ return out
|
||||
+ }
|
||||
// bound current Print slice to [this, next)
|
||||
local obj_start = p
|
||||
local next_p = index_of_from(json, k_print, p + k_print.length())
|
||||
diff --git a/src/mir/builder/builder_calls.rs b/src/mir/builder/builder_calls.rs
|
||||
index 6b5bf7e..3432a75 100644
|
||||
--- a/src/mir/builder/builder_calls.rs
|
||||
+++ b/src/mir/builder/builder_calls.rs
|
||||
@@ -1,6 +1,41 @@
|
||||
// Extracted call-related builders from builder.rs to keep files lean
|
||||
use super::{Effect, EffectMask, FunctionSignature, MirInstruction, MirType, ValueId};
|
||||
use crate::ast::{ASTNode, LiteralValue, MethodCallExpr};
|
||||
+
|
||||
+fn contains_value_return(nodes: &[ASTNode]) -> bool {
|
||||
+ fn node_has_value_return(node: &ASTNode) -> bool {
|
||||
+ match node {
|
||||
+ ASTNode::Return { value: Some(_), .. } => true,
|
||||
+ ASTNode::If { then_body, else_body, .. } => {
|
||||
+ contains_value_return(then_body)
|
||||
+ || else_body
|
||||
+ .as_ref()
|
||||
+ .map_or(false, |body| contains_value_return(body))
|
||||
+ }
|
||||
+ ASTNode::Loop { body, .. } => contains_value_return(body),
|
||||
+ ASTNode::TryCatch {
|
||||
+ try_body,
|
||||
+ catch_clauses,
|
||||
+ finally_body,
|
||||
+ ..
|
||||
+ } => {
|
||||
+ contains_value_return(try_body)
|
||||
+ || catch_clauses
|
||||
+ .iter()
|
||||
+ .any(|clause| contains_value_return(&clause.body))
|
||||
+ || finally_body
|
||||
+ .as_ref()
|
||||
+ .map_or(false, |body| contains_value_return(body))
|
||||
+ }
|
||||
+ ASTNode::Program { statements, .. } => contains_value_return(statements),
|
||||
+ ASTNode::ScopeBox { body, .. } => contains_value_return(body),
|
||||
+ ASTNode::FunctionDeclaration { body, .. } => contains_value_return(body),
|
||||
+ _ => false,
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
+ nodes.iter().any(node_has_value_return)
|
||||
+}
|
||||
use crate::mir::{slot_registry, TypeOpKind};
|
||||
|
||||
impl super::MirBuilder {
|
||||
@@ -321,13 +356,7 @@ impl super::MirBuilder {
|
||||
for _ in ¶ms {
|
||||
param_types.push(MirType::Unknown);
|
||||
}
|
||||
- let mut returns_value = false;
|
||||
- for st in &body {
|
||||
- if let ASTNode::Return { value: Some(_), .. } = st {
|
||||
- returns_value = true;
|
||||
- break;
|
||||
- }
|
||||
- }
|
||||
+ let returns_value = contains_value_return(&body);
|
||||
let ret_ty = if returns_value {
|
||||
MirType::Unknown
|
||||
} else {
|
||||
@@ -365,17 +394,39 @@ impl super::MirBuilder {
|
||||
span: crate::ast::Span::unknown(),
|
||||
};
|
||||
let _last = self.build_expression(program_ast)?;
|
||||
+ if !returns_value && !self.is_current_block_terminated() {
|
||||
+ let void_val = self.value_gen.next();
|
||||
+ self.emit_instruction(MirInstruction::Const {
|
||||
+ dst: void_val,
|
||||
+ value: super::ConstValue::Void,
|
||||
+ })?;
|
||||
+ self.emit_instruction(MirInstruction::Return {
|
||||
+ value: Some(void_val),
|
||||
+ })?;
|
||||
+ }
|
||||
if let Some(ref mut f) = self.current_function {
|
||||
- if let Some(block) = f.get_block(self.current_block.unwrap()) {
|
||||
- if !block.is_terminated() {
|
||||
- let void_val = self.value_gen.next();
|
||||
- self.emit_instruction(MirInstruction::Const {
|
||||
- dst: void_val,
|
||||
- value: super::ConstValue::Void,
|
||||
- })?;
|
||||
- self.emit_instruction(MirInstruction::Return {
|
||||
- value: Some(void_val),
|
||||
- })?;
|
||||
+ if returns_value
|
||||
+ && matches!(f.signature.return_type, MirType::Void | MirType::Unknown)
|
||||
+ {
|
||||
+ let mut inferred: Option<MirType> = None;
|
||||
+ 'search: for (_bid, bb) in f.blocks.iter() {
|
||||
+ for inst in bb.instructions.iter() {
|
||||
+ if let MirInstruction::Return { value: Some(v) } = inst {
|
||||
+ if let Some(mt) = self.value_types.get(v).cloned() {
|
||||
+ inferred = Some(mt);
|
||||
+ break 'search;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ if let Some(MirInstruction::Return { value: Some(v) }) = &bb.terminator {
|
||||
+ if let Some(mt) = self.value_types.get(v).cloned() {
|
||||
+ inferred = Some(mt);
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ if let Some(mt) = inferred {
|
||||
+ f.signature.return_type = mt;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -401,13 +452,7 @@ impl super::MirBuilder {
|
||||
for _ in ¶ms {
|
||||
param_types.push(MirType::Unknown);
|
||||
}
|
||||
- let mut returns_value = false;
|
||||
- for st in &body {
|
||||
- if let ASTNode::Return { value: Some(_), .. } = st {
|
||||
- returns_value = true;
|
||||
- break;
|
||||
- }
|
||||
- }
|
||||
+ let returns_value = contains_value_return(&body);
|
||||
let ret_ty = if returns_value {
|
||||
MirType::Unknown
|
||||
} else {
|
||||
@@ -441,17 +486,45 @@ impl super::MirBuilder {
|
||||
span: crate::ast::Span::unknown(),
|
||||
};
|
||||
let _last = self.build_expression(program_ast)?;
|
||||
+ if !returns_value {
|
||||
+ if let Some(ref mut f) = self.current_function {
|
||||
+ if let Some(block) = f.get_block(self.current_block.unwrap()) {
|
||||
+ if !block.is_terminated() {
|
||||
+ let void_val = self.value_gen.next();
|
||||
+ self.emit_instruction(MirInstruction::Const {
|
||||
+ dst: void_val,
|
||||
+ value: super::ConstValue::Void,
|
||||
+ })?;
|
||||
+ self.emit_instruction(MirInstruction::Return {
|
||||
+ value: Some(void_val),
|
||||
+ })?;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
if let Some(ref mut f) = self.current_function {
|
||||
- if let Some(block) = f.get_block(self.current_block.unwrap()) {
|
||||
- if !block.is_terminated() {
|
||||
- let void_val = self.value_gen.next();
|
||||
- self.emit_instruction(MirInstruction::Const {
|
||||
- dst: void_val,
|
||||
- value: super::ConstValue::Void,
|
||||
- })?;
|
||||
- self.emit_instruction(MirInstruction::Return {
|
||||
- value: Some(void_val),
|
||||
- })?;
|
||||
+ if returns_value
|
||||
+ && matches!(f.signature.return_type, MirType::Void | MirType::Unknown)
|
||||
+ {
|
||||
+ let mut inferred: Option<MirType> = None;
|
||||
+ 'search: for (_bid, bb) in f.blocks.iter() {
|
||||
+ for inst in bb.instructions.iter() {
|
||||
+ if let MirInstruction::Return { value: Some(v) } = inst {
|
||||
+ if let Some(mt) = self.value_types.get(v).cloned() {
|
||||
+ inferred = Some(mt);
|
||||
+ break 'search;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ if let Some(MirInstruction::Return { value: Some(v) }) = &bb.terminator {
|
||||
+ if let Some(mt) = self.value_types.get(v).cloned() {
|
||||
+ inferred = Some(mt);
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+ if let Some(mt) = inferred {
|
||||
+ f.signature.return_type = mt;
|
||||
}
|
||||
}
|
||||
}
|
||||
774
archive/codex-solutions/mini_vm_core-codex-solution.nyash
Normal file
774
archive/codex-solutions/mini_vm_core-codex-solution.nyash
Normal file
@ -0,0 +1,774 @@
|
||||
using selfhost.vm.boxes.json_cur as MiniJson
|
||||
using selfhost.common.mini_vm_scan as MiniVmScan
|
||||
using selfhost.common.mini_vm_binop as MiniVmBinOp
|
||||
using selfhost.common.mini_vm_compare as MiniVmCompare
|
||||
using selfhost.vm.boxes.mini_vm_prints as MiniVmPrints
|
||||
|
||||
static box MiniVm {
|
||||
_is_digit(ch) {
|
||||
if ch == "0" { return 1 }
|
||||
if ch == "1" { return 1 }
|
||||
if ch == "2" { return 1 }
|
||||
if ch == "3" { return 1 }
|
||||
if ch == "4" { return 1 }
|
||||
if ch == "5" { return 1 }
|
||||
if ch == "6" { return 1 }
|
||||
if ch == "7" { return 1 }
|
||||
if ch == "8" { return 1 }
|
||||
if ch == "9" { return 1 }
|
||||
return 0
|
||||
}
|
||||
_str_to_int(s) { return new MiniVmScan()._str_to_int(s) }
|
||||
_int_to_str(n) { return new MiniVmScan()._int_to_str(n) }
|
||||
read_digits(json, pos) { return new MiniJson().read_digits_from(json, pos) }
|
||||
// Read a JSON string starting at position pos (at opening quote); returns the decoded string
|
||||
read_json_string(json, pos) { return new MiniJson().read_quoted_from(json, pos) }
|
||||
// helper: find needle from position pos
|
||||
index_of_from(hay, needle, pos) { return new MiniVmScan().index_of_from(hay, needle, pos) }
|
||||
// helper: next non-whitespace character index from pos
|
||||
next_non_ws(json, pos) { return new MiniJson().next_non_ws(json, pos) }
|
||||
// ——— Helpers (as box methods) ———
|
||||
|
||||
// Minimal: Print(BinaryOp) with operator "+"; supports string+string and int+int
|
||||
// try_print_binop_at moved to MiniVmBinOp
|
||||
|
||||
// Greedy fallback: detect BinaryOp int+int by pattern regardless of field order nuances
|
||||
// try_print_binop_int_greedy moved to MiniVmBinOp
|
||||
|
||||
// Fallback: within the current Print's expression BinaryOp object, scan for two numeric values and sum
|
||||
// try_print_binop_sum_any moved to MiniVmBinOp
|
||||
|
||||
// Deterministic: within the first Print.expression BinaryOp('+'),
|
||||
// find exactly two numeric values from successive '"value":' fields and sum.
|
||||
// Stops after collecting two ints; bounded strictly by the expression object.
|
||||
// try_print_binop_sum_expr_values moved to MiniVmBinOp
|
||||
|
||||
// Simpler deterministic fallback: after the first BinaryOp '+',
|
||||
// scan forward for two successive 'value' fields and sum their integer digits.
|
||||
// This avoids brace matching and remains bounded by two finds.
|
||||
// try_print_binop_sum_after_bop moved to MiniVmBinOp
|
||||
|
||||
// Direct typed BinaryOp(int+int) matcher using explicit left/right literal paths
|
||||
try_print_binop_typed_direct(json) {
|
||||
local k_left = "\"left\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local k_right = "\"right\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local lp = json.indexOf(k_left)
|
||||
if lp < 0 { return -1 }
|
||||
local ld = read_digits(json, lp + k_left.length())
|
||||
if ld == "" { return -1 }
|
||||
local rp = index_of_from(json, k_right, lp + k_left.length())
|
||||
if rp < 0 { return -1 }
|
||||
local rd = read_digits(json, rp + k_right.length())
|
||||
if rd == "" { return -1 }
|
||||
print(_int_to_str(_str_to_int(ld) + _str_to_int(rd)))
|
||||
return rp + k_right.length()
|
||||
}
|
||||
|
||||
// Tokenized typed extractor: search left/right blocks then type/value pairs
|
||||
try_print_binop_typed_tokens(json) {
|
||||
local k_bo = "\"kind\":\"BinaryOp\""
|
||||
local bpos = json.indexOf(k_bo)
|
||||
if bpos < 0 { return -1 }
|
||||
local lp = index_of_from(json, "\"left\":", bpos)
|
||||
if lp < 0 { return -1 }
|
||||
local kt = "\"type\":\"int\""
|
||||
local kv = "\"value\":"
|
||||
local tp1 = index_of_from(json, kt, lp)
|
||||
if tp1 < 0 { return -1 }
|
||||
local vp1 = index_of_from(json, kv, tp1)
|
||||
if vp1 < 0 { return -1 }
|
||||
local ld = read_digits(json, vp1 + kv.length())
|
||||
if ld == "" { return -1 }
|
||||
local rp = index_of_from(json, "\"right\":", lp)
|
||||
if rp < 0 { return -1 }
|
||||
local tp2 = index_of_from(json, kt, rp)
|
||||
if tp2 < 0 { return -1 }
|
||||
local vp2 = index_of_from(json, kv, tp2)
|
||||
if vp2 < 0 { return -1 }
|
||||
local rd = read_digits(json, vp2 + kv.length())
|
||||
if rd == "" { return -1 }
|
||||
print(_int_to_str(_str_to_int(ld) + _str_to_int(rd)))
|
||||
return rp
|
||||
}
|
||||
|
||||
// Fast value-pair extractor: find left/right then first value digits after each
|
||||
try_print_binop_value_pairs(json) {
|
||||
local k_bo = "\"kind\":\"BinaryOp\""
|
||||
local bpos = json.indexOf(k_bo)
|
||||
if bpos < 0 { return -1 }
|
||||
local kl = "\"left\":"
|
||||
local kv = "\"value\":"
|
||||
local lp = index_of_from(json, kl, bpos)
|
||||
if lp < 0 { return -1 }
|
||||
local v1 = index_of_from(json, kv, lp)
|
||||
if v1 < 0 { return -1 }
|
||||
local ld = read_digits(json, v1 + kv.length())
|
||||
if ld == "" { return -1 }
|
||||
local rp = index_of_from(json, "\"right\":", lp)
|
||||
if rp < 0 { return -1 }
|
||||
local v2 = index_of_from(json, kv, rp)
|
||||
if v2 < 0 { return -1 }
|
||||
local rd = read_digits(json, v2 + kv.length())
|
||||
if rd == "" { return -1 }
|
||||
print(_int_to_str(_str_to_int(ld) + _str_to_int(rd)))
|
||||
return v2 + kv.length()
|
||||
}
|
||||
|
||||
// Minimal: Print(Compare) for integers. Prints 1/0 for true/false.
|
||||
// try_print_compare_at moved to MiniVmCompare
|
||||
// Extract first Print literal from JSON v0 Program and return its string representation
|
||||
parse_first_print_literal(json) {
|
||||
// Find a Print statement
|
||||
local k_print = "\"kind\":\"Print\""
|
||||
local p = json.indexOf(k_print)
|
||||
if p < 0 { return null }
|
||||
// Find value type in the expression following Print
|
||||
local k_type = "\"type\":\""
|
||||
local tpos = json.indexOf(k_type)
|
||||
if tpos < 0 { return null }
|
||||
tpos = tpos + k_type.length()
|
||||
// Read type name until next quote
|
||||
local t_end = index_of_from(json, "\"", tpos)
|
||||
if t_end < 0 { return null }
|
||||
local ty = json.substring(tpos, t_end)
|
||||
// Find value field
|
||||
local k_val = "\"value\":"
|
||||
local vpos = index_of_from(json, k_val, t_end)
|
||||
if vpos < 0 { return null }
|
||||
vpos = vpos + k_val.length()
|
||||
if ty == "int" || ty == "i64" || ty == "integer" {
|
||||
// read digits via MiniJson
|
||||
local digits = new MiniJson().read_digits_from(json, vpos)
|
||||
return digits
|
||||
}
|
||||
if ty == "string" {
|
||||
// read quoted via MiniJson
|
||||
local i = index_of_from(json, "\"", vpos)
|
||||
if i < 0 { return null }
|
||||
return new MiniJson().read_quoted_from(json, i)
|
||||
}
|
||||
// Other types not supported yet
|
||||
return null
|
||||
}
|
||||
// helper: find balanced bracket range [ ... ] starting at idx (points to '[')
|
||||
find_balanced_array_end(json, idx) { return new MiniVmScan().find_balanced_array_end(json, idx) }
|
||||
// helper: find balanced object range { ... } starting at idx (points to '{')
|
||||
find_balanced_object_end(json, idx) { return new MiniVmScan().find_balanced_object_end(json, idx) }
|
||||
// Print all Print-Literal values within [start,end] (inclusive slice indices)
|
||||
print_prints_in_slice(json, start, end) { return new MiniVmPrints().print_prints_in_slice(json, start, end) }
|
||||
// Process top-level If with literal condition; print branch prints. Returns printed count.
|
||||
process_if_once(json) { return new MiniVmPrints().process_if_once(json) }
|
||||
print_all_print_literals(json) { return new MiniVmPrints().print_all_print_literals(json) }
|
||||
parse_first_int(json) {
|
||||
local key = "\"value\":{\"type\":\"int\",\"value\":"
|
||||
local idx = json.lastIndexOf(key)
|
||||
if idx < 0 { return "0" }
|
||||
local start = idx + key.length()
|
||||
return read_digits(json, start)
|
||||
}
|
||||
// Fallback: find first BinaryOp and return sum of two numeric values as string; empty if not found
|
||||
parse_first_binop_sum(json) {
|
||||
local k_bo = "\"kind\":\"BinaryOp\""
|
||||
local bpos = json.indexOf(k_bo)
|
||||
if bpos < 0 { return "" }
|
||||
// typed pattern inside left/right.literal.value: {"type":"int","value":<digits>}
|
||||
local k_typed = "\"type\":\"int\",\"value\":"
|
||||
// first number
|
||||
local p1 = index_of_from(json, k_typed, bpos)
|
||||
if p1 < 0 { return "" }
|
||||
local d1 = read_digits(json, p1 + k_typed.length())
|
||||
if d1 == "" { return "" }
|
||||
// second number
|
||||
local p2 = index_of_from(json, k_typed, p1 + k_typed.length())
|
||||
if p2 < 0 { return "" }
|
||||
local d2 = read_digits(json, p2 + k_typed.length())
|
||||
if d2 == "" { return "" }
|
||||
return _int_to_str(_str_to_int(d1) + _str_to_int(d2))
|
||||
}
|
||||
// Linear pass: sum all numbers outside of quotes (fast, finite)
|
||||
sum_numbers_no_quotes(json) { return new MiniVmScan().sum_numbers_no_quotes(json) }
|
||||
// Naive: sum all digit runs anywhere (for simple BinaryOp JSON)
|
||||
sum_all_digits_naive(json) { return new MiniVmScan().sum_all_digits_naive(json) }
|
||||
// Sum first two integers outside quotes; returns string or empty if not found
|
||||
sum_first_two_numbers(json) { return new MiniVmScan().sum_first_two_numbers(json) }
|
||||
|
||||
// Sum two integers near a BinaryOp '+' token; bounded window to keep steps low
|
||||
sum_two_numbers_near_plus(json) {
|
||||
local k_plus = "\"operator\":\"+\""
|
||||
local op = json.indexOf(k_plus)
|
||||
if op < 0 { return "" }
|
||||
local n = json.length()
|
||||
local start = op - 120
|
||||
if start < 0 { start = 0 }
|
||||
local limit = op + 240
|
||||
if limit > n { limit = n }
|
||||
local i = start
|
||||
local found = 0
|
||||
local a = 0
|
||||
loop (i < limit) {
|
||||
local ch = json.substring(i, i+1)
|
||||
if ch == "\"" {
|
||||
// skip to next quote within window
|
||||
local j = index_of_from(json, "\"", i+1)
|
||||
if j < 0 || j > limit { break }
|
||||
i = j + 1
|
||||
continue
|
||||
}
|
||||
local d = read_digits(json, i)
|
||||
if d {
|
||||
if found == 0 {
|
||||
a = _str_to_int(d)
|
||||
found = 1
|
||||
} else {
|
||||
local b = _str_to_int(d)
|
||||
return _int_to_str(a + b)
|
||||
}
|
||||
i = i + d.length()
|
||||
continue
|
||||
}
|
||||
i = i + 1
|
||||
}
|
||||
return ""
|
||||
}
|
||||
// Fallback: sum all bare numbers (not inside quotes) in the JSON; return string or empty if none
|
||||
sum_all_numbers(json) {
|
||||
local cur = new MiniJson()
|
||||
local i = 0
|
||||
local n = json.length()
|
||||
local sum = 0
|
||||
loop (i < n) {
|
||||
local ch = json.substring(i, i+1)
|
||||
if ch == "\"" {
|
||||
// skip quoted string
|
||||
local s = cur.read_quoted_from(json, i)
|
||||
i = i + s.length() + 2
|
||||
continue
|
||||
}
|
||||
// try digits
|
||||
local d = cur.read_digits_from(json, i)
|
||||
if d != "" { sum = sum + _str_to_int(d) i = i + d.length() continue }
|
||||
i = i + 1
|
||||
}
|
||||
if sum == 0 { return "" }
|
||||
return _int_to_str(sum)
|
||||
}
|
||||
// (reserved) helper for future robust binop scan
|
||||
run(json) {
|
||||
// entry: attempt minimal quick shapes first, then broader routes
|
||||
// Quick path: Program-level Print of a single Literal string/int
|
||||
if json.indexOf("\"kind\":\"Program\"") >= 0 && json.indexOf("\"kind\":\"Print\"") >= 0 {
|
||||
// Literal string
|
||||
if json.indexOf("\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"string\"") >= 0 {
|
||||
local ks = "\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"string\",\"value\":\""
|
||||
local ps = json.indexOf(ks)
|
||||
if ps >= 0 {
|
||||
local si = ps + ks.length()
|
||||
local sj = json.indexOf("\"", si)
|
||||
if sj >= 0 { print(json.substring(si, sj)) return 0 }
|
||||
}
|
||||
}
|
||||
// Literal int
|
||||
if json.indexOf("\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\"") >= 0 {
|
||||
local ki = "\"expression\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local pi = json.indexOf(ki)
|
||||
if pi >= 0 {
|
||||
local ii = pi + ki.length()
|
||||
// digits until closing brace
|
||||
local ie = json.indexOf("}", ii)
|
||||
if ie < 0 { ie = ii }
|
||||
local d = json.substring(ii, ie)
|
||||
if d { print(d) return 0 }
|
||||
}
|
||||
}
|
||||
}
|
||||
// Single-purpose fast path for smoke: if BinaryOp '+' exists, try expression-bounded extractor first.
|
||||
if json.indexOf("\"BinaryOp\"") >= 0 && json.indexOf("\"operator\":\"+\"") >= 0 {
|
||||
// Bind to first Print and extract value×2 within expression bounds
|
||||
local k_print = "\"kind\":\"Print\""
|
||||
local p = index_of_from(json, k_print, 0)
|
||||
if p >= 0 {
|
||||
local np0 = new MiniVmBinOp().try_print_binop_sum_expr_values(json, json.length(), p)
|
||||
if np0 > 0 { return 0 }
|
||||
}
|
||||
// Typed direct inside BinaryOp object (fast and finite)
|
||||
local k_bo = "\"kind\":\"BinaryOp\""
|
||||
local bpos = json.indexOf(k_bo)
|
||||
if bpos >= 0 {
|
||||
local k_lint = "\"left\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local li = index_of_from(json, k_lint, bpos)
|
||||
if li >= 0 {
|
||||
local ld = read_digits(json, li + k_lint.length())
|
||||
if ld != "" {
|
||||
local k_rint = "\"right\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local ri = index_of_from(json, k_rint, li + k_lint.length())
|
||||
if ri >= 0 {
|
||||
local rd = read_digits(json, ri + k_rint.length())
|
||||
if rd != "" { print(_int_to_str(_str_to_int(ld) + _str_to_int(rd))) return 0 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// As a final bounded fallback under BinaryOp '+', sum first two numbers outside quotes
|
||||
{
|
||||
local s2 = sum_first_two_numbers(json)
|
||||
if s2 { print(s2) return 0 }
|
||||
}
|
||||
// (skip near-operator windowed scan to avoid high step counts under PyVM)
|
||||
}
|
||||
// Prefer If(literal) branch handling first
|
||||
local ifc = process_if_once(json)
|
||||
if ifc > 0 { return 0 }
|
||||
// Quick conservative path: if BinaryOp exists, sum bare numbers outside quotes
|
||||
// (limited to simple BinaryOp(int,int) JSON)
|
||||
if json.indexOf("\"BinaryOp\"") >= 0 {
|
||||
// Prefer expression-bounded scan first
|
||||
local k_print = "\"kind\":\"Print\""
|
||||
local p = index_of_from(json, k_print, 0)
|
||||
if p >= 0 {
|
||||
// Deterministic: sum the first two numbers from successive 'value' fields
|
||||
local np0 = new MiniVmBinOp().try_print_binop_sum_expr_values(json, json.length(), p)
|
||||
if np0 > 0 { return 0 }
|
||||
}
|
||||
// Brace-free deterministic fallback tied to the first BinaryOp
|
||||
{
|
||||
local np1 = new MiniVmBinOp().try_print_binop_sum_after_bop(json)
|
||||
if np1 > 0 { return 0 }
|
||||
}
|
||||
// avoid global number-sum fallback to keep steps bounded
|
||||
}
|
||||
// 0) direct typed BinaryOp '+' fast-path (explicit left/right literal ints)
|
||||
local k_bo = "\"kind\":\"BinaryOp\""
|
||||
local k_plus = "\"operator\":\"+\""
|
||||
if json.indexOf(k_bo) >= 0 && json.indexOf(k_plus) >= 0 {
|
||||
local np = try_print_binop_typed_direct(json)
|
||||
if np > 0 { return 0 }
|
||||
np = try_print_binop_typed_tokens(json)
|
||||
if np > 0 { return 0 }
|
||||
np = try_print_binop_value_pairs(json)
|
||||
if np > 0 { return 0 }
|
||||
// (skip bounded-window fallback around '+')
|
||||
}
|
||||
// 0) quick path: BinaryOp(int+int) typed fast-path
|
||||
local k_bo = "\"kind\":\"BinaryOp\""
|
||||
local bpos = json.indexOf(k_bo)
|
||||
if bpos >= 0 {
|
||||
// typed left/right ints inside BinaryOp
|
||||
local k_lint = "\"left\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local li = index_of_from(json, k_lint, bpos)
|
||||
if li >= 0 {
|
||||
local ld = read_digits(json, li + k_lint.length())
|
||||
if ld != "" {
|
||||
local k_rint = "\"right\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local ri = index_of_from(json, k_rint, li + k_lint.length())
|
||||
if ri >= 0 {
|
||||
local rd = read_digits(json, ri + k_rint.length())
|
||||
if rd != "" {
|
||||
print(_int_to_str(_str_to_int(ld) + _str_to_int(rd)))
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// fallback: sum two numeric values within the first Print.expression BinaryOp object
|
||||
local k_print = "\"kind\":\"Print\""
|
||||
local p = index_of_from(json, k_print, 0)
|
||||
if p >= 0 {
|
||||
local k_expr = "\"expression\":{"
|
||||
local epos = index_of_from(json, k_expr, p)
|
||||
if epos > 0 {
|
||||
local obj_start = index_of_from(json, "{", epos)
|
||||
local obj_end = find_balanced_object_end(json, obj_start)
|
||||
if obj_start > 0 && obj_end > 0 {
|
||||
local k_bo2 = "\"kind\":\"BinaryOp\""
|
||||
local b2 = index_of_from(json, k_bo2, obj_start)
|
||||
if b2 > 0 && b2 < obj_end {
|
||||
local k_v = "\"value\":"
|
||||
local p1 = index_of_from(json, k_v, obj_start)
|
||||
local d1 = ""
|
||||
loop (p1 > 0 && p1 < obj_end) {
|
||||
d1 = new MiniJson().read_digits_from(json, p1 + k_v.length())
|
||||
if d1 != "" { break }
|
||||
p1 = index_of_from(json, k_v, p1 + k_v.length())
|
||||
}
|
||||
if d1 != "" {
|
||||
local p2 = index_of_from(json, k_v, p1 + k_v.length())
|
||||
local d2 = ""
|
||||
loop (p2 > 0 && p2 < obj_end) {
|
||||
d2 = new MiniJson().read_digits_from(json, p2 + k_v.length())
|
||||
if d2 != "" { break }
|
||||
p2 = index_of_from(json, k_v, p2 + k_v.length())
|
||||
}
|
||||
if d2 != "" {
|
||||
local ai = _str_to_int(d1)
|
||||
local bi = _str_to_int(d2)
|
||||
print(_int_to_str(ai + bi))
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// fallback: parse-first within BinaryOp scope by scanning two numeric values
|
||||
local ssum = new MiniVmBinOp().parse_first_binop_sum(json)
|
||||
if ssum { print(ssum) return 0 }
|
||||
}
|
||||
// Attempt expression-local BinaryOp sum via existing helper on first Print
|
||||
{
|
||||
local k_print = "\"kind\":\"Print\""
|
||||
local p = index_of_from(json, k_print, 0)
|
||||
if p >= 0 {
|
||||
local np = new MiniVmBinOp().try_print_binop_sum_any(json, json.length(), p)
|
||||
if np > 0 { return 0 }
|
||||
}
|
||||
}
|
||||
// 0-c) quick path: Compare(lhs int, rhs int)
|
||||
local k_cp = "\"kind\":\"Compare\""
|
||||
local cpos = json.indexOf(k_cp)
|
||||
if cpos >= 0 {
|
||||
// operation
|
||||
local k_op = "\"operation\":\""
|
||||
local opos = index_of_from(json, k_op, cpos)
|
||||
if opos > 0 {
|
||||
local oi = opos + k_op.length()
|
||||
local oj = index_of_from(json, "\"", oi)
|
||||
if oj > 0 {
|
||||
local op = json.substring(oi, oj)
|
||||
// lhs value
|
||||
local k_lhs = "\"lhs\":{\"kind\":\"Literal\""
|
||||
local hl = index_of_from(json, k_lhs, oj)
|
||||
if hl > 0 {
|
||||
local k_v = "\"value\":"
|
||||
local hv = index_of_from(json, k_v, hl)
|
||||
if hv > 0 {
|
||||
local a = read_digits(json, hv + k_v.length())
|
||||
// rhs value
|
||||
local k_rhs = "\"rhs\":{\"kind\":\"Literal\""
|
||||
local hr = index_of_from(json, k_rhs, hl)
|
||||
if hr > 0 {
|
||||
local rv = index_of_from(json, k_v, hr)
|
||||
if rv > 0 {
|
||||
local b = read_digits(json, rv + k_v.length())
|
||||
if a && b {
|
||||
local ai = _str_to_int(a)
|
||||
local bi = _str_to_int(b)
|
||||
local res = 0
|
||||
if op == "<" { if ai < bi { res = 1 } }
|
||||
if op == "==" { if ai == bi { res = 1 } }
|
||||
if op == "<=" { if ai <= bi { res = 1 } }
|
||||
if op == ">" { if ai > bi { res = 1 } }
|
||||
if op == ">=" { if ai >= bi { res = 1 } }
|
||||
if op == "!=" { if ai != bi { res = 1 } }
|
||||
print(res)
|
||||
return 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// Scan global prints (flat programs)
|
||||
local pc = print_all_print_literals(json)
|
||||
// 2) as a robustness fallback, handle first BinaryOp sum within first Print.expression
|
||||
if pc == 0 {
|
||||
local k_print = "\"kind\":\"Print\""
|
||||
local p = index_of_from(json, k_print, 0)
|
||||
if p >= 0 {
|
||||
local k_expr = "\"expression\":{"
|
||||
local epos = index_of_from(json, k_expr, p)
|
||||
if epos > 0 {
|
||||
local obj_start = index_of_from(json, "{", epos)
|
||||
local obj_end = find_balanced_object_end(json, obj_start)
|
||||
if obj_start > 0 && obj_end > 0 {
|
||||
local k_bo = "\"kind\":\"BinaryOp\""
|
||||
local bpos = index_of_from(json, k_bo, obj_start)
|
||||
if bpos > 0 && bpos < obj_end {
|
||||
// sum two numeric values inside this expression object
|
||||
local cur = new MiniJson()
|
||||
local k_v = "\"value\":"
|
||||
local p1 = index_of_from(json, k_v, obj_start)
|
||||
local d1 = ""
|
||||
loop (p1 > 0 && p1 < obj_end) {
|
||||
d1 = cur.read_digits_from(json, p1 + k_v.length())
|
||||
if d1 != "" { break }
|
||||
p1 = index_of_from(json, k_v, p1 + k_v.length())
|
||||
}
|
||||
if d1 != "" {
|
||||
local p2 = index_of_from(json, k_v, p1 + k_v.length())
|
||||
local d2 = ""
|
||||
loop (p2 > 0 && p2 < obj_end) {
|
||||
d2 = cur.read_digits_from(json, p2 + k_v.length())
|
||||
if d2 != "" { break }
|
||||
p2 = index_of_from(json, k_v, p2 + k_v.length())
|
||||
}
|
||||
if d2 != "" {
|
||||
local ai = _str_to_int(d1)
|
||||
local bi = _str_to_int(d2)
|
||||
print(_int_to_str(ai + bi))
|
||||
pc = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if pc == 0 {
|
||||
// last resort: typed pattern-wide sum, then safe number sum outside quotes, else single int literal
|
||||
local s = new MiniVmBinOp().parse_first_binop_sum(json)
|
||||
if s { print(s) } else {
|
||||
local ts = sum_numbers_no_quotes(json)
|
||||
if ts { print(ts) } else {
|
||||
local n = parse_first_int(json)
|
||||
print(n)
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Pure helper: collect minimal print outputs (literals only) into an array
|
||||
collect_prints(json) {
|
||||
// Ported from self-contained smoke (Hardened minimal scanner)
|
||||
local out = new ArrayBox()
|
||||
local pos = 0
|
||||
local guard = 0
|
||||
// DEV trace: flip to 1 for one-run diagnosis; keep 0 for normal
|
||||
local trace = 0
|
||||
if trace == 1 { print("[collect][start] method entry") }
|
||||
local k_print = "\"kind\":\"Print\""
|
||||
loop (true) {
|
||||
guard = guard + 1
|
||||
if guard > 200 {
|
||||
if trace == 1 { print("[collect][guard_break] guard="+guard) }
|
||||
if trace == 1 { print("[collect][loop_exit] guard="+guard+" out.size="+out.size()) }
|
||||
if trace == 1 { print("[collect][return] out.size="+out.size()) }
|
||||
return out
|
||||
}
|
||||
local p = index_of_from(json, k_print, pos)
|
||||
if trace == 1 { print("[collect][loop] pos="+pos+" p="+p+" guard="+guard) }
|
||||
if p < 0 {
|
||||
if trace == 1 { print("[collect][p_break] p="+p) }
|
||||
if trace == 1 { print("[collect][loop_exit] guard="+guard+" out.size="+out.size()) }
|
||||
if trace == 1 { print("[collect][return] out.size="+out.size()) }
|
||||
return out
|
||||
}
|
||||
// bound current Print slice to [this, next)
|
||||
local obj_start = p
|
||||
local next_p = index_of_from(json, k_print, p + k_print.length())
|
||||
local obj_end = json.length()
|
||||
if next_p > 0 { obj_end = next_p }
|
||||
if trace == 1 { print("[collect][p] "+p) print("[collect][next_p] "+next_p) print("[collect][slice_end] "+obj_end) }
|
||||
if trace == 1 {
|
||||
local k_expr = "\"expression\":{"
|
||||
local epos_dbg = index_of_from(json, k_expr, obj_start)
|
||||
print("[scan][expr] "+epos_dbg)
|
||||
local fc_dbg = index_of_from(json, "\"kind\":\"FunctionCall\"", obj_start)
|
||||
print("[scan][fc] "+fc_dbg)
|
||||
local bo_dbg = index_of_from(json, "\"kind\":\"BinaryOp\"", obj_start)
|
||||
print("[scan][bo] "+bo_dbg)
|
||||
local cp_dbg = index_of_from(json, "\"kind\":\"Compare\"", obj_start)
|
||||
print("[scan][cp] "+cp_dbg)
|
||||
local ts_dbg = index_of_from(json, "\"type\":\"string\"", obj_start)
|
||||
print("[scan][ts] "+ts_dbg)
|
||||
local ti_dbg = index_of_from(json, "\"type\":\"int\"", obj_start)
|
||||
print("[scan][ti] "+ti_dbg)
|
||||
// positions for tight patterns used by branches
|
||||
local ks_pat = "\"type\":\"string\",\"value\":\""
|
||||
print("[scan][ks] "+index_of_from(json, ks_pat, obj_start))
|
||||
local ki_pat = "\"type\":\"int\",\"value\":"
|
||||
print("[scan][ki] "+index_of_from(json, ki_pat, obj_start))
|
||||
}
|
||||
|
||||
// 1) FunctionCall echo/itoa (single literal or empty args)
|
||||
{
|
||||
// Limit search within Print.expression object for stability
|
||||
local k_expr = "\"expression\":{"
|
||||
local epos = index_of_from(json, k_expr, obj_start)
|
||||
if epos > 0 { if epos < obj_end {
|
||||
local expr_start = index_of_from(json, "{", epos)
|
||||
if expr_start > 0 { if expr_start < obj_end {
|
||||
local expr_end = new MiniVmScan().find_balanced_object_end(json, expr_start)
|
||||
if expr_end > 0 { if expr_end <= obj_end {
|
||||
if trace == 1 { print("[collect][expr] "+expr_start+","+expr_end) }
|
||||
local k_fc = "\"kind\":\"FunctionCall\""
|
||||
local fcp = index_of_from(json, k_fc, expr_start)
|
||||
if trace == 1 { print("[collect][fc_in_expr] "+fcp+" (bounds "+expr_start+","+expr_end+")") }
|
||||
if fcp > 0 { if fcp < expr_end {
|
||||
if trace == 1 { print("[collect][fc_found] entering function call handler") }
|
||||
local kn = "\"name\":\""
|
||||
local np = index_of_from(json, kn, fcp)
|
||||
if trace == 1 { print("[collect][name_search] "+np+" (obj_end "+obj_end+")") }
|
||||
if np > 0 { if np < obj_end {
|
||||
local ni = np + kn.length()
|
||||
local nj = index_of_from(json, "\"", ni)
|
||||
if trace == 1 { print("[collect][name_bounds] "+ni+","+nj+" (expr_end "+expr_end+")") }
|
||||
if nj > 0 { if nj <= expr_end {
|
||||
local fname = json.substring(ni, nj)
|
||||
if trace == 1 { print("[collect][fname] '"+fname+"'") }
|
||||
local ka = "\"arguments\":["
|
||||
local ap = index_of_from(json, ka, nj)
|
||||
if ap <= 0 {
|
||||
local ka0 = "\"arguments\":"
|
||||
local ap0 = index_of_from(json, ka0, nj)
|
||||
if ap0 >= 0 { if ap0 < expr_end { ap = ap0 } }
|
||||
}
|
||||
if ap >= 0 { if ap < expr_end {
|
||||
// detect empty args [] quickly: no type token inside balanced array
|
||||
local arr_start = index_of_from(json, "[", ap)
|
||||
if arr_start >= 0 { if arr_start < expr_end {
|
||||
local arr_end = new MiniVmScan().find_balanced_array_end(json, arr_start)
|
||||
if arr_end >= 0 { if arr_end <= expr_end {
|
||||
local kt = "\"type\":\""
|
||||
local atpos = index_of_from(json, kt, arr_start)
|
||||
if trace == 1 { print("[collect][empty_check] atpos="+atpos+" arr_bounds=["+arr_start+","+arr_end+"]") }
|
||||
if atpos < 0 || atpos >= arr_end {
|
||||
if trace == 1 { print("[collect][empty_args] fname='"+fname+"'") }
|
||||
if fname == "echo" { out.push("") pos = obj_end continue }
|
||||
if fname == "itoa" { out.push("0") pos = obj_end continue }
|
||||
}
|
||||
}}}}
|
||||
// string arg
|
||||
local ks = "\"type\":\"string\",\"value\":\""
|
||||
local ps = index_of_from(json, ks, ap)
|
||||
if ps > 0 { if ps < expr_end {
|
||||
local si = ps + ks.length()
|
||||
local sj = index_of_from(json, "\"", si)
|
||||
if sj > 0 { if sj <= expr_end {
|
||||
local sval = json.substring(si, sj)
|
||||
if fname == "echo" { out.push(sval) pos = obj_end + 1 continue }
|
||||
}}
|
||||
}}
|
||||
// int arg
|
||||
local ki = "\"type\":\"int\",\"value\":"
|
||||
local pi = index_of_from(json, ki, ap)
|
||||
if pi > 0 { if pi < expr_end {
|
||||
local ival = read_digits(json, pi + ki.length())
|
||||
if ival != "" { if fname == "itoa" { out.push(ival) pos = obj_end + 1 continue } else { if fname == "echo" { out.push(ival) pos = obj_end + 1 continue } } }
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}}
|
||||
}}
|
||||
}}
|
||||
}
|
||||
|
||||
// 2) BinaryOp(int '+' int)
|
||||
{
|
||||
local k_expr = "\"expression\":{"
|
||||
local epos = index_of_from(json, k_expr, obj_start)
|
||||
if epos > 0 { if epos < obj_end {
|
||||
local k_bo = "\"kind\":\"BinaryOp\""
|
||||
local bpos = index_of_from(json, k_bo, epos)
|
||||
if bpos > 0 { if bpos < obj_end {
|
||||
if index_of_from(json, "\"operator\":\"+\"", bpos) > 0 {
|
||||
local k_l = "\"left\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local k_r = "\"right\":{\"kind\":\"Literal\",\"value\":{\"type\":\"int\",\"value\":"
|
||||
local lp = index_of_from(json, k_l, bpos)
|
||||
if lp > 0 { if lp < obj_end {
|
||||
local ld = read_digits(json, lp + k_l.length())
|
||||
if ld != "" {
|
||||
local rp = index_of_from(json, k_r, lp + k_l.length())
|
||||
if rp > 0 { if rp < obj_end {
|
||||
local rd = read_digits(json, rp + k_r.length())
|
||||
if rd != "" { if trace == 1 { print("[hit][bo-typed] "+ld+"+"+rd) } out.push(_int_to_str(_str_to_int(ld) + _str_to_int(rd))) pos = p + k_print.length() continue }
|
||||
}}
|
||||
}
|
||||
}}
|
||||
// fallback: two successive 'value' digits within expression bounds
|
||||
local k_v = "\"value\":"
|
||||
local v1 = index_of_from(json, k_v, epos)
|
||||
if v1 > 0 { if v1 < obj_end {
|
||||
local d1 = new MiniJson().read_digits_from(json, v1 + k_v.length())
|
||||
if d1 != "" {
|
||||
local v2 = index_of_from(json, k_v, v1 + k_v.length())
|
||||
if v2 > 0 { if v2 < obj_end {
|
||||
local d2 = new MiniJson().read_digits_from(json, v2 + k_v.length())
|
||||
if d2 != "" { if trace == 1 { print("[hit][bo-fallback] "+d1+"+"+d2) } out.push(_int_to_str(_str_to_int(d1) + _str_to_int(d2))) pos = p + k_print.length() continue }
|
||||
}}
|
||||
}
|
||||
}}
|
||||
}
|
||||
}}
|
||||
}}
|
||||
}
|
||||
|
||||
// 3) Compare(lhs/rhs ints)
|
||||
{
|
||||
local k_cp = "\"kind\":\"Compare\""
|
||||
local cpos = index_of_from(json, k_cp, obj_start)
|
||||
if cpos > 0 { if cpos < obj_end {
|
||||
local k_op = "\"operation\":\""
|
||||
local opos = index_of_from(json, k_op, cpos)
|
||||
if opos > 0 { if opos < obj_end {
|
||||
local oi = opos + k_op.length()
|
||||
local oj = index_of_from(json, "\"", oi)
|
||||
if oj > 0 { if oj <= obj_end {
|
||||
local op = json.substring(oi, oj)
|
||||
local k_v = "\"value\":"
|
||||
local lhs_v = index_of_from(json, k_v, oj)
|
||||
if lhs_v > 0 { if lhs_v < obj_end {
|
||||
local la = read_digits(json, lhs_v + k_v.length())
|
||||
if la != "" {
|
||||
local rhs_v = index_of_from(json, k_v, lhs_v + k_v.length())
|
||||
if rhs_v > 0 { if rhs_v < obj_end {
|
||||
local rb = read_digits(json, rhs_v + k_v.length())
|
||||
if rb != "" {
|
||||
local ai = _str_to_int(la)
|
||||
local bi = _str_to_int(rb)
|
||||
local res = 0
|
||||
if op == "<" { if ai < bi { res = 1 } }
|
||||
if op == "==" { if ai == bi { res = 1 } }
|
||||
if op == "<=" { if ai <= bi { res = 1 } }
|
||||
if op == ">" { if ai > bi { res = 1 } }
|
||||
if op == ">=" { if ai >= bi { res = 1 } }
|
||||
if op == "!=" { if ai != bi { res = 1 } }
|
||||
out.push(_int_to_str(res))
|
||||
pos = p + k_print.length()
|
||||
continue
|
||||
}
|
||||
}}
|
||||
}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}}
|
||||
}
|
||||
|
||||
// (FunctionCall branch moved earlier)
|
||||
|
||||
// 4) Literal string
|
||||
{
|
||||
local ks = "\"type\":\"string\",\"value\":\""
|
||||
local ps = index_of_from(json, ks, obj_start)
|
||||
if ps > 0 { if ps < obj_end {
|
||||
local si = ps + ks.length()
|
||||
local sj = index_of_from(json, "\"", si)
|
||||
if sj > 0 { if sj <= obj_end {
|
||||
if trace == 1 { print("[hit][str]") }
|
||||
out.push(json.substring(si, sj)) pos = p + k_print.length() continue
|
||||
}}
|
||||
}}
|
||||
}
|
||||
// 5) Literal int
|
||||
{
|
||||
local ki = "\"type\":\"int\",\"value\":"
|
||||
local pi = index_of_from(json, ki, obj_start)
|
||||
if pi > 0 { if pi < obj_end {
|
||||
local digits = read_digits(json, pi + ki.length())
|
||||
if digits != "" { if trace == 1 { print("[hit][i-lit] "+digits) } out.push(digits) pos = p + k_print.length() continue }
|
||||
}}
|
||||
}
|
||||
// Unknown: skip this Print object entirely to avoid stalls and mis-detection
|
||||
// Use coarse slice end (next Print position) when available; fallback to k_print-length step
|
||||
pos = obj_end + 1
|
||||
if pos <= p { pos = p + k_print.length() }
|
||||
}
|
||||
if trace == 1 { print("[collect][loop_exit] guard="+guard+" out.size="+out.size()) }
|
||||
if trace == 1 { print("[collect][return] out.size="+out.size()) }
|
||||
return out
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user