plcopen
0.22.0
载入中...
搜索中...
未找到
standard_functions.h
浏览该文件的文档.
1
#pragma once
2
3
#include <algorithm>
4
#include <array>
5
#include <cstdint>
6
#include <string>
7
#include <string_view>
8
#include <limits>
9
10
namespace
plcopen::core::st
11
{
12
13
enum class
StandardFunction
: std::uint8_t
14
{
15
abs
,
sqrt
,
ln
,
log
,
exp
,
sin
,
cos
,
tan
,
asin
,
acos
,
atan
,
16
add
,
sub
,
mul
,
div
,
mod
,
expt
,
min
,
max
,
limit
,
sel
,
mux
,
17
gt
,
ge
,
eq
,
le
,
lt
,
ne
,
shl
,
shr
,
rol
,
ror
,
len
,
left
,
right
,
18
mid
,
concat
,
insert
,
delete_
,
replace
,
find
,
add_time
,
19
add_tod_time
,
add_dt_time
,
sub_time
,
sub_date_date
,
sub_tod_time
,
20
sub_dt_dt
,
multime
,
divtime
,
concat_date_tod
,
count
21
};
22
23
struct
StandardFunctionManifestEntry
24
{
25
std::string_view
name
;
26
bool
registered
;
27
};
28
29
inline
constexpr
std::array<StandardFunctionManifestEntry, 51>
30
kStandardFunctionManifest
= {{
31
{
"ABS"
,
true
}, {
"SQRT"
,
true
}, {
"LN"
,
true
}, {
"LOG"
,
true
},
32
{
"EXP"
,
true
}, {
"SIN"
,
true
}, {
"COS"
,
true
}, {
"TAN"
,
true
},
33
{
"ASIN"
,
true
}, {
"ACOS"
,
true
}, {
"ATAN"
,
true
}, {
"ADD"
,
true
},
34
{
"SUB"
,
true
}, {
"MUL"
,
true
}, {
"DIV"
,
true
}, {
"MOD"
,
true
},
35
{
"EXPT"
,
true
}, {
"MIN"
,
true
}, {
"MAX"
,
true
}, {
"LIMIT"
,
true
},
36
{
"SEL"
,
true
}, {
"MUX"
,
true
}, {
"GT"
,
true
}, {
"GE"
,
true
},
37
{
"EQ"
,
true
}, {
"LE"
,
true
}, {
"LT"
,
true
}, {
"NE"
,
true
},
38
{
"SHL"
,
true
}, {
"SHR"
,
true
}, {
"ROL"
,
true
}, {
"ROR"
,
true
},
39
{
"LEN"
,
true
}, {
"LEFT"
,
true
}, {
"RIGHT"
,
true
}, {
"MID"
,
true
},
40
{
"CONCAT"
,
true
}, {
"INSERT"
,
true
}, {
"DELETE"
,
true
},
41
{
"REPLACE"
,
true
}, {
"FIND"
,
true
}, {
"ADD_TIME"
,
true
},
42
{
"ADD_TOD_TIME"
,
true
}, {
"ADD_DT_TIME"
,
true
}, {
"SUB_TIME"
,
true
},
43
{
"SUB_DATE_DATE"
,
true
}, {
"SUB_TOD_TIME"
,
true
}, {
"SUB_DT_DT"
,
true
},
44
{
"MULTIME"
,
true
}, {
"DIVTIME"
,
true
}, {
"CONCAT_DATE_TOD"
,
true
},
45
}};
46
47
inline
constexpr
const
auto
&
standard_function_manifest
()
48
{
49
return
kStandardFunctionManifest
;
50
}
51
52
inline
std::string
standard_function_manifest_dump
()
53
{
54
std::string result;
55
for
(
const
auto
&entry :
kStandardFunctionManifest
) {
56
if
(!result.empty()) result.push_back(
'\n'
);
57
result.append(entry.name);
58
result.append(entry.registered ?
":registered"
:
":missing"
);
59
}
60
return
result;
61
}
62
63
inline
bool
resolve_standard_function
(std::string_view lower,
64
StandardFunction
&function)
65
{
66
for
(std::size_t index = 0; index <
kStandardFunctionManifest
.size(); ++index) {
67
const
std::string_view upper =
kStandardFunctionManifest
[index].name;
68
if
(lower.size() != upper.size())
continue
;
69
bool
equal
=
true
;
70
for
(std::size_t c = 0; c < lower.size(); ++c) {
71
const
char
expected = upper[c] >=
'A'
&& upper[c] <=
'Z'
72
?
static_cast<
char
>
(upper[c] -
'A'
+
'a'
) : upper[c];
73
if
(lower[c] != expected) {
equal
=
false
;
break
; }
74
}
75
if
(
equal
) {
76
function =
static_cast<
StandardFunction
>
(index);
77
return
true
;
78
}
79
}
80
return
false
;
81
}
82
83
inline
constexpr
std::uint32_t
saturating_cost_add
(std::uint32_t
left
,
84
std::uint32_t
right
)
85
{
86
return
right
> std::numeric_limits<std::uint32_t>::max() -
left
87
? std::numeric_limits<std::uint32_t>::max() :
left
+
right
;
88
}
89
90
inline
constexpr
std::uint32_t
saturating_cost_mul
(std::uint32_t
left
,
91
std::uint32_t
right
)
92
{
93
return
left
!= 0U &&
94
right
> std::numeric_limits<std::uint32_t>::max() /
left
95
? std::numeric_limits<std::uint32_t>::max() :
left
*
right
;
96
}
97
98
inline
constexpr
std::uint32_t
standard_string_cost
(
99
StandardFunction
function,
const
std::uint32_t *capacities,
100
std::uint8_t
count
, std::uint32_t destination_capacity)
101
{
102
if
(
count
== 0U)
return
1U;
103
if
(function ==
StandardFunction::len
) {
104
return
std::max(1U,
saturating_cost_mul
(capacities[0], 2U));
105
}
106
if
(function ==
StandardFunction::find
&&
count
>= 2U) {
107
const
std::uint32_t search =
saturating_cost_mul
(capacities[0],
108
capacities[1]);
109
return
std::max(1U,
saturating_cost_add
(
110
search,
saturating_cost_add
(capacities[0], capacities[1])));
111
}
112
std::uint32_t total = destination_capacity;
113
for
(std::uint8_t index = 0; index <
count
; ++index) {
114
total =
saturating_cost_add
(total, capacities[index]);
115
}
116
return
std::max(1U,
saturating_cost_mul
(total, 4U));
117
}
118
119
}
// namespace plcopen::core::st
plcopen::core::st
定义
ast.h:15
plcopen::core::st::saturating_cost_mul
constexpr std::uint32_t saturating_cost_mul(std::uint32_t left, std::uint32_t right)
定义
standard_functions.h:90
plcopen::core::st::kStandardFunctionManifest
constexpr std::array< StandardFunctionManifestEntry, 51 > kStandardFunctionManifest
定义
standard_functions.h:30
plcopen::core::st::StandardFunction
StandardFunction
定义
standard_functions.h:14
plcopen::core::st::StandardFunction::delete_
@ delete_
定义
standard_functions.h:18
plcopen::core::st::StandardFunction::ge
@ ge
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::sub_dt_dt
@ sub_dt_dt
定义
standard_functions.h:20
plcopen::core::st::StandardFunction::gt
@ gt
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::mid
@ mid
定义
standard_functions.h:18
plcopen::core::st::StandardFunction::sub_date_date
@ sub_date_date
定义
standard_functions.h:19
plcopen::core::st::StandardFunction::max
@ max
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::mul
@ mul
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::divtime
@ divtime
定义
standard_functions.h:20
plcopen::core::st::StandardFunction::div
@ div
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::expt
@ expt
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::concat
@ concat
定义
standard_functions.h:18
plcopen::core::st::StandardFunction::cos
@ cos
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::tan
@ tan
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::asin
@ asin
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::add_dt_time
@ add_dt_time
定义
standard_functions.h:19
plcopen::core::st::StandardFunction::atan
@ atan
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::right
@ right
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::sin
@ sin
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::left
@ left
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::shr
@ shr
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::add_tod_time
@ add_tod_time
定义
standard_functions.h:19
plcopen::core::st::StandardFunction::rol
@ rol
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::sub
@ sub
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::sel
@ sel
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::ror
@ ror
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::replace
@ replace
定义
standard_functions.h:18
plcopen::core::st::StandardFunction::limit
@ limit
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::mod
@ mod
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::sub_tod_time
@ sub_tod_time
定义
standard_functions.h:19
plcopen::core::st::StandardFunction::exp
@ exp
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::multime
@ multime
定义
standard_functions.h:20
plcopen::core::st::StandardFunction::shl
@ shl
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::acos
@ acos
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::ne
@ ne
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::min
@ min
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::le
@ le
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::lt
@ lt
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::log
@ log
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::sqrt
@ sqrt
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::eq
@ eq
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::insert
@ insert
定义
standard_functions.h:18
plcopen::core::st::StandardFunction::count
@ count
定义
standard_functions.h:20
plcopen::core::st::StandardFunction::find
@ find
定义
standard_functions.h:18
plcopen::core::st::StandardFunction::concat_date_tod
@ concat_date_tod
定义
standard_functions.h:20
plcopen::core::st::StandardFunction::mux
@ mux
定义
standard_functions.h:16
plcopen::core::st::StandardFunction::len
@ len
定义
standard_functions.h:17
plcopen::core::st::StandardFunction::ln
@ ln
定义
standard_functions.h:15
plcopen::core::st::StandardFunction::abs
@ abs
定义
standard_functions.h:15
plcopen::core::st::saturating_cost_add
constexpr std::uint32_t saturating_cost_add(std::uint32_t left, std::uint32_t right)
定义
standard_functions.h:83
plcopen::core::st::standard_function_manifest
constexpr const auto & standard_function_manifest()
定义
standard_functions.h:47
plcopen::core::st::Op::add_time
@ add_time
定义
bytecode.h:65
plcopen::core::st::Op::sub_time
@ sub_time
定义
bytecode.h:66
plcopen::core::st::resolve_standard_function
bool resolve_standard_function(std::string_view lower, StandardFunction &function)
定义
standard_functions.h:63
plcopen::core::st::standard_function_manifest_dump
std::string standard_function_manifest_dump()
定义
standard_functions.h:52
plcopen::core::st::standard_string_cost
constexpr std::uint32_t standard_string_cost(StandardFunction function, const std::uint32_t *capacities, std::uint8_t count, std::uint32_t destination_capacity)
定义
standard_functions.h:98
plcopen::core::st::BinaryOp::add
@ add
定义
ast.h:63
plcopen::core::st::TokenKind::equal
@ equal
定义
token.h:47
plcopen::core::st::StandardFunctionManifestEntry
定义
standard_functions.h:24
plcopen::core::st::StandardFunctionManifestEntry::registered
bool registered
定义
standard_functions.h:26
plcopen::core::st::StandardFunctionManifestEntry::name
std::string_view name
定义
standard_functions.h:25
core
st
standard_functions.h
制作者
1.15.0