Basic compound parsing, maybe I should unify with calls again

This commit is contained in:
Krzosa Karol
2022-06-10 23:03:21 +02:00
parent cf1237f449
commit 7365d6aa88
4 changed files with 75 additions and 7 deletions

35
ast.cpp
View File

@@ -17,6 +17,8 @@ enum Ast_Kind: U32{
AST_CALL_ITEM,
AST_CALL,
AST_COMPOUND,
AST_COMPOUND_ITEM,
AST_TYPE,
AST_VAR,
AST_CONST,
@@ -80,13 +82,25 @@ 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; // index | name
Ast_Atom *name;
Ast_Expr *item;
};
struct Ast_Call: Ast_Expr{
Ast_Resolved_Type *type; // @todo: to map
Ast_Resolved_Type *type;
Ast_Expr *name;
Array<Ast_Call_Item *> exprs;
};
@@ -283,6 +297,23 @@ 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);

View File

@@ -21,7 +21,8 @@ Boolean: Bool = true
//-----------------------------------------------------------------------------
// Compound expressions
//-----------------------------------------------------------------------------
array1: [4]S64
array1: [4]S64 = {1,2,3,4}
//-----------------------------------------------------------------------------
// Pointers

View File

@@ -200,9 +200,6 @@ int main(int argument_count, char **arguments){
system((const char *)run_program.str);
}
else{
}
#endif
Scratch scratch;
Array<String> files = {scratch};

View File

@@ -145,6 +145,37 @@ 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){
Scratch scratch;
@@ -215,7 +246,6 @@ parse_stmt_scope(Ast_Scope *scope_defined_outside = 0){
init = parse_init_stmt(expr_first);
}
if(token_match(TK_Comma)){
if(!token_is(TK_Comma)) cond = parse_expr();
if(token_match(TK_Comma)){
@@ -351,6 +381,7 @@ binding_power(Binding binding, Token_Kind kind){
case TK_Dereference:
case TK_Keyword:
case TK_OpenParen:
case TK_OpenBrace:
case TK_Sub:
case TK_Add:
case TK_Neg:
@@ -391,6 +422,7 @@ binding_power(Binding binding, Token_Kind kind){
case TK_Decrement:
case TK_OpenBracket:
case TK_OpenParen:
case TK_OpenBrace:
return {21, -2};
default: return{-1,-1};
}
@@ -429,6 +461,10 @@ parse_expr(S64 min_bp){
left = result;
}break;
case TK_OpenBrace: {
left = parse_compound(0);
}break;
case TK_Keyword: {
if(token->intern_val == keyword_true) left = ast_bool(token, 1);
else if(token->intern_val == keyword_false) left = ast_bool(token, 0);
@@ -475,6 +511,9 @@ parse_expr(S64 min_bp){
token_expect(TK_CloseBracket);
left = ast_expr_index(token, left, index);
}break;
case TK_OpenBrace: {
left = parse_compound(left);
}break;
case TK_OpenParen:{
left = parse_expr_call(left);
}break;