Skip to content

Commit c850e24

Browse files
committed
Add AST construction for arithmetic expressions
1 parent 219e36b commit c850e24

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

‎_01_in_2_ri/ast.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ typedef enum {
1414
OPERATION_PLUS,
1515
OPERATION_MINUS,
1616
OPERATION_DIV,
17-
OPERATION_MULT
17+
OPERATION_MULT,
18+
OPERATION_EQUAL,
19+
OPERATION_NOT_EQUAL,
20+
OPERATION_LESS_THAN,
21+
OPERATION_LESS_THAN_EQUAL,
22+
OPERATION_GREATER_THAN,
23+
OPERATION_GREATER_THAN_EQUAL
1824
} _operation_type;
1925

2026
typedef struct {

‎_01_in_2_ri/syntactical_analyzer.c

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ bool _elsif_statement() {
288288
bool result = false;
289289
if (_token == KEY_WORD_ELSIF) {
290290
_read_token();
291-
if (_expression()) {
291+
_ast *_past = (_ast *) malloc(sizeof(_ast));
292+
if (_expression(_past)) {
292293
_read_token();
293294
if (_token == KEY_WORD_THEN) {
294295
_read_token();
@@ -385,33 +386,51 @@ bool _relation_aux(_ast *_past) {
385386
if (DEBUG_MODE == true) printf("_relation_aux() : %s\n", yytext);
386387
bool result = false;
387388
if (_token == DELIMITER_EQUAL) {
389+
*_past = _ast_create_operation_node(OPERATION_EQUAL, *_past, NULL);
390+
_ast *_right = (_ast *) malloc(sizeof(_ast));
388391
_read_token();
389-
if (_simple_expression(_past)) {
392+
if (_simple_expression(_right)) {
393+
(*_past)->value.operation.right = *_right;
390394
result = true;
391395
}
392396
} else if (_token == DELIMITER_DIVIDE_EQUAL) {
397+
*_past = _ast_create_operation_node(OPERATION_NOT_EQUAL, *_past, NULL);
398+
_ast *_right = (_ast *) malloc(sizeof(_ast));
393399
_read_token();
394-
if (_simple_expression(_past)) {
400+
if (_simple_expression(_right)) {
401+
(*_past)->value.operation.right = *_right;
395402
result = true;
396403
}
397404
} else if (_token == DELIMITER_LESS_THAN) {
405+
*_past = _ast_create_operation_node(OPERATION_LESS_THAN, *_past, NULL);
406+
_ast *_right = (_ast *) malloc(sizeof(_ast));
398407
_read_token();
399-
if (_simple_expression(_past)) {
408+
if (_simple_expression(_right)) {
409+
(*_past)->value.operation.right = *_right;
400410
result = true;
401411
}
402412
} else if (_token == DELIMITER_LESS_THAN_EQUAL) {
413+
*_past = _ast_create_operation_node(OPERATION_LESS_THAN_EQUAL, *_past, NULL);
414+
_ast *_right = (_ast *) malloc(sizeof(_ast));
403415
_read_token();
404-
if (_simple_expression(_past)) {
416+
if (_simple_expression(_right)) {
417+
(*_past)->value.operation.right = *_right;
405418
result = true;
406419
}
407420
} else if (_token == DELIMITER_GREATER_THAN) {
421+
*_past = _ast_create_operation_node(OPERATION_GREATER_THAN, *_past, NULL);
422+
_ast *_right = (_ast *) malloc(sizeof(_ast));
408423
_read_token();
409-
if (_simple_expression(_past)) {
424+
if (_simple_expression(_right)) {
425+
(*_past)->value.operation.right = *_right;
410426
result = true;
411427
}
412428
} else if (_token == DELIMITER_GREATER_THAN_EQUAL) {
429+
*_past = _ast_create_operation_node(OPERATION_GREATER_THAN_EQUAL, *_past, NULL);
430+
_ast *_right = (_ast *) malloc(sizeof(_ast));
413431
_read_token();
414-
if (_simple_expression(_past)) {
432+
if (_simple_expression(_right)) {
433+
(*_past)->value.operation.right = *_right;
415434
result = true;
416435
}
417436
} else if (
@@ -457,18 +476,24 @@ bool _simple_expression_aux(_ast *_past) {
457476
if (DEBUG_MODE == true) printf("_simple_expression_aux() : %s\n", yytext);
458477
bool result = false;
459478
if (_token == DELIMITER_PLUS) {
479+
*_past = _ast_create_operation_node(OPERATION_PLUS, *_past, NULL);
480+
_ast *_right = (_ast *) malloc(sizeof(_ast));
460481
_read_token();
461-
if (_term(_past)) {
482+
if (_term(_right)) {
462483
_read_token();
463-
if (_simple_expression_aux(_past)) {
484+
if (_simple_expression_aux(_right)) {
485+
(*_past)->value.operation.right = *_right;
464486
result = true;
465487
}
466488
}
467489
} else if (_token == DELIMITER_DASH) {
490+
*_past = _ast_create_operation_node(OPERATION_MINUS, *_past, NULL);
491+
_ast *_right = (_ast *) malloc(sizeof(_ast));
468492
_read_token();
469-
if (_term(_past)) {
493+
if (_term(_right)) {
470494
_read_token();
471-
if (_simple_expression_aux(_past)) {
495+
if (_simple_expression_aux(_right)) {
496+
(*_past)->value.operation.right = *_right;
472497
result = true;
473498
}
474499
}
@@ -509,18 +534,24 @@ bool _term_aux(_ast *_past) {
509534
if (DEBUG_MODE == true) printf("_term_aux() : %s\n", yytext);
510535
bool result = false;
511536
if (_token == DELIMITER_STAR) {
537+
*_past = _ast_create_operation_node(OPERATION_MULT, *_past, NULL);
538+
_ast *_right = (_ast *) malloc(sizeof(_ast));
512539
_read_token();
513-
if (_factor(_past)) {
540+
if (_factor(_right)) {
514541
_read_token();
515-
if (_term_aux(_past)) {
542+
if (_term_aux(_right)) {
543+
(*_past)->value.operation.right = *_right;
516544
result = true;
517545
}
518546
}
519547
} else if (_token == DELIMITER_SLASH) {
548+
*_past = _ast_create_operation_node(OPERATION_DIV, *_past, NULL);
549+
_ast *_right = (_ast *) malloc(sizeof(_ast));
520550
_read_token();
521-
if (_factor(_past)) {
551+
if (_factor(_right)) {
522552
_read_token();
523-
if (_term_aux(_past)) {
553+
if (_term_aux(_right)) {
554+
(*_past)->value.operation.right = *_right;
524555
result = true;
525556
}
526557
}
@@ -565,8 +596,10 @@ bool _primary(_ast *_past) {
565596
if (DEBUG_MODE == true) printf("_primary() : %s\n", yytext);
566597
bool result = false;
567598
if (_token == INTEGER_VALUE) {
599+
*_past = _ast_create_constant_node(NODE_CONSTANT_INT, atof(yytext));
568600
result = true;
569601
} else if (_token == FLOAT_VALUE) {
602+
*_past = _ast_create_constant_node(NODE_CONSTANT_DOUBLE, atof(yytext));
570603
result = true;
571604
} else if (_token == KEY_WORD_NULL) {
572605
result = true;
@@ -577,6 +610,8 @@ bool _primary(_ast *_past) {
577610
_add_semantic_error(NOT_DECLARED, yylineno, yytext);
578611
} else if (_var_initialized(yytext) == false) {
579612
_add_semantic_error(NOT_INITIALIZED, yylineno, yytext);
613+
} else {
614+
*_past = _ast_create_variable_node(yytext);
580615
}
581616
result = true;
582617
} else if (_token == DELIMITER_PAR_OPENED) {

0 commit comments

Comments
 (0)