Grammar allows empty return but parser and compiler throw exceptions
Closed this issue · 0 comments
wixette commented
How to repro:
SeedLang.Shell 0.2.0-preview20220306085334
Copyright 2021-2022 The SeedV Lab.
>>> def foo():
... return
...
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at SeedLang.X.SeedPythonVisitor.VisitReturn_stmt(Return_stmtContext context) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/X/SeedPythonVisitor.cs:line 171
at SeedLang.X.SeedPythonParser.Return_stmtContext.Accept[TResult](IParseTreeVisitor`1 visitor) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/obj/Debug/net6.0/SeedPythonParser.cs:line 1505
...
The following code change fixes the issue only at the parser level:
public override AstNode VisitReturn_stmt(
[NotNull] SeedPythonParser.Return_stmtContext context) {
return _helper.BuildReturn(context.RETURN().Symbol, context.expressions()?.expression(),
context.expressions()?.COMMA(), this);
}
With this fix, the same code results in an exception at the compiler level:
SeedLang.Shell 0.2.0-preview20220306085050
Copyright 2021-2022 The SeedV Lab.
>>> def foo():
... return
...
---------- Source ----------
1 def foo():
2 return
---------- Run ----------
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at SeedLang.Interpreter.Compiler.Visit(ReturnStatement return) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/Interpreter/Compiler.cs:line 307
at SeedLang.Ast.AstWalker.Visit(Statement statement) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/Ast/AstWalker.cs:line 107
at SeedLang.Interpreter.Compiler.Visit(FuncDefStatement funcDef) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/Interpreter/Compiler.cs:line 264
at SeedLang.Ast.AstWalker.Visit(Statement statement) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/Ast/AstWalker.cs:line 98
at SeedLang.Ast.AstWalker.Visit(AstNode node) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/Ast/AstWalker.cs:line 27
at SeedLang.Interpreter.Compiler.Compile(AstNode node, GlobalEnvironment env, RunMode runMode) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/Interpreter/Compiler.cs:line 49
at SeedLang.Runtime.Executor.Run(String source, String module, SeedXLanguage language, RunType runType, RunMode runMode, DiagnosticCollection collection) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang/Runtime/Executor.cs:line 94
at SeedLang.Shell.Repl.Execute() in /Users/ygwang/src/SeedLang/csharp/src/SeedLang.Shell/Repl.cs:line 49
at SeedLang.Shell.Program.Run(Options options) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang.Shell/Program.cs:line 68
at SeedLang.Shell.Program.<>c__DisplayClass1_0.<Main>b__3(Options options) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang.Shell/Program.cs:line 57
at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)
at SeedLang.Shell.Program.Main(String[] args) in /Users/ygwang/src/SeedLang/csharp/src/SeedLang.Shell/Program.cs:line 53