-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
179 lines (161 loc) · 5.63 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "auto_type.h"
#include <stdbool.h>
#include "j_core.h"
#include "j_double.h"
#include "j_message.h"
#include "j_commands.h"
#include "j_applied_functions.h"
#define PANIC 666
#define MEMORY_SIZE (_R_DATA + 1)
typedef enum j_state {
J_RUN, J_EMPTY, J_FAIL
} j_state;
int running_count_of_steps(j_core *);
int full_running(j_core *);
int run_code_step_by_step(j_core *);
int run_code_step_by_step_with_printing_results(j_core *);
char state_key[2];
char user_answer[10];
int main(int argc, char *args[]) {
var current_state = J_RUN;
var core = new_j_core();
while (true) {
if (current_state != J_EMPTY) {
print_j_commands();
if (!init_core(core, args, argc)) {
var file_name = args[1];
send_file_is_not_found(file_name);
if (!init_core_by_default(core)) {
return PANIC;
}
}
}
fgets(user_answer, 10, stdin);
if (is_full_command(user_answer)) { //Полное выполнение кода
if (full_running(core) != 0) current_state = J_FAIL;
} else if (is_count_command(user_answer)) { //выполнение определенного числа шагов
if (running_count_of_steps(core) != 0) current_state = J_FAIL;
} else if (is_step_by_step_command(user_answer)) { //выполнить код пошагово
if (run_code_step_by_step(core) != 0) current_state = J_FAIL;
} else if (is_step_by_step_with_debug_command(user_answer)) { //выполнить код пошагово с выводом результата
if (run_code_step_by_step_with_printing_results(core) != 0) current_state = J_FAIL;
} else if (is_exit_command(user_answer)) {
free_j_core_materials(core);
return 0;
} else if (is_help_command(user_answer)) {
current_state = J_FAIL;
} else if (is_empty_string(user_answer)) {
current_state = J_EMPTY;
} else {
printf("Not found command_chunk\n");
current_state = J_FAIL;
}
if (current_state == J_RUN) {
write_j_state_in_file(core);
printf("You code is successfully completed!\n");
}
if (current_state == J_FAIL) {
current_state = J_RUN;
}
if (current_state != J_EMPTY) {
free_j_core_materials(core);
}
}
}
int StrToInt(const char *s) {
int temp = 0; // число
int i = 0;
int sign = 0; // знак числа 0- положительное, 1 — отрицательное
if (s[i] == '-') {
sign = 1;
i++;
}
while (s[i] >= 0x30 && s[i] <= 0x39) {
temp = temp + (s[i] & 0x0F);
temp = temp * 10;
i++;
}
temp = temp / 10;
if (sign == 1)
temp = -temp;
return (temp);
}
int running_count_of_steps(j_core *constant) {
printf("Input count of steps \n");
char *string = malloc(sizeof(char) * 100);
memset(string, 0, sizeof(char) * 100);
fgets(string, 10, stdin);
int countSteps = StrToInt(string);
while (countSteps > 0) {
int result = pars(0, constant);
if (result != 0) return result;
countSteps--;
}
return 0;
}
int full_running(j_core *constant) {
while (!feof(constant->input_file_ref)) {
int result = pars(0, constant);
if (result != 0) return result;
}
return 0;
}
int run_code_step_by_step(j_core *constant) {
while (!feof(constant->input_file_ref)) {
printf("s - next step \n");
printf("f - run to end \n");
printf("q - exit \n");
fgets(state_key, 2, stdin);
if (strcmp(state_key, _WHITHOUT) == 0) {
int result = pars(0, constant);
if (result != 0) return result;
}
// break;
if (strcmp(state_key, _EXIT) == 0) {
return 0;
}//завершаем работу программы
if (strcmp(state_key, _FULL) == 0) {
while (!feof(constant->input_file_ref)) {
int result = pars(0, constant);
if (result != 0) return result;
}
}
}
return 0;
}
int run_code_step_by_step_with_printing_results(j_core *constant) {
while (!feof(constant->input_file_ref)) {
printf("w - write memory \n");
printf("s - not write memory \n");
printf("f - run to end \n");
printf("q - exit \n");
fgets(state_key, sizeof(state_key), stdin);
if (strcmp(state_key, _WHITH) == 0) {// Space- следующая команда
{
int result = pars(2, constant);
if (result != 0) return result;
printf(" Ac: %i/%i R: %i; ", constant->accumulator->numerator, constant->accumulator->denominator,
constant->_pointer);
}
}
if (strcmp(state_key, _WHITHOUT) == 0) {
int result = pars(1, constant);
if (result != 0) return result;
printf(" Ac: %i/%i R: %i; ", constant->accumulator->numerator, constant->accumulator->denominator,
constant->_pointer);
}
if (strcmp(state_key, _EXIT) == 0) {//Esc - прекратить выполнение программы
return 0;
}
if (strcmp(state_key, _FULL) == 0) {
while (!feof(constant->input_file_ref)) {
int result = pars(1, constant);
if (result != 0) return result;
}
}
}
return 0;
}