コード例 #1
0
        public static void Compile(
            [Required(Description = "path to Shakespeare source code to compile")]
            string filename,
            [Optional("C", "c", Description = "Code generator: 'C' for C code, 'CS' for C#, 'I' for MSIL/EXE")]
            string compiler,
            [Optional(true, Description = "Include Debug information in MSIL")]
            bool debug
            )
        {
            Console.WriteLine("ShakesCL - Command-line compiler interface for Shakespeare programming lanaguage");
            Console.WriteLine("Copyright 2013, James M. Curran, Novel Theory Software");

            var comp    = CompilerLoader.Load(compiler);
            var grammar = new ShakespeareGrammar(comp);
            var parser  = new Parser(grammar);
            var text    = File.ReadAllText(filename);
            var tree    = parser.Parse(text, filename);

            var app    = new ScriptApp(parser.Language);
            var thread = new ScriptThread(app);
            var param  = new CompilerParams()
            {
                OutFolder   = @"C:\Users\User\Projects\Shakespeare\Executables\Debug\",
                SrcFileName = filename,
                Debug       = debug
            };

            comp.PrepareScope(thread, param);
            var output = (tree.Root.AstNode as AstNode).Evaluate(thread);

            Console.WriteLine(output);
        }
コード例 #2
0
ファイル: Script.cs プロジェクト: MisterDr/FOCommon
 public ScriptDeclaration(string Name, ScriptApp App, ScriptType Type, bool Enabled)
 {
     this.Name = Name;
     this.App = App;
     this.Type = Type;
     this.Enabled = Enabled;
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: 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);
        }
コード例 #4
0
        protected static MtCompiler InternCreateScriptApp(Stream output, Stream debugStream)
        {
            var grammar = new MtGrammar();
            var lang    = new LanguageData(grammar);
            var parser  = new Parser(grammar);
            var runtime = grammar.CreateRuntime(lang) as MultiTasksRuntime;

            if (output != null)
            {
                runtime.OutputStream = output;
            }
#if DEBUG && !SILVERLIGHT
            if (debugStream != null)
            {
                // Add as a listener to debug
                var listener = new TextWriterTraceListener(debugStream);
                Debug.Listeners.Add(listener);
            }
#endif
            var app = new ScriptApp(runtime);

            // Add constants
            app.Globals.Add("TRUE", MtResult.True);
            app.Globals.Add("FALSE", MtResult.False);

#if DEBUG && !SILVERLIGHT
            MultiTasksRuntime.DebugDisplayInfo();
#endif

            return(new MtCompiler(app));
        }
コード例 #5
0
        }                                         // main, *.dll etc.

        public ScriptDeclaration(string Name, ScriptApp App, ScriptType Type, bool Enabled)
        {
            this.Name    = Name;
            this.App     = App;
            this.Type    = Type;
            this.Enabled = Enabled;
        }
コード例 #6
0
 //Default constructor, creates default evaluator 
 public ExpressionEvaluator(ExpressionEvaluatorGrammar grammar) {
   Grammar = grammar;
   Language = new LanguageData(Grammar);
   Parser = new Parser(Language);
   Runtime = Grammar.CreateRuntime(Language);
   App = new ScriptApp(Runtime);
 }
コード例 #7
0
 public Evaluator(CalcGrammar grammar)
 {
     Grammar  = grammar;
     Language = new LanguageData(grammar);
     Parser   = new Parser(Language);
     Runtime  = Grammar.CreateRuntime(Language);
     App      = new ScriptApp(Runtime);
 }
コード例 #8
0
 //Default constructor, creates default evaluator
 public ReportingExpressionEvaluator(InterpretedLanguageGrammar grammar)
 {
     Grammar  = grammar;
     Language = new LanguageData(Grammar);
     Parser   = new Parser(Language);
     Runtime  = Grammar.CreateRuntime(Language);
     App      = new ScriptApp(Runtime);
 }
コード例 #9
0
 //Default constructor, creates default evaluator
 public ExpressionEvaluator(ExpressionEvaluatorGrammar grammar)
 {
     Grammar  = grammar;
     Language = new LanguageData(Grammar);
     Parser   = new Parser(Language);
     Runtime  = Grammar.CreateRuntime(Language);
     App      = new ScriptApp(Runtime);
 }
コード例 #10
0
        public FourthLab()
        {
            var expressionGrammar = new LabGrammar();

            _languageData = new LanguageData(expressionGrammar);
            var labRuntime = new LabRuntime(_languageData);

            _scriptApp = new ScriptApp(labRuntime);
        }
コード例 #11
0
        public void Reset()
        {
            var rethrowBack = App.RethrowExceptions;
            var modeBack    = App.ParserMode;

            Runtime = Grammar.CreateRuntime(Language);
            App     = new ScriptApp(Runtime);
            App.RethrowExceptions = rethrowBack;
            App.ParserMode        = modeBack;
        }
コード例 #12
0
        public ThirdLabModel()
        {
            _random = new Random(Seed);

            var expressionGrammar = new LabGrammar();

            _languageData = new LanguageData(expressionGrammar);
            var labRuntime = new LabRuntime(_languageData);

            _scriptApp = new ScriptApp(labRuntime);
        }
コード例 #13
0
ファイル: AstTest.cs プロジェクト: baban/lp
        private Parser initParser()
        {
            if (staticParser != null)
            {
                return(staticParser);
            }

            var       parser   = new LP.Parser.LpGrammer();
            var       language = new LanguageData(parser);
            ScriptApp app      = new ScriptApp(language);

            staticApp    = app;
            staticParser = app.Parser;
            return(staticParser);
        }
コード例 #14
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}"
                                      ));
        }
コード例 #15
0
ファイル: Program.cs プロジェクト: 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);
        }
コード例 #16
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;
            }
        }
コード例 #17
0
ファイル: Program.cs プロジェクト: baban/lp
        static void Main(string[] args)
        {
            //runTestCode();
            //return;

            if (args.Length == 0)
            {
                sysInit("", args, 0);
                consoleReadFile();
            }
            var options = new Options();
            //コマンドライン引数を解析する
            bool isSuccess = CommandLine.Parser.Default.ParseArguments(args, options);

            if (isSuccess)
            {
                if (options.Verbose)
                {
                    printVersion();
                    return;
                }

                if (options.Evaluate != null)
                {
                    sysInit("", args, 0);
                    Debug.WriteLine("initialize");
                    var       parser   = new Parser.LpGrammer();
                    var       language = new LanguageData(parser);
                    ScriptApp app      = new ScriptApp(language);
                    Debug.WriteLine("parse");
                    return;
                }

                if (options.InputFiles.Count > 0)
                {
                    sysInit("", args, 0);
                    runNode(options.InputFiles.ToArray());
                }
            }
        }
コード例 #18
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");
        }
コード例 #19
0
        public bool Parse()
        {
            if (!File.Exists(filename))
            {
                return(false);
            }

            ScriptApp  App  = ScriptApp.Server;
            ScriptType Type = ScriptType.Module;

            Lines.Clear();
            Lines.AddRange(File.ReadAllLines(filename));
            foreach (string Line in Lines)
            {
                if (string.IsNullOrEmpty(Line?.Trim() ?? string.Empty) || Line.TrimStart(new char[] { ' ', '\t' }).StartsWith("#") || Line.TrimStart(new char[] { ' ', '\t' }).StartsWith(";"))
                {
                    continue;
                }

                if (Line[0] == '[')
                {
                    if (Line.ToLower().Contains("server"))
                    {
                        App = ScriptApp.Server;
                    }
                    else if (Line.ToLower().Contains("client"))
                    {
                        App = ScriptApp.Server;
                    }
                    else if (Line.ToLower().Contains("mapper"))
                    {
                        App = ScriptApp.Server;
                    }
                    else
                    {
                        return(false);
                    }

                    if (Line.ToLower().Contains("scripts"))
                    {
                        Type = ScriptType.Module;
                    }
                    else if (Line.ToLower().Contains("binds"))
                    {
                        Type = ScriptType.Bind;
                    }
                    else
                    {
                        return(false);
                    }

                    continue;
                }

                string[] row        = Line.Split('=');
                string[] parameters = row[1].Split(new char[] { '#', ';' });
                string   param      = String.Join(" ", parameters[0].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries));

                bool   Enabled;
                string Reserved = "";

                Enabled = Type == ScriptType.Bind || param.ToLower() == "load";

                string        Name = row[0].Trim(new char[] { ' ', '\t' });
                List <string> Desc = new List <string>();

                if (Type == ScriptType.Bind)
                {
                    Reserved = param;
                }

                ScriptDeclaration script = new ScriptDeclaration(Name, App, Type, Enabled);

                script.Description   = (parameters.Length > 1) ? parameters[1].Trim(new char[] { ' ', '\t' }) : "";
                script.ReservedPlace = Reserved;
                Scripts.Add(script);
            }
            _IsParsed = true;
            return(true);
        }
コード例 #20
0
 protected MtCompiler(ScriptApp scriptApp)
 {
     _scriptApp = scriptApp;
 }
コード例 #21
0
ファイル: Program.cs プロジェクト: 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);
        }