Ejemplo n.º 1
0
        public static CompileResult Evaluate(string code, bool autoCompleteComma = true)
        {
            if (autoCompleteComma && !code.EndsWith(";")) {
            code += ";";
            }

            var result = new CompileResult();
            result.code = code;

            // find commands at first and expand it if found.
            code = Commands.ConvertIntoCodeIfCommand(code);

            // if not match, eval the code using Mono.
            object ret = null;
            bool hasReturnValue = false;

            var originalOutput = Mono.CSharp.Evaluator.MessageOutput;
            var errorWriter = new System.IO.StringWriter();
            bool isPartial = false;
            Mono.CSharp.Evaluator.MessageOutput = errorWriter;
            try {
            isPartial = Mono.CSharp.Evaluator.Evaluate(code, out ret, out hasReturnValue) != null;
            } catch (System.Exception e) {
            errorWriter.Write(e.Message);
            }
            Mono.CSharp.Evaluator.MessageOutput = originalOutput;

            var error = errorWriter.ToString();
            if (!string.IsNullOrEmpty(error)) {
            error = error.Replace("{interactive}", "");
            var lastLineBreakPos = error.LastIndexOf('\n');
            if (lastLineBreakPos != -1) {
                error = error.Remove(lastLineBreakPos);
            }
            result.type  = CompileResult.Type.Error;
            result.error = error;
            return result;
            }
            errorWriter.Dispose();

            if (isPartial) {
            result.type = CompileResult.Type.Partial;
            return result;
            }

            result.type = CompileResult.Type.Success;
            result.value = (hasReturnValue && ret != null) ? ret : "null";
            return result;
        }