Note that there are some explanatory texts on larger screens.

plurals
  1. POGenerating scala AST for recursive method
    text
    copied!<p>I am generating the scala AST using the following code:</p> <pre><code> val setting = new Settings(error) val reporter = new ConsoleReporter(setting, in, out) { override def displayPrompt = () } val compiler = new Global(setting, reporter) with ASTExtractor{ override def onlyPresentation = true } //setting.PhasesSetting("parser", "parserPhase") val run = new compiler.Run val sourceFiles:List[String] = List("Test.scala") run.compile(sourceFiles.toList) </code></pre> <p>I guess this is the standard code used to run the compiler in the code and generate the AST to work with. The above code worked fine for any valid scala code in Test.scala till now. When I use a recursive function in Test.scala, like </p> <blockquote> <p>def xMethod(x:Int):Int = if(x == 0) -1 else xMethod(x-1)</p> </blockquote> <p>It gives me a java.lang.NullPointerException. The top few lines of the stack trace look like this</p> <pre><code>at scala.tools.nsc.typechecker.Typers$Typer.checkNoDoubleDefsAndAddSynthetics$1(Typers.scala:2170) at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:2196) at scala.tools.nsc.typechecker.Typers$Typer.typedBlock(Typers.scala:1951) at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:3815) at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4124) at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:4177) at scala.tools.nsc.transform.TailCalls$TailCallElimination.transform(TailCalls.scala:199) </code></pre> <p>The code works fine for a method like</p> <pre><code>def aMethod(c:Int):Int = { bMethod(c) } def bMethod(x:Int):Int = aMethod(x) </code></pre> <p>Please let me know if recursive functions need any other setting.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload