Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归方法的scala-AST生成_Scala_Abstract Syntax Tree - Fatal编程技术网

递归方法的scala-AST生成

递归方法的scala-AST生成,scala,abstract-syntax-tree,Scala,Abstract Syntax Tree,我使用以下代码生成scala AST: 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 onl

我使用以下代码生成scala AST:

  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)
我猜这是用于在代码中运行编译器并生成要使用的AST的标准代码。到目前为止,上面的代码对于Test.scala中的任何有效scala代码都运行良好。当我在Test.scala中使用递归函数时,比如

def xMethod(x:Int):Int=if(x==0)-1 else xMethod(x-1)

它给了我一个java.lang.NullPointerException。堆栈跟踪的顶部几行如下所示

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)
该代码适用于以下方法:

def aMethod(c:Int):Int = { bMethod(c) }
def bMethod(x:Int):Int = aMethod(x)

如果递归函数需要任何其他设置,请告诉我。

我无法告诉您哪里做错了,但我使用
编译器.typedTree
在我的项目中获取AST。也许这对你也有用

有关更多上下文,请参阅