Do you think it is feasible to write a GLSL DSL wi...
# dsl
r
Do you think it is feasible to write a GLSL DSL within Kotlin? I feel like an annoying thing would be having keywords and statements like
if
as they're already used by the language, so you'd need
Copy code
`if` (variableA eq variableB)
which feels slightly annoying to use. Is there any alternatives (compiler plugin?) or would it just not be possible? Example of what I'm talking about:
Copy code
buildGlslShader { // this: GlslShaderContext
    glPosition = projMat * modelViewMat * vec4(position, 1.0)
    val iColor = ivec3(color.xyz * 255 + vec3(0.5))

    `if` (iColor eq ivec3(78, 92, 36) {
        // Change some stuff...
     }
}
e
Are you facing any issues with language injection (in strings)?
r
Sorry for the late response @efemoney but I don't quite understand your question. What's language injection?
e
No problem. You can annotate a string literal with a specific language and IJ will provide syntax highlighting & IntelliSense for the string, example
r
Oh, that's pretty cool! Unfortunate the tooling for any kind of GLSL-related things is horrible, especially in IJ.
😞 1
e
Latest Android Studio should support language injection for AGSL strings. But not sure about vanilla IJ support
Ah I see, tough.
m
To understand, why can't you use a Kotlin
if
to conditionally execute DSL expressions? Why do you need an alternative ``if`` ?
r
I'm making a DSL for GLSL code generation, probably should've specified that, sorry
m
no worries, thanks for clarifying. So in your case you would like an expression in your DSL to generate an if for GLSL, correct? If backticks bother you, how about this: have a sealed type for GLSL directives
Copy code
sealed class GlslDirective {
    data object If : GlslDirective() {
        operator fun invoke(value: String, setup: () -> Unit) {
            TODO("Not yet implemented")
        }
    }
    // more stuff if you want to extend the DSL...
}
then you can use it like:
Copy code
buildGlslShader {
    If("I don't know what eq produces") {
        // Change some stuff...
    }
}
would this be useful to you?
I used a sealed hierarchy because you may want some stuff in common to be available to directives
ah yea sorry, the above needs to be hammered onto GlslShaderContext, forgot it 🤦‍♂️
I overlooked that, so maybe in your case it would just be a matter of naming the function "If"...
Copy code
fun GlslShaderContext.If(value: String, setup: GlslShaderContext.() -> Unit) {
    // Do something with value
    setup()
}
sorry, I've complicated things unnecessarily, I'm not sure what I had in mind... 😄
r
Oh that's a good idea! Will definitely try that!
👍 1
m
YW, let me know if it turns out good