int main(int argc, char* argv[])
{
printf("ABC interpreter v0.0.1a by /prog/\n\n");
if(argc < 2)
{
fprintf(stderr, "Error: no program text.\n\nUsage: %s [program text]\n", argv[0]);
return 1;
}
else if(argc > 2)
{
fprintf(stderr, "Warning: more than 1 argument detected, ignoring trailing arguments.\n");
}
int acc = 0;
int l = strlen(argv[1]);
int i;
for(i = 0; i < l; i++)
{
if (argv[1][i] == 'a') { acc++; }
else if (argv[1][i] == 'b') { acc--; }
else if (argv[1][i] == 'c') { printf("%d\n", acc); }
else
{
printf("Error:c%d:unrecognised character.\n", i);
/* print line that error was on, and underneath put a ^ to indicate what char we can't parse */
char* progstr = malloc(i * sizeof(char) + 1);
int j;
for(j = 0; j <= i; j++)
{
progstr[j] = argv[1][j];
}
progstr[i + 1] = '\0';
printf("%s\n", progstr); /* this (rather than just printf(progstr);) is to stop users from putting placeholders in the program string, causing printf() to parse them */
printf("%s\n", arrowstr);