module scheme import reader const syntax_active_bit = u64(1) << 63 struct DatumCompiler { mut: code &Code // A single table records syntax indices and DFS state, preserving sharing // and detecting graph input. Quoted cycles are data; the compiler validates // executable structure after conversion. No reversed flat-list array is needed. indices map[voidptr]u64 } fn (mut converter DatumCompiler) convert(value Value, depth int) !reader.NodeId { if depth > reader.max_depth { return failure('eval datum nesting limit exceeded') } if value.tag == .nil { return reader.nil_node } if value.tag !in [.pair, .vector] { return converter.code.syntax_atom(value, reader.no_node) } pointer := value.pointer() if state := converter.indices[pointer] { converter.code.enable_datum_graphs() return reader.NodeId(-2 - int(state & ~syntax_active_bit)) } if value.tag == .pair { return converter.pair(value, depth) } items := value.vector_pointer().items mut arena := converter.code.syntax_arena() if items.len > 2147483647 - arena.elements.len { return failure('eval vector exceeds supported range') } start := arena.elements.len id := converter.code.syntax_node(SyntaxNode{ kind: .vector, car: reader.NodeId(start), cdr: reader.NodeId(items.len) })! index := u64(-int(id) - 2) converter.indices[pointer] = index | syntax_active_bit arena.elements.ensure_cap(start + items.len) // Only indices live here. Recursive conversion may grow the element arena; // it cannot inspect these slots, and each is filled before compilation starts. unsafe { arena.elements.len = start + items.len } for i, item in items { arena.elements[start + i] = converter.convert(item, depth + 1)! } converter.indices[pointer] = index return id } fn (mut converter DatumCompiler) pair(root Value, depth int) !reader.NodeId { mut cursor := root mut first := reader.no_node mut last := reader.no_node mut tail := reader.nil_node for cursor.tag == .pair { pointer := cursor.pointer() if state := converter.indices[pointer] { converter.code.enable_datum_graphs() tail = reader.NodeId(-2 - int(state & ~syntax_active_bit)) break } id := converter.code.syntax_node(SyntaxNode{ kind: .pair })! converter.indices[pointer] = u64(-int(id) - 2) | syntax_active_bit if first == reader.no_node { first = id } if last != reader.no_node { converter.code.generated.nodes[-int(last) - 2] = SyntaxNode{ ...converter.code.generated.nodes[-int(last) - 2], cdr: id } } last = id pair := cursor.pair_pointer() car := converter.convert(pair.car, depth + 1)! converter.code.generated.nodes[-int(id) - 2] = SyntaxNode{ ...converter.code.generated.nodes[-int(id) - 2], car: car } cursor = pair.cdr if cursor.tag != .pair { tail = converter.convert(cursor, depth + 1)! break } } converter.code.generated.nodes[-int(last) - 2] = SyntaxNode{ ...converter.code.generated.nodes[-int(last) - 2], cdr: tail } // Complete this flat spine after all its cars/tail have been traversed. // Walking existing links avoids retaining a second array of pair pointers. cursor = root for cursor.tag == .pair { pointer := cursor.pointer() state := converter.indices[pointer] if state & syntax_active_bit == 0 { break } converter.indices[pointer] = state & ~syntax_active_bit cursor = cursor.pair_pointer().cdr } return first } fn (mut vm VM) compile_value(expression Value, environment Value) !&Function { return vm.compile_value_at(expression, environment, '') } @[inline] fn (mut vm VM) compile_value_at(expression Value, environment Value, source_path string) !&Function { if vm.uses_bounded_operations() { return vm.owner.bounded_files.compile_value(mut vm, expression, environment, source_path) } return vm.compile_value_inner(expression, environment, source_path) } fn (mut vm VM) compile_value_inner(expression Value, environment Value, source_path string) !&Function { space := vm.environment_space(environment)! mut code := vm.new_code(reader.Document{}, vm.next_context(), space, source_path) mut converter := DatumCompiler{ code: code } root := converter.convert(expression, 0)! return source_program(code, [root], false) } // eval_value evaluates a runtime datum in a same-VM environment specifier, // without printing or reparsing it. Host entries have the same continuation // boundary as eval(source); Scheme eval runs on its caller's existing machine. pub fn (mut vm VM) eval_value(expression Value, environment Value) !Value { vm.ready()! vm.check_entry_control()! function := vm.compile_value(expression, environment)! return vm.run_program(function, vm.next_context()) } const eval_native_descriptors = [ NativeDescriptor{'scheme-report-environment', NativeFn(eval_report_environment)} NativeDescriptor{'null-environment', NativeFn(eval_null_environment)} NativeDescriptor{'interaction-environment', NativeFn(eval_interaction_environment)}, ]! fn (mut vm VM) install_eval_primitives() { function := &Function{ code: unsafe { nil }, params: 2, slot_count: 2, instructions: [bridge_instruction(.eval_enter, 0, 0)] } vm.bind_global('eval', procedure_value(&Procedure{ mode: .closure, owner: vm.owner, name: 'eval', function: function })) for index in 0 .. eval_native_descriptors.len { entry := &eval_native_descriptors[index] vm.bind_global(entry.name, procedure_value(&Procedure{ mode: .native, owner: vm.owner, name: entry.name, native: entry.callback })) } } fn eval_report_environment(mut vm VM, args []Value) !Value { check_arity('scheme-report-environment', args.len, 1, 1)! return vm.report_environment(int(args[0].as_integer()!)) } fn eval_null_environment(mut vm VM, args []Value) !Value { check_arity('null-environment', args.len, 1, 1)! return vm.null_environment(int(args[0].as_integer()!)) } fn eval_interaction_environment(mut vm VM, args []Value) !Value { check_arity('interaction-environment', args.len, 0, 0)! return vm.interaction_environment() } // Compilation is a cold procedure mode, kept out of ordinary application. @[noinline] fn (mut m Machine) enter_eval(mut vm VM) ! { function := vm.compile_value_at(m.env.slots[0], m.env.slots[1], m.source_path())! // The normal closure entry already checked arity/limits, preserved the // caller's continuation and recorded its source site. Tail-replace only // this private bridge; eval introduces no native-entry boundary or frame. scope := m.checked_activation_for(vm, function, unsafe { nil }, true)! m.truncate(m.base) m.function = function m.env = scope m.ip = 0 }