-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: strongly typed expression AST (#191)
- Loading branch information
Showing
147 changed files
with
4,446 additions
and
3,533 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Module maplibre-compose-expressions | ||
|
||
Utilities for creating MapLibre expressions with a type-safe Kotlin DSL. | ||
|
||
# Package dev.sargunv.maplibrecompose.expressions.ast | ||
|
||
The abstract syntax tree (AST) for the expression language. | ||
|
||
# Package dev.sargunv.maplibrecompose.expressions.dsl | ||
|
||
The Kotlin DSL for creating MapLibre expressions. This is the primary API you'll | ||
be using to create expressions. | ||
|
||
# Package dev.sargunv.maplibrecompose.expressions.value | ||
|
||
The interfaces and enums defining the type system for MapLibre expressions. |
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 @@ | ||
@file:OptIn(ExperimentalKotlinGradlePluginApi::class, ExperimentalComposeLibrary::class) | ||
|
||
import org.jetbrains.compose.ExperimentalComposeLibrary | ||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi | ||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget | ||
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSetTree | ||
|
||
plugins { | ||
id("library-conventions") | ||
id("android-library-conventions") | ||
id(libs.plugins.kotlin.multiplatform.get().pluginId) | ||
id(libs.plugins.android.library.get().pluginId) | ||
id(libs.plugins.compose.get().pluginId) | ||
id(libs.plugins.mavenPublish.get().pluginId) | ||
} | ||
|
||
android { namespace = "dev.sargunv.maplibrecompose.expressions" } | ||
|
||
mavenPublishing { | ||
pom { | ||
name = "MapLibre Compose Expressions" | ||
description = "MapLibre expressions DSL for MapLibre Compose." | ||
url = "https://github.com/sargunv/maplibre-compose" | ||
} | ||
} | ||
|
||
kotlin { | ||
androidTarget { | ||
compilerOptions { jvmTarget.set(JvmTarget.JVM_11) } | ||
instrumentedTestVariant.sourceSetTree.set(KotlinSourceSetTree.test) | ||
publishLibraryVariants("release", "debug") | ||
} | ||
iosArm64() | ||
iosSimulatorArm64() | ||
iosX64() | ||
jvm("desktop") | ||
|
||
sourceSets { | ||
commonMain.dependencies { implementation(compose.foundation) } | ||
|
||
commonTest.dependencies { | ||
implementation(kotlin("test")) | ||
implementation(kotlin("test-common")) | ||
implementation(kotlin("test-annotations-common")) | ||
} | ||
|
||
androidUnitTest.dependencies { implementation(compose.desktop.currentOs) } | ||
|
||
androidInstrumentedTest.dependencies { | ||
implementation(compose.desktop.uiTestJUnit4) | ||
implementation(libs.androidx.composeUi.testManifest) | ||
} | ||
} | ||
} |
16 changes: 9 additions & 7 deletions
16
.../maplibrecompose/core/expression/utils.kt → ...v/maplibrecompose/expressions/Defaults.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
43 changes: 43 additions & 0 deletions
43
...ssions/src/commonMain/kotlin/dev/sargunv/maplibrecompose/expressions/ExpressionContext.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,43 @@ | ||
package dev.sargunv.maplibrecompose.expressions | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import dev.sargunv.maplibrecompose.expressions.ast.CompiledExpression | ||
import dev.sargunv.maplibrecompose.expressions.ast.Expression | ||
import dev.sargunv.maplibrecompose.expressions.value.FloatValue | ||
|
||
/** | ||
* The context used while converting a high-level [Expression] to a low-level [CompiledExpression]. | ||
* | ||
* It defines how to resolve certain expressions (TextUnit, bitmaps) to their MapLibre counterparts. | ||
* MapLibre Compose users should not need to implement this interface; it is used internally by the | ||
* MapLibre Compose library. | ||
*/ | ||
public interface ExpressionContext { | ||
/** The scale factor to convert EMs to the desired unit */ | ||
public val emScale: Expression<FloatValue> | ||
|
||
/** The scale factor to convert SPs to the desired unit */ | ||
public val spScale: Expression<FloatValue> | ||
|
||
/** @return the resolved identifier for the [bitmap]. */ | ||
public fun resolveBitmap(bitmap: ImageBitmap): String | ||
|
||
/** @return the resolved identifier for the [painter]. */ | ||
public fun resolvePainter(painter: Painter): String | ||
|
||
/** A context where no complex types can be resolved. */ | ||
public object None : ExpressionContext { | ||
override val emScale: Expression<FloatValue> | ||
get() = error("TextUnit not allowed in this context") | ||
|
||
override val spScale: Expression<FloatValue> | ||
get() = error("TextUnit not allowed in this context") | ||
|
||
override fun resolveBitmap(bitmap: ImageBitmap): String = | ||
error("Bitmap not allowed in this context") | ||
|
||
override fun resolvePainter(painter: Painter): String = | ||
error("Painter not allowed in this context") | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...ssions/src/commonMain/kotlin/dev/sargunv/maplibrecompose/expressions/ast/BitmapLiteral.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,21 @@ | ||
package dev.sargunv.maplibrecompose.expressions.ast | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import dev.sargunv.maplibrecompose.expressions.ExpressionContext | ||
import dev.sargunv.maplibrecompose.expressions.value.StringValue | ||
|
||
/** | ||
* A [Literal] representing an [ImageBitmap] value, which will be loaded as an image into the style | ||
* upon compilation. | ||
*/ | ||
public data class BitmapLiteral private constructor(override val value: ImageBitmap) : | ||
Literal<StringValue, ImageBitmap> { | ||
override fun compile(context: ExpressionContext): StringLiteral = | ||
StringLiteral.of(context.resolveBitmap(value)) | ||
|
||
override fun visit(block: (Expression<*>) -> Unit): Unit = block(this) | ||
|
||
public companion object { | ||
public fun of(value: ImageBitmap): BitmapLiteral = BitmapLiteral(value) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...sions/src/commonMain/kotlin/dev/sargunv/maplibrecompose/expressions/ast/BooleanLiteral.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,16 @@ | ||
package dev.sargunv.maplibrecompose.expressions.ast | ||
|
||
import dev.sargunv.maplibrecompose.expressions.value.BooleanValue | ||
|
||
/** A [Literal] representing a [Boolean] value. */ | ||
public data class BooleanLiteral private constructor(override val value: Boolean) : | ||
CompiledLiteral<BooleanValue, Boolean> { | ||
override fun visit(block: (Expression<*>) -> Unit): Unit = block(this) | ||
|
||
public companion object { | ||
private val True: BooleanLiteral = BooleanLiteral(true) | ||
private val False: BooleanLiteral = BooleanLiteral(false) | ||
|
||
public fun of(value: Boolean): BooleanLiteral = if (value) True else False | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...essions/src/commonMain/kotlin/dev/sargunv/maplibrecompose/expressions/ast/ColorLiteral.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,24 @@ | ||
package dev.sargunv.maplibrecompose.expressions.ast | ||
|
||
import androidx.compose.ui.graphics.Color | ||
import dev.sargunv.maplibrecompose.expressions.value.ColorValue | ||
|
||
/** A [Literal] representing a [Color] value. */ | ||
public data class ColorLiteral private constructor(override val value: Color) : | ||
CompiledLiteral<ColorValue, Color> { | ||
override fun visit(block: (Expression<*>) -> Unit): Unit = block(this) | ||
|
||
public companion object { | ||
private val black = ColorLiteral(Color.Black) | ||
private val white = ColorLiteral(Color.White) | ||
private val transparent = ColorLiteral(Color.Transparent) | ||
|
||
public fun of(value: Color): ColorLiteral = | ||
when (value) { | ||
Color.Black -> black | ||
Color.White -> white | ||
Color.Transparent -> transparent | ||
else -> ColorLiteral(value) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...s/src/commonMain/kotlin/dev/sargunv/maplibrecompose/expressions/ast/CompiledExpression.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,16 @@ | ||
package dev.sargunv.maplibrecompose.expressions.ast | ||
|
||
import dev.sargunv.maplibrecompose.expressions.ExpressionContext | ||
import dev.sargunv.maplibrecompose.expressions.value.ExpressionValue | ||
|
||
/** | ||
* An [Expression] reduced to only those data types supported by the MapLibre SDKs. This can be | ||
* thought of as an intermediate representation between the high level expression DSL and the | ||
* platform-specific encoding. | ||
*/ | ||
public sealed interface CompiledExpression<out T : ExpressionValue> : Expression<T> { | ||
override fun compile(context: ExpressionContext): CompiledExpression<T> = this | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
override fun <X : ExpressionValue> cast(): CompiledExpression<X> = this as CompiledExpression<X> | ||
} |
24 changes: 24 additions & 0 deletions
24
...src/commonMain/kotlin/dev/sargunv/maplibrecompose/expressions/ast/CompiledFunctionCall.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,24 @@ | ||
package dev.sargunv.maplibrecompose.expressions.ast | ||
|
||
import dev.sargunv.maplibrecompose.expressions.value.ExpressionValue | ||
|
||
/** A [Literal] representing an function call with args all of [CompiledExpression] */ | ||
public data class CompiledFunctionCall | ||
private constructor( | ||
val name: String, | ||
val args: List<CompiledExpression<*>>, | ||
val isLiteralArg: (Int) -> Boolean, | ||
) : CompiledExpression<ExpressionValue> { | ||
override fun visit(block: (Expression<*>) -> Unit) { | ||
block(this) | ||
args.forEach { it.visit(block) } | ||
} | ||
|
||
public companion object { | ||
public fun of( | ||
name: String, | ||
args: List<CompiledExpression<*>>, | ||
isLiteralArg: (Int) -> Boolean = { false }, | ||
): CompiledFunctionCall = CompiledFunctionCall(name, args, isLiteralArg) | ||
} | ||
} |
Oops, something went wrong.