Ejemplo n.º 1
0
        protected override void Execute(SmallBasicEngine engine)
        {
            BaseValue value = engine.EvaluationStack.Pop();

            executeAux(engine.Memory, this.array, this.indicesCount);

            BaseValue executeAux(Dictionary <string, BaseValue> memory, string index, int remainingIndices)
            {
                if (remainingIndices == 0)
                {
                    memory[index] = value;
                }
                else
                {
                    Dictionary <string, BaseValue> nextMemory =
                        memory.TryGetValue(index, out BaseValue elementValue) && elementValue is ArrayValue elementArrayValue
                        ? elementArrayValue.ToDictionary()
                        : new Dictionary <string, BaseValue>();

                    string nextIndex = engine.EvaluationStack.Pop().ToString();

                    memory[index] = executeAux(nextMemory, nextIndex, remainingIndices - 1);
                }

                return(new ArrayValue(memory));
            }
        }
Ejemplo n.º 2
0
        protected override sealed void Execute(SmallBasicEngine engine)
        {
            BaseValue second = engine.EvaluationStack.Pop();
            BaseValue first  = engine.EvaluationStack.Pop();

            engine.EvaluationStack.Push(this.Execute(first, second));
        }
Ejemplo n.º 3
0
        private static async Task <SmallBasicEngine> VerifyRuntimeAux(this SmallBasicCompilation compilation, IEngineLibraries libraries, string memoryContents)
        {
            compilation.VerifyDiagnostics();

            SmallBasicEngine engine = new SmallBasicEngine(compilation, libraries);

            while (engine.State != ExecutionState.Terminated)
            {
                engine.State.Should().Be(ExecutionState.Running, "loggers cannot move to another state");
                await engine.Execute().ConfigureAwait(false);
            }

            DebuggerSnapshot snapshot = engine.GetSnapshot();

            snapshot.ExecutionStack.Should().BeEmpty();

            if (!memoryContents.IsDefault())
            {
                string values = Environment.NewLine
                                + snapshot.Memory.Select(pair => $"{pair.Key} = {pair.Value.ToDisplayString()}").Join(Environment.NewLine);

                values.Should().Be(memoryContents);
            }

            return(engine);
        }
Ejemplo n.º 4
0
        protected override void Execute(SmallBasicEngine engine)
        {
            BaseValue value = engine.EvaluationStack.Pop();

            executeAux(new ArrayValue(engine.Memory), this.array, this.indicesCount);

            BaseValue executeAux(ArrayValue memory, string index, int remainingIndices)
            {
                if (remainingIndices == 0)
                {
                    memory.SetIndex(index, value);
                }
                else
                {
                    ArrayValue nextMemory =
                        memory.TryGetValue(index, out BaseValue elementValue) && elementValue is ArrayValue elementArrayValue
                        ? elementArrayValue
                        : new ArrayValue();

                    string nextIndex = engine.EvaluationStack.Pop().ToString();

                    memory.SetIndex(index, executeAux(nextMemory, nextIndex, remainingIndices - 1));
                }

                return(memory);
            }
        }
 protected override int GetNextInstructionIndex(SmallBasicEngine engine)
 {
     if (engine.EvaluationStack.Pop().ToBoolean())
     {
         return(this.trueTargetOpt ?? engine.ExecutionStack.Last().InstructionIndex + 1);
     }
     else
     {
         return(this.falseTargetOpt ?? engine.ExecutionStack.Last().InstructionIndex + 1);
     }
 }
Ejemplo n.º 6
0
 protected override void Execute(SmallBasicEngine engine)
 {
     if (engine.Memory.TryGetValue(this.variable, out BaseValue value))
     {
         engine.EvaluationStack.Push(value);
     }
     else
     {
         engine.EvaluationStack.Push(StringValue.Empty);
     }
 }
Ejemplo n.º 7
0
        public static async Task <SmallBasicEngine> VerifyExecutionState(this SmallBasicCompilation compilation, ExecutionState executionState, ExecutionMode mode = ExecutionMode.RunToEnd)
        {
            compilation.VerifyDiagnostics();

            SmallBasicEngine engine = new SmallBasicEngine(compilation, new LibrariesCollection());

            engine.Mode = mode;
            await engine.Execute().ConfigureAwait(false);

            engine.State.Should().Be(executionState);

            return(engine);
        }
        public static void SetEventCallbacks(this IEngineLibraries libraries, SmallBasicEngine engine)
        {
            libraries.Controls.ButtonClicked += () => engine.RaiseEvent("Controls", "ButtonClicked");
            libraries.Controls.TextTyped     += () => engine.RaiseEvent("Controls", "TextTyped");

            libraries.GraphicsWindow.KeyDown   += () => engine.RaiseEvent("GraphicsWindow", "KeyDown");
            libraries.GraphicsWindow.KeyUp     += () => engine.RaiseEvent("GraphicsWindow", "KeyUp");
            libraries.GraphicsWindow.MouseDown += () => engine.RaiseEvent("GraphicsWindow", "MouseDown");
            libraries.GraphicsWindow.MouseMove += () => engine.RaiseEvent("GraphicsWindow", "MouseMove");
            libraries.GraphicsWindow.MouseUp   += () => engine.RaiseEvent("GraphicsWindow", "MouseUp");
            libraries.GraphicsWindow.TextInput += () => engine.RaiseEvent("GraphicsWindow", "TextInput");

            libraries.Timer.Tick += () => engine.RaiseEvent("Timer", "Tick");
        }
Ejemplo n.º 9
0
        public AsyncEngine(bool isDebugging)
        {
            this.Libraries = new LibrariesCollection();
            this.engine    = new SmallBasicEngine(CompilationStore.Compilation, this.Libraries);

            if (isDebugging)
            {
                this.engine.Mode = ExecutionMode.Debug;
                this.engine.Pause();
            }
            else
            {
                this.engine.Mode = ExecutionMode.RunToEnd;
            }
        }
Ejemplo n.º 10
0
        protected override void Execute(SmallBasicEngine engine)
        {
            IReadOnlyDictionary <string, BaseValue> memory = engine.Memory;
            int    remainingIndices = this.indicesCount;
            string index            = this.array;

            while (remainingIndices > 0 && memory.TryGetValue(index, out BaseValue elementValue) && elementValue is ArrayValue elementArrayValue)
            {
                index  = engine.EvaluationStack.Pop().ToString();
                memory = elementArrayValue;
                remainingIndices--;
            }

            if (remainingIndices > 0 || !memory.TryGetValue(index, out BaseValue value))
            {
                engine.EvaluationStack.Push(StringValue.Empty);
            }
            else
            {
                engine.EvaluationStack.Push(value);
            }
        }
Ejemplo n.º 11
0
 public override sealed Task Execute(SmallBasicEngine engine, Frame frame)
 {
     frame.InstructionIndex++;
     return(this.Execute(engine));
 }
Ejemplo n.º 12
0
 protected override void Execute(SmallBasicEngine engine)
 {
     engine.SetEventCallback(this.library, this.eventName, this.subModule);
 }
Ejemplo n.º 13
0
 protected override Task Execute(SmallBasicEngine engine)
 {
     return(Libraries.Types[this.library].Properties[this.property].Getter(engine));
 }
Ejemplo n.º 14
0
 protected override int GetNextInstructionIndex(SmallBasicEngine engine) => this.target;
Ejemplo n.º 15
0
 protected override void Execute(SmallBasicEngine engine) => engine.Pause();
Ejemplo n.º 16
0
 protected override void Execute(SmallBasicEngine engine) => engine.Terminate();
Ejemplo n.º 17
0
 protected override int GetNextInstructionIndex(SmallBasicEngine engine) => throw new InvalidOperationException("This should have been removed during rewriting.");
Ejemplo n.º 18
0
 public abstract Task Execute(SmallBasicEngine engine, Frame frame);
Ejemplo n.º 19
0
 protected override void Execute(SmallBasicEngine engine) => engine.BlockOnNumberInput();
Ejemplo n.º 20
0
 protected override Task Execute(SmallBasicEngine engine)
 {
     return(Libraries.Types[this.library].Methods[this.method].Execute(engine));
 }
Ejemplo n.º 21
0
 public override sealed Task Execute(SmallBasicEngine engine, Frame frame)
 {
     frame.InstructionIndex = this.GetNextInstructionIndex(engine);
     return(Task.CompletedTask);
 }
Ejemplo n.º 22
0
 protected override void Execute(SmallBasicEngine engine)
 {
     engine.EvaluationStack.Push(this.value);
 }
Ejemplo n.º 23
0
 protected abstract void Execute(SmallBasicEngine engine);
Ejemplo n.º 24
0
 protected abstract int GetNextInstructionIndex(SmallBasicEngine engine);
Ejemplo n.º 25
0
 protected override void Execute(SmallBasicEngine engine)
 {
     engine.Memory[this.variable] = engine.EvaluationStack.Pop();
 }
Ejemplo n.º 26
0
 protected abstract Task Execute(SmallBasicEngine engine);
Ejemplo n.º 27
0
        protected override sealed void Execute(SmallBasicEngine engine)
        {
            BaseValue value = engine.EvaluationStack.Pop();

            engine.EvaluationStack.Push(this.Execute(value));
        }
Ejemplo n.º 28
0
        protected override void Execute(SmallBasicEngine engine)
        {
            Frame frame = new Frame(engine.Modules[this.subModuleName]);

            engine.ExecutionStack.AddLast(frame);
        }