From 6cb8b3b5cd3be52e711963d4be6a58c5b861fdf1 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 28 Oct 2021 23:51:40 +0200 Subject: [PATCH] removed unneeded scope param from lookup() --- .../compiler/astprocessing/AstChecker.kt | 4 +-- .../astprocessing/AstIdentifiersChecker.kt | 4 +-- compilerAst/src/prog8/ast/AstToplevel.kt | 26 +++++++++---------- .../prog8/ast/expressions/AstExpressions.kt | 4 +-- .../src/prog8/ast/statements/AstStatements.kt | 2 +- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt index 5dfe2650a..2fadeef69 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstChecker.kt @@ -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) diff --git a/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt b/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt index 6b2983923..7720464b7 100644 --- a/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt +++ b/compiler/src/prog8/compiler/astprocessing/AstIdentifiersChecker.kt @@ -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) diff --git a/compilerAst/src/prog8/ast/AstToplevel.kt b/compilerAst/src/prog8/ast/AstToplevel.kt index 7d8416b14..b22db6932 100644 --- a/compilerAst/src/prog8/ast/AstToplevel.kt +++ b/compilerAst/src/prog8/ast/AstToplevel.kt @@ -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, localScope: INameScope) : Statement? { + fun lookup(scopedName: List) : Statement? { return if(scopedName.size>1) - lookupQualified(scopedName, localScope) + lookupQualified(scopedName) else { - lookupUnqualified(scopedName[0], localScope) + lookupUnqualified(scopedName[0]) } } - private fun lookupQualified(scopedName: List, startScope: INameScope): Statement? { + private fun lookupQualified(scopedName: List): 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): Node, INameScope { override val statements = mutableListOf() // not used override var parent: Node = ParentSentinel - override fun lookup(scopedName: List, localScope: INameScope): Statement? { + override fun lookup(scopedName: List): Statement? { throw NotImplementedError("use lookup on actual ast node instead") } diff --git a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt index 8f617e042..7e4b8a647 100644 --- a/compilerAst/src/prog8/ast/expressions/AstExpressions.kt +++ b/compilerAst/src/prog8/ast/expressions/AstExpressions.kt @@ -730,7 +730,7 @@ data class IdentifierReference(val nameInSource: List, 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, 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 diff --git a/compilerAst/src/prog8/ast/statements/AstStatements.kt b/compilerAst/src/prog8/ast/statements/AstStatements.kt index cc60f9d32..ee39c88fe 100644 --- a/compilerAst/src/prog8/ast/statements/AstStatements.kt +++ b/compilerAst/src/prog8/ast/statements/AstStatements.kt @@ -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) }