Exemplo n.º 1
0
        public void ExecutionCanBeHaltedWhenAnErrorIsNaturallyEncountered()
        {
            var       host          = new LispHost();
            LispError capturedError = null;

            host.RootFrame.ErrorOccured += (s, e) =>
            {
                capturedError = e.Error;
            };
            var evalResult = host.Eval(@"
(defun test-method ()
    (not-a-method))
(test-method)
");

            // errors _always_ halt execution
            Assert.False(evalResult.ExecutionState.IsExecutionComplete);
            Assert.Equal("Undefined macro/function 'NOT-A-METHOD', found '<null>'", ((LispError)evalResult.LastResult).Message);
            Assert.Equal("Undefined macro/function 'NOT-A-METHOD', found '<null>'", capturedError.Message);
            Assert.True(ReferenceEquals(capturedError, evalResult.LastResult));

            // all future processing stops
            host.EvalContinue(evalResult.ExecutionState);
            Assert.False(evalResult.ExecutionState.IsExecutionComplete);
        }
Exemplo n.º 2
0
        public void ExecutionCanBeHaltedWhenAnErrorIsExplicitlyRaised()
        {
            var       host          = new LispHost();
            LispError capturedError = null;

            host.RootFrame.ErrorOccured += (s, e) =>
            {
                capturedError = e.Error;
            };
            var evalResult = host.Eval(@"
(defun test-method ()
    (error ""test error""))
(test-method)
");

            // errors _always_ halt execution
            Assert.False(evalResult.ExecutionState.IsExecutionComplete);
            Assert.Equal("test error", ((LispError)evalResult.LastResult).Message);
            Assert.Equal("test error", capturedError.Message);
            Assert.True(ReferenceEquals(capturedError, evalResult.LastResult));

            // all future processing stops
            host.EvalContinue(evalResult.ExecutionState);
            Assert.False(evalResult.ExecutionState.IsExecutionComplete);
        }
Exemplo n.º 3
0
        public LispParseResult ParseUntilSourceLocation(string code, LispSourcePosition position)
        {
            var        sourceBindings   = new LispSourceBindings(Host.CurrentPackage);
            LispObject containingObject = null;
            var        eofValue         = new LispError("EOF");
            var        textReader       = new StringReader(code);
            var        textStream       = new LispTextStream("", textReader, TextWriter.Null);

            Host.ObjectReader.SetReaderStream(textStream);
            while (true)
            {
                var result = Host.ObjectReader.Read(false, eofValue, true);
                if (ReferenceEquals(result.LastResult, eofValue))
                {
                    break;
                }

                sourceBindings.TryAddSourceBinding(result.LastResult);

                if (result.LastResult.SourceLocation.HasValue &&
                    result.LastResult.SourceLocation.Value.ContainsPosition(position))
                {
                    containingObject = result.LastResult;
                    break;
                }
            }

            var narrowestChild = containingObject?.GetNarrowestChild(position);
            var parseResult    = new LispParseResult(Host, narrowestChild, sourceBindings);

            return(parseResult);
        }
Exemplo n.º 4
0
        internal void ReportError(LispError error, LispObject parent)
        {
            if (!error.SourceLocation.HasValue)
            {
                error.SourceLocation = parent?.SourceLocation;
            }

            if (error.StackFrame is null)
            {
                error.StackFrame = StackFrame;
            }

            PushArgument(error);
        }
Exemplo n.º 5
0
        internal bool TryMatchInvocationArguments(LispObject[] argumentValues, out Tuple <LispInvocationArgument, LispObject>[] matchedArguments, out LispError error)
        {
            matchedArguments = default;
            error            = default;

            var regularArgumentIndex  = 0;
            var optionalArgumentIndex = 0;
            var argumentValueIndex    = 0;
            var matchedArgumentsList  = new List <Tuple <LispInvocationArgument, LispObject> >();
            var boundArguments        = new HashSet <string>();
            var assignedRest          = false;

            for (; argumentValueIndex < argumentValues.Length; argumentValueIndex++)
            {
                var argumentValue = argumentValues[argumentValueIndex];
                if (argumentValue is LispResolvedSymbol symbol && symbol.IsKeyword)
                {
                    if (argumentValueIndex < argumentValues.Length - 1)
                    {
                        var keywordArgumentName = symbol.Value.Substring(1);
                        if (KeywordArguments.TryGetValue(keywordArgumentName, out var keywordArgument))
                        {
                            if (!boundArguments.Add(keywordArgumentName))
                            {
                                error = new LispError($"Duplicate value for keyword argument {keywordArgumentName}");
                                return(false);
                            }

                            matchedArgumentsList.Add(Tuple.Create((LispInvocationArgument)keywordArgument, argumentValues[argumentValueIndex + 1]));
                            argumentValueIndex++;
                        }
                    }
                    else
                    {
                        error = new LispError($"Missing value for keyword argument {symbol.Value}");
                        return(false);
                    }
                }
Exemplo n.º 6
0
 public LispErrorOccuredEventArgs(LispError error, LispStackFrame stackFrame)
 {
     Error      = error;
     StackFrame = stackFrame;
 }