Skip to content

Commit

Permalink
Fix for Issue #88 in translator
Browse files Browse the repository at this point in the history
  • Loading branch information
adityanarayanan343 committed Sep 19, 2024
1 parent 98e5669 commit ca30d00
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crosstl/src/translator/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def __init__(
def __repr__(self):
return f"ShaderNode({self.name!r}) {self.global_inputs!r} {self.global_outputs!r} {self.global_functions!r} {self.vertex_section!r} {self.fragment_section!r}"

class ArrayIndexNode(ASTNode):
def __init__(self, array_name, index):
self.array_name = array_name
self.index = index
def __repr__(self):
return f"ArrayIndexNode({self.array_name}){self.index}"


class VERTEXShaderNode:
def __init__(self, inputs, outputs, functions, intermidiate):
Expand Down
2 changes: 2 additions & 0 deletions crosstl/src/translator/lexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
("RBRACE", r"\}"),
("LPAREN", r"\("),
("RPAREN", r"\)"),
("LBRACKET", r"\["),
("RBRACKET", r"\]"),
("SEMICOLON", r";"),
("COMMA", r","),
("ASSIGN_ADD", r"\+="),
Expand Down
28 changes: 28 additions & 0 deletions crosstl/src/translator/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
TernaryOpNode,
VERTEXShaderNode,
FRAGMENTShaderNode,
ArrayIndexNode,
)

from .lexer import Lexer
Expand Down Expand Up @@ -454,6 +455,33 @@ def parse_body(self):
else:
raise SyntaxError(f"Unexpected token {self.current_token[0]}")
return body
def parse_function_call_or_identifier(self):
"""Parse a function call, identifier, or array indexing."""

# Parse function calls or identifiers as before
if self.current_token[0] in ["VECTOR", "FLOAT", "DOUBLE", "UINT", "INT", "MATRIX"]:
func_name = self.current_token[1]
self.eat(self.current_token[0])
else:
func_name = self.current_token[1]
self.eat("IDENTIFIER")

# Check for array indexing
if self.current_token[0] == "LBRACKET":
return self.parse_array_index(func_name)

# Handle function calls and member access as usual
if self.current_token[0] == "LPAREN":
return self.parse_function_call(func_name)
elif self.current_token[0] == "DOT":
return self.parse_member_access(func_name)
return VariableNode("", func_name)

def parse_array_index(self, array_name):
self.eat("LBRACKET")
index = self.parse_expression()
self.eat("RBRACKET")
return ArrayIndexNode(array_name, index)

def parse_if_statement(self):
"""Parse an if statement
Expand Down

0 comments on commit ca30d00

Please sign in to comment.