Unify call and compound but have different kinds

This commit is contained in:
Krzosa Karol
2022-06-10 23:45:02 +02:00
parent 7365d6aa88
commit bcddf7f721
3 changed files with 27 additions and 72 deletions

41
ast.cpp
View File

@@ -18,7 +18,6 @@ enum Ast_Kind: U32{
AST_CALL,
AST_COMPOUND,
AST_COMPOUND_ITEM,
AST_TYPE,
AST_VAR,
AST_CONST,
@@ -82,27 +81,19 @@ struct Ast_Atom: Ast_Expr{
INLINE_VALUE_FIELDS;
};
struct Ast_Compound_Item: Ast_Expr{
Ast_Atom *name;
Ast_Expr *index;
Ast_Expr *item;
};
struct Ast_Compound: Ast_Expr{
Ast_Resolved_Type *type;
Ast_Expr *typespec;
Array<Ast_Compound_Item *> exprs;
};
struct Ast_Call_Item: Ast_Expr{
Ast_Atom *name;
Ast_Expr *item;
Ast_Expr *index;
};
struct Ast_Call: Ast_Expr{
Ast_Resolved_Type *type;
Ast_Expr *name;
union{
Ast_Expr *name;
Ast_Expr *typespec;
};
Array<Ast_Call_Item *> exprs;
Ast_Resolved_Type *type;
};
struct Ast_Unary: Ast_Expr{
@@ -297,23 +288,6 @@ ast_expr_binary(Ast_Expr *left, Ast_Expr *right, Token *op){
return result;
}
function Ast_Compound *
ast_compound(Token *pos, Ast_Expr *typespec, Array<Ast_Compound_Item *> exprs){
AST_NEW(Compound, COMPOUND, pos, AST_EXPR);
result->typespec = typespec;
result->exprs = exprs.tight_copy(pctx->perm);
return result;
}
function Ast_Compound_Item *
ast_compound_item(Token *pos, Ast_Atom *name, Ast_Expr *index, Ast_Expr *item){
AST_NEW(Compound_Item, COMPOUND_ITEM, pos, AST_EXPR);
result->name = name;
result->index = index;
result->item = item;
return result;
}
function Ast_Call *
ast_call(Token *pos, Ast_Expr *name, Array<Ast_Call_Item *> exprs){
AST_NEW(Call, CALL, pos, AST_EXPR);
@@ -323,10 +297,11 @@ ast_call(Token *pos, Ast_Expr *name, Array<Ast_Call_Item *> exprs){
}
function Ast_Call_Item *
ast_call_item(Token *pos, Ast_Atom *name, Ast_Expr *item){
ast_call_item(Token *pos, Ast_Atom *name, Ast_Expr *index, Ast_Expr *item){
AST_NEW(Call_Item, CALL_ITEM, pos, AST_EXPR);
result->name = name;
result->item = item;
result->index = index;
return result;
}

View File

@@ -145,46 +145,21 @@ parse_init_stmt(Ast_Expr *expr){
return expr;
}
// @todo: Unify with call ???
function Ast_Compound *
parse_compound(Ast_Expr *left){
Scratch scratch;
Token *pos = token_get();
Array<Ast_Compound_Item *> exprs = {scratch};
while(!token_is(TK_CloseBrace)){
Token *token = token_get();
Ast_Atom *name = 0;
Ast_Expr *item = parse_expr();
if(token_match(TK_Assign)){
assert(is_flag_set(item->flags, AST_ATOM));
name = (Ast_Atom *)item;
item = parse_expr();
}
Ast_Compound_Item *item_comp = ast_compound_item(token, name, 0, item);
exprs.add(item_comp);
if(!token_match(TK_Comma)){
break;
}
}
token_expect(TK_CloseBrace);
Ast_Compound *result = ast_compound(pos, left, exprs);
return result;
}
function Ast_Call *
parse_expr_call(Ast_Expr *left){
parse_expr_call(Ast_Expr *left, Token_Kind close_kind){
Scratch scratch;
Token *pos = token_get();
Array<Ast_Call_Item *> exprs = {scratch};
while(!token_is(TK_CloseParen)){
while(!token_is(close_kind)){
Token *token = token_get();
Ast_Atom *name = 0;
Ast_Expr *index = 0;
if(token_match(TK_OpenBracket)){
index = parse_expr(0);
token_expect(TK_CloseBracket);
token_expect(TK_Assign);
}
Ast_Expr *item = parse_expr();
if(token_match(TK_Assign)){
@@ -193,14 +168,17 @@ parse_expr_call(Ast_Expr *left){
item = parse_expr();
}
Ast_Call_Item *item_comp = ast_call_item(token, name, item);
if(item && 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");
Ast_Call_Item *item_comp = ast_call_item(token, name, index, item);
exprs.add(item_comp);
if(!token_match(TK_Comma)){
break;
}
}
token_expect(TK_CloseParen);
token_expect(close_kind);
Ast_Call *result = ast_call(pos, left, exprs);
return result;
@@ -462,7 +440,8 @@ parse_expr(S64 min_bp){
}break;
case TK_OpenBrace: {
left = parse_compound(0);
left = parse_expr_call(0, TK_CloseBrace);
left->kind = AST_COMPOUND;
}break;
case TK_Keyword: {
@@ -512,10 +491,11 @@ parse_expr(S64 min_bp){
left = ast_expr_index(token, left, index);
}break;
case TK_OpenBrace: {
left = parse_compound(left);
left = parse_expr_call(left, TK_CloseBrace);
left->kind = AST_COMPOUND;
}break;
case TK_OpenParen:{
left = parse_expr_call(left);
left = parse_expr_call(left, TK_CloseParen);
}break;
default:{
assert(token->kind == TK_Increment || token->kind == TK_Decrement);

View File

@@ -814,7 +814,7 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags){
else{
// @note: default values are typechecked when they get resolved
if(lambda_arg->expr){
items.add(ast_call_item(lambda_arg->expr->pos, 0, lambda_arg->expr));
items.add(ast_call_item(lambda_arg->expr->pos, 0, 0, lambda_arg->expr));
}
else compiler_error(lambda_arg->pos, "Required value in lambda call was not passed");
}