diff --git a/lab-5/Makefile b/lab-5/Makefile index e3fe0fe..dd120a9 100644 --- a/lab-5/Makefile +++ b/lab-5/Makefile @@ -42,5 +42,5 @@ parser.c parser.h: parser.bison # clean causes all intermediate files to be deleted. clean: - rm -f parser.c parser.output parser.h scanner.c *.o interpreter.out interpreter_test.out test_evaluate.txt test_print.txt + rm -f parser.c parser.output parser.h scanner.c *.o interpreter.out interpreter_test.out test_evaluate.txt test_print.txt parser_test.out scanner_test.out diff --git a/lab-5/expr.c b/lab-5/expr.c index 13fb759..83f9c3e 100644 --- a/lab-5/expr.c +++ b/lab-5/expr.c @@ -191,13 +191,15 @@ void expr_print(struct expr *e) { close = "]"; } else if (e->kind != EXPR_NAME && e->kind != EXPR_SUBSCRIPT && e->kind != EXPR_INTEGER_LITERAL && e->kind != EXPR_FLOAT_LITERAL && - e->kind != EXPR_STRING_LITERAL && e->kind != EXPR_CUSTOM_FUNCTION) { + e->kind != EXPR_STRING_LITERAL && e->kind != EXPR_CUSTOM_FUNCTION && + e->kind != EXPR_VALUE) { printf("("); close = ")"; } else if (e->kind == EXPR_CUSTOM_FUNCTION) { printf("FUNCTION "); } + expr_print(e->left); switch (e->kind) { @@ -246,6 +248,7 @@ void expr_print(struct expr *e) { case EXPR_LESS_THAN_OR_EQUAL: printf("<="); break; + case EXPR_VALUE: case EXPR_INTEGER_LITERAL: printf("%d", e->integer_value); break; @@ -288,6 +291,10 @@ void stmt_evaluate(struct stmt *s) { case STMT_IF: if (expr_evaluate(s->expr)) { stmt_evaluate(s->body); + } else { + if (s->else_body) { + stmt_evaluate(s->else_body); + } } break; case STMT_PRINT: @@ -409,6 +416,8 @@ const char *expr_string_evaluate(struct expr *e) { case EXPR_LESS_THAN: case EXPR_LESS_THAN_OR_EQUAL: case EXPR_INTEGER_LITERAL: + case EXPR_VALUE: + case EXPR_OCCURS: case EXPR_FLOAT_LITERAL: printf("runtime error: not a string expression\n"); exit(1); @@ -482,6 +491,7 @@ float expr_evaluate(struct expr *e) { float r = expr_evaluate(e->right); float result; + switch (e->kind) { case EXPR_NAME: // Get the variable expression and then evaluate it. diff --git a/lab-5/parser.bison b/lab-5/parser.bison index f5d4518..6f07207 100644 --- a/lab-5/parser.bison +++ b/lab-5/parser.bison @@ -225,7 +225,7 @@ else_parts : else_branch else_branch : TOKEN_ELSE_IF booleanexpr simple_stmt {$$ = stmt_create(STMT_IF, NULL, NULL, $2, NULL, $3, NULL, NULL);} | TOKEN_ELSE simple_stmt - {$$ = stmt_create(STMT_IF, NULL, NULL, NULL, NULL, $2, NULL, NULL);} + {$$ = stmt_create(STMT_IF, NULL, NULL, expr_create(EXPR_EQUAL_EQUAL, expr_create_integer_literal(0), expr_create_integer_literal(0)), NULL, $2, NULL, NULL);} | TOKEN_END_IF {$$ = NULL;} perform_stmt : TOKEN_PERFORM TOKEN_VARYING ident TOKEN_KEYWORD_FROM TOKEN_INTEGER TOKEN_KEYWORD_BY TOKEN_INTEGER TOKEN_UNTIL op_parms diff --git a/lab-5/samples/branching.cbl b/lab-5/samples/branching.cbl index a26299e..dc0bf20 100644 --- a/lab-5/samples/branching.cbl +++ b/lab-5/samples/branching.cbl @@ -1,8 +1,14 @@ IDENTIFICATION DIVISION. PROGRAM-ID. BRANCHING. +DATA DIVISION. + WORKING-STORAGE SECTION. + 05 A PIC S9(2) VALUE 1. + 05 B PIC S9(2) VALUE 2. PROCEDURE DIVISION. - IF A > B THEN + IF A > B DISPLAY 'A is greater than B' + ELSE IF A = B + DISPLAY 'A is equal to B' ELSE DISPLAY 'B is greater than A' END-IF diff --git a/lab-5/samples/outputs/branching_evaluate.txt b/lab-5/samples/outputs/branching_evaluate.txt index e69de29..fc83e69 100644 --- a/lab-5/samples/outputs/branching_evaluate.txt +++ b/lab-5/samples/outputs/branching_evaluate.txt @@ -0,0 +1 @@ +B is greater than A diff --git a/lab-5/samples/outputs/branching_print.txt b/lab-5/samples/outputs/branching_print.txt index e69de29..181f6d0 100644 --- a/lab-5/samples/outputs/branching_print.txt +++ b/lab-5/samples/outputs/branching_print.txt @@ -0,0 +1,21 @@ +section +section +section +section +A = 1; +B = 2; +section +if (A>B) then +print A is greater than B; +endif +else if (0==0) then +if (A==B) then +print A is equal to B; +endif +else if (0==0) then +print B is greater than A; +endif + +endif + +stop run diff --git a/lab-5/samples/outputs/quadratic_print.txt b/lab-5/samples/outputs/quadratic_print.txt index ceb1732..094bde0 100644 --- a/lab-5/samples/outputs/quadratic_print.txt +++ b/lab-5/samples/outputs/quadratic_print.txt @@ -2,9 +2,9 @@ section section section section -a = (); -b = (); -c = (); +a = 1; +b = 5; +c = 6; discriminant = (); root1 = (); root2 = (); @@ -20,13 +20,13 @@ print The equation has two distinct real roots: ; print Root 1: root1; print Root 2: root2; endif -else if then +else if (0==0) then if (discriminant==0) then compute root1 = (-b/(2*a)); print The equation has one real root: ; print Root: root1; endif -else if then +else if (0==0) then print The equation has no real roots.; endif