Calling functions is working, same syntax as compound stmts

This commit is contained in:
Krzosa Karol
2022-05-30 09:04:34 +02:00
parent 802dce749e
commit c305d4da44
6 changed files with 118 additions and 95 deletions

View File

@@ -116,9 +116,9 @@ atom_expr = Int
| 'cast' '(' typespec ',' expr ')'
| 'size_type' '(' typespec ')'
| 'size_expr' '(' expr ')'
| '{' compound_expr '}'
| '{' call_expr '}'
| '(' expr ')'
| '(' ':' typespec ')' '{' compound_expr '}'
| '(' ':' typespec ')' '{' call_expr '}'
postfix_expr = atom_expr ('[' expr ']' | '.' Identifier | ++ | -- | '(' expr_list ')')*
unary_expr = unary ? unary_expr : atom_expr
mul_expr = atom_expr (mul atom_expr)*
@@ -135,48 +135,6 @@ Compound literals
*/
function Ast_Expr *parse_expr(S64 rbp = 0);
function Ast_Compound *
parse_expr_compound(Ast_Expr *left){
Scratch scratch;
Token *pos = token_get();
Array<Ast_Compound_Item *> exprs = {scratch};
while(!token_is(TK_CloseParen)){
Token *token = token_get();
Ast_Expr *index = 0;
Ast_Atom *name = 0;
if(token_match(TK_OpenBracket)){
index = parse_expr();
token_expect(TK_CloseBracket);
token_expect(TK_Assign);
}
else if(token_is(TK_Identifier)){
token = token_next();
name = ast_ident(token, token->intern_val);
token_expect(TK_Assign);
}
Ast_Expr *item = parse_expr();
Ast_Compound_Item *item_comp = ast_expr_compound_item(token, index, name, item);
exprs.add(item_comp);
if(!token_match(TK_Comma)){
break;
}
}
token_expect(TK_CloseParen);
Ast_Compound *result = ast_expr_compound(pos, left, exprs);
return result;
}
function Ast_Expr *
parse_optional_type(){
Ast_Expr *result = 0;
if(token_match(TK_Colon)) result = parse_expr();
return result;
}
function Ast_Init *
parse_init_stmt(Ast_Expr *expr){
Token *token = token_get();
@@ -192,6 +150,44 @@ parse_init_stmt(Ast_Expr *expr){
return 0;
}
function Ast_Call *
parse_expr_call(Ast_Expr *left){
Scratch scratch;
Token *pos = token_get();
Array<Ast_Call_Item *> exprs = {scratch};
while(!token_is(TK_CloseParen)){
Token *token = token_get();
Ast_Expr *index = 0;
Ast_Atom *name = 0;
if(token_match(TK_OpenBracket)){
index = parse_expr();
token_expect(TK_CloseBracket);
token_expect(TK_Assign);
}
Ast_Expr *item = parse_expr();
Ast_Call_Item *item_comp = ast_call_item(token, index, name, item);
exprs.add(item_comp);
if(!token_match(TK_Comma)){
break;
}
}
token_expect(TK_CloseParen);
Ast_Call *result = ast_call(pos, left, exprs);
return result;
}
function Ast_Expr *
parse_optional_type(){
Ast_Expr *result = 0;
if(token_match(TK_Colon)) result = parse_expr();
return result;
}
function Ast_Named *parse_named(B32);
function Ast_Block *
parse_block(){
@@ -358,9 +354,9 @@ parse_expr(S64 rbp){
for(;;){
token = token_get();
// @note: parse postfix
if(postfix_binding_power(token->kind) > rbp){
token_next();
// @note: parse postfix
switch(token->kind){
case TK_OpenBracket:{
Ast_Expr *index = parse_expr();
@@ -368,7 +364,7 @@ parse_expr(S64 rbp){
token_expect(TK_CloseBracket);
}break;
case TK_OpenParen:{
left = parse_expr_compound(left);
left = parse_expr_call(left);
}break;
default:{
if(token->kind == TK_Increment) token->kind = TK_PostIncrement;
@@ -378,6 +374,7 @@ parse_expr(S64 rbp){
}
}
// @note: parse right
else if(rbp < left_binding_power(token->kind)){
token = token_next();
left = left_denotation(token, left);