fix globalnamespace after change

This commit is contained in:
Irmen de Jong 2018-09-05 00:11:52 +02:00
parent d9865a4b97
commit a81c1485d7
4 changed files with 14 additions and 16 deletions

View File

@ -25,21 +25,20 @@ fun main(args: Array<String>) {
val filepath = Paths.get(args[0]).normalize()
val moduleAst = importModule(filepath)
moduleAst.linkParents()
val globalNamespace = moduleAst.definingScope()
// globalNamespace.debugPrint()
val globalNameSpaceBeforeOptimization = moduleAst.definingScope()
// perform syntax checks and optimizations
moduleAst.checkIdentifiers()
moduleAst.optimizeExpressions(globalNamespace)
moduleAst.checkValid(globalNamespace) // check if tree is valid
moduleAst.optimizeExpressions(globalNameSpaceBeforeOptimization)
moduleAst.checkValid(globalNameSpaceBeforeOptimization) // check if tree is valid
val allScopedSymbolDefinitions = moduleAst.checkIdentifiers()
moduleAst.optimizeStatements(globalNamespace, allScopedSymbolDefinitions)
moduleAst.optimizeStatements(globalNameSpaceBeforeOptimization, allScopedSymbolDefinitions)
val globalNamespaceAfterOptimize = moduleAst.definingScope() // it could have changed in the meantime
moduleAst.checkValid(globalNamespaceAfterOptimize) // check if final tree is valid
moduleAst.checkRecursion() // check if there are recursive subroutine calls
// globalNamespaceAfterOptimize.debugPrint()
// determine special compiler options

View File

@ -347,11 +347,7 @@ class Module(override val name: String,
processor.process(this)
}
private val theGlobalNamespace by lazy {
GlobalNamespace("<<<global>>>", statements, position)
}
override fun definingScope(): INameScope = theGlobalNamespace
override fun definingScope(): INameScope = GlobalNamespace("<<<global>>>", statements, position)
override fun usedNames(): Set<String> = throw NotImplementedError("not implemented on sub-scopes")
override fun registerUsedName(name: String) = throw NotImplementedError("not implemented on sub-scopes")

View File

@ -14,10 +14,10 @@ fun Module.optimizeExpressions(globalNamespace: INameScope) {
}
if(optimizer.optimizationsDone==0)
println("[${this.name}] 0 optimizations performed")
println("[${this.name}] 0 expression optimizations performed")
while(optimizer.errors.isEmpty() && optimizer.optimizationsDone>0) {
println("[${this.name}] ${optimizer.optimizationsDone} optimizations performed")
println("[${this.name}] ${optimizer.optimizationsDone} expression optimizations performed")
optimizer.optimizationsDone = 0
this.process(optimizer)
}

View File

@ -8,10 +8,10 @@ fun Module.optimizeStatements(globalNamespace: INameScope, allScopedSymbolDefini
this.process(optimizer)
optimizer.removeUnusedNodes(globalNamespace.usedNames(), allScopedSymbolDefinitions)
if(optimizer.optimizationsDone==0)
println("[${this.name}] 0 optimizations performed")
println("[${this.name}] 0 statement optimizations performed")
while(optimizer.optimizationsDone>0) {
println("[${this.name}] ${optimizer.optimizationsDone} optimizations performed")
println("[${this.name}] ${optimizer.optimizationsDone} statement optimizations performed")
optimizer.reset()
this.process(optimizer)
}
@ -91,7 +91,10 @@ class StatementOptimizer(private val globalNamespace: INameScope) : IAstProcesso
if(!usedNames.contains(name)) {
val parentScope = value.parent as INameScope
val localname = name.substringAfterLast(".")
println("${value.position} Warning: ${value::class.simpleName} '$localname' is never used")
// printing every possible node that is removed can result in many dozens of warnings.
// we chose to just print the blocks that aren't used.
if(value is Block)
println("${value.position} Info: block '$localname' is never used")
parentScope.removeStatement(value)
optimizationsDone++
}