Exemplo n.º 1
0
        private void Execute()
        {
            if (_options.Interactive)
            {
                Repl repl = new();
                repl.Run();
                return;
            }

            string content = File.ReadAllText(@"C:\Source\BrunoLang\Samples\Example1.bruno");
            //var content = "x = 1 + 2";
            BrunoProgram program = ParseService.Parse(content);

            //Console.WriteLine(content);
            //Console.WriteLine("---");
            //Console.WriteLine(program);
            //Console.WriteLine("---");
            Console.WriteLine(program.Evaluate()?.ToString());

            //if (_options.Verbose)
            //{
            //    Console.WriteLine($"Verbose output enabled. Current Arguments: -v {_options.Verbose}");
            //    Console.WriteLine("Quick Start Example! App is in Verbose mode!");
            //}
            //else
            //{
            //    Console.WriteLine($"Current Arguments: -v {_options.Verbose}");
            //    Console.WriteLine("Quick Start Example!");
            //}
        }
Exemplo n.º 2
0
        public void Test1()
        {
            //var content = @"x = 1 + 2
            //                print(x)";

            //string content = @"if 1 == 1;
            //                print(""true"");";


            string[] contents =
            {
                "(1 + 2) * 3",
                "1 + 2 * 3",
                "1 + 4 * 2 - 3",
                "1 + 2 * 3 - a(1, u(2))",
                "((1 + 2) + 3) + a(1, u(2))",
                "1 + 2 * 3 - a(1, u(2))",
                "1 + 1",
                "1 + 2 * 3 - 4",
                "1 + 2 * 3 - a(1, u(2))",
                "1 + Get(1, 2, a(22, z))",
                "12.3 + add(1)",
                "regex.StartMatch",
                "\"yo\".ToString()",
                "add()",
                "add().yo() + add(1, 3 + 2).start(x, 7 * 8)",
                "add().yo(1, 2, 3, 5 * 88)",
                "add(2, 5).yo(1, 2, 32, 5).Start(x, 1 + 2)",
                "add(1).yo(1).start(3, 4)",
                "\"string\".Start",
                "\"string\".Start(1 + 2, \"ya\").Do(x).No()",
                "(1 + 2).ToString",
                "var1",
                "\"string\"",
                "a.ToString + 2",
                "i + j"
                //"add(i)",
                //"(i1(2) + 12)",
                //"add(a(a(i + l) + 2, z, k), j)",
                //"Len( Match(\"string1\", 14).StartMatch)",
                //"Value(i1, i2).Start + 1",
            };


            int i = 1;

            //BrunoExpression exp = default;
            //foreach (string formula in _baselineFormulas) {
            foreach (string content in contents)
            {
                BrunoProgram program = ParseService.Parse(content);
                Assert.NotNull(program);

                //object result = program.Evaluate();

                WriteLine(program.ToString());
                WriteLine("");

                //WriteLine(result?.ToString() ?? "<null>");

                //try
                //{
                //    exp = Parser.Parse(formula);
                //    Assert.AreEqual(formula.Replace(" ", ""), exp.ToString().Replace(" ", ""));
                //    i++;
                //    if (formulas.Length < 100)
                //    {
                //        WriteOutput(formula);
                //    }
                //}
                //catch (Exception)
                //{
                //    WriteOutput(formula);
                //    if (formulas.Length < 100)
                //    {
                //        throw;
                //    }
                //}
            }
        }
Exemplo n.º 3
0
        public void Run()
        {
            Console.CancelKeyPress += CancelKeyPress;

            using (_output = new OutputService(true, true))
            {
                _context = JObject.FromObject(new { input1 = "555-99-5656" });
                PrintContext();

                while (true)
                {
                    try
                    {
                        Console.Write(":> ");
                        _command = Console.ReadLine();
                        _output.WriteLine($"Command: {_command}");

                        if (_context == null)
                        {
                            throw new ApplicationException("Context is empty.");
                        }

                        if (string.IsNullOrEmpty(_command))
                        {
                            _command = "context";
                        }

                        if (_command.ToLower() == "exit")
                        {
                            break;
                        }

                        if (_command.StartsWith("{") || _command.ToLower().StartsWith("context {"))
                        {
                            _context = JObject.Parse(_command.Replace("context ", ""));
                            PrintContext();
                            continue;
                        }

                        if (_command.ToLower().StartsWith("context"))
                        {
                            PrintContext();
                            continue;
                        }

                        try
                        {
                            // Evaluate
                            //var contextJson = _context.ToString();
                            //var parameters = new FormulaRuntimeParameters(contextJson);
                            //var schema = Schema.GetSchemaFromJson(contextJson);

                            //var expr = new FormulaWithParameters(_command, schema);
                            //var value = await runner.RunAsync(expr, parameters);

                            BrunoProgram ast    = ParseService.Parse(_command);
                            object       result = InterpreterService.Evaluate(ast);

                            PrintResult(result);
                            _command = null;
                        }
                        catch (BrunoRuntimeException error)
                        {
                            PrintException(error);
                        }
                    }
                    catch (Exception ex)
                    {
                        PrintException(ex);
                    }
                }
            }
        }