Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crosstl/src/backend/Opengl/OpenglLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)"),
Expand All @@ -35,6 +37,9 @@
("ASSIGN_SUB", r"-="),
("ASSIGN_MUL", r"\*="),
("ASSIGN_DIV", r"/="),
("ASSIGN_OR", r"\|="),
("ASSIGN_AND", r"&="),
("ASSIGN_XOR", r"\^="),
("EQUAL", r"=="),
Copy link
Contributor

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

("ASSIGN_AND", r"&="),
    ("ASSIGN_OR", r"\|="),
    ("ASSIGN_XOR", r"\^="),

("NOT_EQUAL", r"!="),
("WHITESPACE", r"\s+"),
Expand All @@ -51,6 +56,11 @@
("OR", r"\|\|"),
("NOT", r"!"),
("PLUS", r"\+"),
("BITWISE_SHIFT_LEFT", r"<<"),
("BITWISE_SHIFT_RIGHT", r">>"),
("BITWISE_AND", r"&"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for these two token BITWISE_SHIFT_LEFT , BITWISE_SHIFT_RIGHT you havn't done anything as i have seen why have you added these in laxer token. either you can add support also for these two tokens in parser and codegen or you can just remove these token from here so that anyone else can work on this.

("BITWISE_OR", r"\|"),
("BITWISE_XOR", r"\^"),
("MINUS", r"-"),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Nripesh,
I have deleted those comments inside Lexer

("MULTIPLY", r"\*"),
("DIVIDE", r"/"),
Expand Down
61 changes: 49 additions & 12 deletions crosstl/src/backend/Opengl/OpenglParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
FRAGMENTShaderNode,
VersionDirectiveNode,
)
from .OpenglLexer import *


class GLSLParser:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The 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]}"
Expand All @@ -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":
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -443,6 +469,8 @@ def parse_type(self):
"VECTOR",
"FLOAT",
"INT",
"DOUBLE",
"UINT",
"MATRIX",
"BOOLEAN",
"SAMPLER2D",
Expand All @@ -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"]:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsigned int (opengl) -> uint (crossgl )

Copy link
Author

Choose a reason for hiding this comment

The 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:
Expand Down Expand Up @@ -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"]:
Expand Down Expand Up @@ -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",
Expand All @@ -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":
Expand Down
16 changes: 14 additions & 2 deletions crosstl/src/backend/Opengl/openglCrossglCodegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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": "&",
Copy link
Contributor

Choose a reason for hiding this comment

The 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 |= .

Copy link
Author

@ashwith2427 ashwith2427 Sep 13, 2024

Choose a reason for hiding this comment

The 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.

}
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Loading