Esempio n. 1
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: kecaknoah <input file>");
                return;
            }

            var txt    = File.ReadAllText(args[0]);
            var lexer  = new KecaknoahLexer();
            var parser = new KecaknoahParser();
            var lr     = lexer.AnalyzeFromSource(txt);

            if (!lr.Success)
            {
                Console.WriteLine($"字句解析エラー ({lr.Error.Column}, {lr.Error.Line}): {lr.Error.Message}");
                Console.ReadLine();
                Environment.Exit(1);
            }
            var ast = parser.Parse(lr);

            if (!ast.Success)
            {
                Console.WriteLine($"構文解析エラー ({ast.Error.Column}, {ast.Error.Line}): {ast.Error.Message}");
                Console.ReadLine();
                Environment.Exit(1);
            }
            var prc = new KecaknoahPrecompiler();
            var src = prc.PrecompileAll(ast);

#if DEBUG
            var asm = AssembleSource(src);
            File.WriteAllLines(args[0] + ".asm", asm);
            Console.WriteLine("Assembled source was generated.");
            Console.ReadLine();
#endif
            var environment = new KecaknoahEnvironment();
            var module      = environment.CreateModule("Main");
            module.RegisterStandardLibraries();
            module.RegisterSource(src);
            var ctx   = module.CreateContext();
            var il    = module["main"];
            var kargs = new List <KecaknoahObject>();
            if (args.Length >= 2)
            {
                kargs.Add(new KecaknoahArray(args.Skip(1).Select(p => p.AsKecaknoahString())));
            }
            else
            {
                kargs.Add(new KecaknoahArray(new List <KecaknoahObject>()));
            }
            if (il != KecaknoahNil.Instance)
            {
                ctx.Initialize(il, kargs);
                while (ctx.MoveNext())
                {
                    ;
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var lexer       = new KecaknoahLexer();
            var parser      = new KecaknoahParser();
            var precompiler = new KecaknoahPrecompiler();
            var environment = new KecaknoahEnvironment();
            var sw          = new Stopwatch();
            var module      = environment.CreateModule("Himanoa");
            var ctx         = module.CreateContext();

            lexer.DefaultSourceName = "himanoa";
            var input = "";

            //var il = "";
            do
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("himanoa > ");
                input = Console.ReadLine();

                /*
                 * il = "";
                 * input = "";
                 * do
                 * {
                 *  il += input + Environment.NewLine;
                 *  input = Console.ReadLine();
                 * } while (input != "---");
                 */
                //var ret = lexer.AnalyzeFromSource(il);
                var ret = lexer.AnalyzeFromSource(input);
                if (ret.Success)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("字句解析結果--------------------");
                    Console.WriteLine(string.Join(", ", ret.Tokens.Select(p => $"{{{p.TokenString}}}")));
                    var ast = parser.ParseAsExpression(ret);
                    //var ast = parser.Parse(ret);
                    if (ast.Success)
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("抽象構文木----------------------");
                        foreach (var i in ast.RootNode.ToDebugStringList())
                        {
                            Console.WriteLine(i);
                        }

                        var il = precompiler.PrecompileExpression(ast);

                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine("KecaknoahIL---------------------");
                        foreach (var i in il.Codes)
                        {
                            Console.WriteLine(i.ToString());
                        }

                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("実行結果------------------------");
                        try
                        {
                            var sf = ctx.ExecuteWithStackFrame(il);
                            Console.WriteLine(sf.ReturningObject);

                            /*
                             * Console.ForegroundColor = ConsoleColor.DarkRed;
                             * Console.WriteLine("環境情報------------------------");
                             * foreach (var i in sf.Locals)
                             * {
                             *  Console.WriteLine($"{i.Key} : {i.Value.RawObject.ToString()}");
                             * }
                             * sw.Reset();
                             * sw.Start();
                             * for (int i = 0; i < 1048576; i++)
                             * {
                             *  var val = ctx.Execute(il);
                             * }
                             * sw.Stop();
                             * Console.WriteLine($"{sw.ElapsedMilliseconds / 1048576.0 }ms/call");
                             */
                        }
                        catch (Exception e)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("実行時エラー");
                            Console.WriteLine($"{nameof(e)}: {e.Message}");
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine($"{ast.SourceName}({ast.Error.Column}, {ast.Error.Line})\n{ast.Error.Message}");
                    }
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("字句解析エラー");
                    Console.WriteLine($"{ret.SourceName}({ret.Error.Column}, {ret.Error.Line})\n{ret.Error.Message}");
                }
                Console.WriteLine();
            } while (input != "exit");
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var lexer       = new KecaknoahLexer();
            var parser      = new KecaknoahParser();
            var precompiler = new KecaknoahPrecompiler();
            var environment = new KecaknoahEnvironment();
            var sw          = new Stopwatch();
            var module      = environment.CreateModule("Himanoa");
            var ctx         = module.CreateContext();

            lexer.DefaultSourceName = "himanoa";
            var input = "";

            //var il = "";
            do
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("himanoa > ");
                input = Console.ReadLine();

                /*
                 * il = "";
                 * input = "";
                 * do
                 * {
                 *  il += input + Environment.NewLine;
                 *  input = Console.ReadLine();
                 * } while (input != "---");
                 */
                //var ret = lexer.AnalyzeFromSource(il);
                var ret = lexer.AnalyzeFromSource(input);
                if (ret.Success)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("字句解析結果--------------------");
                    Console.WriteLine(string.Join(", ", ret.Tokens.Select(p => $"{{{p.TokenString}}}")));
                    var ast = parser.ParseAsExpression(ret);
                    //var ast = parser.Parse(ret);
                    if (ast.Success)
                    {
                        Console.ForegroundColor = ConsoleColor.Cyan;
                        Console.WriteLine("抽象構文木----------------------");
                        foreach (var i in ast.RootNode.ToDebugStringList())
                        {
                            Console.WriteLine(i);
                        }

                        var il = precompiler.PrecompileExpression(ast);

                        Console.ForegroundColor = ConsoleColor.Magenta;
                        Console.WriteLine("KecaknoahIL---------------------");
                        foreach (var i in il.Codes)
                        {
                            Console.WriteLine(i.ToString());
                        }

                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine("実行結果------------------------");
                        try
                        {
                            var sf = ctx.ExecuteExpressionIL(il);
                            Console.WriteLine(sf.ToString());
                        }
                        catch (Exception e)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.WriteLine("実行時エラー");
                            Console.WriteLine($"{nameof(e)}: {e.Message}");
                        }
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine($"{ast.SourceName}({ast.Error.Column}, {ast.Error.Line})\n{ast.Error.Message}");
                    }
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("字句解析エラー");
                    Console.WriteLine($"{ret.SourceName}({ret.Error.Column}, {ret.Error.Line})\n{ret.Error.Message}");
                }
                Console.WriteLine();
            } while (input != "exit");
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("Usage: kecaknoah <input file>");
                return;
            }

            var txt    = File.ReadAllText(args[0]);
            var lexer  = new KecaknoahLexer();
            var parser = new KecaknoahParser();
            var lr     = lexer.AnalyzeFromSource(txt);

            if (!lr.Success)
            {
                Console.WriteLine($"字句解析エラー ({lr.Error.Column}, {lr.Error.Line}): {lr.Error.Message}");
                Console.ReadLine();
                return;
            }
            var ast = parser.Parse(lr);

            if (!ast.Success)
            {
                Console.WriteLine($"構文解析エラー ({ast.Error.Column}, {ast.Error.Line}): {ast.Error.Message}");
                Console.ReadLine();
            }
            var prc = new KecaknoahPrecompiler();
            var src = prc.PrecompileAll(ast);

            var environment = new KecaknoahEnvironment();
            var module      = environment.CreateModule("Main");

            module.RegisterSource(src);
            var ctx   = module.CreateContext();
            var il    = module["main"];
            var kargs = new List <KecaknoahString>();

            if (args.Length > 2)
            {
                kargs.AddRange(args.Skip(1).Select(p => p.AsKecaknoahString()));
            }
            if (il != KecaknoahNil.Instance)
            {
                ctx.Execute(il, kargs.ToArray());
            }

            /*
             * var sw = new Stopwatch();
             * sw.Start();
             * for (int i = 0; i < 10; i++)
             * {
             *  ctx.Execute(il);
             * }
             * sw.Stop();
             * Console.WriteLine($"{sw.ElapsedMilliseconds}");
             */
            var asm = AssembleSource(src);

            File.WriteAllLines(args[0] + ".asm", asm);
            Console.ReadLine();
        }