private TiState StepPrim(TiState state, TiNode.Primitive primitive)
        {
            return(primitive.Type switch
            {
                PrimitiveType.Constructor constr => PrimConstr(state, constr),

                PrimitiveType.Neg _ => PrimUnary(state, a => - a),
                PrimitiveType.Add _ => PrimBinaryArith(state, (a, b) => a + b),
                PrimitiveType.Sub _ => PrimBinaryArith(state, (a, b) => a - b),
                PrimitiveType.Mul _ => PrimBinaryArith(state, (a, b) => a * b),
                PrimitiveType.Div _ => PrimBinaryArith(state, (a, b) => a / b),

                PrimitiveType.Greater _ => PrimBinaryComp(state, (a, b) => a > b),
                PrimitiveType.GreaterEqual _ => PrimBinaryComp(state, (a, b) => a >= b),
                PrimitiveType.Less _ => PrimBinaryComp(state, (a, b) => a < b),
                PrimitiveType.LessEqual _ => PrimBinaryComp(state, (a, b) => a <= b),
                PrimitiveType.Equal _ => PrimBinaryComp(state, (a, b) => a == b),
                PrimitiveType.NotEqual _ => PrimBinaryComp(state, (a, b) => a != b),

                PrimitiveType.Abort _ => throw new Exception("aborting"),
                PrimitiveType.If _ => PrimIf(state),
                PrimitiveType.CasePair _ => PrimCasePair(state),
                PrimitiveType.CaseList _ => PrimCaseList(state),
                PrimitiveType.Stop _ => PrimStop(state),
                PrimitiveType.Print _ => PrimPrint(state),

                _ => throw new ArgumentOutOfRangeException(nameof(primitive))
            });
        private static PrettyPrinter.Node ShowState(TiState state)
        {
            var(stack, usages1) = ShowStack(state.Stack);
            var(dump, usages2)  = ShowDump(state.Dump);

            var usages = usages1.Union(usages2).ToHashSet();

            var(heap, usages3) = ShowHeap(state.Heap, usages);

            usages = usages.Union(usages3).ToHashSet();

            var visibleHeap = Append(heap.Where(x => usages.Contains(x.Key)).Select(x => x.Value).ToArray());

            return(Append(
                       Str("digraph {"),
                       Newline(),
                       Str("  "),
                       Indent(Append(
                                  Str("node [shape=record style=filled];"),
                                  Newline(),
                                  stack,
                                  dump,
                                  visibleHeap
                                  )),
                       Newline(),
                       Str("}")));
        }
예제 #3
0
        public IEnumerable <TiState> Evaluate(TiState state)
        {
            yield return(state);

            while (!IsComplete(state))
            {
                state = Step(state);

                // TODO use some stats to do this, instead of everytime
                state = TiGarbageCollector.Collect(state);

                yield return(state);
            }
        }
        public static TiState Collect(TiState state)
        {
            ImmutableStack <int> newStack;
            ImmutableStack <ImmutableStack <int> > newDump;
            ImmutableDictionary <Name, int>        newGlobals;

            var newHeap = state.Heap;

            (newHeap, newStack)   = MarkFromStack(newHeap, state.Stack);
            (newHeap, newDump)    = MarkFromDump(newHeap, state.Dump);
            (newHeap, newGlobals) = MarkFromGlobals(newHeap, state.Globals);
            newHeap = ScanHeap(newHeap);

            return(new TiState(state.Output, newStack, newDump, newHeap, newGlobals));
        }
예제 #5
0
        private TiState Step(TiState state)
        {
            var headAddr = state.Stack.Peek();
            var headNode = state.Heap[headAddr];

            return(headNode switch
            {
                TiNode.Number number => StepNum(state, number),
                TiNode.Application application => StepAp(state, application),
                TiNode.Supercombinator supercombinator => StepSc(state, supercombinator),
                TiNode.Indirection indirection => StepInd(state, indirection),
                TiNode.Primitive primitive => StepPrim(state, primitive),
                TiNode.Data data => StepData(state, data),

                _ => throw new ArgumentOutOfRangeException(nameof(headNode))
            });
 private static PrettyPrinter.Node ShowState(TiState state) => Append(ShowOutput(state.Output), Newline(), ShowStack(state.Heap, state.Stack), Newline());
        public static string Print(TiState state)
        {
            var node = ShowState(state);

            return(PrettyPrinter.Display(node));
        }