-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
97 lines (87 loc) · 2.64 KB
/
main.cpp
File metadata and controls
97 lines (87 loc) · 2.64 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//
// Created by Anirudh Lath on 1/23/2022.
//
#define CATCH_CONFIG_RUNNER
#include "catch.hpp"
#include "Parser.h"
#include "Val.h"
#include <iostream>
void helper(int argc, char **argv);
using namespace std;
int main(int argc, char **argv) {
try {
helper(argc, argv);
return 0;
}
catch (runtime_error e) {
cerr << e.what() << endl;
return 1;
}
}
void helper(int argc, char **argv) {
const std::string TEST = "--test";
const std::string INTERP = "--interp";
const std::string PRINT = "--print";
const std::string PRETTY_PRINT = "--pretty-print";
const string commands[] = {TEST, INTERP, PRINT, PRETTY_PRINT};
if (argc > 1) {
bool isValidCommand = false;
for (int i = 0; i < commands->size(); ++i) {
if (argv[1] == commands[i]) {
isValidCommand = true;
}
}
if (isValidCommand) {
if (argv[1] == TEST) {
if (Catch::Session().run() != 0) {
exit(1);
}
}
if (argv[1] == INTERP) {
PTR(Expr)e = parse(cin);
try {
cout << e->interp()->to_string() << endl;
exit(0);
}
catch (const runtime_error &error) {
cout << "Invalid expression." << endl;
exit(1);
}
}
if (argv[1] == PRINT) {
PTR(Expr)e = parse(cin);
try {
cout << e->to_string(false) << endl;
exit(0);
}
catch (const runtime_error &error) {
cout << "Invalid expression." << endl;
exit(1);
}
}
if (argv[1] == PRETTY_PRINT) {
PTR(Expr)e = parse(cin);
try {
cout << e->to_string(true) << endl;
exit(0);
}
catch (const runtime_error &error) {
cout << "Invalid expression." << endl;
exit(1);
}
}
}
else {
cerr << "Invalid command. ";
cout << "The only valid commands are:" << endl;
for (int i = 0; i < commands->size(); ++i) {
cout << commands[i] << endl;
}
cout << endl << "Please try again using valid commands, the program will now exit." << endl;
exit(1);
}
}
else {
std::cout << "The program has successfully compiled!" << std::endl;
}
}