/// <summary> /// Evaluates the provided expression or list of statements, returning the evaluated value. /// /// This method will propagate all types of errors to the caller. If multiple errors are encountered, only the /// first will be thrown. /// /// Output printed to the standard output stream will be silently discarded by this method. For tests which need /// to make assertions based on the output printed, see e.g. the <see cref="EvalReturningOutput"/> method. /// /// Note that compiler warnings will be propagated as exceptions to the caller; in other words, this resembles /// the `-Werror` flag being enabled. /// </summary> /// <param name="source">A valid Perlang program.</param> /// <returns>The result of evaluating the provided expression, or `null` if provided a list of /// statements.</returns> internal static object Eval(string source) { var interpreter = new PerlangInterpreter(AssertFailRuntimeErrorHandler, _ => { }); return(interpreter.Eval( source, AssertFailScanErrorHandler, AssertFailParseErrorHandler, AssertFailNameResolutionErrorHandler, AssertFailValidationErrorHandler, AssertFailValidationErrorHandler, AssertFailCompilerWarningHandler )); }
/// <summary> /// Evaluates the provided expression or list of statements, returning an <see cref="EvalResult{T}"/> with <see /// cref="EvalResult{T}.Value"/> set to the evaluated value. /// /// Output printed to the standard output stream will be available in <see cref="EvalResult{T}.Output"/>. /// /// This method will propagate all errors apart from <see cref="ScanError"/> to the caller. Scan errors /// will be available in the returned <see cref="EvalResult{T}.Errors"/> property. /// /// If any warnings are emitted, they will be available in the returned <see /// cref="EvalResult{T}.CompilerWarnings"/> property. "Warnings as errors" will be disabled for all warnings. /// </summary> /// <param name="source">A valid Perlang program.</param> /// <returns>An <see cref="EvalResult{T}"/> with the <see cref="EvalResult{T}.Value"/> property set to the /// result of the provided expression. If not provided a valid expression, <see cref="EvalResult{T}.Value"/> /// will be set to `null`.</returns> internal static EvalResult <ScanError> EvalWithScanErrorCatch(string source) { var result = new EvalResult <ScanError>(); var interpreter = new PerlangInterpreter(AssertFailRuntimeErrorHandler, result.OutputHandler); result.Value = interpreter.Eval( source, result.ErrorHandler, AssertFailParseErrorHandler, AssertFailNameResolutionErrorHandler, AssertFailValidationErrorHandler, AssertFailValidationErrorHandler, result.WarningHandler ); return(result); }
/// <summary> /// Evaluates the provided expression or list of statements, returning an <see cref="EvalResult{T}"/> with <see /// cref="EvalResult{T}.Value"/> set to the evaluated value. /// /// Output printed to the standard output stream will be available in <see cref="EvalResult{T}.Output"/>. /// /// This method will propagate all kinds of errors to the caller, throwing an exception on the first error /// encountered. If any warnings are emitted, they will be available in the returned <see /// cref="EvalResult{T}.CompilerWarnings"/> property. This can be seen as "warnings as errors" is disabled /// for all warnings; the caller need to explicitly check for warnings and fail if appropriate. /// </summary> /// <param name="source">A valid Perlang program.</param> /// <param name="arguments">Zero or more arguments to be passed to the program.</param> /// <returns>An <see cref="EvalResult{T}"/> with the <see cref="EvalResult{T}.Value"/> property set to the /// result of the provided expression. If not provided a valid expression, <see cref="EvalResult{T}.Value"/> /// will be set to `null`.</returns> internal static EvalResult <Exception> EvalWithResult(string source, params string[] arguments) { var result = new EvalResult <Exception>(); var interpreter = new PerlangInterpreter(AssertFailRuntimeErrorHandler, result.OutputHandler, null, arguments); result.Value = interpreter.Eval( source, AssertFailScanErrorHandler, AssertFailParseErrorHandler, AssertFailNameResolutionErrorHandler, AssertFailValidationErrorHandler, AssertFailValidationErrorHandler, result.WarningHandler ); return(result); }
internal Program( bool replMode, IEnumerable <string> arguments = null, IEnumerable <WarningType> disabledWarningsAsErrors = null, Action <string> standardOutputHandler = null, Action <RuntimeError> runtimeErrorHandler = null) { // TODO: Make these be separate handlers at some point, so the caller can separate between these types of // output. this.standardOutputHandler = standardOutputHandler ?? Console.WriteLine; this.standardErrorHandler = standardOutputHandler ?? Console.Error.WriteLine; this.disabledWarningsAsErrors = (disabledWarningsAsErrors ?? Enumerable.Empty <WarningType>()).ToHashSet(); interpreter = new PerlangInterpreter( runtimeErrorHandler ?? RuntimeError, this.standardOutputHandler, null, arguments ?? new List <string>(), replMode: replMode ); }