Cleaning up parsing / typechecking of calls slightly, adding any vargs

This commit is contained in:
Krzosa Karol
2022-06-20 09:28:38 +02:00
parent aa5741203f
commit 4e288dcfab
7 changed files with 84 additions and 67 deletions

View File

@@ -164,27 +164,27 @@ parse_expr_call(Ast_Expr *left, Token_Kind close_kind){
Array<Ast_Call_Item *> exprs = {scratch};
while(!token_is(close_kind)){
Token *token = token_get();
Ast_Atom *name = 0;
Ast_Expr *index = 0;
Ast_Expr *item = parse_expr();
Ast_Call_Item *item_comp = ast_new(Ast_Call_Item, AST_CALL_ITEM, token_get(), AST_EXPR);
item_comp->item = parse_expr();
if(token_match(TK_Assign)){
assert(is_flag_set(item->flags, AST_ATOM));
if(item->kind != AST_IDENT){
index = item;
} else{
name = (Ast_Atom *)item;
assert(is_flag_set(item_comp->item->flags, AST_ATOM));
if(item_comp->item->kind != AST_IDENT){
item_comp->index = item_comp->item;
set_flag(item_comp->call_flags, CALL_INDEX);
}
item = parse_expr();
else{
item_comp->name = (Ast_Atom *)item_comp->item;
set_flag(item_comp->call_flags, CALL_NAME);
}
item_comp->item = parse_expr();
}
if(name && index) compiler_error(token, "Both index and name are present, that is invalid");
if(close_kind == TK_OpenParen && index) compiler_error(token, "Lambda calls can't have indexed arguments");
if(close_kind == TK_OpenParen && is_flag_set(item_comp->call_flags, CALL_INDEX))
compiler_error(item_comp->pos, "Lambda calls can't have indexed arguments");
Ast_Call_Item *item_comp = ast_call_item(token, name, index, item);
exprs.add(item_comp);
if(!token_match(TK_Comma)){
break;
}
@@ -234,7 +234,7 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
}
else if(token_match_keyword(keyword_switch)){
Ast_Switch *result = new_ast(Ast_Switch, AST_SWITCH, token, AST_STMT);
Ast_Switch *result = ast_new(Ast_Switch, AST_SWITCH, token, AST_STMT);
result->value = parse_expr();
result->cases = {scratch};
@@ -245,7 +245,7 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
continue;
}
Ast_Switch_Case *switch_case = new_ast(Ast_Switch_Case, AST_SWITCH_CASE, token_get(), AST_STMT);
Ast_Switch_Case *switch_case = ast_new(Ast_Switch_Case, AST_SWITCH_CASE, token_get(), AST_STMT);
if(token_match_pound(pctx->intern("fallthrough"_s)))
switch_case->fallthrough = true;
@@ -392,27 +392,27 @@ function Ast_Lambda *
parse_lambda(Token *token){
Scratch scratch;
B32 has_var_args = false;
Array<Ast_Decl *> params = {scratch};
if(!token_is(TK_CloseParen)){
for(;;){
Token *name = token_get();
if(token_match(TK_Identifier)){
token_expect(TK_Colon);
Ast_Expr *typespec = parse_expr();
Ast_Decl *param = ast_new(Ast_Decl, AST_VAR, name, AST_DECL);
param->name = name->intern_val;
Ast_Expr *default_value = 0;
if(token_match(TK_Assign)) {
default_value = parse_expr();
if(token_match(TK_TwoDots)){
set_flag(param->flags, AST_ANY_VARGS);
}
else{
param->typespec = parse_expr();
if(token_match(TK_Assign))
param->expr = parse_expr();
}
Ast_Decl *param = ast_var(name, typespec, name->intern_val, default_value);
params.add(param);
}
else if(token_match(TK_ThreeDots)){
has_var_args = true;
break;
}
else compiler_error(name, "Expected [Identifier] or [...] when parsing lambda arguments");
if(!token_match(TK_Comma))