forked from cuffscript/cuffscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (72 loc) · 1.82 KB
/
Copy pathmain.cpp
File metadata and controls
80 lines (72 loc) · 1.82 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
#include "engine/CuffEngine.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <filesystem>
namespace
{
void printUsage(const char *prog)
{
std::cerr << "Usage: " << prog << " [--ast|--tokens] [script.cuff]\n"
<< " (no file) read the script from stdin\n"
<< " --ast print tokens + AST instead of running the script\n";
}
}
int main(int argc, char *argv[])
{
bool debugMode = false;
std::string path;
for (int i = 1; i < argc; ++i)
{
std::string arg = argv[i];
if (arg == "--ast" || arg == "--tokens" || arg == "--debug")
{
debugMode = true;
}
else if (arg == "-h" || arg == "--help")
{
printUsage(argv[0]);
return 0;
}
else
{
path = arg;
}
}
std::string source;
std::string scriptDir = ".";
if (!path.empty())
{
std::ifstream file(path);
if (!file)
{
std::cerr << "Error: Cannot open file '" << path << "'\n";
return 1;
}
std::ostringstream ss;
ss << file.rdbuf();
source = ss.str();
std::filesystem::path p(path);
scriptDir = p.has_parent_path() ? p.parent_path().string() : ".";
}
else
{
std::ostringstream ss;
ss << std::cin.rdbuf();
source = ss.str();
}
if (debugMode)
{
cuff::CuffEngine::Result result = cuff::CuffEngine::run(source);
cuff::CuffEngine::debugDump(result);
return result.success ? 0 : 1;
}
cuff::CuffEngine::Result result = cuff::CuffEngine::execute(source, scriptDir);
if (!result.success)
{
std::cerr << "ERROR: " << result.error << "\n";
return 1;
}
return 0;
}