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)
        {
            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();
        }