-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcvm.c
197 lines (156 loc) · 4.29 KB
/
cvm.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "cvmkernel.h"
#define CVM_HELP "help"
#define CVM_RUN "run"
#define CVM_BUILD "build"
#define CVM_OUTFILE "main.bcd"
enum {
ERR_NONE = 0x00,
ERR_ARGLEN = 0x01,
ERR_COMMAND = 0x02,
ERR_INOPEN = 0x03,
ERR_OUTOPEN = 0x04,
ERR_COMPILE = 0x05,
ERR_MEMSIZ = 0x06,
ERR_RUN = 0x07,
};
static const char *errors[] = {
[ERR_NONE] = "",
[ERR_ARGLEN] = "len argc < 3",
[ERR_COMMAND] = "unknown command",
[ERR_INOPEN] = "open input file",
[ERR_OUTOPEN] = "open output file",
[ERR_COMPILE] = "compile code",
[ERR_MEMSIZ] = "memory size overflow",
[ERR_RUN] = "run byte code",
};
static int file_build(const char *outputf, const char *inputf);
static int file_run(const char *filename, int **output, int *input);
static void print_json_failed(int retcode);
static void print_json_success(int *array, int size);
int main(int argc, char const *argv[]) {
const char *outfile;
int input[argc];
int *output;
int retcode;
int is_build;
int is_run;
outfile = CVM_OUTFILE;
retcode = ERR_COMMAND;
// cvm help
if (argc == 2 && strcmp(argv[1], CVM_HELP) == 0) {
printf("help: \n\t$ cvm [build|run] <infile> {if build [-o <outfile>]}\n");
return ERR_NONE;
}
// cvm | cvm undefined
if (argc < 3) {
fprintf(stderr, "error: %s\n", errors[ERR_ARGLEN]);
return ERR_ARGLEN;
}
is_build = strcmp(argv[1], CVM_BUILD) == 0;
is_run = strcmp(argv[1], CVM_RUN) == 0;
// cvm undefined x
if (!is_build && !is_run) {
fprintf(stderr, "error: %s\n", errors[ERR_COMMAND]);
return ERR_COMMAND;
}
// cvm build file [-o outfile]
if (is_build) {
if (argc == 5 && strcmp(argv[3], "-o") == 0) {
outfile = argv[4];
}
retcode = file_build(outfile, argv[2]);
if (retcode != ERR_NONE) {
fprintf(stderr, "error: %s\n", errors[retcode]);
}
}
// cvm run file [args]
if (is_run) {
input[0] = argc-3;
for (int i = 0; i < input[0]; ++i) {
input[i+1] = atoi(argv[i+3]);
}
retcode = file_run(argv[2], &output, input);
if (retcode == ERR_NONE) {
print_json_success(output+1, output[0]);
free(output);
} else {
print_json_failed(retcode);
}
}
return retcode;
}
static int file_build(const char *outputf, const char *inputf) {
FILE *output, *input;
int retcode;
input = fopen(inputf, "r");
if (input == NULL) {
return ERR_INOPEN;
}
output = fopen(outputf, "wb");
if (output == NULL) {
fclose(input);
return ERR_OUTOPEN;
}
retcode = cvm_compile(output, input);
fclose(input);
fclose(output);
if (retcode != ERR_NONE) {
return ERR_COMPILE;
}
return ERR_NONE;
}
static int file_run(const char *inputf, int **output, int *input) {
unsigned char *memory;
int fsize, retcode;
FILE *reader;
reader = fopen(inputf, "rb");
if (reader == NULL) {
return ERR_INOPEN;
}
// read size of code
fseek(reader, 0, SEEK_END);
fsize = ftell(reader);
fseek(reader, 0, SEEK_SET);
// insert code into memory
memory = (unsigned char*)malloc(sizeof(char)*fsize);
fread(memory, fsize, sizeof(char), reader);
fclose(reader);
fsize = cvm_load(memory, fsize);
free(memory);
if (fsize < 0) {
return ERR_MEMSIZ;
}
// run code in memory
retcode = cvm_run(output, input);
if (retcode != ERR_NONE) {
return ERR_RUN;
}
return ERR_NONE;
}
static void print_json_failed(int retcode) {
// begin object
printf("{\n");
// error:string
printf("\t\"error\": \"%s\",\n", errors[retcode]);
// return:int
printf("\t\"return\": %d\n", retcode);
// end object
printf("}\n");
}
static void print_json_success(int *array, int size) {
// begin object
printf("{\n");
// result:array
printf("\t\"result\": [");
for (int i = 0; i < size; ++i) {
printf("%d%c", array[i], (i == size-1) ? '\0' : ',');
}
printf("],\n");
// return:int
printf("\t\"return\": 0\n");
// end object
printf("}\n");
}