Skip to content

Commit

Permalink
Make ThunkFactory generic to allow different types to be used for the…
Browse files Browse the repository at this point in the history
… environment.
  • Loading branch information
dlurton committed Apr 29, 2022
1 parent 3c583bc commit afce4bc
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 101 deletions.
2 changes: 1 addition & 1 deletion lang/src/org/partiql/lang/CompilerPipeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ interface CompilerPipeline {
fun build(): CompilerPipeline {
val compileOptionsToUse = compileOptions ?: CompileOptions.standard()

when (compileOptionsToUse.thunkReturnTypeAssertions) {
when (compileOptionsToUse.thunkOptions.thunkReturnTypeAssertions) {
ThunkReturnTypeAssertions.DISABLED -> { /* intentionally blank */ }
ThunkReturnTypeAssertions.ENABLED -> {
check(this.globalTypeBindings != null) {
Expand Down
6 changes: 3 additions & 3 deletions lang/src/org/partiql/lang/eval/CompileOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,14 @@ enum class ThunkReturnTypeAssertions {
* @param defaultTimezoneOffset Default timezone offset to be used when TIME WITH TIME ZONE does not explicitly
* specify the time zone. Defaults to [ZoneOffset.UTC]
*/
@Suppress("DataClassPrivateConstructor")
data class CompileOptions private constructor (
val undefinedVariable: UndefinedVariableBehavior,
val projectionIteration: ProjectionIterationBehavior = ProjectionIterationBehavior.FILTER_MISSING,
val visitorTransformMode: VisitorTransformMode = VisitorTransformMode.DEFAULT,
val thunkOptions: ThunkOptions = ThunkOptions.standard(),
val typingMode: TypingMode = TypingMode.LEGACY,
val typedOpBehavior: TypedOpBehavior = TypedOpBehavior.LEGACY,
val thunkReturnTypeAssertions: ThunkReturnTypeAssertions = ThunkReturnTypeAssertions.DISABLED,
val defaultTimezoneOffset: ZoneOffset = ZoneOffset.UTC
) {

Expand Down Expand Up @@ -177,7 +177,7 @@ data class CompileOptions private constructor (
fun build(options: CompileOptions, block: Builder.() -> Unit) = Builder(options).apply(block).build()

/**
* Creates a [CompileOptions] instance with the standard values.
* Creates a [CompileOptions] instance with the standard values for use by the legacy AST compiler.
*/
@JvmStatic
fun standard() = Builder().build()
Expand All @@ -194,7 +194,7 @@ data class CompileOptions private constructor (
fun typingMode(value: TypingMode) = set { copy(typingMode = value) }
fun typedOpBehavior(value: TypedOpBehavior) = set { copy(typedOpBehavior = value) }
fun thunkOptions(value: ThunkOptions) = set { copy(thunkOptions = value) }
fun evaluationTimeTypeChecks(value: ThunkReturnTypeAssertions) = set { copy(thunkReturnTypeAssertions = value) }
fun thunkOptions(build: ThunkOptions.Builder.() -> Unit) = set { copy(thunkOptions = ThunkOptions.build(build)) }
fun defaultTimezoneOffset(value: ZoneOffset) = set { copy(defaultTimezoneOffset = value) }

private inline fun set(block: CompileOptions.() -> CompileOptions): Builder {
Expand Down
21 changes: 20 additions & 1 deletion lang/src/org/partiql/lang/eval/EvaluatingCompiler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ import java.util.TreeSet
import java.util.regex.Pattern
import kotlin.Comparator

/**
* A thunk with no parameters other than the current environment.
*
* See https://en.wikipedia.org/wiki/Thunk
*
* This name was chosen because it is a thunk that accepts an instance of `Environment`.
*/
private typealias ThunkEnv = Thunk<Environment>

/**
* A thunk taking a single [T] argument and the current environment.
*
* See https://en.wikipedia.org/wiki/Thunk
*
* This name was chosen because it is a thunk that accepts an instance of `Environment` and an [ExprValue] as
* its arguments.
*/
private typealias ThunkEnvValue<T> = ThunkValue<Environment, T>

/**
* A basic compiler that converts an instance of [PartiqlAst] to an [Expression].
*
Expand Down Expand Up @@ -116,7 +135,7 @@ internal class EvaluatingCompiler(
private val compileOptions: CompileOptions = CompileOptions.standard()
) {
private val errorSignaler = compileOptions.typingMode.createErrorSignaler(valueFactory)
private val thunkFactory = compileOptions.typingMode.createThunkFactory(compileOptions, valueFactory)
private val thunkFactory = compileOptions.typingMode.createThunkFactory<Environment>(compileOptions.thunkOptions, valueFactory)

private val compilationContextStack = Stack<CompilationContext>()

Expand Down
4 changes: 4 additions & 0 deletions lang/src/org/partiql/lang/eval/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ fun fillErrorContext(errorContext: PropertyValueMap, metaContainer: MetaContaine
}
}

/**
* Returns the [SourceLocationMeta] as an error context if the [SourceLocationMeta.TAG] exists in the passed
* [metaContainer]. Otherwise, returns an empty map.
*/
fun errorContextFrom(metaContainer: MetaContainer?): PropertyValueMap {
if (metaContainer == null) {
return PropertyValueMap()
Expand Down
Loading

0 comments on commit afce4bc

Please sign in to comment.