diff --git a/crosstl/src/translator/ast.py b/crosstl/src/translator/ast.py index f8f2f0b..b72690a 100644 --- a/crosstl/src/translator/ast.py +++ b/crosstl/src/translator/ast.py @@ -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): diff --git a/crosstl/src/translator/lexer.py b/crosstl/src/translator/lexer.py index 5b84282..2bf4ff8 100644 --- a/crosstl/src/translator/lexer.py +++ b/crosstl/src/translator/lexer.py @@ -28,6 +28,8 @@ ("RBRACE", r"\}"), ("LPAREN", r"\("), ("RPAREN", r"\)"), + ("LBRACKET", r"\["), + ("RBRACKET", r"\]"), ("SEMICOLON", r";"), ("COMMA", r","), ("ASSIGN_ADD", r"\+="), diff --git a/crosstl/src/translator/parser.py b/crosstl/src/translator/parser.py index 83f0daa..695dbb1 100644 --- a/crosstl/src/translator/parser.py +++ b/crosstl/src/translator/parser.py @@ -16,6 +16,7 @@ TernaryOpNode, VERTEXShaderNode, FRAGMENTShaderNode, + ArrayIndexNode, ) from .lexer import Lexer @@ -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