feat: Add builtins to grammar

This commit is contained in:
overflowerror 2024-04-26 15:44:07 +02:00
parent f91af58c43
commit e95e2adc6d
4 changed files with 52 additions and 2 deletions

View file

@ -141,6 +141,24 @@ struct expression* macro_expression_new(char* id, char* arg) {
return expr;
}
struct expression* builtin_call_expression_new(void) {
_new(expr, expression);
expr->kind = BUILTIN_CALL;
expr->type = UNKNOWN_TYPE;
expr->builtin_call = (struct builtin_call_expression) {
.id = NULL,
.argument_number = 0,
.arguments = NULL,
};
return expr;
}
void builtin_call_expression_add_argument(struct expression* builtin_call, struct expression* argument) {
safe_realloc(builtin_call->builtin_call.arguments, ++builtin_call->builtin_call.argument_number * sizeof(struct expression*));
builtin_call->builtin_call.arguments[builtin_call->builtin_call.argument_number - 1] = argument;
}
struct expression* calc_expression_new(struct expression* operand1, struct expression* operand2, enum calc_operator operator) {
_new(expr, expression);
expr->kind = CALCULATION;

View file

@ -13,6 +13,7 @@ enum expression_kind {
LITERAL,
VARIABLE,
MACRO,
BUILTIN_CALL,
CALCULATION,
};
@ -50,6 +51,12 @@ struct macro_expression {
char* argument;
};
struct builtin_call_expression {
char* id;
size_t argument_number;
struct expression** arguments;
};
enum calc_operator {
ADDITION,
SUBTRACTION,
@ -76,6 +83,7 @@ struct expression {
struct literal_expression literal;
struct variable_expression variable;
struct macro_expression macro;
struct builtin_call_expression builtin_call;
struct calc_expression calc;
};
};
@ -135,6 +143,8 @@ struct expression* literal_expression_str_new(char*);
struct expression* literal_expression_num_new(long long);
struct expression* variable_expression_new(char*);
struct expression* macro_expression_new(char*, char*);
struct expression* builtin_call_expression_new(void);
void builtin_call_expression_add_argument(struct expression*, struct expression*);
struct expression* calc_expression_new(struct expression*, struct expression*, enum calc_operator);
#endif

View file

@ -78,6 +78,7 @@ strbuf_t strbuf = NULL;
<INITIAL>"*" { return TIMES; }
<INITIAL>"/" { return DIVIDE; }
<INITIAL>"%" { return MOD; }
<INITIAL>"," { return COMMA; }
<INITIAL>"==" { return EQUAL; }
<INITIAL>"!=" { return NOT_EQUAL; }

View file

@ -34,7 +34,7 @@ extern struct block* program;
%type <block> stats optelse block
%type <statement> stat print definition assignment macrostat if while
%type <expr> expr literal variable macroexpr calcexpr
%type <expr> expr literal variable macroexpr builtincall calcexpr argumentlist
%type <op> op
%token <number> NUM
@ -55,6 +55,7 @@ extern struct block* program;
%token AND
%token OR
%token NOT
%token COMMA
%token OPENING_BRACKETS
%token CLOSING_BRACKETS
@ -149,9 +150,10 @@ macrostat: macroexpr
}
;
expr: literal
expr: literal
| variable
| macroexpr
| builtincall
| OPENING_BRACKETS expr CLOSING_BRACKETS
{
$$ = $2;
@ -159,6 +161,25 @@ expr: literal
| calcexpr
;
builtincall: ID OPENING_BRACKETS argumentlist CLOSING_BRACKETS
{
$$ = $3;
$$->builtin_call.id = $1;
}
;
argumentlist: expr
{
$$ = builtin_call_expression_new();
builtin_call_expression_add_argument($$, $1);
}
| expr COMMA argumentlist
{
$$ = $1;
builtin_call_expression_add_argument($$, $3);
}
;
calcexpr: OPENING_BRACKETS expr op expr CLOSING_BRACKETS
{
$$ = calc_expression_new($2, $4, $3);