Updated error parsing, compilation errors and test suite

This commit is contained in:
Veit Heller
2014-10-12 13:38:33 +02:00
parent bb59f48e09
commit 76537151a1
3 changed files with 46 additions and 11 deletions

View File

@@ -180,8 +180,9 @@ program vm_parse(char *filename){
while(getline(&line, &linelength, file) != -1){ while(getline(&line, &linelength, file) != -1){
short found = FALSE; short found = FALSE;
unsigned int elemc = 0;
strtok(line, "\n"); strtok(line, "\n");
command = str_split(line, ' '); command = str_split(line, ' ', &elemc);
if(codep == size){ if(codep == size){
size += 100; size += 100;
@@ -202,6 +203,12 @@ program vm_parse(char *filename){
if(found && ins[i].operands > 0){ if(found && ins[i].operands > 0){
int nargs = ins[i].operands; int nargs = ins[i].operands;
if(nargs != elemc-1){
fprintf(stderr,
"Line %s called with wrong number of arguments\
(got %d, expected %d)\n", line, elemc-1, nargs);
die(127, "Compilation failed.");
}
for(i = 1; i <= nargs; i++){ for(i = 1; i <= nargs; i++){
code[codep++] = (int) strtol(command[i], (char **) NULL, 10); code[codep++] = (int) strtol(command[i], (char **) NULL, 10);
} }
@@ -221,9 +228,8 @@ program vm_parse(char *filename){
return prog; return prog;
} }
char** str_split(char* a_str, const char a_delim){ char** str_split(char* a_str, const char a_delim, unsigned int* elemc){
char** result = 0; char** result = 0;
size_t count = 0;
char* tmp = a_str; char* tmp = a_str;
char* last_comma = 0; char* last_comma = 0;
char delim[2]; char delim[2];
@@ -232,30 +238,32 @@ char** str_split(char* a_str, const char a_delim){
while (*tmp){ while (*tmp){
if (a_delim == *tmp){ if (a_delim == *tmp){
count++; (*elemc)++;
last_comma = tmp; last_comma = tmp;
} }
tmp++; tmp++;
} }
count += last_comma < (a_str + strlen(a_str) - 1); *elemc += last_comma < (a_str + strlen(a_str) - 1);
count++; (*elemc)++;
result = malloc(sizeof(char*) * count); result = malloc(sizeof(char*) * *elemc);
if (result){ if (result){
size_t idx = 0; size_t idx = 0;
char* token = strtok(a_str, delim); char* token = strtok(a_str, delim);
while (token){ while (token){
assert(idx < count); assert(idx < *elemc);
*(result + idx++) = strdup(token); *(result + idx++) = strdup(token);
token = strtok(0, delim); token = strtok(0, delim);
} }
assert(idx == count - 1); assert(idx == *elemc - 1);
*(result + idx) = 0; *(result + idx) = 0;
} }
(*elemc)--;
return result; return result;
} }

View File

@@ -38,6 +38,13 @@ void vm_execute(int[], int, int, unsigned long);
*/ */
program vm_parse(char*); program vm_parse(char*);
char** str_split(char*, const char); /**
* @brief str_split
* @param a_str -> the input string
* @param a_delim -> the delimiter
* @param elemc -> the counter of elements
*
*/
char** str_split(char*, const char, unsigned int*);
#endif #endif

View File

@@ -1,9 +1,29 @@
#!/bin/bash #!/bin/bash
for i in `ls test` ; do for i in `ls test | grep -v fail` ; do
echo Running test $i: echo Running test $i:
echo --------------------- echo ---------------------
bin/vvm test/$i bin/vvm test/$i
return=$?
if [ $return != 0 ] ; then
echo Test $i failed.
exit 1
fi
echo
echo ---------------------
echo End of test $i
echo
done
for i in `ls test/*fail*` ; do
echo Running expected fail test $i:
echo ---------------------
bin/vvm $i
return=$?
if [ $return != 127 ] ; then
echo Test $i failed.
exit 1
fi
echo echo
echo --------------------- echo ---------------------
echo End of test $i echo End of test $i