-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
75 lines (61 loc) · 1.43 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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "headers/parse.h"
/* arm is a simple interpreted language */
bool SHOWTREE = false;
bool VARDUMP = false;
void printUsage(char *progName) {
fprintf(stderr, "usage: %s [-tv] [inputFile]\n", progName);
}
int main(int argc, char *argv[]) {
FILE *inputFile = NULL; // Default place to get input
bool fromInp = false;
// -Options
// -t prints syntax tree after each statement
int fileArg = 1;
if (argc > 1 && argv[1][0] == '-') {
// Can't just have '-' as an argument
if (argv[1][1] == '\0') {
fprintf(stderr, "Option must be specified\n");
printUsage(argv[0]);
abort();
}
for (int i=1; argv[1][i] != '\0'; i++) {
switch (argv[1][i]) {
case 't':
SHOWTREE = true;
break;
case 'v':
VARDUMP = true;
break;
default:
fprintf(stderr, "Unknown option '%c'\n", argv[1][i]);
printUsage(argv[0]);
abort();
}
}
argc--; fileArg = 2; // So we can continue as normal
}
if (argc == 1) {
inputFile = stdin;
fromInp = true;
} else if (argc == 2) {
inputFile = fopen(argv[fileArg], "r");
if (!inputFile) {
fprintf(stderr, "Failed to open '%s'\n", argv[fileArg]);
return 1;
}
printf("Taking input from file '%s'\n", argv[fileArg]);
} else {
printUsage(argv[0]);
return 1;
}
if (!parse(inputFile)) {
fprintf(stderr, "!Failed to parse!\n");
}
if (!fromInp) {
fclose(inputFile);
}
return 0;
}