feat: Add map statement to parser

This commit is contained in:
overflowerror 2024-05-02 15:20:26 +02:00
parent a381d95ff1
commit 3567a3c40b
4 changed files with 36 additions and 1 deletions

View file

@ -79,6 +79,18 @@ struct statement* while_statement_new(struct expression* condition, struct block
return stat;
}
struct statement* map_statement_new(char* index_id, char* ref_id, char* list_id, struct block* block) {
_new(stat, statement);
stat->kind = MAP_STATEMENT;
stat->map_loop = (struct map_statement) {
.index_id = index_id,
.ref_id = ref_id,
.list_id = list_id,
.block = block,
};
return stat;
}
struct expression* literal_expression_char_new(char c) {
_new(expr, expression);
expr->kind = LITERAL;

View file

@ -32,6 +32,7 @@ enum statement_kind {
EXPR_STATEMENT,
IF_STATEMENT,
WHILE_STATEMENT,
MAP_STATEMENT,
};
struct array_literal {
@ -117,6 +118,13 @@ struct while_statement {
struct block* block;
};
struct map_statement {
char* index_id;
char* ref_id;
char* list_id;
struct block* block;
};
struct expr_statement {
struct expression* expr;
};
@ -128,6 +136,7 @@ struct statement {
struct expr_statement expr;
struct if_statement if_else;
struct while_statement while_loop;
struct map_statement map_loop;
};
};
@ -144,6 +153,7 @@ struct statement* assignment_statement_new(struct expression*, struct expression
struct statement* expr_statement_new(struct expression*);
struct statement* if_statement_new(struct expression*, struct block*, struct block*);
struct statement* while_statement_new(struct expression*, struct block*);
struct statement* map_statement_new(char*, char*, char*, struct block*);
struct expression* literal_expression_char_new(char);
struct expression* literal_expression_str_new(char*);

View file

@ -9,6 +9,8 @@ var "var"
if "if"
else "else"
while "while"
map "map"
in "in"
char "'"([^\\]|[\\]n|[\\]t|[\\]{2})"'"
id [a-zA-Z_][a-zA-Z0-9_]*
@ -79,6 +81,8 @@ strbuf_t strbuf = NULL;
<INITIAL>{if} { return IF; }
<INITIAL>{else} { return ELSE; }
<INITIAL>{while} { return WHILE; }
<INITIAL>{map} { return MAP; }
<INITIAL>{in} { return IN; }
<INITIAL>"=" { return ASSIGNMENT; }

View file

@ -33,7 +33,7 @@ extern struct block* program;
}
%type <block> stats optelse block
%type <statement> stat definition assignment exprstat if while
%type <statement> stat definition assignment exprstat if while map
%type <expr> expr literal variable macroexpr builtincall calcexpr argumentlist arrayliteral
%type <op> op
@ -72,6 +72,8 @@ extern struct block* program;
%token IF
%token ELSE
%token WHILE
%token MAP
%token IN
%start file
@ -99,6 +101,7 @@ stat: definition SEMICOLON
| exprstat SEMICOLON
| if
| while
| map
;
definition: VAR assignment
@ -135,6 +138,12 @@ while: WHILE expr block
}
;
map: MAP OPENING_BRACKETS ID COMMA ID IN ID CLOSING_BRACKETS block
{
$$ = map_statement_new($3, $5, $7, $9);
}
;
block: OPENING_BRACES stats CLOSING_BRACES
{