diff options
| author | Li Jin <dragon-fly@qq.com> | 2025-04-17 11:05:06 +0800 |
|---|---|---|
| committer | Li Jin <dragon-fly@qq.com> | 2025-04-17 11:05:06 +0800 |
| commit | 0446b12d7be2ba4e8534987864fedfc586510291 (patch) | |
| tree | 3ed0b3efa0b6e4247cf7b4832704f3667fabd2ca | |
| parent | 1c47083a8e2fe8a08a9dcf7d4520b4de98134c5d (diff) | |
| download | yuescript-0446b12d7be2ba4e8534987864fedfc586510291.tar.gz yuescript-0446b12d7be2ba4e8534987864fedfc586510291.tar.bz2 yuescript-0446b12d7be2ba4e8534987864fedfc586510291.zip | |
Fixed issue #209.
| -rw-r--r-- | makefile | 2 | ||||
| -rw-r--r-- | spec/inputs/literals.yue | 9 | ||||
| -rw-r--r-- | spec/outputs/5.1/literals.lua | 44 | ||||
| -rw-r--r-- | spec/outputs/literals.lua | 8 | ||||
| -rw-r--r-- | src/yuescript/yue_compiler.cpp | 31 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.cpp | 28 | ||||
| -rw-r--r-- | src/yuescript/yue_parser.h | 1 |
7 files changed, 110 insertions, 13 deletions
| @@ -391,6 +391,7 @@ test: debug | |||
| 391 | @./$(BIN_NAME) $(TEST_INPUT)/try_catch.yue -o $(TEST_OUTPUT)/5.1/try_catch.lua --target=5.1 | 391 | @./$(BIN_NAME) $(TEST_INPUT)/try_catch.yue -o $(TEST_OUTPUT)/5.1/try_catch.lua --target=5.1 |
| 392 | @./$(BIN_NAME) $(TEST_INPUT)/attrib.yue -o $(TEST_OUTPUT)/5.1/attrib.lua --target=5.1 | 392 | @./$(BIN_NAME) $(TEST_INPUT)/attrib.yue -o $(TEST_OUTPUT)/5.1/attrib.lua --target=5.1 |
| 393 | @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(TEST_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 | 393 | @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(TEST_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 |
| 394 | @./$(BIN_NAME) $(TEST_INPUT)/literals.yue -o $(TEST_OUTPUT)/5.1/literals.lua --target=5.1 | ||
| 394 | @./$(BIN_NAME) -e spec/inputs/compile_doc.yue $(TEST_OUTPUT) | 395 | @./$(BIN_NAME) -e spec/inputs/compile_doc.yue $(TEST_OUTPUT) |
| 395 | @echo -en "Compile time: " | 396 | @echo -en "Compile time: " |
| 396 | @$(END_TIME) | 397 | @$(END_TIME) |
| @@ -409,6 +410,7 @@ gen: release | |||
| 409 | @./$(BIN_NAME) $(TEST_INPUT)/loops.yue -o $(GEN_OUTPUT)/5.1/loops.lua --target=5.1 | 410 | @./$(BIN_NAME) $(TEST_INPUT)/loops.yue -o $(GEN_OUTPUT)/5.1/loops.lua --target=5.1 |
| 410 | @./$(BIN_NAME) $(TEST_INPUT)/try_catch.yue -o $(GEN_OUTPUT)/5.1/try_catch.lua --target=5.1 | 411 | @./$(BIN_NAME) $(TEST_INPUT)/try_catch.yue -o $(GEN_OUTPUT)/5.1/try_catch.lua --target=5.1 |
| 411 | @./$(BIN_NAME) $(TEST_INPUT)/attrib.yue -o $(GEN_OUTPUT)/5.1/attrib.lua --target=5.1 | 412 | @./$(BIN_NAME) $(TEST_INPUT)/attrib.yue -o $(GEN_OUTPUT)/5.1/attrib.lua --target=5.1 |
| 413 | @./$(BIN_NAME) $(TEST_INPUT)/literals.yue -o $(GEN_OUTPUT)/5.1/literals.lua --target=5.1 | ||
| 412 | @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(GEN_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 | 414 | @./$(BIN_NAME) $(TEST_INPUT)/test/loops_spec.yue -o $(GEN_OUTPUT)/5.1/test/loops_spec.lua --target=5.1 |
| 413 | @./$(BIN_NAME) -e spec/inputs/compile_doc.yue $(GEN_OUTPUT) | 415 | @./$(BIN_NAME) -e spec/inputs/compile_doc.yue $(GEN_OUTPUT) |
| 414 | @echo -en "Compile time: " | 416 | @echo -en "Compile time: " |
diff --git a/spec/inputs/literals.yue b/spec/inputs/literals.yue index 6b666f0..32cf7e7 100644 --- a/spec/inputs/literals.yue +++ b/spec/inputs/literals.yue | |||
| @@ -10,10 +10,19 @@ _ = { | |||
| 10 | 0xfF2323 | 10 | 0xfF2323 |
| 11 | 0xabcdef | 11 | 0xabcdef |
| 12 | 0xABCDEF | 12 | 0xABCDEF |
| 13 | 0XFB_C4_00 | ||
| 13 | 0x123p-123 | 14 | 0x123p-123 |
| 14 | 0xABCP+321 | 15 | 0xABCP+321 |
| 15 | 0x.1p-111 | 16 | 0x.1p-111 |
| 16 | 0xA_B_CP-3_2_1 | 17 | 0xA_B_CP-3_2_1 |
| 18 | 0x0.1E | ||
| 19 | 0xA23p-4 | ||
| 20 | 0X1.921FB54442D18P+1 | ||
| 21 | |||
| 22 | 0b01 | ||
| 23 | 0b00_00_10_00 | ||
| 24 | 0B1111 | ||
| 25 | 0B00_11_00_10_01 | ||
| 17 | 26 | ||
| 18 | .2323 | 27 | .2323 |
| 19 | .2323e-1 | 28 | .2323e-1 |
diff --git a/spec/outputs/5.1/literals.lua b/spec/outputs/5.1/literals.lua new file mode 100644 index 0000000..36c705a --- /dev/null +++ b/spec/outputs/5.1/literals.lua | |||
| @@ -0,0 +1,44 @@ | |||
| 1 | local _ = { | ||
| 2 | 121, | ||
| 3 | 121.2323, | ||
| 4 | 121.2323e-1, | ||
| 5 | 121.2323e13434, | ||
| 6 | 2323E34, | ||
| 7 | 0x12323, | ||
| 8 | 0xfF2323, | ||
| 9 | 0xabcdef, | ||
| 10 | 0xABCDEF, | ||
| 11 | 0XFBC400, | ||
| 12 | 2.7365508487142853e-35, | ||
| 13 | 0xABCP321, | ||
| 14 | 2.4074124304840448e-35, | ||
| 15 | 6.4326233113470805e-94, | ||
| 16 | 0.1171875, | ||
| 17 | 162.1875, | ||
| 18 | 3.1415926535897931, | ||
| 19 | 1, | ||
| 20 | 8, | ||
| 21 | 15, | ||
| 22 | 201, | ||
| 23 | .2323, | ||
| 24 | .2323e-1, | ||
| 25 | .2323e13434, | ||
| 26 | 1LL, | ||
| 27 | 1ULL, | ||
| 28 | 9332LL, | ||
| 29 | 9332, | ||
| 30 | 0x2aLL, | ||
| 31 | 0x2aULL, | ||
| 32 | 1000000.0000001, | ||
| 33 | 1234e5678, | ||
| 34 | 1234E-5678, | ||
| 35 | 0xDEADBEEF, | ||
| 36 | [[ hello world ]], | ||
| 37 | [=[ hello world ]=], | ||
| 38 | [====[ hello world ]====], | ||
| 39 | "another world", | ||
| 40 | 'what world', | ||
| 41 | "\n hello world\n ", | ||
| 42 | 'yeah\n what is going on\n here is something cool' | ||
| 43 | } | ||
| 44 | return nil | ||
diff --git a/spec/outputs/literals.lua b/spec/outputs/literals.lua index a578d58..6de5411 100644 --- a/spec/outputs/literals.lua +++ b/spec/outputs/literals.lua | |||
| @@ -8,10 +8,18 @@ local _ = { | |||
| 8 | 0xfF2323, | 8 | 0xfF2323, |
| 9 | 0xabcdef, | 9 | 0xabcdef, |
| 10 | 0xABCDEF, | 10 | 0xABCDEF, |
| 11 | 0XFBC400, | ||
| 11 | 0x123p-123, | 12 | 0x123p-123, |
| 12 | 0xABCP+321, | 13 | 0xABCP+321, |
| 13 | 0x.1p-111, | 14 | 0x.1p-111, |
| 14 | 0xABCP-321, | 15 | 0xABCP-321, |
| 16 | 0x0.1E, | ||
| 17 | 0xA23p-4, | ||
| 18 | 0X1.921FB54442D18P+1, | ||
| 19 | 1, | ||
| 20 | 8, | ||
| 21 | 15, | ||
| 22 | 201, | ||
| 15 | .2323, | 23 | .2323, |
| 16 | .2323e-1, | 24 | .2323e-1, |
| 17 | .2323e13434, | 25 | .2323e13434, |
diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index 68ce9b5..d5db8a1 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp | |||
| @@ -16,6 +16,8 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI | |||
| 16 | #include <unordered_set> | 16 | #include <unordered_set> |
| 17 | #include <variant> | 17 | #include <variant> |
| 18 | #include <vector> | 18 | #include <vector> |
| 19 | #include <iomanip> | ||
| 20 | #include <ostream> | ||
| 19 | 21 | ||
| 20 | #include "yuescript/yue_compiler.h" | 22 | #include "yuescript/yue_compiler.h" |
| 21 | #include "yuescript/yue_parser.h" | 23 | #include "yuescript/yue_parser.h" |
| @@ -75,7 +77,7 @@ static std::unordered_set<std::string> Metamethods = { | |||
| 75 | "close"s // Lua 5.4 | 77 | "close"s // Lua 5.4 |
| 76 | }; | 78 | }; |
| 77 | 79 | ||
| 78 | const std::string_view version = "0.27.4"sv; | 80 | const std::string_view version = "0.27.5"sv; |
| 79 | const std::string_view extension = "yue"sv; | 81 | const std::string_view extension = "yue"sv; |
| 80 | 82 | ||
| 81 | class CompileError : public std::logic_error { | 83 | class CompileError : public std::logic_error { |
| @@ -7144,6 +7146,33 @@ private: | |||
| 7144 | void transformNum(Num_t* num, str_list& out) { | 7146 | void transformNum(Num_t* num, str_list& out) { |
| 7145 | std::string numStr = _parser.toString(num); | 7147 | std::string numStr = _parser.toString(num); |
| 7146 | numStr.erase(std::remove(numStr.begin(), numStr.end(), '_'), numStr.end()); | 7148 | numStr.erase(std::remove(numStr.begin(), numStr.end(), '_'), numStr.end()); |
| 7149 | if (numStr.size() > 2 && numStr[0] == '0') { | ||
| 7150 | if (numStr[1] == 'b' || numStr[1] == 'B') { | ||
| 7151 | std::string binaryPart = numStr.substr(2); | ||
| 7152 | try { | ||
| 7153 | unsigned long long value = std::stoull(binaryPart, nullptr, 2); | ||
| 7154 | numStr = std::to_string(value); | ||
| 7155 | } catch (const std::exception& e) { | ||
| 7156 | throw CompileError("invalid binary literal"sv, num); | ||
| 7157 | } | ||
| 7158 | } else if (getLuaTarget(num) < 502) { | ||
| 7159 | if (numStr[1] == 'x' || numStr[1] == 'X') { | ||
| 7160 | if (numStr.find_first_of(".-"sv) != std::string::npos) { | ||
| 7161 | std::stringstream ss(numStr); | ||
| 7162 | double v; | ||
| 7163 | ss >> std::hexfloat >> v; | ||
| 7164 | if (ss.fail() || !std::isfinite(v)) { | ||
| 7165 | throw CompileError("invalid hex‑float literal"sv, num); | ||
| 7166 | } | ||
| 7167 | std::ostringstream outSs; | ||
| 7168 | outSs << std::setprecision(17) << v; | ||
| 7169 | numStr = outSs.str(); | ||
| 7170 | } else { | ||
| 7171 | numStr.erase(std::remove(numStr.begin(), numStr.end(), '+'), numStr.end()); | ||
| 7172 | } | ||
| 7173 | } | ||
| 7174 | } | ||
| 7175 | } | ||
| 7147 | out.push_back(numStr); | 7176 | out.push_back(numStr); |
| 7148 | } | 7177 | } |
| 7149 | 7178 | ||
diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index ceb1f7c..77c5901 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp | |||
| @@ -67,24 +67,28 @@ YueParser::YueParser() { | |||
| 67 | num_char = range('0', '9') >> *(range('0', '9') | '_' >> and_(range('0', '9'))); | 67 | num_char = range('0', '9') >> *(range('0', '9') | '_' >> and_(range('0', '9'))); |
| 68 | num_char_hex = range('0', '9') | range('a', 'f') | range('A', 'F'); | 68 | num_char_hex = range('0', '9') | range('a', 'f') | range('A', 'F'); |
| 69 | num_lit = num_char_hex >> *(num_char_hex | '_' >> and_(num_char_hex)); | 69 | num_lit = num_char_hex >> *(num_char_hex | '_' >> and_(num_char_hex)); |
| 70 | num_bin_lit = set("01") >> *(set("01") | '_' >> and_(set("01"))); | ||
| 70 | Num = | 71 | Num = |
| 71 | "0x" >> ( | 72 | '0' >> ( |
| 72 | +num_lit >> ( | 73 | set("xX") >> ( |
| 73 | '.' >> +num_lit >> -num_expo_hex | | 74 | num_lit >> ( |
| 74 | num_expo_hex | | 75 | '.' >> num_lit >> -num_expo_hex | |
| 75 | lj_num | | 76 | num_expo_hex | |
| 76 | true_() | 77 | lj_num | |
| 77 | ) | ( | 78 | true_() |
| 78 | '.' >> +num_lit >> -num_expo_hex | 79 | ) | ( |
| 79 | ) | 80 | '.' >> num_lit >> -num_expo_hex |
| 81 | ) | ||
| 82 | ) | | ||
| 83 | set("bB") >> num_bin_lit | ||
| 80 | ) | | 84 | ) | |
| 81 | +num_char >> ( | 85 | num_char >> ( |
| 82 | '.' >> +num_char >> -num_expo | | 86 | '.' >> num_char >> -num_expo | |
| 83 | num_expo | | 87 | num_expo | |
| 84 | lj_num | | 88 | lj_num | |
| 85 | true_() | 89 | true_() |
| 86 | ) | | 90 | ) | |
| 87 | '.' >> +num_char >> -num_expo; | 91 | '.' >> num_char >> -num_expo; |
| 88 | 92 | ||
| 89 | cut = false_(); | 93 | cut = false_(); |
| 90 | Seperator = true_(); | 94 | Seperator = true_(); |
diff --git a/src/yuescript/yue_parser.h b/src/yuescript/yue_parser.h index 02292e1..7281ec3 100644 --- a/src/yuescript/yue_parser.h +++ b/src/yuescript/yue_parser.h | |||
| @@ -164,6 +164,7 @@ private: | |||
| 164 | NONE_AST_RULE(num_char); | 164 | NONE_AST_RULE(num_char); |
| 165 | NONE_AST_RULE(num_char_hex); | 165 | NONE_AST_RULE(num_char_hex); |
| 166 | NONE_AST_RULE(num_lit); | 166 | NONE_AST_RULE(num_lit); |
| 167 | NONE_AST_RULE(num_bin_lit); | ||
| 167 | NONE_AST_RULE(num_expo); | 168 | NONE_AST_RULE(num_expo); |
| 168 | NONE_AST_RULE(num_expo_hex); | 169 | NONE_AST_RULE(num_expo_hex); |
| 169 | NONE_AST_RULE(lj_num); | 170 | NONE_AST_RULE(lj_num); |
