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