module scheme import arena import reader struct QQGraphJob { id reader.NodeId depth int leaving bool } struct QQGraphScratch { mut: storage arena.Arena active map[reader.NodeId]bool done map[u64]bool jobs []QQGraphJob } fn new_qq_graph_scratch() &QQGraphScratch { mut storage := arena.new(chunk_size: 1024) mut published := false storage.push() defer { storage.pop() if !published { storage.free() } } mut scratch := &QQGraphScratch{ storage: storage } unsafe { scratch.jobs.flags.set(.noslices) } published = true return scratch } fn (mut scratch QQGraphScratch) add(id reader.NodeId, depth int, leaving bool) { if scratch.jobs.len == scratch.jobs.cap { scratch.reserve_job() } scratch.jobs << QQGraphJob{id, depth, leaving} } fn (mut scratch QQGraphScratch) reserve_job() { scratch.storage.push() defer { scratch.storage.pop() } scratch.jobs.ensure_cap(scratch.jobs.len + 1) } fn (mut scratch QQGraphScratch) mark(id reader.NodeId, key u64, leaving bool) { // Only pointer-free traversal storage is allocated in this scope. Reader, // name caches, errors and emitted values always keep their outer owner. scratch.storage.push() defer { scratch.storage.pop() } if leaving { scratch.active.delete(id) scratch.done[key] = true } else { scratch.active[id] = true } } fn (mut scratch QQGraphScratch) release() { // free clears its receiver after releasing the region containing this header. // Keep that receiver as an independent, stack-local handle for the final call. mut storage := scratch.storage storage.free() } // R7RS-small ยง2.4 rejects circular quasiquote templates. Shared acyclic nodes // remain valid. An active unquote's argument is an expression, so its compiler // handles any literal graph inside that expression using the ordinary rules. // Completed visits include depth: the same node can have different meaning at // different nesting depths. Active-path identity does not need that distinction. fn (mut b Builder) qq_graph_active(id reader.NodeId, depth int) !bool { mut scratch := new_qq_graph_scratch() defer { scratch.release() } scratch.add(id, depth, false) mut active := false for scratch.jobs.len > 0 { job := scratch.jobs.pop() if job.depth > 512 { return failure('quasiquote nesting limit exceeded') } canonical := b.code.canonical_syntax_id(job.id)! key := syntax_capture_key(int(canonical), i32(job.depth)) if job.leaving { scratch.mark(canonical, key, true) continue } if canonical in scratch.active { return failure('cannot evaluate circular quasiquote syntax') } if key in scratch.done { continue } node := b.code.node(job.id)! if node.kind !in [.pair, .vector] { continue } scratch.mark(canonical, key, false) scratch.add(canonical, job.depth, true) if node.kind == .pair { name, argument := b.qq_marker(job.id)! if name != '' { if name != 'quasiquote' && job.depth == 1 { active = true } else { scratch.add(argument, job.depth + if name == 'quasiquote' { 1 } else { -1 }, false) } } else { scratch.add(node.cdr, job.depth, false) scratch.add(node.car, job.depth, false) } } else { for index in 0 .. node.count { scratch.add(b.code.vector_at(job.id, index)!, job.depth, false) } } } return active }