コード例 #1
0
        public static async Task Run(Options opt, CancellationToken token)
        {
            var global      = opt.BuildGlobal();
            var builder     = new ScriptBuilder();
            var blockParser = new BlockParser();

            blockParser.Parse(opt.Code);

            // BEGINブロック
            foreach (var begin in blockParser.BeginBlock)
            {
                builder.AddCodeBlock(begin, ScriptBuilder.BlockType.Begin);
            }

            // ENDブロック
            foreach (var end in blockParser.EndBlock)
            {
                builder.AddCodeBlock(end, ScriptBuilder.BlockType.End);
            }

            // Mainブロック
            foreach (var main in blockParser.MainBlock)
            {
                builder.AddCodeBlock(main);
            }


            if (!string.IsNullOrEmpty(opt.Imports))
            {
                var imports = opt.Imports.Split(',', StringSplitOptions.RemoveEmptyEntries);
                if (imports.Any())
                {
                    builder.AddImports(imports);
                }
            }

            var(script, diagnostics) = builder.Build(token);
            if (opt.ShowGeneratedCode)
            {
                Logger.LogInformation($"Generated Code\n{builder.GeneratedCode}");
            }

            foreach (var diagnostic in diagnostics)
            {
                if (diagnostic.WarningLevel == 0)
                {
                    throw new Exception(diagnostic.ToString());
                }
                Logger.LogWarning(diagnostic.ToString());
            }

            var state = await script.RunAsync(global, null, token);

            if (state.Exception != null)
            {
                throw state.Exception;
            }
        }