aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/yuescript/yue_compiler.cpp31
-rw-r--r--src/yuescript/yue_parser.cpp28
-rw-r--r--src/yuescript/yue_parser.h1
3 files changed, 47 insertions, 13 deletions
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
78const std::string_view version = "0.27.4"sv; 80const std::string_view version = "0.27.5"sv;
79const std::string_view extension = "yue"sv; 81const std::string_view extension = "yue"sv;
80 82
81class CompileError : public std::logic_error { 83class 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);