Skip to content

Commit

Permalink
Make ThunkFactory generic (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlurton authored May 10, 2022
1 parent c57fdff commit ec7d390
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ParserErrorExample(out: PrintStream) : Example(out) {

throw Exception("ParserException was not thrown")
} catch (e: ParserException) {
val errorContext = e.errorContext!!
val errorContext = e.errorContext

val errorInformation = "errorCode: ${e.errorCode}" +
"\nLINE_NUMBER: ${errorContext[Property.LINE_NUMBER]}" +
Expand Down
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
10 changes: 6 additions & 4 deletions lang/src/org/partiql/lang/SqlException.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.partiql.lang.errors.ErrorCode
import org.partiql.lang.errors.Property
import org.partiql.lang.errors.PropertyValueMap
import org.partiql.lang.errors.UNKNOWN
import org.partiql.lang.util.propertyValueMapOf

/**
* General exception class for the interpreter.
Expand All @@ -33,7 +34,7 @@ import org.partiql.lang.errors.UNKNOWN
*
* @param message the message for this exception
* @param errorCode the error code for this exception
* @param propertyValueMap context for this error
* @param errorContextArg context for this error, contains details like line & column number among other attributes.
* @param cause for this exception
*
* @constructor a custom error [message], the [errorCode], error context as a [propertyValueMap] and optional [cause] creates an
Expand All @@ -43,10 +44,11 @@ import org.partiql.lang.errors.UNKNOWN
open class SqlException(
override var message: String,
val errorCode: ErrorCode,
val errorContext: PropertyValueMap? = null,
errorContextArg: PropertyValueMap? = null,
cause: Throwable? = null
) :
RuntimeException(message, cause) {
) : RuntimeException(message, cause) {

val errorContext: PropertyValueMap = errorContextArg ?: propertyValueMapOf()

/**
* Indicates if this exception is due to an internal error or not.
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 ec7d390

Please sign in to comment.