aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Jin <dragon-fly@qq.com>2020-02-22 01:24:47 +0800
committerLi Jin <dragon-fly@qq.com>2020-02-22 01:24:47 +0800
commit39457b75c0923cf287c9145fd9c9a6ba4a86767b (patch)
tree975f5adcad63d17821b2f40020dc2e15fae1bdac
parent2e9e28ceb3444e0aaf0ff7c704800b9cdc25dc87 (diff)
downloadyuescript-39457b75c0923cf287c9145fd9c9a6ba4a86767b.tar.gz
yuescript-39457b75c0923cf287c9145fd9c9a6ba4a86767b.tar.bz2
yuescript-39457b75c0923cf287c9145fd9c9a6ba4a86767b.zip
change some interfaces.
-rw-r--r--src/MoonP/moon_compiler.cpp17
-rw-r--r--src/MoonP/moon_compiler.h12
-rw-r--r--src/MoonP/moon_parser.cpp6
-rw-r--r--src/MoonP/moon_parser.h6
-rw-r--r--src/moonp.cpp4
5 files changed, 31 insertions, 14 deletions
diff --git a/src/MoonP/moon_compiler.cpp b/src/MoonP/moon_compiler.cpp
index e6d010c..64152cf 100644
--- a/src/MoonP/moon_compiler.cpp
+++ b/src/MoonP/moon_compiler.cpp
@@ -35,9 +35,9 @@ const char* moonScriptVersion() {
35 return "0.5.0-r0.1.3"; 35 return "0.5.0-r0.1.3";
36} 36}
37 37
38class MoonCompiler { 38class MoonCompilerImpl {
39public: 39public:
40 std::tuple<std::string,std::string,GlobalVars> compile(const std::string& codes, const MoonConfig& config) { 40 std::tuple<std::string,std::string,GlobalVars> compile(std::string_view codes, const MoonConfig& config) {
41 _config = config; 41 _config = config;
42 _info = _parser.parse<File_t>(codes); 42 _info = _parser.parse<File_t>(codes);
43 GlobalVars globals; 43 GlobalVars globals;
@@ -4275,10 +4275,17 @@ private:
4275 } 4275 }
4276}; 4276};
4277 4277
4278const std::string MoonCompiler::Empty; 4278const std::string MoonCompilerImpl::Empty;
4279 4279
4280std::tuple<std::string,std::string,GlobalVars> moonCompile(const std::string& codes, const MoonConfig& config) { 4280MoonCompiler::MoonCompiler():
4281 return MoonCompiler{}.compile(codes, config); 4281_compiler(std::make_unique<MoonCompilerImpl>())
4282{ }
4283
4284MoonCompiler::~MoonCompiler()
4285{ }
4286
4287std::tuple<std::string,std::string,GlobalVars> MoonCompiler::compile(std::string_view codes, const MoonConfig& config) {
4288 return _compiler->compile(codes, config);
4282} 4289}
4283 4290
4284} // namespace MoonP 4291} // namespace MoonP
diff --git a/src/MoonP/moon_compiler.h b/src/MoonP/moon_compiler.h
index 0739a59..36fd77a 100644
--- a/src/MoonP/moon_compiler.h
+++ b/src/MoonP/moon_compiler.h
@@ -9,6 +9,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
9#pragma once 9#pragma once
10 10
11#include <string> 11#include <string>
12#include <string_view>
12#include <tuple> 13#include <tuple>
13#include <list> 14#include <list>
14#include <memory> 15#include <memory>
@@ -31,6 +32,15 @@ struct GlobalVar {
31 32
32using GlobalVars = std::unique_ptr<std::list<GlobalVar>>; 33using GlobalVars = std::unique_ptr<std::list<GlobalVar>>;
33 34
34std::tuple<std::string,std::string,GlobalVars> moonCompile(const std::string& codes, const MoonConfig& config = {}); 35class MoonCompilerImpl;
36
37class MoonCompiler {
38public:
39 MoonCompiler();
40 virtual ~MoonCompiler();
41 std::tuple<std::string,std::string,GlobalVars> compile(std::string_view codes, const MoonConfig& config = {});
42private:
43 std::unique_ptr<MoonCompilerImpl> _compiler;
44};
35 45
36} // namespace MoonP 46} // namespace MoonP
diff --git a/src/MoonP/moon_parser.cpp b/src/MoonP/moon_parser.cpp
index 39551d2..1502f3c 100644
--- a/src/MoonP/moon_parser.cpp
+++ b/src/MoonP/moon_parser.cpp
@@ -493,11 +493,11 @@ MoonParser::MoonParser() {
493 File = White >> -Shebang >> Block >> eof(); 493 File = White >> -Shebang >> Block >> eof();
494} 494}
495 495
496ParseInfo MoonParser::parse(const std::string& codes, rule& r) { 496ParseInfo MoonParser::parse(std::string_view codes, rule& r) {
497 ParseInfo res; 497 ParseInfo res;
498 try { 498 try {
499 res.codes = std::make_unique<input>(); 499 res.codes = std::make_unique<input>();
500 *(res.codes) = _converter.from_bytes(codes); 500 *(res.codes) = _converter.from_bytes(codes.begin(), codes.end());
501 } catch (const std::range_error&) { 501 } catch (const std::range_error&) {
502 res.error = "Invalid text encoding."sv; 502 res.error = "Invalid text encoding."sv;
503 return res; 503 return res;
@@ -537,7 +537,7 @@ std::string MoonParser::toString(input::iterator begin, input::iterator end) {
537} 537}
538 538
539input MoonParser::encode(std::string_view codes) { 539input MoonParser::encode(std::string_view codes) {
540 return _converter.from_bytes(std::string(codes)); 540 return _converter.from_bytes(codes.begin(), codes.end());
541} 541}
542 542
543std::string MoonParser::decode(const input& codes) { 543std::string MoonParser::decode(const input& codes) {
diff --git a/src/MoonP/moon_parser.h b/src/MoonP/moon_parser.h
index 7a1a8a9..215c6a4 100644
--- a/src/MoonP/moon_parser.h
+++ b/src/MoonP/moon_parser.h
@@ -45,7 +45,7 @@ public:
45 MoonParser(); 45 MoonParser();
46 46
47 template<class AST> 47 template<class AST>
48 ParseInfo parse(const std::string& codes) { 48 ParseInfo parse(std::string_view codes) {
49 error_list errors; 49 error_list errors;
50 auto res = parse(codes, getRule<AST>()); 50 auto res = parse(codes, getRule<AST>());
51 if (res.node.template is<AST>()) { 51 if (res.node.template is<AST>()) {
@@ -55,7 +55,7 @@ public:
55 } 55 }
56 56
57 template <class AST> 57 template <class AST>
58 bool match(const std::string& codes) { 58 bool match(std::string_view codes) {
59 auto rEnd = rule(getRule<AST>() >> eof()); 59 auto rEnd = rule(getRule<AST>() >> eof());
60 return parse(codes, rEnd).node; 60 return parse(codes, rEnd).node;
61 } 61 }
@@ -67,7 +67,7 @@ public:
67 std::string decode(const input& input); 67 std::string decode(const input& input);
68 68
69protected: 69protected:
70 ParseInfo parse(const std::string& codes, rule& r); 70 ParseInfo parse(std::string_view codes, rule& r);
71 71
72 struct State { 72 struct State {
73 State() { 73 State() {
diff --git a/src/moonp.cpp b/src/moonp.cpp
index be5f536..3f36d9d 100644
--- a/src/moonp.cpp
+++ b/src/moonp.cpp
@@ -91,7 +91,7 @@ int main(int narg, const char** args) {
91 std::istreambuf_iterator<char>()); 91 std::istreambuf_iterator<char>());
92 if (dumpCompileTime) { 92 if (dumpCompileTime) {
93 auto start = std::chrono::high_resolution_clock::now(); 93 auto start = std::chrono::high_resolution_clock::now();
94 auto result = MoonP::moonCompile(s, config); 94 auto result = MoonP::MoonCompiler{}.compile(s, config);
95 auto end = std::chrono::high_resolution_clock::now(); 95 auto end = std::chrono::high_resolution_clock::now();
96 if (!std::get<0>(result).empty()) { 96 if (!std::get<0>(result).empty()) {
97 std::chrono::duration<double> diff = end - start; 97 std::chrono::duration<double> diff = end - start;
@@ -109,7 +109,7 @@ int main(int narg, const char** args) {
109 return 1; 109 return 1;
110 } 110 }
111 } 111 }
112 auto result = MoonP::moonCompile(s, config); 112 auto result = MoonP::MoonCompiler{}.compile(s, config);
113 if (!std::get<0>(result).empty()) { 113 if (!std::get<0>(result).empty()) {
114 if (!writeToFile) { 114 if (!writeToFile) {
115 std::cout << std::get<0>(result) << '\n'; 115 std::cout << std::get<0>(result) << '\n';