Changed to used macro with new function return

main
Øyvind Skaaden 2022-02-18 17:10:54 +01:00
parent 32c86018dc
commit a70bb5d50d
1 changed files with 65 additions and 63 deletions

View File

@ -1,5 +1,7 @@
%{
#include <vslc.h>
#define NODE(type, data, n_children, children...) node_init(malloc(sizeof(node_t)), type, data, n_children, ##children)
%}
%define api.value.type {node_t}
@ -31,253 +33,253 @@
program:
global_list {
node_init(root = malloc(sizeof(node_t)), PROGRAM, NULL, 1, $1);
root = NODE(PROGRAM, NULL, 1, $1);
}
;
;
global_list:
global {
node_init($$ = malloc(sizeof(node_t)), GLOBAL_LIST, NULL, 1, $1);
$$ = NODE(GLOBAL_LIST, NULL, 1, $1);
}
| global_list global {
node_init($$ = malloc(sizeof(node_t)), GLOBAL_LIST, NULL, 2, $1, $2);
$$ = NODE(GLOBAL_LIST, NULL, 2, $1, $2);
}
;
global:
function {
node_init($$ = malloc(sizeof(node_t)), GLOBAL, NULL, 1, $1);
$$ = NODE(GLOBAL, NULL, 1, $1);
}
| declaration {
node_init($$ = malloc(sizeof(node_t)), GLOBAL, NULL, 1, $1);
$$ = NODE(GLOBAL, NULL, 1, $1);
}
;
statement_list:
statement {
node_init($$ = malloc(sizeof(node_t)), STATEMENT_LIST, NULL, 1, $1);
$$ = NODE(STATEMENT_LIST, NULL, 1, $1);
}
| statement_list statement {
node_init($$ = malloc(sizeof(node_t)), STATEMENT_LIST, NULL, 2, $1, $2);
$$ = NODE(STATEMENT_LIST, NULL, 2, $1, $2);
}
;
print_list:
print_item {
node_init($$ = malloc(sizeof(node_t)), PRINT_LIST, NULL, 1, $1);
$$ = NODE(PRINT_LIST, NULL, 1, $1);
}
| print_list ',' print_item {
node_init($$ = malloc(sizeof(node_t)), PRINT_LIST, NULL, 2, $1, $3);
$$ = NODE(PRINT_LIST, NULL, 2, $1, $3);
}
;
expression_list:
expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION_LIST, NULL, 1, $1);
$$ = NODE(EXPRESSION_LIST, NULL, 1, $1);
}
| expression_list ',' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION_LIST, NULL, 2, $1, $3);
$$ = NODE(EXPRESSION_LIST, NULL, 2, $1, $3);
}
;
variable_list:
identifier {
node_init($$ = malloc(sizeof(node_t)), VARIABLE_LIST, NULL, 1, $1);
$$ = NODE(VARIABLE_LIST, NULL, 1, $1);
}
| variable_list ',' identifier {
node_init($$ = malloc(sizeof(node_t)), VARIABLE_LIST, NULL, 2, $1, $3);
$$ = NODE(VARIABLE_LIST, NULL, 2, $1, $3);
}
;
argument_list:
expression_list {
node_init($$ = malloc(sizeof(node_t)), ARGUMENT_LIST, NULL, 1, $1);
$$ = NODE(ARGUMENT_LIST, NULL, 1, $1);
}
| /* epsilon */ {
node_init($$ = malloc(sizeof(node_t)), ARGUMENT_LIST, NULL, 0);
$$ = NODE(ARGUMENT_LIST, NULL, 0);
}
;
parameter_list:
variable_list {
node_init($$ = malloc(sizeof(node_t)), PARAMETER_LIST, NULL, 1, $1);
$$ = NODE(PARAMETER_LIST, NULL, 1, $1);
}
| /* epsilon */ {
node_init($$ = malloc(sizeof(node_t)), PARAMETER_LIST, NULL, 0);
$$ = NODE(PARAMETER_LIST, NULL, 0);
}
;
declaration_list:
declaration {
node_init($$ = malloc(sizeof(node_t)), DECLARATION_LIST, NULL, 1, $1);
$$ = NODE(DECLARATION_LIST, NULL, 1, $1);
}
| declaration_list declaration {
node_init($$ = malloc(sizeof(node_t)), DECLARATION_LIST, NULL, 2, $1, $2);
$$ = NODE(DECLARATION_LIST, NULL, 2, $1, $2);
}
;
function:
FUNC identifier '(' parameter_list ')' statement {
node_init($$ = malloc(sizeof(node_t)), FUNCTION, NULL, 3, $2, $4, $6);
$$ = NODE(FUNCTION, NULL, 3, $2, $4, $6);
}
;
statement:
assignment_statement {
node_init($$ = malloc(sizeof(node_t)), STATEMENT, NULL, 1, $1);
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| return_statement {
node_init($$ = malloc(sizeof(node_t)), STATEMENT, NULL, 1, $1);
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| print_statement {
node_init($$ = malloc(sizeof(node_t)), STATEMENT, NULL, 1, $1);
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| if_statement {
node_init($$ = malloc(sizeof(node_t)), STATEMENT, NULL, 1, $1);
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| while_statement {
node_init($$ = malloc(sizeof(node_t)), STATEMENT, NULL, 1, $1);
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| null_statement {
node_init($$ = malloc(sizeof(node_t)), STATEMENT, NULL, 1, $1);
$$ = NODE(STATEMENT, NULL, 1, $1);
}
| block {
node_init($$ = malloc(sizeof(node_t)), STATEMENT, NULL, 1, $1);
$$ = NODE(STATEMENT, NULL, 1, $1);
}
;
block:
OPENBLOCK declaration_list statement_list CLOSEBLOCK {
node_init($$ = malloc(sizeof(node_t)), BLOCK, NULL, 2, $2, $3);
$$ = NODE(BLOCK, NULL, 2, $2, $3);
}
| OPENBLOCK statement_list CLOSEBLOCK {
node_init($$ = malloc(sizeof(node_t)), BLOCK, NULL, 1, $2);
$$ = NODE(BLOCK, NULL, 1, $2);
}
;
assignment_statement:
identifier ':' '=' expression {
node_init($$ = malloc(sizeof(node_t)), ASSIGNMENT_STATEMENT, NULL, 2, $1, $4);
$$ = NODE(ASSIGNMENT_STATEMENT, NULL, 2, $1, $4);
}
| identifier '+' '=' expression {
node_init($$ = malloc(sizeof(node_t)), ADD_STATEMENT, NULL, 2, $1, $4);
$$ = NODE(ADD_STATEMENT, NULL, 2, $1, $4);
}
| identifier '-' '=' expression {
node_init($$ = malloc(sizeof(node_t)), SUBTRACT_STATEMENT, NULL, 2, $1, $4);
$$ = NODE(SUBTRACT_STATEMENT, NULL, 2, $1, $4);
}
| identifier '*' '=' expression {
node_init($$ = malloc(sizeof(node_t)), MULTIPLY_STATEMENT, NULL, 2, $1, $4);
$$ = NODE(MULTIPLY_STATEMENT, NULL, 2, $1, $4);
}
| identifier '/' '=' expression {
node_init($$ = malloc(sizeof(node_t)), DIVIDE_STATEMENT, NULL, 2, $1, $4);
$$ = NODE(DIVIDE_STATEMENT, NULL, 2, $1, $4);
}
;
return_statement:
RETURN expression {
node_init($$ = malloc(sizeof(node_t)), RETURN_STATEMENT, NULL, 1, $2);
$$ = NODE(RETURN_STATEMENT, NULL, 1, $2);
}
;
print_statement:
PRINT print_list {
node_init($$ = malloc(sizeof(node_t)), PRINT_STATEMENT, NULL, 1, $2);
$$ = NODE(PRINT_STATEMENT, NULL, 1, $2);
}
;
null_statement:
CONTINUE {
node_init($$ = malloc(sizeof(node_t)), NULL_STATEMENT, NULL, 0);
$$ = NODE(NULL_STATEMENT, NULL, 0);
}
;
if_statement:
IF relation THEN statement {
node_init($$ = malloc(sizeof(node_t)), IF_STATEMENT, NULL, 2, $2, $4);
$$ = NODE(IF_STATEMENT, NULL, 2, $2, $4);
}
| IF relation THEN statement ELSE statement {
node_init($$ = malloc(sizeof(node_t)), IF_STATEMENT, NULL, 3, $2, $4, $6);
$$ = NODE(IF_STATEMENT, NULL, 3, $2, $4, $6);
}
;
while_statement:
WHILE relation DO statement {
node_init($$ = malloc(sizeof(node_t)), WHILE_STATEMENT, NULL, 2, $2, $4);
$$ = NODE(WHILE_STATEMENT, NULL, 2, $2, $4);
}
;
relation:
expression '=' expression {
node_init($$ = malloc(sizeof(node_t)), RELATION, strdup("="), 2, $1, $3);
$$ = NODE(RELATION, strdup("="), 2, $1, $3);
}
| expression '<' expression {
node_init($$ = malloc(sizeof(node_t)), RELATION, strdup("<"), 2, $1, $3);
$$ = NODE(RELATION, strdup("<"), 2, $1, $3);
}
| expression '>' expression {
node_init($$ = malloc(sizeof(node_t)), RELATION, strdup(">"), 2, $1, $3);
$$ = NODE(RELATION, strdup(">"), 2, $1, $3);
}
;
expression:
expression '|' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("|"), 2, $1, $3);
$$ = NODE(EXPRESSION, strdup("|"), 2, $1, $3);
}
| expression '^' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("^"), 2, $1, $3);
$$ = NODE(EXPRESSION, strdup("^"), 2, $1, $3);
}
| expression '&' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("&"), 2, $1, $3);
$$ = NODE(EXPRESSION, strdup("&"), 2, $1, $3);
}
| expression '+' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("+"), 2, $1, $3);
$$ = NODE(EXPRESSION, strdup("+"), 2, $1, $3);
}
| expression '-' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("-"), 2, $1, $3);
$$ = NODE(EXPRESSION, strdup("-"), 2, $1, $3);
}
| expression '*' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("*"), 2, $1, $3);
$$ = NODE(EXPRESSION, strdup("*"), 2, $1, $3);
}
| expression '/' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("/"), 2, $1, $3);
$$ = NODE(EXPRESSION, strdup("/"), 2, $1, $3);
}
| '-' expression %prec UMINUS {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("-"), 1, $2);
$$ = NODE(EXPRESSION, strdup("-"), 1, $2);
}
| '~' expression {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, strdup("~"), 1, $2);
$$ = NODE(EXPRESSION, strdup("~"), 1, $2);
}
| '(' expression ')' {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, /*NULL*/ strdup("group"), 1, $2);
$$ = NODE(EXPRESSION, /*NULL*/ strdup("group"), 1, $2);
}
| number {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, /*NULL*/ strdup("number"), 1, $1);
$$ = NODE(EXPRESSION, /*NULL*/ strdup("number"), 1, $1);
}
| identifier {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, /*NULL*/ strdup("identifier"), 1, $1);
$$ = NODE(EXPRESSION, /*NULL*/ strdup("identifier"), 1, $1);
}
| identifier '(' argument_list ')' {
node_init($$ = malloc(sizeof(node_t)), EXPRESSION, /*NULL*/ strdup("function_call"), 2, $1, $3);
$$ = NODE(EXPRESSION, /*NULL*/ strdup("function_call"), 2, $1, $3);
}
;
declaration:
VAR variable_list {
node_init($$ = malloc(sizeof(node_t)), DECLARATION, NULL, 1, $2);
$$ = NODE(DECLARATION, NULL, 1, $2);
}
;
print_item:
expression {
node_init($$ = malloc(sizeof(node_t)), PRINT_ITEM, NULL, 1, $1);
$$ = NODE(PRINT_ITEM, NULL, 1, $1);
}
| string {
node_init($$ = malloc(sizeof(node_t)), PRINT_ITEM, NULL, 1, $1);
$$ = NODE(PRINT_ITEM, NULL, 1, $1);
}
;
identifier:
IDENTIFIER {
node_init($$ = malloc(sizeof(node_t)), IDENTIFIER_DATA, strdup(yytext), 0); // Zero children
$$ = NODE(IDENTIFIER_DATA, strdup(yytext), 0); // Zero children
}
;
@ -285,13 +287,13 @@ number:
NUMBER {
uint64_t* p_number = malloc(sizeof(uint64_t));
*p_number = strtol(yytext, NULL, 10);
node_init($$ = malloc(sizeof(node_t)), NUMBER_DATA, p_number, 0); // Zero children
$$ = NODE(NUMBER_DATA, p_number, 0); // Zero children
}
;
string:
STRING {
node_init($$ = malloc(sizeof(node_t)), STRING_DATA, strdup(yytext), 0); // Zero children
$$ = NODE(STRING_DATA, strdup(yytext), 0); // Zero children
}
;