removed unneeded scope param from lookup()

This commit is contained in:
Irmen de Jong 2021-10-28 23:51:40 +02:00
parent 2bf4017f2b
commit 6cb8b3b5cd
5 changed files with 19 additions and 21 deletions

View File

@ -456,7 +456,7 @@ internal class AstChecker(private val program: Program,
val targetIdentifier = assignTarget.identifier
if (targetIdentifier != null) {
val targetName = targetIdentifier.nameInSource
when (val targetSymbol = assignment.definingScope.lookup(targetName, assignment.definingScope)) {
when (val targetSymbol = assignment.definingScope.lookup(targetName)) {
null -> {
errors.err("undefined symbol: ${targetIdentifier.nameInSource.joinToString(".")}", targetIdentifier.position)
return
@ -1073,7 +1073,7 @@ internal class AstChecker(private val program: Program,
override fun visit(postIncrDecr: PostIncrDecr) {
if(postIncrDecr.target.identifier != null) {
val targetName = postIncrDecr.target.identifier!!.nameInSource
val target = postIncrDecr.definingScope.lookup(targetName, postIncrDecr.definingScope)
val target = postIncrDecr.definingScope.lookup(targetName)
if(target==null) {
val symbol = postIncrDecr.target.identifier!!
errors.err("undefined symbol: ${symbol.nameInSource.joinToString(".")}", symbol.position)

View File

@ -42,7 +42,7 @@ internal class AstIdentifiersChecker(private val program: Program, private val e
if(decl.name in compTarget.machine.opcodeNames)
errors.err("can't use a cpu opcode name as a symbol: '${decl.name}'", decl.position)
val existing = decl.definingScope.lookup(listOf(decl.name), decl.definingScope)
val existing = decl.definingScope.lookup(listOf(decl.name))
if (existing != null && existing !== decl)
nameError(decl.name, decl.position, existing)
@ -65,7 +65,7 @@ internal class AstIdentifiersChecker(private val program: Program, private val e
// if (subroutine.parameters.any { it.name in BuiltinFunctions })
// checkResult.add(NameError("builtin function name cannot be used as parameter", subroutine.position))
val existing = subroutine.lookup(listOf(subroutine.name), subroutine)
val existing = subroutine.lookup(listOf(subroutine.name))
if (existing != null && existing !== subroutine)
nameError(subroutine.name, subroutine.position, existing)

View File

@ -162,24 +162,22 @@ interface IStatementContainer {
interface INameScope: IStatementContainer, INamedStatement {
fun subScope(name: String): INameScope? = statements.firstOrNull { it is INameScope && it.name==name } as? INameScope
// TODO is localscope really needed?
// TODO return INamedStatement instead?
fun lookup(scopedName: List<String>, localScope: INameScope) : Statement? {
fun lookup(scopedName: List<String>) : Statement? {
return if(scopedName.size>1)
lookupQualified(scopedName, localScope)
lookupQualified(scopedName)
else {
lookupUnqualified(scopedName[0], localScope)
lookupUnqualified(scopedName[0])
}
}
private fun lookupQualified(scopedName: List<String>, startScope: INameScope): Statement? {
private fun lookupQualified(scopedName: List<String>): Statement? {
// a scoped name refers to a name in another namespace.
// look "up" from our current scope to search for the correct one.
val localScope = this.subScope(scopedName[0])
if(localScope!=null)
return resolveLocally(localScope, scopedName.drop(1))
var statementScope = startScope
var statementScope = this
while(statementScope !is GlobalNamespace) {
if(statementScope !is Module && statementScope.name==scopedName[0]) {
return resolveLocally(statementScope, scopedName.drop(1))
@ -189,10 +187,10 @@ interface INameScope: IStatementContainer, INamedStatement {
}
// not found, try again but now assume it's a globally scoped name starting with the name of a block
for(module in (startScope as Node).definingModule.program.modules) {
for(module in (this as Node).definingModule.program.modules) {
module.statements.forEach {
if(it is Block && it.name==scopedName[0])
return it.lookup(scopedName, it)
return it.lookup(scopedName)
}
}
return null
@ -208,18 +206,18 @@ interface INameScope: IStatementContainer, INamedStatement {
return scope!!.searchLabelOrVariableNotSubscoped(name.last(), true)
}
private fun lookupUnqualified(name: String, startScope: INameScope): Statement? {
val builtinFunctionsNames = (startScope as Node).definingModule.program.builtinFunctions.names
private fun lookupUnqualified(name: String): Statement? {
val builtinFunctionsNames = (this as Node).definingModule.program.builtinFunctions.names
if(name in builtinFunctionsNames) {
// builtin functions always exist, return a dummy placeholder for them
val builtinPlaceholder = Label("builtin::$name", startScope.position)
val builtinPlaceholder = Label("builtin::$name", this.position)
builtinPlaceholder.parent = ParentSentinel
return builtinPlaceholder
}
// search for the unqualified name in the current scope (and possibly in any anonymousscopes it may contain)
// if it's not found there, jump up one higher in the namespaces and try again.
var statementScope = startScope
var statementScope = this
while(statementScope !is GlobalNamespace) {
val symbol = statementScope.searchLabelOrVariableNotSubscoped(name, true)
if(symbol!=null)
@ -449,7 +447,7 @@ class GlobalNamespace(val modules: Iterable<Module>): Node, INameScope {
override val statements = mutableListOf<Statement>() // not used
override var parent: Node = ParentSentinel
override fun lookup(scopedName: List<String>, localScope: INameScope): Statement? {
override fun lookup(scopedName: List<String>): Statement? {
throw NotImplementedError("use lookup on actual ast node instead")
}

View File

@ -730,7 +730,7 @@ data class IdentifierReference(val nameInSource: List<String>, override val posi
if(nameInSource.size==1 && nameInSource[0] in program.builtinFunctions.names)
BuiltinFunctionStatementPlaceholder(nameInSource[0], position, parent)
else
definingScope.lookup(nameInSource, definingScope)
definingScope.lookup(nameInSource)
fun targetVarDecl(program: Program): VarDecl? = targetStatement(program) as? VarDecl
fun targetSubroutine(program: Program): Subroutine? = targetStatement(program) as? Subroutine
@ -747,7 +747,7 @@ data class IdentifierReference(val nameInSource: List<String>, override val posi
}
override fun constValue(program: Program): NumericLiteralValue? {
val node = definingScope.lookup(nameInSource, definingScope) ?: throw UndefinedSymbolError(this)
val node = definingScope.lookup(nameInSource) ?: throw UndefinedSymbolError(this)
val vardecl = node as? VarDecl
if(vardecl==null) {
return null

View File

@ -395,7 +395,7 @@ data class AssignTarget(var identifier: IdentifierReference?,
fun inferType(program: Program): InferredTypes.InferredType {
if (identifier != null) {
val symbol = definingScope.lookup(identifier!!.nameInSource, definingScope) ?: return InferredTypes.unknown()
val symbol = definingScope.lookup(identifier!!.nameInSource) ?: return InferredTypes.unknown()
if (symbol is VarDecl) return InferredTypes.knownFor(symbol.datatype)
}