| ... | ... | @@ -903,6 +903,7 @@ static AstNode *ast_parse_block(ParseContext *pc, int *token_index, bool mandato |
| 903 | 903 | static AstNode *ast_parse_if_expr(ParseContext *pc, int *token_index, bool mandatory); |
| 904 | 904 | static AstNode *ast_parse_block_expr(ParseContext *pc, int *token_index, bool mandatory); |
| 905 | 905 | static AstNode *ast_parse_unwrap_maybe_expr(ParseContext *pc, int *token_index, bool mandatory); |
| 906 | static AstNode *ast_parse_prefix_op_expr(ParseContext *pc, int *token_index, bool mandatory); |
| 906 | 907 | |
| 907 | 908 | static void ast_expect_token(ParseContext *pc, Token *token, TokenId token_id) { |
| 908 | 909 | if (token->id == token_id) { |
| ... | ... | @@ -998,7 +999,7 @@ static AstNode *ast_parse_param_decl(ParseContext *pc, int *token_index) { |
| 998 | 999 | *token_index += 1; |
| 999 | 1000 | ast_expect_token(pc, colon, TokenIdColon); |
| 1000 | 1001 | |
| 1001 | | node->data.param_decl.type = ast_parse_unwrap_maybe_expr(pc, token_index, true); |
| 1002 | node->data.param_decl.type = ast_parse_prefix_op_expr(pc, token_index, true); |
| 1002 | 1003 | |
| 1003 | 1004 | return node; |
| 1004 | 1005 | } |
| ... | ... | @@ -1114,7 +1115,7 @@ static AstNode *ast_parse_array_type_expr(ParseContext *pc, int *token_index, bo |
| 1114 | 1115 | node->data.array_type.is_const = true; |
| 1115 | 1116 | } |
| 1116 | 1117 | |
| 1117 | | node->data.array_type.child_type = ast_parse_unwrap_maybe_expr(pc, token_index, true); |
| 1118 | node->data.array_type.child_type = ast_parse_prefix_op_expr(pc, token_index, true); |
| 1118 | 1119 | |
| 1119 | 1120 | return node; |
| 1120 | 1121 | } |
| ... | ... | @@ -1159,7 +1160,7 @@ static void ast_parse_asm_output_item(ParseContext *pc, int *token_index, AstNod |
| 1159 | 1160 | if (token->id == TokenIdSymbol) { |
| 1160 | 1161 | ast_buf_from_token(pc, token, &asm_output->variable_name); |
| 1161 | 1162 | } else if (token->id == TokenIdArrow) { |
| 1162 | | asm_output->return_type = ast_parse_unwrap_maybe_expr(pc, token_index, true); |
| 1163 | asm_output->return_type = ast_parse_prefix_op_expr(pc, token_index, true); |
| 1163 | 1164 | } else { |
| 1164 | 1165 | ast_invalid_token_error(pc, token); |
| 1165 | 1166 | } |
| ... | ... | @@ -1402,81 +1403,23 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, int *token_index, bool |
| 1402 | 1403 | } |
| 1403 | 1404 | |
| 1404 | 1405 | /* |
| 1405 | | SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression | ContainerInitExpression) |
| 1406 | | FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen) |
| 1407 | | ArrayAccessExpression : token(LBracket) Expression token(RBracket) |
| 1408 | | SliceExpression : token(LBracket) Expression token(Ellipsis) option(Expression) token(RBracket) option(token(Const)) |
| 1409 | | FieldAccessExpression : token(Dot) token(Symbol) |
| 1406 | CurlySuffixExpression : PrefixOpExpression option(ContainerInitExpression) |
| 1410 | 1407 | ContainerInitExpression : token(LBrace) ContainerInitBody token(RBrace) |
| 1411 | 1408 | ContainerInitBody : list(StructLiteralField, token(Comma)) | list(Expression, token(Comma)) |
| 1412 | | StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression |
| 1413 | 1409 | */ |
| 1414 | | static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, bool mandatory) { |
| 1415 | | AstNode *primary_expr = ast_parse_primary_expr(pc, token_index, mandatory); |
| 1416 | | if (!primary_expr) { |
| 1410 | static AstNode *ast_parse_curly_suffix_expr(ParseContext *pc, int *token_index, bool mandatory) { |
| 1411 | AstNode *prefix_op_expr = ast_parse_prefix_op_expr(pc, token_index, mandatory); |
| 1412 | if (!prefix_op_expr) { |
| 1417 | 1413 | return nullptr; |
| 1418 | 1414 | } |
| 1419 | 1415 | |
| 1420 | 1416 | while (true) { |
| 1421 | 1417 | Token *first_token = &pc->tokens->at(*token_index); |
| 1422 | | if (first_token->id == TokenIdLParen) { |
| 1423 | | *token_index += 1; |
| 1424 | | |
| 1425 | | AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, first_token); |
| 1426 | | node->data.fn_call_expr.fn_ref_expr = primary_expr; |
| 1427 | | ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params); |
| 1428 | | |
| 1429 | | primary_expr = node; |
| 1430 | | } else if (first_token->id == TokenIdLBracket) { |
| 1431 | | *token_index += 1; |
| 1432 | | |
| 1433 | | AstNode *expr_node = ast_parse_expression(pc, token_index, true); |
| 1434 | | |
| 1435 | | Token *ellipsis_or_r_bracket = &pc->tokens->at(*token_index); |
| 1436 | | |
| 1437 | | if (ellipsis_or_r_bracket->id == TokenIdEllipsis) { |
| 1438 | | *token_index += 1; |
| 1439 | | |
| 1440 | | AstNode *node = ast_create_node(pc, NodeTypeSliceExpr, first_token); |
| 1441 | | node->data.slice_expr.array_ref_expr = primary_expr; |
| 1442 | | node->data.slice_expr.start = expr_node; |
| 1443 | | node->data.slice_expr.end = ast_parse_expression(pc, token_index, false); |
| 1444 | | |
| 1445 | | ast_eat_token(pc, token_index, TokenIdRBracket); |
| 1446 | | |
| 1447 | | Token *const_tok = &pc->tokens->at(*token_index); |
| 1448 | | if (const_tok->id == TokenIdKeywordConst) { |
| 1449 | | *token_index += 1; |
| 1450 | | node->data.slice_expr.is_const = true; |
| 1451 | | } |
| 1452 | | |
| 1453 | | primary_expr = node; |
| 1454 | | } else if (ellipsis_or_r_bracket->id == TokenIdRBracket) { |
| 1455 | | *token_index += 1; |
| 1456 | | |
| 1457 | | AstNode *node = ast_create_node(pc, NodeTypeArrayAccessExpr, first_token); |
| 1458 | | node->data.array_access_expr.array_ref_expr = primary_expr; |
| 1459 | | node->data.array_access_expr.subscript = expr_node; |
| 1460 | | |
| 1461 | | primary_expr = node; |
| 1462 | | } else { |
| 1463 | | ast_invalid_token_error(pc, first_token); |
| 1464 | | } |
| 1465 | | } else if (first_token->id == TokenIdDot) { |
| 1466 | | *token_index += 1; |
| 1467 | | |
| 1468 | | Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol); |
| 1469 | | |
| 1470 | | AstNode *node = ast_create_node(pc, NodeTypeFieldAccessExpr, first_token); |
| 1471 | | node->data.field_access_expr.struct_expr = primary_expr; |
| 1472 | | ast_buf_from_token(pc, name_token, &node->data.field_access_expr.field_name); |
| 1473 | | |
| 1474 | | primary_expr = node; |
| 1475 | | } else if (first_token->id == TokenIdLBrace) { |
| 1418 | if (first_token->id == TokenIdLBrace) { |
| 1476 | 1419 | *token_index += 1; |
| 1477 | 1420 | |
| 1478 | 1421 | AstNode *node = ast_create_node(pc, NodeTypeContainerInitExpr, first_token); |
| 1479 | | node->data.container_init_expr.type = primary_expr; |
| 1422 | node->data.container_init_expr.type = prefix_op_expr; |
| 1480 | 1423 | |
| 1481 | 1424 | Token *token = &pc->tokens->at(*token_index); |
| 1482 | 1425 | if (token->id == TokenIdDot) { |
| ... | ... | @@ -1536,6 +1479,81 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, boo |
| 1536 | 1479 | } |
| 1537 | 1480 | } |
| 1538 | 1481 | |
| 1482 | prefix_op_expr = node; |
| 1483 | } else { |
| 1484 | return prefix_op_expr; |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | /* |
| 1490 | SuffixOpExpression : PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression) |
| 1491 | FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen) |
| 1492 | ArrayAccessExpression : token(LBracket) Expression token(RBracket) |
| 1493 | SliceExpression : token(LBracket) Expression token(Ellipsis) option(Expression) token(RBracket) option(token(Const)) |
| 1494 | FieldAccessExpression : token(Dot) token(Symbol) |
| 1495 | StructLiteralField : token(Dot) token(Symbol) token(Eq) Expression |
| 1496 | */ |
| 1497 | static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, int *token_index, bool mandatory) { |
| 1498 | AstNode *primary_expr = ast_parse_primary_expr(pc, token_index, mandatory); |
| 1499 | if (!primary_expr) { |
| 1500 | return nullptr; |
| 1501 | } |
| 1502 | |
| 1503 | while (true) { |
| 1504 | Token *first_token = &pc->tokens->at(*token_index); |
| 1505 | if (first_token->id == TokenIdLParen) { |
| 1506 | *token_index += 1; |
| 1507 | |
| 1508 | AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, first_token); |
| 1509 | node->data.fn_call_expr.fn_ref_expr = primary_expr; |
| 1510 | ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params); |
| 1511 | |
| 1512 | primary_expr = node; |
| 1513 | } else if (first_token->id == TokenIdLBracket) { |
| 1514 | *token_index += 1; |
| 1515 | |
| 1516 | AstNode *expr_node = ast_parse_expression(pc, token_index, true); |
| 1517 | |
| 1518 | Token *ellipsis_or_r_bracket = &pc->tokens->at(*token_index); |
| 1519 | |
| 1520 | if (ellipsis_or_r_bracket->id == TokenIdEllipsis) { |
| 1521 | *token_index += 1; |
| 1522 | |
| 1523 | AstNode *node = ast_create_node(pc, NodeTypeSliceExpr, first_token); |
| 1524 | node->data.slice_expr.array_ref_expr = primary_expr; |
| 1525 | node->data.slice_expr.start = expr_node; |
| 1526 | node->data.slice_expr.end = ast_parse_expression(pc, token_index, false); |
| 1527 | |
| 1528 | ast_eat_token(pc, token_index, TokenIdRBracket); |
| 1529 | |
| 1530 | Token *const_tok = &pc->tokens->at(*token_index); |
| 1531 | if (const_tok->id == TokenIdKeywordConst) { |
| 1532 | *token_index += 1; |
| 1533 | node->data.slice_expr.is_const = true; |
| 1534 | } |
| 1535 | |
| 1536 | primary_expr = node; |
| 1537 | } else if (ellipsis_or_r_bracket->id == TokenIdRBracket) { |
| 1538 | *token_index += 1; |
| 1539 | |
| 1540 | AstNode *node = ast_create_node(pc, NodeTypeArrayAccessExpr, first_token); |
| 1541 | node->data.array_access_expr.array_ref_expr = primary_expr; |
| 1542 | node->data.array_access_expr.subscript = expr_node; |
| 1543 | |
| 1544 | primary_expr = node; |
| 1545 | } else { |
| 1546 | ast_invalid_token_error(pc, first_token); |
| 1547 | } |
| 1548 | } else if (first_token->id == TokenIdDot) { |
| 1549 | *token_index += 1; |
| 1550 | |
| 1551 | Token *name_token = ast_eat_token(pc, token_index, TokenIdSymbol); |
| 1552 | |
| 1553 | AstNode *node = ast_create_node(pc, NodeTypeFieldAccessExpr, first_token); |
| 1554 | node->data.field_access_expr.struct_expr = primary_expr; |
| 1555 | ast_buf_from_token(pc, name_token, &node->data.field_access_expr.field_name); |
| 1556 | |
| 1539 | 1557 | primary_expr = node; |
| 1540 | 1558 | } else { |
| 1541 | 1559 | return primary_expr; |
| ... | ... | @@ -1623,10 +1641,10 @@ static BinOpType ast_parse_mult_op(ParseContext *pc, int *token_index, bool mand |
| 1623 | 1641 | } |
| 1624 | 1642 | |
| 1625 | 1643 | /* |
| 1626 | | MultiplyExpression : PrefixOpExpression MultiplyOperator MultiplyExpression | PrefixOpExpression |
| 1644 | MultiplyExpression : CurlySuffixExpression MultiplyOperator MultiplyExpression | CurlySuffixExpression |
| 1627 | 1645 | */ |
| 1628 | 1646 | static AstNode *ast_parse_mult_expr(ParseContext *pc, int *token_index, bool mandatory) { |
| 1629 | | AstNode *operand_1 = ast_parse_prefix_op_expr(pc, token_index, mandatory); |
| 1647 | AstNode *operand_1 = ast_parse_curly_suffix_expr(pc, token_index, mandatory); |
| 1630 | 1648 | if (!operand_1) |
| 1631 | 1649 | return nullptr; |
| 1632 | 1650 | |
| ... | ... | @@ -1636,7 +1654,7 @@ static AstNode *ast_parse_mult_expr(ParseContext *pc, int *token_index, bool man |
| 1636 | 1654 | if (mult_op == BinOpTypeInvalid) |
| 1637 | 1655 | return operand_1; |
| 1638 | 1656 | |
| 1639 | | AstNode *operand_2 = ast_parse_prefix_op_expr(pc, token_index, true); |
| 1657 | AstNode *operand_2 = ast_parse_curly_suffix_expr(pc, token_index, true); |
| 1640 | 1658 | |
| 1641 | 1659 | AstNode *node = ast_create_node(pc, NodeTypeBinOpExpr, token); |
| 1642 | 1660 | node->data.bin_op_expr.op1 = operand_1; |
| ... | ... | @@ -1948,7 +1966,7 @@ static AstNode *ast_parse_if_expr(ParseContext *pc, int *token_index, bool manda |
| 1948 | 1966 | node->data.if_var_expr.var_decl.expr = ast_parse_expression(pc, token_index, true); |
| 1949 | 1967 | } else if (eq_or_colon->id == TokenIdColon) { |
| 1950 | 1968 | *token_index += 1; |
| 1951 | | node->data.if_var_expr.var_decl.type = ast_parse_unwrap_maybe_expr(pc, token_index, true); |
| 1969 | node->data.if_var_expr.var_decl.type = ast_parse_prefix_op_expr(pc, token_index, true); |
| 1952 | 1970 | |
| 1953 | 1971 | ast_eat_token(pc, token_index, TokenIdMaybeAssign); |
| 1954 | 1972 | node->data.if_var_expr.var_decl.expr = ast_parse_expression(pc, token_index, true); |
| ... | ... | @@ -2028,7 +2046,7 @@ static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, int *token |
| 2028 | 2046 | node->data.variable_declaration.expr = ast_parse_expression(pc, token_index, true); |
| 2029 | 2047 | return node; |
| 2030 | 2048 | } else if (eq_or_colon->id == TokenIdColon) { |
| 2031 | | node->data.variable_declaration.type = ast_parse_unwrap_maybe_expr(pc, token_index, true); |
| 2049 | node->data.variable_declaration.type = ast_parse_prefix_op_expr(pc, token_index, true); |
| 2032 | 2050 | Token *eq_token = &pc->tokens->at(*token_index); |
| 2033 | 2051 | if (eq_token->id == TokenIdEq) { |
| 2034 | 2052 | *token_index += 1; |
| ... | ... | @@ -2394,7 +2412,7 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, int *token_index, bool mand |
| 2394 | 2412 | ast_parse_param_decl_list(pc, token_index, &node->data.fn_proto.params, &node->data.fn_proto.is_var_args); |
| 2395 | 2413 | |
| 2396 | 2414 | Token *next_token = &pc->tokens->at(*token_index); |
| 2397 | | node->data.fn_proto.return_type = ast_parse_unwrap_maybe_expr(pc, token_index, false); |
| 2415 | node->data.fn_proto.return_type = ast_parse_prefix_op_expr(pc, token_index, false); |
| 2398 | 2416 | if (!node->data.fn_proto.return_type) { |
| 2399 | 2417 | node->data.fn_proto.return_type = ast_create_void_type_node(pc, next_token); |
| 2400 | 2418 | } |