aboutsummaryrefslogtreecommitdiff
path: root/src/moonc.cpp
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-02-07 17:29:34 +0800
committerLi Jin <dragon-fly@qq.com>2020-02-07 17:29:34 +0800
commitc241ea241e8e9c152f6eb14f163b2ae39749f7bf (patch)
tree2fd05ca6866ea60ca778fb6ff31c7ec429e138c4 /src/moonc.cpp
parent2e50c15bfe67d4709880a0377d37fca191be2f3e (diff)
downloadyuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.tar.gz
yuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.tar.bz2
yuescript-c241ea241e8e9c152f6eb14f163b2ae39749f7bf.zip
releasing moonplus as a lib.
Diffstat (limited to 'src/moonc.cpp')
-rw-r--r--src/moonc.cpp179
1 files changed, 0 insertions, 179 deletions
diff --git a/src/moonc.cpp b/src/moonc.cpp
deleted file mode 100644
index d04ce47..0000000
--- a/src/moonc.cpp
+++ /dev/null
@@ -1,179 +0,0 @@
1/* Copyright (c) 2020 Jin Li, http://www.luvfight.me
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8#include <iostream>
9#include <iomanip>
10#include <fstream>
11#include <chrono>
12#include <future>
13#include "MoonP/moon_compiler.h"
14#include "MoonP/moon_parser.h"
15
16int main(int narg, const char** args) {
17 const char* help =
18"Usage: moonc [options|files] ...\n\n"
19" -h Print this message\n"
20" -t path Specify where to place compiled files\n"
21" -o file Write output to file\n"
22" -p Write output to standard out\n"
23" -b Dump compile time (doesn't write output)\n"
24" -l Write line numbers from source codes\n"
25" -v Print version\n";
26 if (narg == 0) {
27 std::cout << help;
28 return 0;
29 }
30 MoonP::MoonConfig config;
31 config.reserveLineNumber = false;
32 bool writeToFile = true;
33 bool dumpCompileTime = false;
34 std::string targetPath;
35 std::string resultFile;
36 std::list<std::string> files;
37 for (int i = 1; i < narg; ++i) {
38 switch (hash(args[i])) {
39 case "-l"_id:
40 config.reserveLineNumber = true;
41 break;
42 case "-p"_id:
43 writeToFile = false;
44 break;
45 case "-t"_id:
46 ++i;
47 if (i < narg) {
48 targetPath = args[i];
49 } else {
50 std::cout << help;
51 return 1;
52 }
53 break;
54 case "-b"_id:
55 dumpCompileTime = true;
56 break;
57 case "-h"_id:
58 std::cout << help;
59 return 0;
60 case "-v"_id:
61 std::cout << "Moonscript version: " << MoonP::moonScriptVersion() << '\n';
62 return 0;
63 case "-o"_id:
64 ++i;
65 if (i < narg) {
66 resultFile = args[i];
67 } else {
68 std::cout << help;
69 return 1;
70 }
71 break;
72 default:
73 files.push_back(args[i]);
74 break;
75 }
76 }
77 if (files.empty()) {
78 std::cout << help;
79 return 0;
80 }
81 if (!resultFile.empty() && files.size() > 1) {
82 std::cout << "Error: -o can not be used with multiple input files.\n";
83 std::cout << help;
84 }
85 if (targetPath.back() != '/' && targetPath.back() != '\\') {
86 targetPath.append("/");
87 }
88 std::list<std::future<std::result_of_t<std::decay_t<int()>()>>> results;
89 for (const auto& file : files) {
90 auto task = std::async(std::launch::async, [=]() {
91 std::ifstream input(file, input.in);
92 if (input) {
93 std::string s(
94 (std::istreambuf_iterator<char>(input)),
95 std::istreambuf_iterator<char>());
96 if (dumpCompileTime) {
97 auto start = std::chrono::high_resolution_clock::now();
98 auto result = MoonP::moonCompile(s, config);
99 auto end = std::chrono::high_resolution_clock::now();
100 if (!std::get<0>(result).empty()) {
101 std::chrono::duration<double> diff = end - start;
102 start = std::chrono::high_resolution_clock::now();
103 MoonP::MoonParser{}.parse<MoonP::File_t>(s);
104 end = std::chrono::high_resolution_clock::now();
105 std::chrono::duration<double> parseDiff = end - start;
106 std::cout << file << " \n";
107 std::cout << "Parse time: " << std::setprecision(5) << parseDiff.count() * 1000 << " ms\n";
108 std::cout << "Compile time: " << std::setprecision(5) << (diff.count() - parseDiff.count()) * 1000 << " ms\n\n";
109 return 0;
110 } else {
111 std::cout << "Fail to compile: " << file << ".\n";
112 std::cout << std::get<1>(result) << '\n';
113 return 1;
114 }
115 }
116 auto result = MoonP::moonCompile(s, config);
117 if (!std::get<0>(result).empty()) {
118 if (!writeToFile) {
119 std::cout << std::get<0>(result) << '\n';
120 return 1;
121 } else {
122 std::string targetFile;
123 if (resultFile.empty()) {
124 std::string ext;
125 targetFile = file;
126 size_t pos = file.rfind('.');
127 if (pos != std::string::npos) {
128 ext = file.substr(pos + 1);
129 for (size_t i = 0; i < ext.length(); i++) {
130 ext[i] = static_cast<char>(tolower(ext[i]));
131 }
132 targetFile = file.substr(0, pos) + ".lua";
133 }
134 if (!targetPath.empty()) {
135 std::string name;
136 pos = targetFile.find_last_of("/\\");
137 if (pos == std::string::npos) {
138 name = targetFile;
139 } else {
140 name = targetFile.substr(pos + 1);
141 }
142 targetFile = targetPath + name;
143 }
144 } else {
145 targetFile = resultFile;
146 }
147 std::ofstream output(targetFile, output.trunc | output.out);
148 if (output) {
149 const auto& codes = std::get<0>(result);
150 output.write(codes.c_str(), codes.size());
151 std::cout << "Built " << file << '\n';
152 return 0;
153 } else {
154 std::cout << "Fail to write file: " << targetFile << ".\n";
155 return 1;
156 }
157 }
158 } else {
159 std::cout << "Fail to compile: " << file << ".\n";
160 std::cout << std::get<1>(result) << '\n';
161 return 1;
162 }
163 } else {
164 std::cout << "Fail to read file: " << file << ".\n";
165 return 1;
166 }
167 });
168 results.push_back(std::move(task));
169 }
170 int ret = 0;
171 for (auto& result : results) {
172 int val = result.get();
173 if (val != 0) {
174 ret = val;
175 }
176 }
177 return ret;
178}
179