fix branching

This commit is contained in:
vel 2024-11-17 01:20:58 -08:00
parent 5ca58de9c6
commit fd10dae2ba
Signed by: velvox
GPG Key ID: 59D9762F674151DF
7 changed files with 47 additions and 9 deletions

View File

@ -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

View File

@ -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.

View File

@ -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

View File

@ -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

View File

@ -0,0 +1 @@
B is greater than A

View File

@ -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

View File

@ -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