Beispiel #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));
            }
        }
        protected override sealed void Execute(SmallBasicEngine engine)
        {
            BaseValue second = engine.EvaluationStack.Pop();
            BaseValue first  = engine.EvaluationStack.Pop();

            engine.EvaluationStack.Push(this.Execute(first, second));
        }
Beispiel #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);
        }
Beispiel #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);
     }
 }
Beispiel #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);
     }
 }
Beispiel #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");
        }
        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;
            }
        }
Beispiel #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);
            }
        }
 public override sealed Task Execute(SmallBasicEngine engine, Frame frame)
 {
     frame.InstructionIndex++;
     return(this.Execute(engine));
 }
Beispiel #12
0
 protected override void Execute(SmallBasicEngine engine)
 {
     engine.SetEventCallback(this.library, this.eventName, this.subModule);
 }
Beispiel #13
0
 protected override Task Execute(SmallBasicEngine engine)
 {
     return(Libraries.Types[this.library].Properties[this.property].Getter(engine));
 }
 protected override int GetNextInstructionIndex(SmallBasicEngine engine) => this.target;
Beispiel #15
0
 protected override void Execute(SmallBasicEngine engine) => engine.Pause();
Beispiel #16
0
 protected override void Execute(SmallBasicEngine engine) => engine.Terminate();
 protected override int GetNextInstructionIndex(SmallBasicEngine engine) => throw new InvalidOperationException("This should have been removed during rewriting.");
 public abstract Task Execute(SmallBasicEngine engine, Frame frame);
Beispiel #19
0
 protected override void Execute(SmallBasicEngine engine) => engine.BlockOnNumberInput();
Beispiel #20
0
 protected override Task Execute(SmallBasicEngine engine)
 {
     return(Libraries.Types[this.library].Methods[this.method].Execute(engine));
 }
 public override sealed Task Execute(SmallBasicEngine engine, Frame frame)
 {
     frame.InstructionIndex = this.GetNextInstructionIndex(engine);
     return(Task.CompletedTask);
 }
Beispiel #22
0
 protected override void Execute(SmallBasicEngine engine)
 {
     engine.EvaluationStack.Push(this.value);
 }
 protected abstract void Execute(SmallBasicEngine engine);
 protected abstract int GetNextInstructionIndex(SmallBasicEngine engine);
Beispiel #25
0
 protected override void Execute(SmallBasicEngine engine)
 {
     engine.Memory[this.variable] = engine.EvaluationStack.Pop();
 }
 protected abstract Task Execute(SmallBasicEngine engine);
        protected override sealed void Execute(SmallBasicEngine engine)
        {
            BaseValue value = engine.EvaluationStack.Pop();

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

            engine.ExecutionStack.AddLast(frame);
        }