Skip to content

Commit

Permalink
Small lint.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaatsalya123 committed Aug 7, 2024
1 parent 01f2afc commit 3bb45b6
Show file tree
Hide file tree
Showing 18 changed files with 404 additions and 236 deletions.
2 changes: 1 addition & 1 deletion crosstl/src/backend/DirectX/DirectxAst.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def __init__(self, left, op, right):
self.right = right

def __repr__(self):
return f"BinaryOpNode(left={self.left}, operator={self.op}, right={self.right})"
return f"BinaryOpNode(left={self.left}, op={self.op}, right={self.right})"


class MemberAccessNode(ASTNode):
Expand Down
5 changes: 4 additions & 1 deletion crosstl/src/backend/DirectX/DirectxCrossGLCodeGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,15 @@ def generate_function_body(self, body, indent=0, is_main=False):
for stmt in body:
code += " " * indent
if isinstance(stmt, VariableNode):
if stmt.vtype not in self.type_map.keys():
if stmt.vtype in ["VSOutput", "PSOutput"]:
continue
else:
code += f"{self.map_type(stmt.vtype)} {stmt.name};\n"
elif isinstance(stmt, AssignmentNode):
code += self.generate_assignment(stmt, is_main) + ";\n"

elif isinstance(stmt, BinaryOpNode):
code += f"{self.generate_expression(stmt.left, is_main)} {stmt.op} {self.generate_expression(stmt.right, is_main)};\n"
elif isinstance(stmt, ReturnNode):
if not is_main:
code += f"return {self.generate_expression(stmt.value, is_main)};\n"
Expand Down
50 changes: 27 additions & 23 deletions crosstl/src/backend/DirectX/DirectxLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
TOKENS = [
("COMMENT_SINGLE", r"//.*"),
("COMMENT_MULTI", r"/\*[\s\S]*?\*/"),
("STRUCT", r"struct"),
("CBUFFER", r"cbuffer"),
("TEXTURE2D", r"Texture2D"),
("SAMPLER_STATE", r"SamplerState"),
("FVECTOR", r"float[2-4]"),
("FLOAT", r"float"),
("INT", r"int"),
("UINT", r"uint"),
("BOOL", r"bool"),
("MATRIX", r"float[2-4]x[2-4]"),
("VOID", r"void"),
("RETURN", r"return"),
("IF", r"if"),
("ELSE", r"else"),
("FOR", r"for"),
("REGISTER", r"register"),
("STRUCT", r"\bstruct\b"),
("CBUFFER", r"\bcbuffer\b"),
("TEXTURE2D", r"\bTexture2D\b"),
("SAMPLER_STATE", r"\bSamplerState\b"),
("FVECTOR", r"\bfloat[2-4]\b"),
("FLOAT", r"\bfloat\b"),
("INT", r"\bint\b"),
("UINT", r"\buint\b"),
("BOOL", r"\bbool\b"),
("MATRIX", r"\bfloat[2-4]x[2-4]\b"),
("VOID", r"\bvoid\b"),
("RETURN", r"\breturn\b"),
("IF", r"\bif\b"),
("ELSE", r"\belse\b"),
("FOR", r"\bfor\b"),
("REGISTER", r"\bregister\b"),
("SEMANTIC", r": [A-Z_][A-Z0-9_]*"),
("IDENTIFIER", r"[a-zA-Z_][a-zA-Z0-9_]*"),
("NUMBER", r"\d+(\.\d+)?"),
Expand All @@ -32,20 +32,24 @@
("COMMA", r","),
("COLON", r":"),
("QUESTION", r"\?"),
("EQUALS", r"="),
("PLUS", r"\+"),
("MINUS", r"-"),
("MULTIPLY", r"\*"),
("DIVIDE", r"/"),
("LESS_THAN", r"<"),
("GREATER_THAN", r">"),
("LESS_EQUAL", r"<="),
("GREATER_EQUAL", r">="),
("LESS_THAN", r"<"),
("GREATER_THAN", r">"),
("EQUAL", r"=="),
("NOT_EQUAL", r"!="),
("PLUS_EQUALS", r"\+="),
("MINUS_EQUALS", r"-="),
("MULTIPLY_EQUALS", r"\*="),
("DIVIDE_EQUALS", r"/="),
("AND", r"&&"),
("OR", r"\|\|"),
("DOT", r"\."),
("MULTIPLY", r"\*"),
("DIVIDE", r"/"),
("PLUS", r"\+"),
("MINUS", r"-"),
("EQUALS", r"="),
("WHITESPACE", r"\s+"),
]

Expand Down
20 changes: 17 additions & 3 deletions crosstl/src/backend/DirectX/DirectxParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,23 @@ def parse_variable_declaration_or_assignment(self):
self.eat("SEMICOLON")
return left
else:
expr = self.parse_expression()
self.eat("SEMICOLON")
return expr
if self.current_token[0] in [
"PLUS_EQUALS",
"MINUS_EQUALS",
"MULTIPLY_EQUALS",
"DIVIDE_EQUALS",
"EQUAL",
]:
op = self.current_token[1]
self.eat(self.current_token[0])
expr = self.parse_expression()
self.eat("SEMICOLON")
return BinaryOpNode(VariableNode("", first_token[1]), op, expr)
# This handles cases like "float3(1.0, 1.0, 1.0);"
else:
expr = self.parse_expression()
self.eat("SEMICOLON")
return expr
else:
expr = self.parse_expression()
self.eat("SEMICOLON")
Expand Down
4 changes: 3 additions & 1 deletion crosstl/src/backend/Metal/MetalCrossGLCodeGen.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def generate_function_body(self, body, indent=0, is_main=False):
for stmt in body:
code += " " * indent
if isinstance(stmt, VariableNode):
if stmt.vtype not in self.type_map.keys():
if stmt.vtype in ["Vertex_OUTPUT", "Fragment_OUTPUT"]:
continue
else:
code += f"{self.map_type(stmt.vtype)} {stmt.name};\n"
Expand All @@ -148,6 +148,8 @@ def generate_function_body(self, body, indent=0, is_main=False):
elif isinstance(stmt, ReturnNode):
if not is_main:
code += f"return {self.generate_expression(stmt.value, is_main)};\n"
elif isinstance(stmt, BinaryOpNode):
code += f"{self.generate_expression(stmt.left, is_main)} {stmt.op} {self.generate_expression(stmt.right, is_main)};\n"
elif isinstance(stmt, ForNode):
code += self.generate_for_loop(stmt, indent, is_main)
elif isinstance(stmt, IfNode):
Expand Down
66 changes: 35 additions & 31 deletions crosstl/src/backend/Metal/MetalLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
("COMMENT_SINGLE", r"//.*"),
("COMMENT_MULTI", r"/\*[\s\S]*?\*/"),
("PREPROCESSOR", r"#\w+"),
("STRUCT", r"struct"),
("CONSTANT", r"constant"),
("TEXTURE2D", r"texture2d"),
("SAMPLER", r"sampler"),
("VECTOR", r"(float|half|int|uint)[2-4]"),
("FLOAT", r"float"),
("HALF", r"half"),
("INT", r"int"),
("UINT", r"uint"),
("STRUCT", r"\bstruct\b"),
("CONSTANT", r"\bconstant\b"),
("TEXTURE2D", r"\btexture2d\b"),
("SAMPLER", r"\bsampler\b"),
("VECTOR", r"\b(float|half|int|uint)[2-4]\b"),
("FLOAT", r"\bfloat\b"),
("HALF", r"\bhalf\b"),
("INT", r"\bint\b"),
("UINT", r"\buint\b"),
("QUESTION", r"\?"),
("BOOL", r"bool"),
("VOID", r"void"),
("RETURN", r"return"),
("IF", r"if"),
("ELSE", r"else"),
("FOR", r"for"),
("KERNEL", r"kernel"),
("VERTEX", r"vertex"),
("FRAGMENT", r"fragment"),
("USING", r"using"),
("NAMESPACE", r"namespace"),
("METAL", r"metal"),
("DEVICE", r"device"),
("THREADGROUP", r"threadgroup"),
("THREAD", r"thread"),
("BOOL", r"\bbool\b"),
("VOID", r"\bvoid\b"),
("RETURN", r"\breturn\b"),
("IF", r"\bif\b"),
("ELSE", r"\belse\b"),
("FOR", r"\bfor\b"),
("KERNEL", r"\bkernel\b"),
("VERTEX", r"\bvertex\b"),
("FRAGMENT", r"\bfragment\b"),
("USING", r"\busing\b"),
("NAMESPACE", r"\bnamespace\b"),
("METAL", r"\bmetal\b"),
("DEVICE", r"\bdevice\b"),
("THREADGROUP", r"\bthreadgroup\b"),
("THREAD", r"\bthread\b"),
("ATTRIBUTE", r"\[\[.*?\]\]"),
("IDENTIFIER", r"[a-zA-Z_][a-zA-Z0-9_]*"),
("NUMBER", r"\d+(\.\d+)?"),
Expand All @@ -42,20 +42,24 @@
("STRING", r'"[^"]*"'),
("COMMA", r","),
("COLON", r":"),
("EQUALS", r"="),
("PLUS", r"\+"),
("MINUS", r"-"),
("MULTIPLY", r"\*"),
("DIVIDE", r"/"),
("LESS_THAN", r"<"),
("GREATER_THAN", r">"),
("LESS_EQUAL", r"<="),
("GREATER_EQUAL", r">="),
("LESS_THAN", r"<"),
("GREATER_THAN", r">"),
("EQUAL", r"=="),
("NOT_EQUAL", r"!="),
("PLUS_EQUALS", r"\+="),
("MINUS_EQUALS", r"-="),
("MULTIPLY_EQUALS", r"\*="),
("DIVIDE_EQUALS", r"/="),
("PLUS", r"\+"),
("MINUS", r"-"),
("MULTIPLY", r"\*"),
("DIVIDE", r"/"),
("AND", r"&&"),
("OR", r"\|\|"),
("DOT", r"\."),
("EQUALS", r"="),
("WHITESPACE", r"\s+"),
]

Expand Down
20 changes: 17 additions & 3 deletions crosstl/src/backend/Metal/MetalParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,23 @@ def parse_variable_declaration_or_assignment(self):
self.eat("SEMICOLON")
return left
else:
expr = self.parse_expression()
self.eat("SEMICOLON")
return expr
if self.current_token[0] in [
"PLUS_EQUALS",
"MINUS_EQUALS",
"MULTIPLY_EQUALS",
"DIVIDE_EQUALS",
"EQUAL",
]:
op = self.current_token[1]
self.eat(self.current_token[0])
expr = self.parse_expression()
self.eat("SEMICOLON")
return BinaryOpNode(VariableNode("", first_token[1]), op, expr)
# This handles cases like "float3(1.0, 1.0, 1.0);"
else:
expr = self.parse_expression()
self.eat("SEMICOLON")
return expr
else:
expr = self.parse_expression()
self.eat("SEMICOLON")
Expand Down
5 changes: 3 additions & 2 deletions crosstl/src/backend/Opengl/OpenglAst.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ def __repr__(self):


class LayoutNode:
def __init__(self, section, location_number, dtype, name):
def __init__(self, section, location_number, dtype, name,io_type):
self.section = section
self.location_number = location_number
self.dtype = dtype
self.name = name
self.io_type = io_type

def __repr__(self):
return f"LayoutNode(section={self.section}, location_number={self.location_number}, dtype={self.dtype}, name={self.name})"
return f"LayoutNode(section={self.section}, location_number={self.location_number}, dtype={self.dtype}, name={self.name}, io_type={self.io_type})"


class ShaderNode:
Expand Down
65 changes: 32 additions & 33 deletions crosstl/src/backend/Opengl/OpenglLexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
TOKENS = [
("COMMENT_SINGLE", r"//.*"),
("COMMENT_MULTI", r"/\*[\s\S]*?\*/"),
("VERSION", r"#version"), # Match #version as a separate token
("NUMBER", r"\d+(\.\d+)?"), # Match version numbers like 330 or 330.0
("CORE", r"core"), # Match the keyword core
("SHADER", r"shader"),
("INPUT", r"input"),
("OUTPUT", r"output"),
("VOID", r"void"),
("MAIN", r"main"),
("UNIFORM", r"uniform"),
("VECTOR", r"vec[2-4]"),
("MATRIX", r"mat[2-4]"),
("BOOL", r"bool"),
("FLOAT", r"float"),
("INT", r"int"),
("SAMPLER2D", r"sampler2D"),
("PRE_INCREMENT", r"\+\+(?=\w)"), # Lookahead to match pre-increment
("PRE_DECREMENT", r"--(?=\w)"), # Lookahead to match pre-decrement
("POST_INCREMENT", r"(?<=\w)\+\+"), # Lookbehind to match post-increment
("POST_DECREMENT", r"(?<=\w)--"), # Lookbehind to match post-decrement
("VERSION", r"#version"),
("NUMBER", r"\d+(\.\d+)?"),
("CORE", r"\bcore\b"),
("SHADER", r"\bshader\b"),
("INPUT", r"\binput\b"),
("OUTPUT", r"\boutput\b"),
("VOID", r"\bvoid\b"),
("MAIN", r"\bmain\b"),
("UNIFORM", r"\buniform\b"),
("VECTOR", r"\bvec[2-4]\b"),
("MATRIX", r"\bmat[2-4]\b"),
("BOOL", r"\bbool\b"),
("FLOAT", r"\bfloat\b"),
("INT", r"\bint\b"),
("SAMPLER2D", r"\bsampler2D\b"),
("PRE_INCREMENT", r"\+\+(?=\w)"),
("PRE_DECREMENT", r"--(?=\w)"),
("POST_INCREMENT", r"(?<=\w)\+\+"),
("POST_DECREMENT", r"(?<=\w)--"),
("IDENTIFIER", r"[a-zA-Z_][a-zA-Z_0-9]*"),
("LBRACE", r"\{"),
("RBRACE", r"\}"),
Expand All @@ -34,19 +34,17 @@
("ASSIGN_SUB", r"-="),
("ASSIGN_MUL", r"\*="),
("ASSIGN_DIV", r"/="),
("EQUALS", r"="),
("EQUAL", r"=="),
("NOT_EQUAL", r"!="),
("WHITESPACE", r"\s+"),
("IF", r"if"),
("ELSE", r"else"),
("FOR", r"for"),
("RETURN", r"return"),
("LESS_THAN", r"<"),
("GREATER_THAN", r">"),
("INCREMENT", r"\+\+"),
("DECREMENT", r"--"),
("IF", r"\bif\b"),
("ELSE", r"\belse\b"),
("FOR", r"\bfor\b"),
("RETURN", r"\breturn\b"),
("LESS_EQUAL", r"<="),
("GREATER_EQUAL", r">="),
("EQUAL", r"=="),
("LESS_THAN", r"<"),
("GREATER_THAN", r">"),
("NOT_EQUAL", r"!="),
("AND", r"&&"),
("OR", r"\|\|"),
Expand All @@ -56,11 +54,12 @@
("MULTIPLY", r"\*"),
("DIVIDE", r"/"),
("DOT", r"\."),
("EQUALS", r"="),
("QUESTION", r"\?"),
("COLON", r":"),
("LAYOUT", r"layout"),
("IN", r"in"),
("OUT", r"out"),
("LAYOUT", r"\blayout\b"),
("IN", r"\bin\b"),
("OUT", r"\bout\b"),
]

KEYWORDS = {
Expand Down Expand Up @@ -115,4 +114,4 @@ def tokenize(self):
f"Illegal character '{unmatched_char}' at position {pos}\n{highlighted_code}"
)

self.tokens.append(("EOF", None)) # End of file token
self.tokens.append(("EOF", None))
Loading

0 comments on commit 3bb45b6

Please sign in to comment.