-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppscript.cpp
74 lines (65 loc) · 2.19 KB
/
cppscript.cpp
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
#include <iostream>
#include <cstdlib>
#include <filesystem>
#include <fstream>
const int SUCCESS = 0;
const int MAX_ARGUMENTS = 2;
const std::string importTag = "@import";
const std::string compilerString = "clang++ --std=c++17 -Wall -Werror";
int main(int argCount, char *argValues[])
{
if (argCount != MAX_ARGUMENTS)
{
std::cout << "Usage: cppscript programInput.cpp" << std::endl;
return 0;
}
// Process command-line arguments.
std::string programInput = argValues[1];
const std::string programOutput =
programInput.substr(0,programInput.find_last_of('.')) + ".run";
const std::string toExecuteString = "./" + programOutput;
// Process @imports.
std::ifstream programFile(programInput);
if (programFile.is_open())
{
std::string line;
while (std::getline(programFile, line))
{
// std::cout << "Looking through line: " << line << std::endl;
size_t index = line.find(importTag);
if (index != std::string::npos)
{
programInput += " " + line.substr(
index + importTag.length() + 1);
std::cout << "index " << index
<< " programInput " << programInput << std::endl;
}
}
}
else
{
std::cout << "Could not open file." << std::endl;
}
// Compile.
const std::string toCompileString =
compilerString + " " + programInput + " -o " + programOutput;
std::cout << "Compiling " << programInput << std::endl;
std::cout << toCompileString << std::endl;
int result = std::system(toCompileString.c_str());
std::cout << "Finished compiling " << programInput << " (" << result << ")"
<< std::endl;
// Execute.
if (result == SUCCESS)
{
std::cout << "Running " << programOutput << std::endl;
std::cout << toExecuteString << std::endl;
result = std::system(toExecuteString.c_str());
std::cout << "Finished executing " << programOutput << " (" << result << ")"
<< std::endl;
}
else
{
std::cout << "Since there was an error, program was not run." << std::endl;
}
return 0;
}