frontend: implement basic scope analysis

This commit is contained in:
2023-09-14 14:16:08 -07:00
parent aadc8282a5
commit 821aa3563a
15 changed files with 172 additions and 45 deletions

View File

@ -15,7 +15,8 @@ class RootCommand : CliktCommand(
ReprintCommand(),
ParseCommand(),
AstCommand(),
AttributeCommand()
AttributeCommand(),
ScopeAnalysisCommand()
)
}

View File

@ -0,0 +1,27 @@
package gay.pizza.pork.tool
import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.parameters.arguments.argument
import gay.pizza.dough.fs.PlatformFsProvider
import gay.pizza.pork.frontend.scope.WorldScope
import gay.pizza.pork.minimal.FileTool
class ScopeAnalysisCommand : CliktCommand(help = "Run Scope Analysis", name = "scope-analysis") {
val path by argument("file")
override fun run() {
val tool = FileTool(PlatformFsProvider.resolve(path))
val world = tool.buildWorld()
val root = world.load(tool.rootImportLocator)
val scope = WorldScope(world).apply { index(root) }
val rootScope = scope.scope(root)
val visibleScopeSymbols = rootScope.findInternallyVisibleSymbols()
for (visibleScopeSymbol in visibleScopeSymbols) {
println(
"symbol ${visibleScopeSymbol.scopeSymbol.symbol.id} " +
"type=${visibleScopeSymbol.scopeSymbol.definition.type.name} " +
"internal=${visibleScopeSymbol.isInternalSymbol}"
)
}
}
}