make move statements work
This commit is contained in:
parent
fd10dae2ba
commit
5e5e151f27
17
lab-5/expr.c
17
lab-5/expr.c
|
|
@ -161,9 +161,9 @@ void stmt_print(struct stmt *s) {
|
||||||
break;
|
break;
|
||||||
case STMT_MOVE:
|
case STMT_MOVE:
|
||||||
printf("move ");
|
printf("move ");
|
||||||
expr_print(s->decl->name);
|
|
||||||
printf(" to ");
|
|
||||||
expr_print(s->decl->value);
|
expr_print(s->decl->value);
|
||||||
|
printf(" to ");
|
||||||
|
expr_print(s->decl->name);
|
||||||
printf(";\n");
|
printf(";\n");
|
||||||
break;
|
break;
|
||||||
case STMT_END_EXECUTION:
|
case STMT_END_EXECUTION:
|
||||||
|
|
@ -282,6 +282,7 @@ void stmt_evaluate(struct stmt *s) {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
switch (s->kind) {
|
switch (s->kind) {
|
||||||
|
case STMT_MOVE:
|
||||||
case STMT_DECL:
|
case STMT_DECL:
|
||||||
decl_evaluate(s->decl);
|
decl_evaluate(s->decl);
|
||||||
break;
|
break;
|
||||||
|
|
@ -325,9 +326,11 @@ void stmt_evaluate_print(struct expr *e) {
|
||||||
printf("%s", expr_string_evaluate(e));
|
printf("%s", expr_string_evaluate(e));
|
||||||
} else if (e->kind == EXPR_FLOAT_LITERAL) {
|
} else if (e->kind == EXPR_FLOAT_LITERAL) {
|
||||||
printf("%f", expr_evaluate(e));
|
printf("%f", expr_evaluate(e));
|
||||||
} else if (e->kind == EXPR_INTEGER_LITERAL ||
|
} else if (e->kind == EXPR_INTEGER_LITERAL || e->kind == EXPR_SUBSCRIPT) {
|
||||||
e->kind == EXPR_NAME || e->kind == EXPR_SUBSCRIPT) {
|
|
||||||
printf("%.0f", expr_evaluate(e));
|
printf("%.0f", expr_evaluate(e));
|
||||||
|
} else if (e->kind == EXPR_NAME) {
|
||||||
|
struct expr *value = scope_lookup(e->name);
|
||||||
|
stmt_evaluate_print(value);
|
||||||
} else {
|
} else {
|
||||||
printf("runtime error: print expression is not literal\n");
|
printf("runtime error: print expression is not literal\n");
|
||||||
}
|
}
|
||||||
|
|
@ -356,9 +359,15 @@ void decl_evaluate(struct decl *d) {
|
||||||
struct expr *e = expr_sub_evaluate(d->value);
|
struct expr *e = expr_sub_evaluate(d->value);
|
||||||
scope_bind(d->name->name, e);
|
scope_bind(d->name->name, e);
|
||||||
} else if (d->name->kind == EXPR_NAME) {
|
} else if (d->name->kind == EXPR_NAME) {
|
||||||
|
if (d->value->kind != EXPR_STRING_LITERAL) {
|
||||||
float value = expr_evaluate(d->value);
|
float value = expr_evaluate(d->value);
|
||||||
struct expr *e = expr_create_float_literal(value);
|
struct expr *e = expr_create_float_literal(value);
|
||||||
scope_bind(d->name->name, e);
|
scope_bind(d->name->name, e);
|
||||||
|
} else {
|
||||||
|
const char *value = expr_string_evaluate(d->value);
|
||||||
|
struct expr *e = expr_create_string_literal(value);
|
||||||
|
scope_bind(d->name->name, e);
|
||||||
|
}
|
||||||
} else if (d->name->kind == EXPR_SUBSCRIPT) {
|
} else if (d->name->kind == EXPR_SUBSCRIPT) {
|
||||||
float value = expr_evaluate(d->value);
|
float value = expr_evaluate(d->value);
|
||||||
decl_subscript_evaluate(d->name, value);
|
decl_subscript_evaluate(d->name, value);
|
||||||
|
|
|
||||||
|
|
@ -117,24 +117,6 @@ UTEST_F_TEARDOWN(InterpreterTestFile) {
|
||||||
ASSERT_STREQ(actual_evaluate, expected_evaluate);
|
ASSERT_STREQ(actual_evaluate, expected_evaluate);
|
||||||
}
|
}
|
||||||
|
|
||||||
UTEST_F(InterpreterTestFile, helloworld) {
|
|
||||||
utest_fixture->test_file = "samples/hello-world.cbl";
|
|
||||||
utest_fixture->print_file = "samples/outputs/hello-world_print.txt";
|
|
||||||
utest_fixture->evaluated_file = "samples/outputs/hello-world_evaluate.txt";
|
|
||||||
}
|
|
||||||
|
|
||||||
UTEST_F(InterpreterTestFile, quadratic) {
|
|
||||||
utest_fixture->test_file = "samples/quadratic-snippet.cbl";
|
|
||||||
utest_fixture->print_file = "samples/outputs/quadratic_print.txt";
|
|
||||||
utest_fixture->evaluated_file = "samples/outputs/quadratic_evaluate.txt";
|
|
||||||
}
|
|
||||||
|
|
||||||
UTEST_F(InterpreterTestFile, sorting) {
|
|
||||||
utest_fixture->test_file = "samples/sorting-snippet.cbl";
|
|
||||||
utest_fixture->print_file = "samples/outputs/sorting_print.txt";
|
|
||||||
utest_fixture->evaluated_file = "samples/outputs/sorting_evaluate.txt";
|
|
||||||
}
|
|
||||||
|
|
||||||
UTEST_F(InterpreterTestFile, assignment) {
|
UTEST_F(InterpreterTestFile, assignment) {
|
||||||
utest_fixture->test_file = "samples/assignment.cbl";
|
utest_fixture->test_file = "samples/assignment.cbl";
|
||||||
utest_fixture->print_file = "samples/outputs/assignment_print.txt";
|
utest_fixture->print_file = "samples/outputs/assignment_print.txt";
|
||||||
|
|
@ -158,3 +140,21 @@ UTEST_F(InterpreterTestFile, looping) {
|
||||||
utest_fixture->print_file = "samples/outputs/looping_print.txt";
|
utest_fixture->print_file = "samples/outputs/looping_print.txt";
|
||||||
utest_fixture->evaluated_file = "samples/outputs/looping_evaluate.txt";
|
utest_fixture->evaluated_file = "samples/outputs/looping_evaluate.txt";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UTEST_F(InterpreterTestFile, helloworld) {
|
||||||
|
utest_fixture->test_file = "samples/hello-world.cbl";
|
||||||
|
utest_fixture->print_file = "samples/outputs/hello-world_print.txt";
|
||||||
|
utest_fixture->evaluated_file = "samples/outputs/hello-world_evaluate.txt";
|
||||||
|
}
|
||||||
|
|
||||||
|
UTEST_F(InterpreterTestFile, quadratic) {
|
||||||
|
utest_fixture->test_file = "samples/quadratic-snippet.cbl";
|
||||||
|
utest_fixture->print_file = "samples/outputs/quadratic_print.txt";
|
||||||
|
utest_fixture->evaluated_file = "samples/outputs/quadratic_evaluate.txt";
|
||||||
|
}
|
||||||
|
|
||||||
|
UTEST_F(InterpreterTestFile, sorting) {
|
||||||
|
utest_fixture->test_file = "samples/sorting-snippet.cbl";
|
||||||
|
utest_fixture->print_file = "samples/outputs/sorting_print.txt";
|
||||||
|
utest_fixture->evaluated_file = "samples/outputs/sorting_evaluate.txt";
|
||||||
|
}
|
||||||
|
|
@ -94,9 +94,9 @@ extern struct stmt *parser_result = 0;
|
||||||
struct type *type;
|
struct type *type;
|
||||||
}
|
}
|
||||||
|
|
||||||
%type <stmt> statement_list statement section stop_run sect_data simple_stmt cbl_func_stmt cbl_function if_branch else_parts else_branch perform_stmt data_space
|
%type <stmt> statement_list statement section stop_run sect_data simple_stmt cbl_func_stmt if_branch else_parts else_branch perform_stmt data_space display_stmt assignment_stmt
|
||||||
%type <expr> mathmaticalexpr booleanexpr term op_parm container_expr type_expr op_parms math_op categry_contain category_value ident ext_function
|
%type <expr> mathmaticalexpr booleanexpr term op_parm container_expr type_expr op_parms math_op categry_contain category_value ident ext_function
|
||||||
%type <decl> assignment_stmt simple_decl complex_decl data_declaration category_spec
|
%type <decl> simple_decl complex_decl data_declaration category_spec
|
||||||
%type <type> data_category complete_category data_clause
|
%type <type> data_category complete_category data_clause
|
||||||
%%
|
%%
|
||||||
file : statement_list
|
file : statement_list
|
||||||
|
|
@ -139,15 +139,17 @@ simple_stmt : cbl_func_stmt
|
||||||
{$$ = $1;}
|
{$$ = $1;}
|
||||||
| perform_stmt
|
| perform_stmt
|
||||||
;
|
;
|
||||||
cbl_func_stmt : cbl_function type_expr assignment_stmt
|
cbl_func_stmt : display_stmt
|
||||||
{ $3->name = $2; $$= stmt_create($1->kind, $3, NULL, NULL, NULL, NULL, NULL, NULL);}
|
{$$ = $1;}
|
||||||
| cbl_function op_parms
|
| assignment_stmt
|
||||||
{$$ = stmt_create($1->kind, NULL, NULL, $2, NULL, NULL, NULL, NULL);}
|
{$$ = $1;}
|
||||||
;
|
;
|
||||||
assignment_stmt : TOKEN_EQUAL op_parms
|
display_stmt : TOKEN_DISPLAY op_parms
|
||||||
{$$ = decl_create(NULL, NULL, $2, NULL, NULL);}
|
{$$ = stmt_create(STMT_PRINT, NULL, NULL, $2, NULL, NULL, NULL, NULL);}
|
||||||
| TOKEN_KEYWORD_TO op_parms
|
assignment_stmt : TOKEN_KEYWORD_COMPUTE ident TOKEN_EQUAL op_parms
|
||||||
{$$ = decl_create(NULL, NULL, $2, NULL, NULL);}
|
{$$ = stmt_create(STMT_COMPUTE, decl_create($2, NULL, $4, NULL, NULL), NULL, NULL, NULL, NULL, NULL, NULL);}
|
||||||
|
| TOKEN_MOVE type_expr TOKEN_KEYWORD_TO ident
|
||||||
|
{$$ = stmt_create(STMT_MOVE, decl_create($4, NULL, $2, NULL, NULL), NULL, NULL, NULL, NULL, NULL, NULL);}
|
||||||
;
|
;
|
||||||
op_parms : op_parm
|
op_parms : op_parm
|
||||||
{$$ = $1;}
|
{$$ = $1;}
|
||||||
|
|
@ -208,13 +210,6 @@ type_expr : ident
|
||||||
ext_function : TOKEN_KEYWORD_FUNCTION ident TOKEN_LEFT_PARENTHESIS ident TOKEN_RIGHT_PARENTHESIS
|
ext_function : TOKEN_KEYWORD_FUNCTION ident TOKEN_LEFT_PARENTHESIS ident TOKEN_RIGHT_PARENTHESIS
|
||||||
{$$ = expr_create(EXPR_CUSTOM_FUNCTION, $2, $4);}
|
{$$ = expr_create(EXPR_CUSTOM_FUNCTION, $2, $4);}
|
||||||
;
|
;
|
||||||
cbl_function : TOKEN_DISPLAY
|
|
||||||
{$$ = stmt_create(STMT_PRINT, NULL, NULL, NULL, NULL, NULL, NULL, NULL);}
|
|
||||||
| TOKEN_MOVE
|
|
||||||
{$$ = stmt_create(STMT_MOVE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);}
|
|
||||||
| TOKEN_KEYWORD_COMPUTE
|
|
||||||
{$$ = stmt_create(STMT_COMPUTE, NULL, NULL, NULL, NULL, NULL, NULL, NULL);}
|
|
||||||
;
|
|
||||||
if_branch : TOKEN_IF booleanexpr statement_list else_parts
|
if_branch : TOKEN_IF booleanexpr statement_list else_parts
|
||||||
{$$ = stmt_create(STMT_IF, NULL, NULL, $2, NULL, $3, $4, NULL);}
|
{$$ = stmt_create(STMT_IF, NULL, NULL, $2, NULL, $3, $4, NULL);}
|
||||||
;
|
;
|
||||||
|
|
@ -267,9 +262,7 @@ category_value : TOKEN_KEYWORD_VALUE TOKEN_INTEGER
|
||||||
|
|
|
|
||||||
{$$ = expr_create(EXPR_NULL, NULL, NULL);}
|
{$$ = expr_create(EXPR_NULL, NULL, NULL);}
|
||||||
;
|
;
|
||||||
category_spec : complete_category
|
category_spec : complete_category data_clause category_value
|
||||||
{$$ = decl_create(NULL, $1, NULL, NULL, NULL);}
|
|
||||||
| complete_category data_clause category_value
|
|
||||||
{ $1->level = $2->level; $$ = decl_create(NULL, $1, $3, NULL, NULL);}
|
{ $1->level = $2->level; $$ = decl_create(NULL, $1, $3, NULL, NULL);}
|
||||||
;
|
;
|
||||||
//TODO: implement levels
|
//TODO: implement levels
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,13 @@ PROGRAM-ID. ASSIGNMENT.
|
||||||
DATA DIVISION.
|
DATA DIVISION.
|
||||||
WORKING-STORAGE SECTION.
|
WORKING-STORAGE SECTION.
|
||||||
05 A PIC 9(2).
|
05 A PIC 9(2).
|
||||||
|
05 B PIC 9(2).
|
||||||
|
05 C PIC X(10).
|
||||||
PROCEDURE DIVISION.
|
PROCEDURE DIVISION.
|
||||||
MOVE 1 TO A
|
MOVE 1 TO A
|
||||||
|
MOVE 2 TO B
|
||||||
|
MOVE "Hello, World!" TO C
|
||||||
DISPLAY A
|
DISPLAY A
|
||||||
|
DISPLAY B
|
||||||
|
DISPLAY C
|
||||||
STOP RUN.
|
STOP RUN.
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
2
|
2.000000
|
||||||
|
|
|
||||||
|
|
@ -1 +1,3 @@
|
||||||
1
|
1.000000
|
||||||
|
2.000000
|
||||||
|
Hello, World!
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,13 @@ section
|
||||||
section
|
section
|
||||||
section
|
section
|
||||||
A = ();
|
A = ();
|
||||||
|
B = ();
|
||||||
|
C = ();
|
||||||
section
|
section
|
||||||
move 1 to A;
|
move 1 to A;
|
||||||
|
move 2 to B;
|
||||||
|
move Hello, World! to C;
|
||||||
print A;
|
print A;
|
||||||
|
print B;
|
||||||
|
print C;
|
||||||
stop run
|
stop run
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
EQUATION: (1x^2) + 5x + 6 = 0
|
EQUATION: (1x^2) + 5x + 6 = 0
|
||||||
The equation has two distinct real roots:
|
The equation has two distinct real roots:
|
||||||
Root 1: -2
|
Root 1: -2.000000
|
||||||
Root 2: -3
|
Root 2: -3.000000
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue