private static GmState StepUnwind(GmInstruction.Unwind _, GmState state) { var head = state.Stack.Peek(); var headNode = state.Heap[head]; return(headNode switch { GmNode.Number num => UnwindNum(num, state), GmNode.Application ap => UnwindAp(ap, state), GmNode.Global global => UnwindGlobal(global, state), GmNode.Indirection ind => UnwindInd(ind, state), _ => throw new ArgumentOutOfRangeException(nameof(headNode)) });
private (ImmutableDictionary <int, GmNode> Heap, ImmutableDictionary <Name, int> Globals) BuildInitialHeap(IEnumerable <SupercombinatorDefinition <Name> > supercombinatorDefinitions) { var heap = new Dictionary <int, GmNode>(); var globals = new Dictionary <Name, int>(); foreach (var def in supercombinatorDefinitions) { var addr = heap.Count; heap[addr] = new GmNode.Global(def.Parameters.Count, CompileSc(def.Parameters, def.Body)); globals[def.Name] = addr; } foreach (var(name, args, insts) in CompiledPrimitives) { var addr = heap.Count; heap[addr] = new GmNode.Global(args, ImmutableQueue <GmInstruction> .Empty.EnqueueRange(insts)); globals[new Name(name)] = addr; } return(heap.ToImmutableDictionary(), globals.ToImmutableDictionary()); }