提交 360f833e 编写于 作者: M Miguel de Icaza

[repl] Fix running specified scripts from command line, allow #! syntax, add print command

We were calling the Reset () method after we had initialized the driver
with the new command line arguments, clearing the list of files to execute
before we executed them.

Added support for #! in the csharp repl, and added the print() command to
the InteractiveBase

Add a better way of dealing with -e, without adding hacks to the core that wont work
in the future
上级 1b8d34db
......@@ -42,21 +42,16 @@ namespace Mono.CSharp
static readonly char[] argument_value_separator = new char [] { ';', ',' };
#if !STATIC
// The expression to evaluate if -e is passed to the REPL
public static string EvalExpression;
#endif
private Driver (CompilerContext ctx)
{
this.ctx = ctx;
}
public static Driver Create (string[] args, bool require_files, ReportPrinter printer)
public static Driver Create (string[] args, bool require_files, Func<string [], int, int> unknown_option_parser, ReportPrinter printer)
{
Driver d = new Driver (new CompilerContext (new Report (printer)));
if (!d.ParseArguments (args, require_files))
if (!d.ParseArguments (args, require_files, unknown_option_parser))
return null;
return d;
......@@ -238,7 +233,7 @@ namespace Mono.CSharp
{
Location.InEmacs = Environment.GetEnvironmentVariable ("EMACS") == "t";
var crp = new ConsoleReportPrinter ();
Driver d = Driver.Create (args, true, crp);
Driver d = Driver.Create (args, true, null, crp);
if (d == null)
return 1;
......@@ -345,7 +340,7 @@ namespace Mono.CSharp
Location.AddFile (Report, f);
}
bool ParseArguments (string[] args, bool require_files)
bool ParseArguments (string[] args, bool require_files, Func<string [], int, int> unknown_option_parser)
{
List<string> response_file_list = null;
bool parsing_options = true;
......@@ -397,6 +392,14 @@ namespace Mono.CSharp
if (CSCParseOption (csc_opt, ref args))
continue;
if (unknown_option_parser != null){
var ret = unknown_option_parser (args, i);
if (ret != -1){
i = ret;
return true;
}
}
Error_WrongOption (arg);
return false;
}
......@@ -753,17 +756,6 @@ namespace Mono.CSharp
RootContext.LoadDefaultReferences = false;
return true;
#if !STATIC
case "-e":
if ((i + 1) >= args.Length){
Report.Error (
1900,
"-e requires an expression");
Environment.Exit (1);
}
EvalExpression = args [++i];
return true;
#endif
default:
if (arg.StartsWith ("--fatal")){
if (arg.StartsWith ("--fatal=")){
......@@ -1632,7 +1624,7 @@ namespace Mono.CSharp
{
try {
StreamReportPrinter srp = new StreamReportPrinter (error);
Driver d = Driver.Create (args, true, srp);
Driver d = Driver.Create (args, true, null, srp);
if (d == null)
return false;
......
......@@ -92,6 +92,11 @@ namespace Mono.CSharp
return ctx.Report.SetPrinter (report_printer);
}
public static string [] InitAndGetStartupFiles (string [] args)
{
return InitAndGetStartupFiles (args, null);
}
/// <summary>
/// Optional initialization for the Evaluator.
/// </summary>
......@@ -108,8 +113,13 @@ namespace Mono.CSharp
///
/// This method return an array of strings that contains any
/// files that were specified in `args'.
///
/// If the unknownOptionParser is not null, this function is invoked
/// with the current args array and the index of the option that is not
/// known. A value of true means that the value was processed, otherwise
/// it will be reported as an error
/// </remarks>
public static string [] InitAndGetStartupFiles (string [] args)
public static string [] InitAndGetStartupFiles (string [] args, Func<string [], int, int> unknownOptionParser)
{
lock (evaluator_lock){
if (inited)
......@@ -117,7 +127,7 @@ namespace Mono.CSharp
CompilerCallableEntryPoint.Reset ();
var crp = new ConsoleReportPrinter ();
driver = Driver.Create (args, false, crp);
driver = Driver.Create (args, false, unknownOptionParser, crp);
if (driver == null)
throw new Exception ("Failed to create compiler driver with the given arguments");
......@@ -148,12 +158,6 @@ namespace Mono.CSharp
}
}
public static string StartupEvalExpression {
get {
return Driver.EvalExpression;
}
}
static void Init ()
{
Init (new string [0]);
......
......@@ -32,6 +32,7 @@ using Mono.CSharp;
namespace Mono {
public class Driver {
public static string StartupEvalExpression;
static int Main (string [] args)
{
......@@ -53,7 +54,7 @@ namespace Mono {
{
string[] startup_files;
try {
startup_files = Evaluator.InitAndGetStartupFiles (args);
startup_files = Evaluator.InitAndGetStartupFiles (args, HandleExtraArguments);
Evaluator.DescribeTypeExpressions = true;
Evaluator.SetInteractiveBaseClass (typeof (InteractiveBaseShell));
} catch {
......@@ -61,6 +62,16 @@ namespace Mono {
}
return new CSharpShell ().Run (startup_files);
}
static int HandleExtraArguments (string [] args, int pos)
{
if (args [pos] == "-e" && pos+1 < args.Length){
StartupEvalExpression = args [pos+1];
return pos+1;
}
return -1;
}
}
public class InteractiveBaseShell : InteractiveBase {
......@@ -201,11 +212,11 @@ namespace Mono {
void ExecuteSources (IEnumerable<string> sources, bool ignore_errors)
{
bool first = true;
foreach (string file in sources){
try {
try {
bool first = true;
using (System.IO.StreamReader r = System.IO.File.OpenText (file)){
ReadEvalPrintLoopWith (p => {
var line = r.ReadLine ();
......@@ -273,23 +284,18 @@ namespace Mono {
public int ReadEvalPrintLoop ()
{
if (startup_files != null && startup_files.Length == 0)
InitTerminal (startup_files.Length == 0 && Evaluator.StartupEvalExpression == null);
InitTerminal (startup_files.Length == 0 && Driver.StartupEvalExpression == null);
InitializeUsing ();
LoadStartupFiles ();
//
// Interactive or startup files provided?
//
string startup_expression = Evaluator.StartupEvalExpression;
if (startup_files.Length != 0)
ExecuteSources (startup_files, false);
else if (Evaluator.StartupEvalExpression != null){
else if (Driver.StartupEvalExpression != null){
ReadEvalPrintLoopWith (p => {
var ret = startup_expression;
startup_expression = null;
var ret = Driver.StartupEvalExpression;
Driver.StartupEvalExpression = null;
return ret;
});
} else
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册