-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_prog.c
More file actions
73 lines (68 loc) · 2.12 KB
/
test_prog.c
File metadata and controls
73 lines (68 loc) · 2.12 KB
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
#include <stdio.h> // FILE{}, fgets()
#include <unistd.h> // STD*_FILENO
#include "token.h"
#include "bcode.h"
#include "program.h"
FILE *Debug;
int
main()
{
char buffer[ 256 ];
byte_t bytes[ 256 ];
int bsize, ret, num;
bcode_t b;
Debug = stderr;
token_init();
prog_init();
while (fgets(buffer, 256, stdin) != NULL) {
if (*buffer == 0 || *buffer == '\n')
break;
fprintf(Debug, "===== %s", buffer);
token_source(buffer);
ret = bsize = bcode_compile(bytes);
if (bsize < 0) {
fprintf(Debug, "***** Error: %s\n", bcode_result(ret));
break;
}
fprintf(Debug, ">>>>> Compiled bytecodes [ %d bytes ]\n", bsize);
#ifdef DEBUG
bcode_dump(bytes);
#endif // DEBUG
bcode_start(bytes);
bcode_read(&b);
if (b.inst == BCODE_LIST) {
fprintf(Debug, "-----------------------------------------\n");
ret = prog_list(STDOUT_FILENO, 1, Lines[1].num, Lines[Lsize].num);
fprintf(Debug, "-----------------------------------------\n");
} else if (b.inst == BCODE_NEW) {
prog_init();
continue;
} else if (b.type == BCODE_TYPE_NUMBER10) {
num = b.num;
if (bcode_next() == BCODE_EOL) {
ret = prog_delete(num,num);
if (ret) {
fprintf(Debug, "***** Error: %s\n", prog_result(ret));
continue;
}
fprintf(Debug, ">>>>> Line %d deleted\n", num);
} else {
bcode_read(&b);
ret = prog_insert(num, &bytes[ b.pos ], bsize - 3);
if (ret) {
fprintf(Debug, "***** Error: %s\n", prog_result(ret));
continue;
}
fprintf(Debug, ">>>>> Line %d inserted\n", num);
}
} else {
fprintf(Debug, "***** Error: illegal program line\n");
continue;
}
prog_dumpbytes();
printf("\n");
prog_dumplines();
ret = 0;
}
return 0;
}