Ejemplo n.º 1
0
Archivo: Program.cs Proyecto: baban/lp
        static long runNode(string[] argv)
        {
            string fileName = argv[0];
            string code     = readFile(fileName);

            var       parser   = new Parser.LpGrammer();
            var       language = new LanguageData(parser);
            ScriptApp app      = new ScriptApp(language);
            var       tree     = app.Parser.Parse(code);

            Object.LpObject result = null;
            try
            {
                result = (Object.LpObject)app.Evaluate(tree);
                if (result == null)
                {
                    Debug.WriteLine("null");
                }
                else
                {
                    Console.WriteLine(result);
                    Console.WriteLine("result: {0}", result);
                    result.funcall("display", new Object.LpObject[] { }, null);
                }
            }
            catch (Error.LpError e)
            {
                Console.WriteLine(e.ToString());
            }
            Debug.WriteLine("Finish");

            return(0);
        }
Ejemplo n.º 2
0
Archivo: AstTest.cs Proyecto: baban/lp
        private LP.Object.LpObject evaluate(string code)
        {
            var parser = initParser();
            var tree   = parser.Parse(code);
            var ret    = staticApp.Evaluate(tree) as LP.Object.LpObject;

            return(ret);
        }
Ejemplo n.º 3
0
        public static IEnumerable <BoxModel> Calculate(
            ParseTree tree,
            LabRuntime runtime,
            double a,
            double b,
            double precision,
            string method,
            string subtype
            )
        {
            yield return(new LaTeXBox("Input", $"f\\left(x\\right) = {tree.ToLaTeX()}"));

            var app = new ScriptApp(runtime);

            double Function(double x)
            {
                app.Globals["x"] = x;
                return((double)app.Evaluate(tree));
            }

            var count        = 4;
            var integralSing = 1;

            if (a > b)
            {
                (a, b)       = (b, a);
                integralSing = -1;
            }

            double oldIntegral = integralSing * Approximate(method, subtype, Function, a, b, count / 2);
            double integral    = integralSing * Approximate(method, subtype, Function, a, b, count);
            double q           = (method == "simpson" ? 15 : 3);

            while (Math.Abs(integral - oldIntegral) >= precision * q)
            {
                oldIntegral = integral;
                count      *= 2;
                integral    = integralSing * Approximate(method, subtype, Function, a, b, count);
            }


            yield return(new LaTeXBox($"Result",
                                      $"N = {count}\\\\" +
                                      $"\\int_{{{a}}}^{{{b}}} f\\left(x\\right) dx \\approx {integral} \\\\" +
                                      $"\\Delta \\approx {Math.Abs(integral - oldIntegral) / q}"
                                      ));
        }
Ejemplo n.º 4
0
Archivo: Program.cs Proyecto: baban/lp
        static void consoleReadFile()
        {
            printVersion();
            // Console.WriteLine("[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin");
            // Console.WriteLine("[Type 'help' 'copyright' 'credits' or 'licence' for more information");
            Debug.WriteLine("initialize");
            var parser = new Parser.LpGrammer();
            //Console.WriteLine("initialize parser");
            var language = new LanguageData(parser);
            //Console.WriteLine("initialize language");
            ScriptApp app = new ScriptApp(language);

            Debug.WriteLine("parse");

            string line = null;

            do
            {
                Console.Write(" >> ");
                line = Console.ReadLine();
                try {
                    var tree = app.Parser.Parse(line);
                    if (tree == null)
                    {
                        Debug.WriteLine("parse error");
                    }
                    else
                    {
                        Object.LpObject result = (Object.LpObject)app.Evaluate(tree);
                        if (result == null)
                        {
                            Debug.WriteLine("null");
                        }
                        else
                        {
                            result.funcall("display", new Object.LpObject[] { }, null);
                        }
                    }
                }
                catch (Error.LpError e) {
                    printError(e);
                }
            } while (true);
        }
Ejemplo n.º 5
0
        private void SubmitButtonClicked(object sender, EventArgs e)
        {
            try
            {
                var app     = new ScriptApp(language);
                var command = this.CommandEntry.Text;

                if (string.IsNullOrEmpty(command))
                {
                    this.StdOut.Text = "null";
                    return;
                }

                int result = (int)app.Evaluate(command);
                this.StdOut.Text = result.ToString();
            }
            catch (ScriptException ex)
            {
                this.StdOut.Text = ex.Location + " " + ex.Message;
            }
        }
Ejemplo n.º 6
0
 public MtResult Evaluate(string str)
 {
     try
     {
         var ret = _scriptApp.Evaluate(str);
         if (ret == null)
         {
             throw new Exception("scriptApp.Evalute returned null.");
         }
         var wrkRet = ret as MtResult;
         if (wrkRet == null)
         {
             throw new Exception("scriptApp.Evalute returned a non-MtResult.");
         }
         return(wrkRet);
     }
     catch (Exception e)
     {
         throw new Exception("Exception evaluating script.", e);
     }
 }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Start");
            var content = "1 + 1";

            Console.WriteLine("create");
            ScriptApp app = new ScriptApp(new LanguageData(new Parser.LpGrammer()));

            Console.WriteLine("parse");
            var tree = app.Parser.Parse(content);

            string result = (string)app.Evaluate(tree);

            if (result == null)
            {
                Console.WriteLine("null");
            }
            else
            {
                Console.WriteLine(result);
                Console.WriteLine("result: {0}", result);
            }
            Console.WriteLine("Finish");
        }
Ejemplo n.º 8
0
Archivo: Program.cs Proyecto: baban/lp
        static long runTestCode()
        {
            /*
             * Console.WriteLine("benckmark:start");
             * System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
             * sw.Start();
             */
            //string code = readFile(argv[0]);
            //string code = "/* 111 */ 2";
            //string code = "1";
            //string code = ":aaaa";
            //string code = "\"Hello\"";
            //string code = "true";
            //string code = "nl";
            //string code = "/regex/";
            //string code = "[]";
            //string code = "{}";
            //string code = "do |a| end";
            //string code = "-> do |a| end";
            //string code = "a=1";
            //string code = "a=1; a";
            //string code = "let a=1; a";
            //string code = "b? = 2; b?";
            //string code = "@a = 3; @a";
            //string code = "@@a = 4; @@a";
            //string code = "let a";
            //string code = "1; 2; 3";
            //string code = "a='(1+2); ?a";
            //string code = "a='(1+2); `(1+3)";
            //string code = "a='(2+3); `(1+?a)";
            //string code = "1+a";
            //string code = "1+1";
            //string code = "2*3";
            //string code = "!true";
            //string code = "1+2*3+4";
            //string code = "1.to_s()";
            //string code = "Console";
            string code = "Console.WriteLine(\"Hello,World\")";

            //string code = "def hoge() end";
            //string code = "def hoge(a) 1; 2; 3 end";
            //string code = "def hoge(a, b) 1; 2; 3 end";
            //string code = "public def hoge(a) 1; 2; 3 end";
            //string code = "abc=1+5*5; abc";
            //string code = "def bbb(a,b,c) 1; 2; c end; bbb(1,2,3)";
            //string code = "class Aaa; 1;2;3 end";
            //string code = "public class A; 1;2;3 end";
            //string code = "public class A < B; 1;2;3 end";
            //string code = "module Aaa; 1;2;3 end";
            //string code = "public module Aaa; 1;2;3 end";
            //string code = "if true; 1 end";
            //string code = "if false; 1 else 2 end";
            //string code = "if false; 1 elsif true; 2 end";
            //string code = "case 1; end";
            //string code = "case false; else 1 end";
            //string code = "case 1; when 1; 3 end";
            Debug.WriteLine("initialize");
            var parser = new Parser.LpGrammer();
            //Console.WriteLine("initialize parser");
            var language = new LanguageData(parser);
            //Console.WriteLine("initialize language");
            ScriptApp app = new ScriptApp(language);

            Debug.WriteLine("parse");
            var tree = app.Parser.Parse(code);

            //Console.WriteLine("tree");
            //Console.WriteLine(tree);

            /*
             * if (tree.HasErrors())
             * {
             *  Console.WriteLine(tree.ParserMessages.First().Message);
             *  Console.WriteLine(tree.FileName);
             *  Console.WriteLine(tree.ParserMessages.First().Location);
             *  return 0;
             * }
             */


            Debug.WriteLine("evaluate");

            Object.LpObject result = null;
            try
            {
                result = (Object.LpObject)app.Evaluate(tree);
                if (result == null)
                {
                    Debug.WriteLine("null");
                }
                else
                {
                    Console.WriteLine(result);
                    Console.WriteLine("result: {0}", result);
                    result.funcall("display", new Object.LpObject[] { }, null);
                }
            }
            catch (Error.LpError e)
            {
                Console.WriteLine(e.ToString());
            }
            Debug.WriteLine("Finish");

            /*
             * sw.Stop();
             * Console.WriteLine(sw.Elapsed.TotalSeconds);
             * Console.WriteLine("benckmark:end");
             */
            return(0);
        }