src/onyxparser.c
// Returns:
//     0 - if this was not a symbol declaration.
//     1 - if this was a local declaration.
//     2 - if this was binding declaration.
// ret is set to the statement to insert
// <symbol> : <type> = <expr>
// <symbol> : <type> : <expr>
// <symbol> := <expr>
// <symbol> :: <expr>
// <symbol> (, <symbol>)* : <type>
// <symbol> (, <symbol>)* := <expr>
if this was not a symbol declaration.
1 - if this was a local declaration.
2 - if this was binding declaration.
if this was not a symbol declaration . 1 - if this was a local declaration . 2 - if this was binding declaration .
static i32 parse_possible_symbol_declaration(OnyxParser* parser, AstNode** ret) {
    if (parser->curr->type != Token_Type_Symbol) return 0;
    if (peek_token(1)->type == ',') {
        return parse_possible_compound_symbol_declaration(parser, ret);
    }
    if (peek_token(1)->type != ':')         return 0;
    OnyxToken* symbol = expect_token(parser, Token_Type_Symbol);
    expect_token(parser, ':');
    if (parser->curr->type == ':') {
        AstBlock* current_block = parser->block_stack[bh_arr_length(parser->block_stack) - 1];
        assert(current_block->binding_scope != NULL);
        AstBinding* binding = parse_top_level_binding(parser, symbol);
        if (parser->hit_unexpected_token) return 2;
        symbol_introduce(current_block->binding_scope, symbol, binding->node);
        return 2;
    }
    AstType* type_node = NULL;
    if (parser->curr->type != '=') {
        type_node = parse_type(parser);
    }
    AstLocal* local = make_local(parser->allocator, symbol, type_node);
    *ret = (AstNode *) local;
    if (parser->curr->type == '=') {
        AstBinaryOp* assignment = make_node(AstBinaryOp, Ast_Kind_Binary_Op);
        assignment->operation = Binary_Op_Assign;
        local->next = (AstNode *) assignment;
        assignment->token = parser->curr;
        consume_token(parser);
        AstTyped* expr = parse_expression(parser, 1);
        if (expr == NULL) return 1;
        assignment->right = expr;
        AstNode* left_symbol = make_node(AstNode, Ast_Kind_Symbol);
        left_symbol->token = symbol;
        assignment->left = (AstTyped *) left_symbol;
    }
    return 1;
}
static i32 parse_possible_symbol_declaration ( OnyxParser * parser , AstNode * * ret ) { if ( parser -> curr -> type != Token_Type_Symbol ) return 0 ; if ( peek_token ( 1 ) -> type == ' ' ) { return parse_possible_compound_symbol_declaration ( parser , ret ) ; } if ( peek_token ( 1 ) -> type != ' ' ) return 0 ; OnyxToken * symbol = expect_token ( parser , Token_Type_Symbol ) ; expect_token ( parser , ' ' ) ; if ( parser -> curr -> type == ' ' ) { AstBlock * current_block = parser -> block_stack [ bh_arr_length ( parser -> block_stack ) - 1 ] ; assert ( current_block -> binding_scope != NULL ) ; AstBinding * binding = parse_top_level_binding ( parser , symbol ) ; if ( parser -> hit_unexpected_token ) return 2 ; symbol_introduce ( current_block -> binding_scope , symbol , binding -> node ) ; return 2 ; } AstType * type_node = NULL ; if ( parser -> curr -> type != ' ' ) { type_node = parse_type ( parser ) ; } AstLocal * local = make_local ( parser -> allocator , symbol , type_node ) ; * ret = ( AstNode * ) local ; if ( parser -> curr -> type == ' ' ) { AstBinaryOp * assignment = make_node ( AstBinaryOp , Ast_Kind_Binary_Op ) ; assignment -> operation = Binary_Op_Assign ; local -> next = ( AstNode * ) assignment ; assignment -> token = parser -> curr ; consume_token ( parser ) ; AstTyped * expr = parse_expression ( parser , 1 ) ; if ( expr == NULL ) return 1 ; assignment -> right = expr ; AstNode * left_symbol = make_node ( AstNode , Ast_Kind_Symbol ) ; left_symbol -> token = symbol ; assignment -> left = ( AstTyped * ) left_symbol ; } return 1 ; }
Returns:
0 - if this was not a symbol declaration.
Returns : 0 - if this was not a symbol declaration .
// Has to start with a symbol to be a declaration // If the token after the symbol is a comma, assume this is a compound declaration. // NOTE: var: type