aboutsummaryrefslogtreecommitdiff
path: root/src/moonc.cpp
blob: 02d5275ee7291562f41862f959de1764fe30203d (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Copyright (c) 2020 Jin Li, http://www.luvfight.me

Permission 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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE 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. */
#include <iostream>
#include <iomanip>
#include <fstream>
#include <chrono>
#include "MoonP/moon_compiler.h"
#include "MoonP/parser.hpp"
#include "MoonP/moon_ast.h"

int main(int narg, const char** args) {
	const char* help =
"Usage: moonc [options|files] ...\n\n"
"    -h          Print this message\n"
"    -t path     Specify where to place compiled files\n"
"    -o file     Write output to file\n"
"    -p          Write output to standard out\n"
"    -b          Dump compile time (doesn't write output)\n"
"    -l          Write line numbers from source codes\n"
"    -a          Allow expression list not in the end of body block\n"
"    -s          Use space over tab\n"
"    -v          Print version\n";
	if (narg == 0) {
		std::cout << help;
		return 0;
	}
	MoonP::MoonConfig config;
	bool writeToFile = true;
	bool dumpCompileTime = false;
	std::string targetPath;
	std::string resultFile;
	std::list<std::string> files;
	for (int i = 1; i < narg; ++i) {
		switch (hash(args[i])) {
			case "-a"_id:
				config.allowExprNotInTheEndOfBody = true;
				break;
			case "-s"_id:
				config.spaceOverTab = true;
				break;
			case "-l"_id:
				config.reserveLineNumber = true;
				break;
			case "-r"_id:
				config.reuseVariable = true;
				break;
			case "-p"_id:
				writeToFile = false;
				break;
			case "-t"_id:
				++i;
				if (i < narg) {
					targetPath = args[i];
				} else {
					std::cout << help;
					return 1;
				}
				break;
			case "-b"_id:
				dumpCompileTime = true;
				break;
			case "-h"_id:
				std::cout << help;
				return 0;
			case "-v"_id:
				std::cout << "Moonscript version: " << MoonP::moonScriptVersion() << '\n';
				return 0;
			case "-o"_id:
				++i;
				if (i < narg) {
					resultFile = args[i];
				} else {
					std::cout << help;
					return 1;
				}
				break;
			default:
				files.push_back(args[i]);
				break;
		}
	}
	if (files.empty()) {
		std::cout << help;
		return 0;
	}
	if (!resultFile.empty() && files.size() > 1) {
		std::cout << "Error: -o can not be used with multiple input files.\n";
		std::cout << help;
	}
	for (const auto& file : files) {
		std::ifstream input(file, input.in);
		if (input) {
			std::string s(
				(std::istreambuf_iterator<char>(input)),
				std::istreambuf_iterator<char>());
			if (dumpCompileTime) {
				auto start = std::chrono::high_resolution_clock::now();
				auto result = MoonP::moonCompile(s, config);
				auto end = std::chrono::high_resolution_clock::now();
				if (!std::get<0>(result).empty()) {
					std::chrono::duration<double> diff = end - start;
					error_list el;
					MoonP::State st;
					start = std::chrono::high_resolution_clock::now();
					auto input = Converter{}.from_bytes(s);
					parserlib::parse<MoonP::File_t>(input, MoonP::File, el, &st);
					end = std::chrono::high_resolution_clock::now();
					std::chrono::duration<double> parseDiff = end - start;
					std::cout << file << " \n";
					std::cout << "Parse time:     " << std::setprecision(5) << parseDiff.count() * 1000 << " ms\n";
					std::cout << "Compile time:   " << std::setprecision(5) << (diff.count() - parseDiff.count()) * 1000 << " ms\n\n";
				} else {
					std::cout << "Fail to compile: " << file << ".\n";
					std::cout << std::get<1>(result) << '\n';
					return 1;
				}
				continue;
			}
			auto result = MoonP::moonCompile(s, config);
			if (!std::get<0>(result).empty()) {
				if (!writeToFile) {
					std::cout << std::get<0>(result) << '\n';
				} else {
					std::string targetFile;
					if (resultFile.empty()) {
						std::string ext;
						targetFile = file;
						size_t pos = file.rfind('.');
						if (pos != std::string::npos) {
							ext = file.substr(pos + 1);
							for (size_t i = 0; i < ext.length(); i++) {
								ext[i] = static_cast<char>(tolower(ext[i]));
							}
							targetFile = file.substr(0, pos) + ".lua";
						}
						if (!targetPath.empty()) {
							std::string name;
							pos = targetFile.find_last_of("/\\");
							if (pos == std::string::npos) {
								name = targetFile;
							} else {
								name = targetFile.substr(pos + 1);
							}
							if (targetPath.back() != '/' && targetPath.back() != '\\') {
								targetPath.append("/");
							}
							targetFile = targetPath + name;
						}
					} else {
						targetFile = resultFile;
					}
					std::ofstream output(targetFile, output.trunc | output.out);
					if (output) {
						const auto& codes = std::get<0>(result);
						output.write(codes.c_str(), codes.size());
						std::cout << "Built " << file << '\n';
					} else {
						std::cout << "Fail to write file: " << targetFile << ".\n";
						return 1;
					}
				}
			} else {
				std::cout << "Fail to compile: " << file << ".\n";
				std::cout << std::get<1>(result) << '\n';
				return 1;
			}
		} else {
			std::cout << "Fail to read file: " << file << ".\n";
			return 1;
		}
	}
	return 0;
}