Exemplo n.º 1
0
 public Machine(IContext context)
 {
     this.context = context;
     stack        = new Stack <Frame>();
     operations   = new Operations.Operations();
     running      = false;
     table        = new Lazy <TableMaker>(() =>
                                          new TableMaker(("Address", Justification.Left), ("Operation", Justification.Left), ("Stack", Justification.Left)));
     debugState  = DebugState.Starting;
     globalFrame = new GlobalFrame();
 }
Exemplo n.º 2
0
        public IResult <Unit> Execute()
        {
            stack.Clear();
            globalFrame = new GlobalFrame();
            stack.Push(globalFrame);
            operations.Goto(0);
            running = true;

            while (!context.Cancelled() && operations.More && running)
            {
                if (operations.Current.If(out var operation))
                {
                    trace(operations.Address, () => operation.ToString());
                    var currentAddress = operations.Address;
                    if (operation.Execute(this).ValueOrOriginal(out var result, out var original) && running && result.ClassName != "Void")
                    {
                        stack.Peek().Push(result);
                    }
                    else if (original.IfNot(out var anyException) && anyException.If(out var exception))
                    {
                        if (Tracing)
                        {
                            context.PrintLine(table.Value.ToString());
                        }

                        if (GetErrorHandler().If(out var address))
                        {
                            stack.Peek().Push(new Failure(exception.Message));
                            operations.Goto(address);
                        }
                        else
                        {
                            return(failure <Unit>(exception));
                        }
                    }

                    if (operation.Increment && currentAddress == operations.Address)
                    {
                        operations.Advance(1);
                    }
                }