Skip to content

Commit

Permalink
Fixed #103: Missing != operator
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Jan 2, 2024
1 parent cfa49a6 commit cb8d305
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,23 @@ class CreateShaderAST[Q <: Quotes](using val qq: Q) extends ShaderMacroUtils:

case Apply(Select(term, op), xs) =>
op match
case "+" | "-" | "*" | "/" | "<" | ">" | "==" | "<=" | ">=" | "&&" | "||" =>
case "<" | "<=" | ">" | ">=" | "==" | "!=" =>
// Vector Relational Functions, according to the spec TL;DR: the left and right side types must be the same.
// However, we don't have a nice way to do that check yet.
val lhs = walkTerm(term, envVarName)
val rhs = xs.headOption.map(tt => walkTerm(tt, envVarName)).getOrElse(ShaderAST.Empty())
val rt = findReturnType(lhs)
ShaderAST.Infix(op, lhs, rhs, rt)

case "+" | "-" | "*" | "/" =>
// Math operators.
val lhs = walkTerm(term, envVarName)
val rhs = xs.headOption.map(tt => walkTerm(tt, envVarName)).getOrElse(ShaderAST.Empty())
val rt = findReturnType(lhs)
ShaderAST.Infix(op, lhs, rhs, rt)

case "&&" | "||" =>
// Logical operators.
val lhs = walkTerm(term, envVarName)
val rhs = xs.headOption.map(tt => walkTerm(tt, envVarName)).getOrElse(ShaderAST.Empty())
val rt = findReturnType(lhs)
Expand All @@ -1210,7 +1226,23 @@ class CreateShaderAST[Q <: Quotes](using val qq: Q) extends ShaderMacroUtils:

case Apply(Apply(Ident(op), List(l)), List(r)) =>
op match
case "+" | "-" | "*" | "/" | "<" | ">" | "==" | "<=" | ">=" | "&&" | "||" =>
case "<" | "<=" | ">" | ">=" | "==" | "!=" =>
// Vector Relational Functions, according to the spec. TL;DR: the left and right side types must be the same.
// However, we don't have a nice way to do that check yet.
val lhs = walkTerm(l, envVarName)
val rhs = walkTerm(r, envVarName)
val rt = findReturnType(lhs)
ShaderAST.Infix(op, lhs, rhs, rt)

case "+" | "-" | "*" | "/" =>
// Math operators.
val lhs = walkTerm(l, envVarName)
val rhs = walkTerm(r, envVarName)
val rt = findReturnType(lhs)
ShaderAST.Infix(op, lhs, rhs, rt)

case "&&" | "||" =>
// Logical operators.
val lhs = walkTerm(l, envVarName)
val rhs = walkTerm(r, envVarName)
val rt = findReturnType(lhs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,30 @@ class GLSLIfStatementTests extends munit.FunSuite {
)
}

test("if statement with not operator comparison") {
inline def fragment: Shader[FragEnv, vec4] =
Shader { _ =>
val x = 1.0f
if x != 2.0f then vec4(10.0f) else vec4(20.0f)
}

val actual =
fragment.toGLSL[WebGL2].toOutput.code

// DebugAST.toAST(fragment)
// println(actual)

assertEquals(
actual,
s"""
|float x=1.0;
|if(x!=2.0){
| vec4(10.0);
|}else{
| vec4(20.0);
|}
|""".stripMargin.trim
)
}

}

0 comments on commit cb8d305

Please sign in to comment.