示例#1
0
        static void Main(string[] args)
        {
            var secure = false;
            var legacy = false;

            foreach (var arg in args)
            {
                if (arg == "--secure")
                {
                    secure = true;
                }
                if (arg == "--legacy")
                {
                    legacy = true;
                }
            }
            var interpreter = new Interpreter(secure, legacy);

            interpreter.SetStandardInput(Console.In);
            interpreter.SetStandardOutput(Console.Out);
            var modulepath = new ValueList();

            foreach (var arg in args)
            {
                if (arg.StartsWith("-I"))
                {
                    modulepath.AddItem(new ValueString(arg.Substring(2)));
                }
            }
            modulepath.MakeReadonly();
            interpreter.GetEnvironment().Put("checkerlang_module_path", modulepath);
            foreach (var arg in args)
            {
                if (arg.StartsWith("--"))
                {
                    continue;
                }
                if (arg.StartsWith("-I"))
                {
                    continue;
                }
                interpreter.Interpret(File.ReadAllText(arg, Encoding.UTF8), arg);
            }
            Console.Write("> ");
            var line = Console.ReadLine();

            while (line != "exit")
            {
                try
                {
                    Parser.Parse(line, "{stdin}");
                }
                catch (SyntaxError e)
                {
                    if (e.Message.StartsWith("Unexpected end of input"))
                    {
                        Console.Write("+ ");
                        line += "\n" + Console.ReadLine();
                        continue;
                    }
                }
                catch (Exception)
                {
                    Console.Write("+ ");
                    line += "\n" + Console.ReadLine();
                    continue;
                }

                if (!line.Equals(";"))
                {
                    try
                    {
                        var value = interpreter.Interpret(line, "{stdin}");
                        if (value.IsReturn())
                        {
                            value = value.AsReturn().value;
                        }
                        if (value != ValueNull.NULL)
                        {
                            Console.WriteLine(value);
                        }
                    }
                    catch (ControlErrorException e)
                    {
                        Console.WriteLine("ERR: " + e.GetErrorValue().AsString().GetValue() + " (Line " + e.GetPos() + ")");
                        Console.WriteLine(e.GetStacktrace().ToString());
                    }
                    catch (SyntaxError e)
                    {
                        Console.WriteLine(e.Message + (e.GetPos() != null ? " (Line " + e.GetPos() + ")" : ""));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                }

                Console.Write("> ");
                line = Console.ReadLine();
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    Console.Error.WriteLine("Syntax: ckl-run-dotnet [--secure] [--legacy] [-I<moduledir>] scriptname [scriptargs...]");
                    System.Environment.Exit(1);
                }

                var    secure     = false;
                var    legacy     = false;
                string scriptname = null;
                var    scriptargs = new ValueList();
                var    modulepath = new ValueList();

                var in_options = true;
                foreach (var arg in args)
                {
                    if (in_options)
                    {
                        if (arg == "--secure")
                        {
                            secure = true;
                        }
                        else if (arg == "--legacy")
                        {
                            legacy = true;
                        }
                        else if (arg.StartsWith("-I"))
                        {
                            modulepath.AddItem(new ValueString(arg.Substring(2)));
                        }
                        else if (arg.StartsWith("--"))
                        {
                            Console.Error.WriteLine("Unknown option " + arg);
                            System.Environment.Exit(1);
                        }
                        else
                        {
                            in_options = false;
                            scriptname = arg;
                        }
                    }
                    else
                    {
                        scriptargs.AddItem(new ValueString(arg));
                    }
                }
                modulepath.MakeReadonly();

                if (scriptname == null)
                {
                    Console.Error.WriteLine("Syntax: ckl-run-dotnet [--secure] [--legacy] [-I<moduledir>] scriptname [scriptargs...]");
                    System.Environment.Exit(1);
                }

                if (!File.Exists(scriptname))
                {
                    Console.Error.WriteLine("File not found " + scriptname);
                    System.Environment.Exit(1);
                }

                var interpreter = new Interpreter(secure, legacy);
                interpreter.SetStandardInput(Console.In);
                interpreter.SetStandardOutput(Console.Out);
                interpreter.GetEnvironment().Put("scriptname", new ValueString(scriptname));
                interpreter.GetEnvironment().Put("args", scriptargs);
                interpreter.GetEnvironment().Put("checkerlang_module_path", modulepath);
                var value = interpreter.Interpret(File.ReadAllText(scriptname, Encoding.UTF8), scriptname);
                if (value.IsReturn())
                {
                    value = value.AsReturn().value;
                }
                if (value != ValueNull.NULL)
                {
                    Console.Out.WriteLine(value);
                }
            } catch (ControlErrorException e) {
                Console.Out.WriteLine("ERR: " + e.GetErrorValue().AsString().GetValue() + " (Line " + e.GetPos() + ")");
                Console.Out.WriteLine(e.GetStacktrace().ToString());
            } catch (SyntaxError e) {
                Console.Out.WriteLine(e.Message + (e.GetPos() != null ? " (Line " + e.GetPos() + ")" : ""));
            } catch (Exception e) {
                Console.Out.WriteLine(e.Message);
            }
        }