-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added bitwise and or xor support #129 #126
base: main
Are you sure you want to change the base?
Changes from all commits
307833f
67e5a22
6d8293e
7e62aab
90d0640
e3e47b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
("BOOL", r"\bbool\b"), | ||
("FLOAT", r"\bfloat\b"), | ||
("INT", r"\bint\b"), | ||
("UINT", r"\bunsigned int\b"), | ||
("DOUBLE", r"\bdouble\b"), | ||
("SAMPLER2D", r"\bsampler2D\b"), | ||
("PRE_INCREMENT", r"\+\+(?=\w)"), | ||
("PRE_DECREMENT", r"--(?=\w)"), | ||
|
@@ -35,6 +37,9 @@ | |
("ASSIGN_SUB", r"-="), | ||
("ASSIGN_MUL", r"\*="), | ||
("ASSIGN_DIV", r"/="), | ||
("ASSIGN_OR", r"\|="), | ||
("ASSIGN_AND", r"&="), | ||
("ASSIGN_XOR", r"\^="), | ||
("EQUAL", r"=="), | ||
("NOT_EQUAL", r"!="), | ||
("WHITESPACE", r"\s+"), | ||
|
@@ -51,6 +56,11 @@ | |
("OR", r"\|\|"), | ||
("NOT", r"!"), | ||
("PLUS", r"\+"), | ||
("BITWISE_SHIFT_LEFT", r"<<"), | ||
("BITWISE_SHIFT_RIGHT", r">>"), | ||
("BITWISE_AND", r"&"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for these two token |
||
("BITWISE_OR", r"\|"), | ||
("BITWISE_XOR", r"\^"), | ||
("MINUS", r"-"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi Nripesh, |
||
("MULTIPLY", r"\*"), | ||
("DIVIDE", r"/"), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,6 @@ | |
FRAGMENTShaderNode, | ||
VersionDirectiveNode, | ||
) | ||
from .OpenglLexer import * | ||
|
||
|
||
class GLSLParser: | ||
|
@@ -177,7 +176,14 @@ def parse_shader(self, version_node): | |
elif self.current_token[0] == "VERSION": | ||
self.parse_version_directive() | ||
|
||
elif self.current_token[0] in ["VOID", "FLOAT", "VECTOR"]: | ||
elif self.current_token[0] in [ | ||
"VOID", | ||
"FLOAT", | ||
"VECTOR", | ||
"INT", | ||
"UINT", | ||
"DOUBLE", | ||
]: | ||
self.skip_comments() | ||
if current_section: | ||
function_node = self.parse_function() | ||
|
@@ -226,8 +232,6 @@ def parse_shader_section(self, current_section): | |
functions = [] | ||
layout_qualifiers = [] | ||
|
||
self.eat("LBRACE") | ||
|
||
while self.current_token[0] != "RBRACE" and self.current_token[0] != "EOF": | ||
if self.current_token[0] == "LAYOUT": | ||
self.skip_comments() | ||
|
@@ -319,20 +323,25 @@ def parse_variable(self, type_name): | |
raise SyntaxError( | ||
f"Expected ';' after variable assignment, found: {self.current_token[0]}" | ||
) | ||
|
||
elif self.current_token[0] in ( | ||
"ASSIGN_ADD", | ||
"ASSIGN_SUB", | ||
"ASSIGN_MUL", | ||
"ASSIGN_DIV", | ||
"ASSIGN_XOR", | ||
"ASSIGN_OR", | ||
"ASSIGN_AND", | ||
): | ||
op = self.current_token[0] | ||
op_name = self.current_token[1] | ||
self.current_token[1] | ||
self.eat(op) | ||
value = self.parse_expression() | ||
if self.current_token[0] == "SEMICOLON": | ||
self.eat("SEMICOLON") | ||
return BinaryOpNode(VariableNode(type_name, name), op_name, value) | ||
return AssignmentNode( | ||
VariableNode(type_name, name), | ||
BinaryOpNode(VariableNode("", name), op, value), | ||
) | ||
else: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you please have one check and assign it as the current_token[0] in binop node operator node . |
||
raise SyntaxError( | ||
f"Expected ';' after compound assignment, found: {self.current_token[0]}" | ||
|
@@ -344,7 +353,14 @@ def parse_variable(self, type_name): | |
|
||
def parse_assignment_or_function_call(self): | ||
type_name = "" | ||
if self.current_token[0] in ["VECTOR", "FLOAT", "INT", "MATRIX"]: | ||
if self.current_token[0] in [ | ||
"VECTOR", | ||
"FLOAT", | ||
"INT", | ||
"MATRIX", | ||
"DOUBLE", | ||
"UINT", | ||
]: | ||
type_name = self.current_token[1] | ||
self.eat(self.current_token[0]) | ||
if self.current_token[0] == "IDENTIFIER": | ||
|
@@ -414,8 +430,18 @@ def parse_body(self): | |
body.append(self.parse_for()) | ||
elif self.current_token[0] == "RETURN": | ||
body.append(self.parse_return()) | ||
elif self.current_token[0] in ["VECTOR", "IDENTIFIER", "FLOAT", "INT"]: | ||
elif self.current_token[0] in [ | ||
"VECTOR", | ||
"IDENTIFIER", | ||
"FLOAT", | ||
"INT", | ||
"DOUBLE", | ||
"UINT", | ||
]: | ||
body.append(self.parse_assignment_or_function_call()) | ||
elif self.current_token[0] in ["COMMENT_SINGLE", "COMMENT_MULTI"]: | ||
tok = self.current_token[0] | ||
self.eat(tok) | ||
else: | ||
raise SyntaxError(f"Unexpected token {self.current_token[0]}") | ||
return body | ||
|
@@ -443,6 +469,8 @@ def parse_type(self): | |
"VECTOR", | ||
"FLOAT", | ||
"INT", | ||
"DOUBLE", | ||
"UINT", | ||
"MATRIX", | ||
"BOOLEAN", | ||
"SAMPLER2D", | ||
|
@@ -453,7 +481,7 @@ def parse_type(self): | |
elif self.current_token[0] == "IDENTIFIER": | ||
type_name = self.current_token[1] | ||
self.eat("IDENTIFIER") | ||
if type_name in ["int", "float"]: | ||
if type_name in ["int", "float", "unsigned int", "double"]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unsigned int (opengl) -> uint (crossgl ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah thats true iam mapping the types in codegen file. you can test it. |
||
return type_name | ||
return type_name | ||
else: | ||
|
@@ -554,6 +582,15 @@ def parse_function_call_or_identifier(self): | |
return self.parse_member_access(func_name) | ||
return VariableNode("", func_name) | ||
|
||
def parse_binop(self): | ||
left = self.parse_additive() | ||
while self.current_token[0] in ["BITWISE_AND", "BITWISE_XOR", "BITWISE_OR"]: | ||
op = self.current_token[0] | ||
self.eat(op) | ||
right = self.parse_additive() | ||
left = BinaryOpNode(left, op, right) | ||
return left | ||
|
||
def parse_additive(self): | ||
left = self.parse_multiplicative() | ||
while self.current_token[0] in ["PLUS", "MINUS"]: | ||
|
@@ -595,7 +632,7 @@ def parse_multiplicative(self): | |
return left | ||
|
||
def parse_expression(self): | ||
left = self.parse_additive() | ||
left = self.parse_binop() | ||
while self.current_token[0] in [ | ||
"LESS_THAN", | ||
"GREATER_THAN", | ||
|
@@ -608,7 +645,7 @@ def parse_expression(self): | |
]: | ||
op = self.current_token[0] | ||
self.eat(op) | ||
right = self.parse_additive() | ||
right = self.parse_binop() | ||
left = BinaryOpNode(left, op, right) | ||
|
||
if self.current_token[0] == "QUESTION": | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,12 +220,12 @@ def generate_expression(self, expr, shader_type): | |
if isinstance(expr, str): | ||
return self.translate_expression(expr, shader_type) | ||
elif isinstance(expr, VariableNode): | ||
return f"{expr.vtype} {self.translate_expression(expr.name, shader_type)}" | ||
return f"{self.map_type(expr.vtype)} {self.translate_expression(expr.name, shader_type)}" | ||
elif isinstance(expr, BinaryOpNode): | ||
left = self.generate_expression(expr.left, shader_type) | ||
right = self.generate_expression(expr.right, shader_type) | ||
op = self.map_operator(expr.op) | ||
return f"{left} {op} {right}" | ||
return f"({left} {op} {right})" | ||
elif isinstance(expr, FunctionCallNode): | ||
args = ", ".join( | ||
self.generate_expression(arg, shader_type) for arg in expr.args | ||
|
@@ -284,6 +284,8 @@ def map_type(self, vtype): | |
"float": "float", | ||
"int": "int", | ||
"bool": "bool", | ||
"double": "double", | ||
"unsigned int": "uint", | ||
} | ||
return type_map.get(vtype, vtype) | ||
|
||
|
@@ -301,5 +303,15 @@ def map_operator(self, op): | |
"NOT_EQUAL": "!=", | ||
"AND": "&&", | ||
"OR": "||", | ||
"BITWISE_OR": "|", | ||
"BITWISE_XOR": "^", | ||
"BITWISE_AND": "&", | ||
"ASSIGN_ADD": "+", | ||
"ASSIGN_MUL": "*", | ||
"ASSIGN_DIV": "/", | ||
"ASSIGN_SUB": "-", | ||
"ASSIGN_OR": "|", | ||
"ASSIGN_XOR": "^", | ||
"ASSIGN_AND": "&", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why here the bitwise_and and assign_and same and same thing for bitwise_or ,bitwise_and . the assign_and should be like &= and assign_or |= . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah in lexer it is &= and |= but when we map those we need to do & and then we assign right. Once can you check the result of assign. |
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also add tests for codegen file also the changes you have mode. |
||
return op_map.get(op, op) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hii @ashwith2427 can you please change the name of tokens to be like