aboutsummaryrefslogtreecommitdiff
path: root/src/moonc.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/moonc.cpp')
-rw-r--r--src/moonc.cpp178
1 files changed, 178 insertions, 0 deletions
diff --git a/src/moonc.cpp b/src/moonc.cpp
new file mode 100644
index 0000000..d9d3ce3
--- /dev/null
+++ b/src/moonc.cpp
@@ -0,0 +1,178 @@
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 "MoonP/moon_compiler.h"
13#include "MoonP/parser.hpp"
14#include "MoonP/moon_ast.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" -a Allow expression list not in the end of body block\n"
26" -s Use space over tab\n"
27" -v Print version\n";
28 if (narg == 0) {
29 std::cout << help;
30 return 0;
31 }
32 MoonP::MoonConfig config;
33 bool writeToFile = true;
34 bool dumpCompileTime = false;
35 std::string targetPath;
36 std::string resultFile;
37 std::list<std::string> files;
38 for (int i = 1; i < narg; ++i) {
39 switch (hash(args[i])) {
40 case "-a"_id:
41 config.allowExprNotInTheEndOfBody = true;
42 break;
43 case "-s"_id:
44 config.spaceOverTab = true;
45 break;
46 case "-l"_id:
47 config.reserveLineNumber = true;
48 break;
49 case "-r"_id:
50 config.reuseVariable = true;
51 break;
52 case "-p"_id:
53 writeToFile = false;
54 break;
55 case "-t"_id:
56 ++i;
57 if (i < narg) {
58 targetPath = args[i];
59 } else {
60 std::cout << help;
61 return 1;
62 }
63 break;
64 case "-b"_id:
65 dumpCompileTime = true;
66 break;
67 case "-h"_id:
68 std::cout << help;
69 return 0;
70 case "-v"_id:
71 std::cout << "Moonscript version: " << MoonP::moonScriptVersion() << '\n';
72 break;
73 case "-o"_id:
74 ++i;
75 if (i < narg) {
76 resultFile = args[i];
77 } else {
78 std::cout << help;
79 return 1;
80 }
81 break;
82 default:
83 files.push_back(args[i]);
84 break;
85 }
86 }
87 if (files.empty()) {
88 std::cout << help;
89 return 0;
90 }
91 if (!resultFile.empty() && files.size() > 1) {
92 std::cout << "Error: -o can not be used with multiple input files.\n";
93 std::cout << help;
94 }
95 for (const auto& file : files) {
96 std::ifstream input(file, input.in);
97 if (input) {
98 std::string s(
99 (std::istreambuf_iterator<char>(input)),
100 std::istreambuf_iterator<char>());
101 if (dumpCompileTime) {
102 auto start = std::chrono::high_resolution_clock::now();
103 auto result = MoonP::moonCompile(s, config);
104 auto end = std::chrono::high_resolution_clock::now();
105 if (!std::get<0>(result).empty()) {
106 std::chrono::duration<double> diff = end - start;
107 error_list el;
108 MoonP::State st;
109 start = std::chrono::high_resolution_clock::now();
110 auto input = Converter{}.from_bytes(s);
111 parserlib::parse<MoonP::File_t>(input, MoonP::File, el, &st);
112 end = std::chrono::high_resolution_clock::now();
113 std::chrono::duration<double> parseDiff = end - start;
114 std::cout << file << " \n";
115 std::cout << "Parse time: " << std::setprecision(5) << parseDiff.count() * 1000 << " ms\n";
116 std::cout << "Compile time: " << std::setprecision(5) << (diff.count() - parseDiff.count()) * 1000 << " ms\n\n";
117 } else {
118 std::cout << "Fail to compile: " << file << ".\n";
119 std::cout << std::get<1>(result) << '\n';
120 return 1;
121 }
122 continue;
123 }
124 auto result = MoonP::moonCompile(s, config);
125 if (!std::get<0>(result).empty()) {
126 if (!writeToFile) {
127 std::cout << std::get<0>(result) << '\n';
128 } else {
129 std::string targetFile;
130 if (resultFile.empty()) {
131 std::string ext;
132 targetFile = file;
133 size_t pos = file.rfind('.');
134 if (pos != std::string::npos) {
135 ext = file.substr(pos + 1);
136 for (size_t i = 0; i < ext.length(); i++) {
137 ext[i] = static_cast<char>(tolower(ext[i]));
138 }
139 targetFile = file.substr(0, pos) + ".lua";
140 }
141 if (!targetPath.empty()) {
142 std::string name;
143 pos = targetFile.find_last_of("/\\");
144 if (pos == std::string::npos) {
145 name = targetFile;
146 } else {
147 name = targetFile.substr(pos + 1);
148 }
149 if (targetPath.back() != '/' && targetPath.back() != '\\') {
150 targetPath.append("/");
151 }
152 targetFile = targetPath + name;
153 }
154 } else {
155 targetFile = resultFile;
156 }
157 std::ofstream output(targetFile, output.trunc | output.out);
158 if (output) {
159 const auto& codes = std::get<0>(result);
160 output.write(codes.c_str(), codes.size());
161 std::cout << "Built " << file << '\n';
162 } else {
163 std::cout << "Fail to write file: " << targetFile << ".\n";
164 return 1;
165 }
166 }
167 } else {
168 std::cout << "Fail to compile: " << file << ".\n";
169 std::cout << std::get<1>(result) << '\n';
170 return 1;
171 }
172 } else {
173 std::cout << "Fail to read file: " << file << ".\n";
174 return 1;
175 }
176 }
177 return 0;
178}