Skip to content

Commit

Permalink
Add support for SHIFT_LEFT (<<) operator and other assignment operato… (
Browse files Browse the repository at this point in the history
#205)

* Add support for SHIFT_LEFT (<<) operator and other assignment operators in DirectX backend

* fix added shift left before less then

* feat: added parsing for shift left

---------

Co-authored-by: samunder singh <[email protected]>
Co-authored-by: samthakur587 <[email protected]>
  • Loading branch information
3 people authored Oct 27, 2024
1 parent 676f901 commit cf3f439
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions crosstl/src/backend/DirectX/DirectxLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
("COMMA", r","),
("COLON", r":"),
("QUESTION", r"\?"),
("SHIFT_LEFT", r"<<"),
("LESS_EQUAL", r"<="),
("GREATER_EQUAL", r">="),
("LESS_THAN", r"<"),
Expand Down
5 changes: 5 additions & 0 deletions crosstl/src/backend/DirectX/DirectxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def parse_variable_declaration_or_assignment(self):
"ASSIGN_XOR",
"ASSIGN_OR",
"ASSIGN_AND",
"SHIFT_LEFT",
]:
# Handle assignment operators (e.g., =, +=, -=, ^=, etc.)
op = self.current_token[1]
Expand All @@ -255,6 +256,7 @@ def parse_variable_declaration_or_assignment(self):
"ASSIGN_XOR",
"ASSIGN_OR",
"ASSIGN_AND",
"SHIFT_LEFT",
]:
# Handle assignment operators (e.g., =, +=, -=, ^=, etc.)
op = self.current_token[1]
Expand All @@ -275,6 +277,7 @@ def parse_variable_declaration_or_assignment(self):
"ASSIGN_XOR",
"ASSIGN_OR",
"ASSIGN_AND",
"SHIFT_LEFT",
]:
op = self.current_token[1]
self.eat(self.current_token[0])
Expand Down Expand Up @@ -403,6 +406,7 @@ def parse_assignment(self):
"ASSIGN_XOR",
"ASSIGN_OR",
"ASSIGN_AND",
"SHIFT_LEFT",
]:
op = self.current_token[1]
self.eat(self.current_token[0])
Expand Down Expand Up @@ -441,6 +445,7 @@ def parse_relational(self):
left = self.parse_additive()
while self.current_token[0] in [
"LESS_THAN",
"SHIFT_LEFT",
"GREATER_THAN",
"LESS_EQUAL",
"GREATER_EQUAL",
Expand Down
6 changes: 4 additions & 2 deletions tests/test_backend/test_directx/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,17 +426,19 @@ def test_assignment_ops_parsing():
out_color /= 2.0;
}
// Testing SHIFT_LEFT (<<) operator on some condition
if (input.in_position.r == 0.5) {
uint redValue = asuint(output.out_color.r);
output.redValue ^= 0x1;
output.out_color.r = asfloat(redValue);
output.redValue |= 0x2;
// Applying shift left operation
output.redValue << 1; // Shift left by 1
output.redValue &= 0x3;
}
return output;
}
"""
Expand Down
7 changes: 6 additions & 1 deletion tests/test_backend/test_directx/test_lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,21 @@ def test_assignment_ops_tokenization():
output.out_color /= 2.0;
}
// Testing SHIFT_LEFT (<<) operator on some condition
if (input.in_position.r == 0.5) {
uint redValue = asuint(output.out_color.r);
redValue ^= 0x1;
output.redValue ^= 0x1;
output.out_color.r = asfloat(redValue);
output.redValue |= 0x2;
// Applying shift left operation
output.redValue << 1; // Shift left by 1
redValue |= 0x2;
redValue &= 0x3;
}
return output;
}
"""
Expand Down
5 changes: 4 additions & 1 deletion tests/test_backend/test_directx/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,19 @@ def test_assignment_ops_parsing():
out_color /= 2.0;
}
// Testing SHIFT_LEFT (<<) operator on some condition
if (input.in_position.r == 0.5) {
uint redValue = asuint(output.out_color.r);
output.redValue ^= 0x1;
output.out_color.r = asfloat(redValue);
output.redValue |= 0x2;
// Applying shift left operation
output.redValue << 1; // Shift left by 1
output.redValue &= 0x3;
}
return output;
}
"""
Expand Down

0 comments on commit cf3f439

Please sign in to comment.