diff --git a/AST.jai b/AST.jai index fab7f95..7e71e6a 100644 --- a/AST.jai +++ b/AST.jai @@ -54,6 +54,8 @@ AST_Node :: struct { token : Token; + array_field : bool; + source_location : Source_Range; type_variable : Type_Variable_Handle; @@ -105,7 +107,15 @@ pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Bu print_to_builder(builder, tprint("(:= %", node.name)); if node.kind != .Unnamed_Field && node.token.ident_value.count > 0 { - print_to_builder(builder, tprint(" %", node.token.ident_value)); + if node.array_field { + append(builder, " ["); + pretty_print_node(node.children[0], 0, builder); + append(builder, "]."); + print_to_builder(builder, "%", node.token.ident_value); + } else { + print_to_builder(builder, " %", node.token.ident_value); + } + } for hint : node.hint_tokens { @@ -114,7 +124,7 @@ pretty_print_field :: (node : *AST_Node, indentation : int, builder : *String_Bu } } - if node.children.count > 0 { + if !node.array_field && node.children.count > 0 { append(builder, " "); pretty_print_children(node, indentation, builder); } @@ -271,10 +281,16 @@ pretty_print_node :: (node : *AST_Node, indentation : int, builder : *String_Bui pretty_print_variable :: (node : *AST_Node, indentation : int, builder : *String_Builder) { indent(builder, indentation); print_to_builder(builder, "%", node.name); + for child : node.children { + print("%\n", child.kind); if child.kind == .Variable { append(builder, "."); pretty_print_variable(child, indentation, builder); + } else if child.kind == .Unary { + append(builder, "["); + pretty_print_node(child.children[0], 0, builder); + append(builder, "]"); } } } diff --git a/Parsing.jai b/Parsing.jai index 2e0d663..8214d00 100644 --- a/Parsing.jai +++ b/Parsing.jai @@ -84,6 +84,8 @@ parse_rules :: #run -> [(cast(int)Token_Kind.TOKEN_ERROR) + 1]Parse_Rule { rules[Token_Kind.TOKEN_RIGHTPAREN] = .{null, null, .PREC_NONE}; rules[Token_Kind.TOKEN_LEFTBRACE] = .{null, null, .PREC_NONE}; rules[Token_Kind.TOKEN_RIGHTBRACE] = .{null, null, .PREC_NONE}; + rules[Token_Kind.TOKEN_LEFTBRACKET] = .{null, array_access, .PREC_CALL}; + rules[Token_Kind.TOKEN_RIGHTBRACKET] = .{null, null, .PREC_NONE}; rules[Token_Kind.TOKEN_COMMA] = .{null, null, .PREC_NONE}; rules[Token_Kind.TOKEN_DOT] = .{null, dot, .PREC_CALL}; rules[Token_Kind.TOKEN_PROPERTIES] = .{named_variable, null, .PREC_CALL}; @@ -429,6 +431,36 @@ binary :: (parse_state : *Parse_State, left : *AST_Node) -> *AST_Node { return binary_expression; } +array_access :: (parse_state : *Parse_State, left : *AST_Node) -> *AST_Node { + identifier := parse_state.tokens[parse_state.current_token_index - 3]; + left_bracket := parse_state.tokens[parse_state.current_token_index - 2]; + + array_access := make_node(parse_state, .Unary); + array_access.token = left_bracket; + array_index := expression(parse_state); + add_child(array_access, array_index); + + add_child(left, array_access); + + consume(parse_state, .TOKEN_RIGHTBRACKET, "Expected ']' after array index."); + + source_location : Source_Range; + source_location.begin = left.source_location.begin; + + if check(parse_state, .TOKEN_ASSIGN) { + advance(parse_state); + + node := make_node(parse_state, .Binary); + node.token = parse_state.previous; + add_child(node, left); + add_child(node, expression(parse_state)); + return node; + } + + source_location.end = parse_state.previous; + return left; +} + unary :: (parse_state : *Parse_State, left : *AST_Node) -> *AST_Node { op := parse_state.previous.*; rule := get_rule(op.kind); @@ -441,6 +473,10 @@ unary :: (parse_state : *Parse_State, left : *AST_Node) -> *AST_Node { case .TOKEN_MINUS; { unary_expression.token = op; } + case .TOKEN_LEFTBRACKET; { + unary_expression.token = op; + consume(parse_state, .TOKEN_RIGHTBRACKET, "Expect ']' after array access."); + } } return unary_expression; @@ -588,6 +624,17 @@ field_declaration :: (parse_state : *Parse_State, identifier_token : *Token) -> type_identifier := parse_state.current; node.token = type_identifier; advance(parse_state); + } else if check(parse_state, .TOKEN_LEFTBRACKET) { + advance(parse_state); + array_size_expression := expression(parse_state); + add_child(node, array_size_expression); + consume(parse_state, .TOKEN_RIGHTBRACKET, "Expected closing ']' in array declaration."); + consume(parse_state, .TOKEN_DOT, "Expected '.' before array type."); + + type_identifier := parse_state.current; + node.token = type_identifier; + advance(parse_state); + node.array_field = true; } else { missing_type_specifier(parse_state, identifier_token, "Expected type specifier after field name."); return node; diff --git a/test/arrays.shd b/test/arrays.shd new file mode 100644 index 0000000..021f2ec --- /dev/null +++ b/test/arrays.shd @@ -0,0 +1,5 @@ +vertex main :: () -> float4 @position { + arr : [16].float4; + arr[0] = float4(1,1,1); + return arr[0]; +} diff --git a/test/lex/arrays.golden b/test/lex/arrays.golden new file mode 100644 index 0000000..2312b77 --- /dev/null +++ b/test/lex/arrays.golden @@ -0,0 +1,40 @@ +{kind = TOKEN_VERTEX; ; index = 0 ; length = 6 line = 1 ; column = 0 ; value ='vertex'; } +{kind = TOKEN_IDENTIFIER; ; index = 7 ; length = 4 line = 1 ; column = 7 ; value ='main'; } +{kind = TOKEN_DOUBLECOLON; ; index = 12 ; length = 2 line = 1 ; column = 12 ; value ='::'; } +{kind = TOKEN_LEFTPAREN; ; index = 15 ; length = 1 line = 1 ; column = 15 ; value ='('; } +{kind = TOKEN_RIGHTPAREN; ; index = 16 ; length = 1 line = 1 ; column = 16 ; value =')'; } +{kind = TOKEN_ARROW; ; index = 18 ; length = 2 line = 1 ; column = 18 ; value ='->'; } +{kind = TOKEN_IDENTIFIER; ; index = 21 ; length = 6 line = 1 ; column = 21 ; value ='float4'; } +{kind = TOKEN_AT; ; index = 28 ; length = 1 line = 1 ; column = 28 ; value ='@'; } +{kind = TOKEN_IDENTIFIER; ; index = 29 ; length = 8 line = 1 ; column = 29 ; value ='position'; } +{kind = TOKEN_LEFTBRACE; ; index = 38 ; length = 1 line = 1 ; column = 38 ; value ='{'; } +{kind = TOKEN_IDENTIFIER; ; index = 42 ; length = 3 line = 2 ; column = 0 ; value ='arr'; } +{kind = TOKEN_COLON; ; index = 46 ; length = 1 line = 2 ; column = 4 ; value =':'; } +{kind = TOKEN_LEFTBRACKET; ; index = 48 ; length = 1 line = 2 ; column = 6 ; value ='['; } +{kind = TOKEN_INTLITERAL; ; index = 49 ; length = 2 line = 2 ; column = 7 ; value ='16'; } +{kind = TOKEN_RIGHTBRACKET; ; index = 51 ; length = 1 line = 2 ; column = 9 ; value =']'; } +{kind = TOKEN_DOT; ; index = 52 ; length = 1 line = 2 ; column = 10 ; value ='.'; } +{kind = TOKEN_IDENTIFIER; ; index = 53 ; length = 6 line = 2 ; column = 11 ; value ='float4'; } +{kind = TOKEN_SEMICOLON; ; index = 59 ; length = 1 line = 2 ; column = 17 ; value =';'; } +{kind = TOKEN_IDENTIFIER; ; index = 63 ; length = 3 line = 3 ; column = 0 ; value ='arr'; } +{kind = TOKEN_LEFTBRACKET; ; index = 66 ; length = 1 line = 3 ; column = 3 ; value ='['; } +{kind = TOKEN_INTLITERAL; ; index = 67 ; length = 1 line = 3 ; column = 4 ; value ='0'; } +{kind = TOKEN_RIGHTBRACKET; ; index = 68 ; length = 1 line = 3 ; column = 5 ; value =']'; } +{kind = TOKEN_ASSIGN; ; index = 70 ; length = 1 line = 3 ; column = 7 ; value ='='; } +{kind = TOKEN_IDENTIFIER; ; index = 72 ; length = 6 line = 3 ; column = 9 ; value ='float4'; } +{kind = TOKEN_LEFTPAREN; ; index = 78 ; length = 1 line = 3 ; column = 15 ; value ='('; } +{kind = TOKEN_INTLITERAL; ; index = 79 ; length = 1 line = 3 ; column = 16 ; value ='1'; } +{kind = TOKEN_COMMA; ; index = 80 ; length = 1 line = 3 ; column = 17 ; value =','; } +{kind = TOKEN_INTLITERAL; ; index = 81 ; length = 1 line = 3 ; column = 18 ; value ='1'; } +{kind = TOKEN_COMMA; ; index = 82 ; length = 1 line = 3 ; column = 19 ; value =','; } +{kind = TOKEN_INTLITERAL; ; index = 83 ; length = 1 line = 3 ; column = 20 ; value ='1'; } +{kind = TOKEN_RIGHTPAREN; ; index = 84 ; length = 1 line = 3 ; column = 21 ; value =')'; } +{kind = TOKEN_SEMICOLON; ; index = 85 ; length = 1 line = 3 ; column = 22 ; value =';'; } +{kind = TOKEN_RETURN; ; index = 89 ; length = 6 line = 4 ; column = 0 ; value ='return'; } +{kind = TOKEN_IDENTIFIER; ; index = 96 ; length = 3 line = 4 ; column = 7 ; value ='arr'; } +{kind = TOKEN_LEFTBRACKET; ; index = 99 ; length = 1 line = 4 ; column = 10 ; value ='['; } +{kind = TOKEN_INTLITERAL; ; index = 100 ; length = 1 line = 4 ; column = 11 ; value ='0'; } +{kind = TOKEN_RIGHTBRACKET; ; index = 101 ; length = 1 line = 4 ; column = 12 ; value =']'; } +{kind = TOKEN_SEMICOLON; ; index = 102 ; length = 1 line = 4 ; column = 13 ; value =';'; } +{kind = TOKEN_RIGHTBRACE; ; index = 105 ; length = 1 line = 5 ; column = 0 ; value ='}'; } +{kind = TOKEN_EOF; ; index = 108 ; length = 0 line = 6 ; column = 0 ; value =''; } diff --git a/test/parse/arrays.golden b/test/parse/arrays.golden new file mode 100644 index 0000000..f518314 --- /dev/null +++ b/test/parse/arrays.golden @@ -0,0 +1,6 @@ +(program + (fun vertex vs_main -> float4 (@position) + [] + (:= arr [16].float4) + (= arr[0] (float4 1 1 1)) + (return arr[0]))) \ No newline at end of file