libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
format_frontend_binding_float.hpp
1#pragma once
2
7namespace ArgumentResolution
8{
9
21[[nodiscard]] consteval ResolvedField ResolveFloatField(const ParsedField& parsed,
22 ArgumentKind kind)
23{
24 if (parsed.has_precision && parsed.precision > Config::max_float_precision)
25 {
26 return ResolvedField{.error = Error::FloatPrecisionLimitExceeded};
27 }
28
29 char presentation =
30 parsed.presentation == 0 ? DefaultFloatPresentation() : parsed.presentation;
31 if (presentation == 0)
32 {
33 return ResolvedField{.error = Error::ArgumentTypeMismatch};
34 }
35 bool upper_case = presentation == 'F' || presentation == 'E' || presentation == 'G';
36
37 FormatType type = FormatType::End;
38 FormatPackKind pack = FormatPackKind::F32;
39
40 auto pick_type = [&](FormatType f32_type, FormatType f64_type,
41 FormatType ld_type) consteval -> bool
42 {
43 switch (kind)
44 {
45 case ArgumentKind::Float32:
46 type = f32_type;
47 pack = FormatPackKind::F32;
48 return true;
49 case ArgumentKind::Float64:
50 type = Config::enable_float_double ? f64_type : f32_type;
51 pack = Config::enable_float_double ? FormatPackKind::F64 : FormatPackKind::F32;
52 return true;
53 case ArgumentKind::LongDouble:
54 if (!Config::enable_float_long_double)
55 {
56 return false;
57 }
58 type = ld_type;
59 pack = FormatPackKind::LongDouble;
60 return true;
61 default:
62 return false;
63 }
64 };
65
66 switch (presentation)
67 {
68 case 'f':
69 case 'F':
70 if (!Config::enable_float_fixed ||
71 !pick_type(FormatType::FloatFixed, FormatType::DoubleFixed,
72 FormatType::LongDoubleFixed))
73 {
74 return ResolvedField{.error = Error::ArgumentTypeMismatch};
75 }
76 break;
77 case 'e':
78 case 'E':
79 if (!Config::enable_float_scientific ||
80 !pick_type(FormatType::FloatScientific, FormatType::DoubleScientific,
81 FormatType::LongDoubleScientific))
82 {
83 return ResolvedField{.error = Error::ArgumentTypeMismatch};
84 }
85 break;
86 case 'g':
87 case 'G':
88 if (!Config::enable_float_general ||
89 !pick_type(FormatType::FloatGeneral, FormatType::DoubleGeneral,
90 FormatType::LongDoubleGeneral))
91 {
92 return ResolvedField{.error = Error::ArgumentTypeMismatch};
93 }
94 break;
95 default:
96 return ResolvedField{.error = Error::ArgumentTypeMismatch};
97 }
98
99 return ResolvedField{.field = MakeField(parsed, type, pack, upper_case)};
100}
101
110template <typename... Args>
111[[nodiscard]] consteval ResolvedField ResolveField(const ParsedField& parsed)
112{
113 constexpr auto argument_summaries = ClassifyArguments<Args...>();
114 if (parsed.arg_index >= argument_summaries.size())
115 {
116 return ResolvedField{.error = Error::MissingArgument};
117 }
118
119 auto argument = argument_summaries[parsed.arg_index];
120 switch (argument.kind)
121 {
122 case ArgumentKind::Bool:
123 return ResolveBoolField(parsed);
124 case ArgumentKind::Character:
125 return ResolveCharacterField(parsed);
126 case ArgumentKind::Signed:
127 return ResolveIntegerField(parsed, true, argument.uses_64bit_storage);
128 case ArgumentKind::Unsigned:
129 return ResolveIntegerField(parsed, false, argument.uses_64bit_storage);
130 case ArgumentKind::String:
131 return ResolveStringField(parsed);
132 case ArgumentKind::Pointer:
133 return ResolvePointerField(parsed);
134 case ArgumentKind::Float32:
135 case ArgumentKind::Float64:
136 case ArgumentKind::LongDouble:
137 return ResolveFloatField(parsed, argument.kind);
138 case ArgumentKind::Unsupported:
139 default:
140 return ResolvedField{.error = Error::UnsupportedArgumentType};
141 }
142}
143} // namespace ArgumentResolution
brace 前端共享的参数分类与字段形状构造辅助函数 / Shared brace-frontend argument classification and field-shape helpers
consteval ResolvedField ResolveIntegerField(const ParsedField &parsed, bool signed_decimal, bool uses_64bit_storage)
针对整数类参数解析一个已解析的 brace 字段 / Resolve one parsed brace field for an integer-like argument
constexpr FormatField MakeField(const ParsedField &parsed, FormatType type, FormatPackKind pack, bool upper_case=false)
根据 brace 字段属性构造一条共享 FormatField 记录 / Build one shared FormatField record from parsed brace-field prop...
consteval ResolvedField ResolveBoolField(const ParsedField &parsed)
针对 bool 参数解析一个已解析的 brace 字段 / Resolve one parsed brace field for a bool argument
consteval ResolvedField ResolvePointerField(const ParsedField &parsed)
针对指针类参数解析一个已解析的 brace 字段 / Resolve one parsed brace field for a pointer-like argument
consteval auto ClassifyArguments()
为 brace 前端归类整组 C++ 参数类型 / Classify one full C++ argument list for the brace frontend
consteval ResolvedField ResolveField(const ParsedField &parsed)
先判断一个已解析 brace 字段指向哪类参数,再选择匹配的字段构造逻辑 / Check which argument family a parsed brace field points to,...
consteval ResolvedField ResolveCharacterField(const ParsedField &parsed)
针对字符参数解析一个已解析的 brace 字段 / Resolve one parsed brace field for a character argument
consteval ResolvedField ResolveFloatField(const ParsedField &parsed, ArgumentKind kind)
针对 float、double 或 long double 参数解析一个已解析的 brace 字段 / Resolve one parsed brace field for a float,...
consteval ResolvedField ResolveStringField(const ParsedField &parsed)
针对字符串类参数解析一个已解析的 brace 字段 / Resolve one parsed brace field for a string-like argument
constexpr char DefaultFloatPresentation()
在当前功能开关下选择前端默认浮点展示字符 / Choose the frontend default float presentation under current feature gates