Ejemplo n.º 1
0
        public async Task ProgramInterpreterInvokeExternMethod7()
        {
            var parser = new BaZicParser();

            var inputCode =
                @"EXTERN FUNCTION Main(args[])
END FUNCTION

EXTERN ASYNC FUNCTION Method1(arg)
    VARIABLE var1 = AWAIT System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(3.0))
    RETURN arg
END FUNCTION";

            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var errors = await interpreter.Build();

                Assert.IsNull(errors);

                var t      = interpreter.StartReleaseAsync(true);
                var result = (Task)interpreter.InvokeMethod(true, "Method1", true, 123);

                Assert.AreEqual(TaskStatus.WaitingForActivation, result.Status);

                await Task.Delay(5000);

                Assert.AreEqual(TaskStatus.RanToCompletion, result.Status);
                Assert.AreEqual(123, ((dynamic)result).Result);
                Assert.AreEqual(BaZicInterpreterState.Idle, interpreter.State);

                await interpreter.Stop();

                Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State);
            }
        }
Ejemplo n.º 2
0
        public async Task ProgramInterpreterInvokeExternMethod6()
        {
            var parser = new BaZicParser();

            var inputCode =
                @"EXTERN FUNCTION Main(args[])
    AWAIT MethodAsync(345, 2.0)
END FUNCTION

EXTERN FUNCTION Method1(arg)
    RETURN arg
END FUNCTION

ASYNC FUNCTION MethodAsync(value, timeToWait)
    VARIABLE var1 = AWAIT System.Threading.Tasks.Task.Delay(System.TimeSpan.FromSeconds(timeToWait))
    RETURN value
END FUNCTION";

            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var errors = await interpreter.Build();

                Assert.IsNull(errors);

                var t      = interpreter.StartReleaseAsync(true);
                var result = await interpreter.InvokeMethod(true, "Method1", true, 123);

                Assert.AreEqual(123, result);

                // Idle is expected because InvokeMethod waits that the main
                // thread (used by Main()) is free before running. So the async
                // function is complete before reaching this assert.
                Assert.AreEqual(BaZicInterpreterState.Idle, interpreter.State);

                await interpreter.Stop();

                Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State);
            }

            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var t      = interpreter.StartReleaseAsync(true);
                var result = await interpreter.InvokeMethod(true, "Method1", true, 123);

                Assert.AreEqual(123, result);

                // Idle is expected because InvokeMethod waits that the main
                // thread (used by Main()) is free before running. So the async
                // function is complete before reaching this assert.
                Assert.AreEqual(BaZicInterpreterState.Idle, interpreter.State);

                await interpreter.Stop();

                Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State);
            }
        }
Ejemplo n.º 3
0
        public async Task ProgramInterpreterInvokeExternMethodUI12()
        {
            var parser = new BaZicParser();

            var inputCode =
                @"
VARIABLE globVar = 1

EXTERN FUNCTION Main(args[])
END FUNCTION

EXTERN FUNCTION Method1()
    DO WHILE TRUE
    LOOP
END FUNCTION";

            var xamlCode = @"
<Window xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" Name=""Window1"">
    <StackPanel>
        <Button Name=""Button1"" Content=""Hello""/>
    </StackPanel>
</Window>";

            using (var interpreter = new BaZicInterpreter(inputCode, xamlCode))
            {
                var t = interpreter.StartDebugAsync(true);
                t = interpreter.InvokeMethod(true, "Method1", true);

                await Task.Delay(3000);

                Assert.AreEqual(BaZicInterpreterState.Running, interpreter.State);

                await interpreter.Stop();

                Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State);
            }

            using (var interpreter = new BaZicInterpreter(inputCode, xamlCode))
            {
                var errors = await interpreter.Build();

                Assert.IsNull(errors);

                var t = interpreter.StartReleaseAsync(true);
                t = interpreter.InvokeMethod(true, "Method1", true);

                await Task.Delay(10000);

                Assert.AreEqual(BaZicInterpreterState.Running, interpreter.State);

                await interpreter.Stop();

                Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State);
            }
        }
Ejemplo n.º 4
0
        internal static async Task RunRelease(List <string> resultReceiver, string inputBaZicCode, string xamlCode, params object[] args)
        {
            using (var interpreter = new BaZicInterpreter(inputBaZicCode, xamlCode, optimize: false))
            {
                interpreter.SetDependencies("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
                await interpreter.StartReleaseAsync(false, args);

                if (interpreter.Error != null)
                {
                    throw interpreter.Error.Exception;
                }

                resultReceiver.Add(interpreter.ProgramResult?.ToString());
            }
        }
Ejemplo n.º 5
0
        public async Task ProgramInterpreterInvokeExternMethod11()
        {
            var parser = new BaZicParser();

            var inputCode =
                @"
VARIABLE globVar = 1

EXTERN FUNCTION Main(args[])
    DO WHILE TRUE
    LOOP
END FUNCTION

EXTERN FUNCTION Method1()
    RETURN TRUE
END FUNCTION";


            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var t = interpreter.StartDebugAsync(true);
                t = interpreter.InvokeMethod(true, "Method1", true);

                await Task.Delay(3000);

                Assert.AreEqual(BaZicInterpreterState.Running, interpreter.State);

                await interpreter.Stop();

                Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State);
            }

            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var t = interpreter.StartReleaseAsync(true);
                t = interpreter.InvokeMethod(true, "Method1", true);

                await Task.Delay(10000);

                Assert.AreEqual(BaZicInterpreterState.Running, interpreter.State);

                await interpreter.Stop();

                Assert.AreEqual(BaZicInterpreterState.Stopped, interpreter.State);
            }
        }
Ejemplo n.º 6
0
        private void RunProgramReleaseButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(BaZicCodeTextBox.Text))
            {
                MessageBox.Show("There is no BaZic code to run.");
                return;
            }

            Logs = string.Empty;

            if (string.IsNullOrWhiteSpace(XamlCodeTextBox.Text))
            {
                XamlCodeTextBox.Text = string.Empty;
            }

            var lexer  = new BaZicLexer();
            var parser = new BaZicParser();

            var tokens             = lexer.Tokenize(BaZicCodeTextBox.Text);
            var abstractSyntaxTree = parser.Parse(tokens, XamlCodeTextBox.Text, optimize: OptimizeCheckBox.IsChecked.Value);

            foreach (var issue in abstractSyntaxTree.Issues.InnerExceptions.OfType <BaZicParserException>())
            {
                Log(issue.ToString());
            }

            if (abstractSyntaxTree.Program != null && abstractSyntaxTree.Issues.InnerExceptions.OfType <BaZicParserException>().All(issue => issue.Level != Core.Enums.BaZicParserExceptionLevel.Error))
            {
                RunProgramButton.Visibility        = Visibility.Collapsed;
                RunProgramReleaseButton.Visibility = Visibility.Collapsed;
                OptimizeCheckBox.Visibility        = Visibility.Collapsed;
                PauseButton.Visibility             = Visibility.Visible;
                StopButton.Visibility = Visibility.Visible;

                _interpreter = new BaZicInterpreter(abstractSyntaxTree.Program);
                _interpreter.StateChanged += Interpreter_StateChanged;
                var t = _interpreter.StartReleaseAsync(true);
            }
            else
            {
                Log("The program has not been build.");
            }
        }
Ejemplo n.º 7
0
        public async Task IterationInterpreterInfiniteLoopReleaseMode()
        {
            var parser    = new BaZicParser();
            var inputCode =
                @"EXTERN FUNCTION Main(args[])
    VARIABLE file = NEW System.IO.FileStream(""LocalWebPage.html"", System.IO.FileMode.OpenOrCreate)
    DO
        # The file LocalWebPage.html must be protected in write as it's a busy resource.
    LOOP WHILE True

    RETURN 1
END FUNCTION";
            var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program);
            var t           = interpreter.StartReleaseAsync(true);

            await Task.Delay(10000);

            try
            {
                File.ReadAllText("LocalWebPage.html"); // Must crash.
                Assert.Fail();
            }
            catch (IOException)
            {
            }
            catch
            {
                Assert.Fail();
            }

            await interpreter.Stop();

            var fileContent = File.ReadAllText("LocalWebPage.html");

            Assert.IsTrue(interpreter.State == BaZicInterpreterState.Stopped);
            Assert.IsFalse(string.IsNullOrWhiteSpace(fileContent));
        }
Ejemplo n.º 8
0
        public async Task ProgramInterpreterInvokeExternMethodUI15()
        {
            var inputCode =
                @"
EXTERN FUNCTION Main(args[])
END FUNCTION

EXTERN FUNCTION Method1()
    VARIABLE var1 = Button1.Content
    IF var1 = ""Hello"" THEN
        Button1.Content = ""Hello World""
        var1 = Button1.Content
        IF var1 = ""Hello World"" THEN
            RETURN TRUE
        END IF
    END IF

    RETURN FALSE
END FUNCTION

# The XAML will be provided separatly";

            var xamlCode = @"
<StackPanel xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
    <Button Name=""Button1"" Content=""Hello""/>
</StackPanel>";

            var task1 = Task.Run(() =>
            {
                using (var interpreter = new BaZicInterpreter(inputCode, xamlCode))
                {
                    var result = interpreter.InvokeMethod(true, "Method1", true).Result;

                    Assert.IsNull(result);
                    Assert.AreEqual("The variable 'Button1' does not exist or is not accessible.", interpreter.Error.Exception.Message);

                    var t  = interpreter.StartDebugAsync(true);
                    result = interpreter.InvokeMethod(true, "Method1", true).Result;

                    Assert.AreEqual(true, result);
                }
            });

            var task2 = Task.Run(() =>
            {
                using (var interpreter = new BaZicInterpreter(inputCode, xamlCode))
                {
                    var errors = interpreter.Build().Result;

                    Assert.IsNull(errors);

                    var result = interpreter.InvokeMethod(true, "Method1", true).Result;

                    Assert.IsNull(result);
                    Assert.AreEqual("Object reference not set to an instance of an object.", interpreter.Error.Exception.InnerException.Message);

                    var t  = interpreter.StartReleaseAsync(true);
                    result = interpreter.InvokeMethod(true, "Method1", true).Result;

                    Assert.AreEqual(true, result);
                }
            });

            var task3 = Task.Run(() =>
            {
                using (var interpreter = new BaZicInterpreter(inputCode, xamlCode))
                {
                    var t      = interpreter.StartReleaseAsync(true);
                    var result = interpreter.InvokeMethod(true, "Method1", true).Result;

                    Assert.AreEqual(true, result);
                }
            });

            await Task.WhenAll(task1, task2, task3);
        }
Ejemplo n.º 9
0
        public async Task ProgramInterpreterInvokeExternMethod9()
        {
            var parser = new BaZicParser();

            var inputCode =
                @"
VARIABLE globVar = 1

EXTERN FUNCTION Main(args[])
END FUNCTION

EXTERN FUNCTION Method1()
    globVar = globVar + 1
    RETURN globVar
END FUNCTION";

            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var t      = interpreter.StartDebugAsync(true);
                var result = await interpreter.InvokeMethod(true, "Method1", true);

                Assert.AreEqual(2, result);

                result = await interpreter.InvokeMethod(true, "Method1", true);

                Assert.AreEqual(3, result);
            }

            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var result = await interpreter.InvokeMethod(true, "Method1", true);

                Assert.AreEqual(2, result);

                result = await interpreter.InvokeMethod(true, "Method1", true);

                Assert.AreEqual(3, result);
            }

            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var errors = await interpreter.Build();

                Assert.IsNull(errors);

                var t      = interpreter.StartReleaseAsync(true);
                var result = await interpreter.InvokeMethod(true, "Method1", true);

                Assert.AreEqual(2, result);

                result = await interpreter.InvokeMethod(true, "Method1", true);

                Assert.AreEqual(3, result);
            }

            using (var interpreter = new BaZicInterpreter(parser.Parse(inputCode, false).Program))
            {
                var errors = await interpreter.Build();

                Assert.IsNull(errors);

                var result = await interpreter.InvokeMethod(true, "Method1", true);

                Assert.AreEqual(2, result);

                result = await interpreter.InvokeMethod(true, "Method1", true);

                Assert.AreEqual(3, result);
            }
        }