-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved symbol resolution and type computation APIs
Signed-off-by: Lorenzo Addazi <[email protected]>
- Loading branch information
Showing
17 changed files
with
564 additions
and
469 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
core/src/main/kotlin/com/strumenta/kolasu/semantics/ScopeProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.strumenta.kolasu.semantics | ||
|
||
import com.strumenta.kolasu.model.KReferenceByName | ||
import com.strumenta.kolasu.model.Node | ||
import com.strumenta.kolasu.model.PossiblyNamed | ||
import com.strumenta.kolasu.utils.memoize | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.isSuperclassOf | ||
|
||
// instance | ||
|
||
class ScopeProvider( | ||
private val scopeResolutionRules: MutableMap<KReferenceByName<out Node>, (Node) -> Scope> = mutableMapOf(), | ||
private val scopeConstructionRules: MutableMap<KClass<out Node>, (Node) -> Scope> = mutableMapOf() | ||
) { | ||
fun loadFrom(configuration: ScopeProviderConfiguration, semantics: Semantics) { | ||
configuration.scopeResolutionRules.mapValuesTo(this.scopeResolutionRules) { | ||
(_, scopeResolutionRule) -> | ||
{ node: Node -> semantics.scopeResolutionRule(node) } | ||
} | ||
configuration.scopeConstructionRules.mapValuesTo(this.scopeConstructionRules) { | ||
(_, scopeConstructionRule) -> | ||
{ node: Node -> semantics.scopeConstructionRule(node) } | ||
} | ||
} | ||
|
||
fun scopeFor(referenceByName: KReferenceByName<out Node>, node: Node? = null): Scope { | ||
return node?.let { this.scopeResolutionRules[referenceByName] }?.invoke(node) ?: Scope() | ||
} | ||
|
||
fun scopeFrom(node: Node? = null): Scope { | ||
return node?.let { | ||
this.scopeConstructionRules.keys | ||
.filter { it.isSuperclassOf(node::class) } | ||
.sortedWith { left, right -> | ||
when { | ||
left.isSuperclassOf(right) -> 1 | ||
right.isSuperclassOf(left) -> -1 | ||
else -> 0 | ||
} | ||
}.firstOrNull() | ||
}?.let { this.scopeConstructionRules[it] }?.invoke(node) ?: Scope() | ||
} | ||
} | ||
|
||
// configuration | ||
|
||
class ScopeProviderConfiguration( | ||
val scopeResolutionRules: MutableMap<KReferenceByName<out Node>, Semantics.(Node) -> Scope> = mutableMapOf(), | ||
val scopeConstructionRules: MutableMap<KClass<out Node>, Semantics.(Node) -> Scope> = mutableMapOf() | ||
) { | ||
inline fun <reified N : Node> scopeFor( | ||
referenceByName: KReferenceByName<N>, | ||
crossinline scopeResolutionRule: Semantics.(N) -> Scope | ||
) { | ||
this.scopeResolutionRules.putIfAbsent( | ||
referenceByName, | ||
{ semantics: Semantics, node: Node -> | ||
if (node is N) semantics.scopeResolutionRule(node) else Scope() | ||
}.memoize() | ||
) | ||
} | ||
|
||
inline fun <reified N : Node> scopeFrom( | ||
nodeType: KClass<N>, | ||
crossinline scopeConstructionRule: Semantics.(N) -> Scope | ||
) { | ||
this.scopeConstructionRules.putIfAbsent( | ||
nodeType, | ||
{ semantics: Semantics, node: Node -> | ||
if (node is N) semantics.scopeConstructionRule(node) else Scope() | ||
}.memoize() | ||
) | ||
} | ||
} | ||
|
||
// builder | ||
|
||
fun scopeProvider(init: ScopeProviderConfiguration.() -> Unit) = ScopeProviderConfiguration().apply(init) | ||
|
||
// scopes | ||
|
||
// TODO handle multiple symbols (e.g. function overloading) | ||
// TODO allow other than name-based symbol binding (e.g. predicated, numbered, etc.) | ||
data class Scope( | ||
var parent: Scope? = null, | ||
val symbolTable: MutableMap<String, MutableList<PossiblyNamed>> = mutableMapOf(), | ||
val ignoreCase: Boolean = false | ||
) { | ||
fun define(symbol: PossiblyNamed) { | ||
this.symbolTable.computeIfAbsent(symbol.name.toSymbolTableKey()) { mutableListOf() }.add(symbol) | ||
} | ||
|
||
fun resolve(name: String? = null, type: KClass<out PossiblyNamed> = PossiblyNamed::class): PossiblyNamed? { | ||
val key = name.toSymbolTableKey() | ||
return this.symbolTable.getOrDefault(key, mutableListOf()).find { type.isInstance(it) } | ||
?: this.parent?.resolve(key, type) | ||
} | ||
|
||
private fun String?.toSymbolTableKey() = when { | ||
this != null && ignoreCase -> this.lowercase() | ||
this != null -> this | ||
else -> throw IllegalArgumentException("The given symbol must have a name") | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
core/src/main/kotlin/com/strumenta/kolasu/semantics/Semantics.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.strumenta.kolasu.semantics | ||
|
||
import com.strumenta.kolasu.validation.Issue | ||
|
||
// instance | ||
|
||
class Semantics( | ||
issues: MutableList<Issue> = mutableListOf(), | ||
configuration: SemanticsConfiguration | ||
) { | ||
val typeComputer = TypeComputer().apply { | ||
configuration.typeComputer?.let { | ||
this.loadFrom(it, this@Semantics) | ||
} | ||
} | ||
val symbolResolver = SymbolResolver().apply { | ||
configuration.symbolResolver?.let { | ||
this.loadFrom(it, this@Semantics) | ||
} | ||
} | ||
} | ||
|
||
// configuration | ||
|
||
class SemanticsConfiguration( | ||
var typeComputer: TypeComputerConfiguration? = null, | ||
var symbolResolver: SymbolResolverConfiguration? = null | ||
) { | ||
fun typeComputer(init: TypeComputerConfiguration.() -> Unit) { | ||
this.typeComputer = TypeComputerConfiguration().apply(init) | ||
} | ||
fun symbolResolver(init: SymbolResolverConfiguration.() -> Unit) { | ||
this.symbolResolver = SymbolResolverConfiguration().apply(init) | ||
} | ||
} | ||
|
||
// builder | ||
|
||
fun semantics( | ||
issues: MutableList<Issue> = mutableListOf(), | ||
init: SemanticsConfiguration.() -> Unit | ||
) = Semantics(issues, SemanticsConfiguration().apply(init)) |
56 changes: 56 additions & 0 deletions
56
core/src/main/kotlin/com/strumenta/kolasu/semantics/SymbolResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.strumenta.kolasu.semantics | ||
|
||
import com.strumenta.kolasu.model.* | ||
import com.strumenta.kolasu.traversing.walkChildren | ||
import kotlin.reflect.KClass | ||
|
||
// instance | ||
|
||
class SymbolResolver( | ||
private val scopeProvider: ScopeProvider = ScopeProvider() | ||
) { | ||
fun loadFrom(configuration: SymbolResolverConfiguration, semantics: Semantics) { | ||
this.scopeProvider.loadFrom(configuration.scopeProvider, semantics) | ||
} | ||
|
||
@Suppress("unchecked_cast") | ||
fun resolve(node: Node) { | ||
node.kReferenceByNameProperties().forEach { this.resolve(it as KReferenceByName<out Node>, node) } | ||
node.walkChildren().forEach(this::resolve) | ||
} | ||
|
||
@Suppress("unchecked_cast") | ||
fun resolve(property: KReferenceByName<out Node>, node: Node) { | ||
(node.properties.find { it.name == property.name }?.value as ReferenceByName<PossiblyNamed>?)?.apply { | ||
this.referred = scopeProvider.scopeFor(property, node).resolve(this.name, property.getReferredType()) | ||
} | ||
} | ||
|
||
fun scopeFor(property: KReferenceByName<out Node>, node: Node? = null): Scope { | ||
return this.scopeProvider.scopeFor(property, node) | ||
} | ||
|
||
fun scopeFrom(node: Node? = null): Scope { | ||
return this.scopeProvider.scopeFrom(node) | ||
} | ||
} | ||
|
||
// configuration | ||
|
||
class SymbolResolverConfiguration( | ||
val scopeProvider: ScopeProviderConfiguration = ScopeProviderConfiguration() | ||
) { | ||
inline fun <reified N : Node> scopeFor( | ||
referenceByName: KReferenceByName<N>, | ||
crossinline scopeResolutionRule: Semantics.(N) -> Scope | ||
) = this.scopeProvider.scopeFor(referenceByName, scopeResolutionRule) | ||
|
||
inline fun <reified N : Node> scopeFrom( | ||
nodeType: KClass<N>, | ||
crossinline scopeConstructionRule: Semantics.(N) -> Scope | ||
) = this.scopeProvider.scopeFrom(nodeType, scopeConstructionRule) | ||
} | ||
|
||
// builder | ||
|
||
fun symbolResolver(init: SymbolResolverConfiguration.() -> Unit) = SymbolResolverConfiguration().apply(init) |
54 changes: 54 additions & 0 deletions
54
core/src/main/kotlin/com/strumenta/kolasu/semantics/TypeComputer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.strumenta.kolasu.semantics | ||
|
||
import com.strumenta.kolasu.model.Node | ||
import com.strumenta.kolasu.utils.memoize | ||
import kotlin.reflect.KClass | ||
import kotlin.reflect.full.isSuperclassOf | ||
|
||
// instance | ||
|
||
class TypeComputer( | ||
private val typingRules: MutableMap<KClass<out Node>, (Node) -> Node?> = mutableMapOf() | ||
) { | ||
fun loadFrom(configuration: TypeComputerConfiguration, semantics: Semantics) { | ||
configuration.typingRules.mapValuesTo(this.typingRules) { | ||
(_, typingRule) -> | ||
{ node: Node -> semantics.typingRule(node) } | ||
} | ||
} | ||
|
||
fun typeFor(node: Node? = null): Node? { | ||
return node?.let { | ||
this.typingRules.keys | ||
.filter { it.isSuperclassOf(node::class) } | ||
.sortedWith { left, right -> | ||
when { | ||
left.isSuperclassOf(right) -> 1 | ||
right.isSuperclassOf(left) -> -1 | ||
else -> 0 | ||
} | ||
}.firstOrNull()?.let { this.typingRules[it] }?.invoke(node) | ||
} | ||
} | ||
} | ||
|
||
// configuration | ||
|
||
class TypeComputerConfiguration( | ||
val typingRules: MutableMap<KClass<out Node>, Semantics.(Node) -> Node?> = mutableMapOf( | ||
Node::class to { it } | ||
) | ||
) { | ||
inline fun <reified N : Node> typeFor(nodeType: KClass<N>, crossinline typingRule: Semantics.(N) -> Node?) { | ||
this.typingRules.putIfAbsent( | ||
nodeType, | ||
{ semantics: Semantics, node: Node -> | ||
if (node is N) semantics.typingRule(node) else null | ||
}.memoize() | ||
) | ||
} | ||
} | ||
|
||
// builder | ||
|
||
fun typeComputer(init: TypeComputerConfiguration.() -> Unit) = TypeComputerConfiguration().apply(init) |
Oops, something went wrong.