FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
interpret_arithmetic/interpret_arithmetic/export.cpp at develop · broccolimicro/interpret_arithmetic · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
broccolimicro
/
interpret_arithmetic
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
interpret_arithmetic
/
interpret_arithmetic
/
export.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
241 lines (207 loc) · 7.57 KB
Breadcrumbs
interpret_arithmetic
/
interpret_arithmetic
/
export.cpp
Copy path
File metadata and controls
241 lines (207 loc) · 7.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#
include
"
export.h
"
#
include
<
arithmetic/value.h
>
namespace
arithmetic
{
string
export_value
(
const
Value &v) {
if
(v.
isUndef
()) {
return
"
undef
"
;
}
else
if
(v.
isUnstable
()) {
return
"
unstable
"
;
}
else
if
(v.
isUnknown
()) {
return
"
unknown
"
;
}
else
if
(v.
type
== Value::
WIRE
) {
if
(v.
isNeutral
()) {
return
"
gnd
"
;
}
else
if
(v.
isValid
()) {
return
"
vdd
"
;
}
}
else
if
(v.
type
== Value::
BOOL
) {
if
(v.
bval
) {
return
"
true
"
;
}
else
{
return
"
false
"
;
}
}
else
if
(v.
type
== Value::
INT
) {
return
::
to_string
(v.
ival
);
}
else
if
(v.
type
== Value::
REAL
) {
return
::
to_string
(v.
rval
);
}
else
if
(v.
type
== Value::
STRING
) {
return
"
\"
"
+ v.
sval
+
"
\"
"
;
}
else
if
(v.
type
== Value::
LABEL
) {
return
v.
sval
;
}
internal
(
"
"
,
"
unrecognized value in export_value()
"
, __FILE__, __LINE__);
return
"
"
;
}
parse_expression::expression::argument
ExpressionExporter::export_constant
(Value value)
const
{
internal
(
"
"
,
"
constant export not defined
"
, __FILE__, __LINE__);
return
{-
1
,
nullptr
};
}
parse_expression::expression::argument
ExpressionExporter::export_literal
(
size_t
index)
const
{
internal
(
"
"
,
"
literal export not defined
"
, __FILE__, __LINE__);
return
{-
1
,
nullptr
};
}
parse_expression::expression
ExpressionExporter::export_special
(
int
func,
const
vector<parse_expression::expression::argument> &args)
const
{
if
(func >=
0
and
func < (
int
)arithmetic::Operation::operators.
size
()) {
internal
(
"
"
,
"
no export for operator '
"
+ arithmetic::Operation::operators[func].
to_string
() +
"
'
"
, __FILE__, __LINE__);
}
else
{
internal
(
"
"
,
"
operator
"
+ ::
to_string
(func) +
"
not defined
"
, __FILE__, __LINE__);
}
return
parse_expression::expression
();
}
//
Functions that don't need to be overridden
parse_expression::expression::argument
ExpressionExporter::export_argument
(Operand op,
const
vector<parse_expression::expression> *sub)
const
{
if
(op.
isUndef
()) {
return
{-
1
,
nullptr
};
}
else
if
(op.
isConst
()) {
if
(op.
cnst
.
type
== arithmetic::Value::
ARRAY
or
op.
cnst
.
type
== arithmetic::Value::
STRUCT
) {
return
{-
1
, std::shared_ptr<parse::syntax>(
export_expression
(op.
cnst
.
type
, op.
cnst
.
arr
).
clone
())};
}
return
export_constant
(op.
cnst
);
}
else
if
(op.
isVar
()) {
return
export_literal
(op.
index
);
}
else
if
(op.
isExpr
()) {
if
(sub ==
nullptr
) {
internal
(
"
"
,
"
no sub expressions for lookup
"
, __FILE__, __LINE__);
return
{-
1
,
nullptr
};
}
parse_expression::expression::argument result;
result.
type
= -
1
;
result.
ptr
= std::shared_ptr<parse::syntax>((*sub)[op.
index
].
clone
());
return
result;
}
internal
(
"
"
,
"
operand export not defined
"
, __FILE__, __LINE__);
return
{-
1
,
nullptr
};
}
vector<parse_expression::expression::argument>
ExpressionExporter::export_arguments
(
const
vector<Operand> &args,
const
vector<parse_expression::expression> *sub)
const
{
vector<parse_expression::expression::argument> result;
for
(
const
Operand &arg : args) {
result.
push_back
(
export_argument
(arg, sub));
}
return
result;
}
parse_expression::expression
ExpressionExporter::export_expression
(
int
type,
const
vector<Value> &arr)
const
{
vector<Operand> args;
for
(
const
Value &v : arr) {
args.
push_back
(
Operand
(v));
}
if
(type == Value::
ARRAY
) {
return
export_expression
(Operation::
ARRAY
, args);
}
else
if
(type == Value::
STRUCT
) {
return
export_expression
(Operation::
STRUCT
, args);
}
return
export_expression
(Operation::
IDENTITY
, {
Operand::undef
()});
}
parse_expression::expression
ExpressionExporter::export_member_call
(
const
vector<parse_expression::expression::argument> &args)
const
{
using
OpType = arithmetic::Operation::OpType;
const
parse_expression::precedence_set &order =
precedence
();
auto
callOp =
export_operator
(OpType::
CALL
);
auto
memberOp =
export_operator
(OpType::
MEMBER
);
if
(callOp.
empty
()
or
memberOp.
empty
()) {
internal
(
"
"
,
"
call and member operators not defined
"
, __FILE__, __LINE__);
return
parse_expression::expression
();
}
auto
callIdx = order.
find
(-
1
, callOp);
auto
memberIdx = order.
find
(-
1
, memberOp);
if
(args.
size
() <
2u
) {
parse_expression::expression result;
result.
valid
=
true
;
result.
level
= callIdx.
level
;
result.
type
= order.
type
(result.
level
);
result.
operators
.
push_back
(callOp);
result.
arguments
= args;
return
result;
}
parse_expression::expression member;
member.
valid
=
true
;
member.
level
= memberIdx.
level
;
member.
type
= order.
type
(member.
level
);
member.
operators
.
push_back
(memberOp);
member.
arguments
.
push_back
(args[
1
]);
member.
arguments
.
push_back
(args[
0
]);
parse_expression::expression top;
top.
valid
=
true
;
top.
level
= callIdx.
level
;
top.
type
= order.
type
(top.
level
);
top.
operators
.
push_back
(callOp);
top.
arguments
.
push_back
({-
1
, std::shared_ptr<parse::syntax>(member.
clone
())});
top.
arguments
.
insert
(top.
arguments
.
end
(), args.
begin
()+
2
, args.
end
());
return
top;
}
parse_expression::expression
ExpressionExporter::export_expression
(
int
func, vector<Operand> args,
const
vector<parse_expression::expression> *sub)
const
{
const
parse_expression::precedence_set &order =
precedence
();
if
(func == Operation::
VALIDITY
) {
args.
insert
(args.
begin
(),
Operand::labelOf
(
"
valid
"
));
func = Operation::
CALL
;
}
else
if
(func == Operation::
TRUTHINESS
) {
args.
insert
(args.
begin
(),
Operand::labelOf
(
"
true
"
));
func = Operation::
CALL
;
}
else
if
(func == Operation::
NEGATIVE
) {
args.
push_back
(
Operand::intOf
(
0
));
func = Operation::
LESS
;
}
else
if
(func == Operation::
INVERSE
) {
args.
insert
(args.
begin
(),
Operand::realOf
(
1.0
));
func = Operation::
INTDIV
;
}
else
if
(func == Operation::
MEMBER_CALL
) {
return
export_member_call
(
export_arguments
(args, sub));
}
auto
op =
export_operator
(func);
if
(op.
empty
()) {
return
export_special
(func,
export_arguments
(args, sub));
}
auto
idx = order.
find
(-
1
, op);
parse_expression::expression result;
result.
valid
=
true
;
result.
level
= idx.
level
;
result.
type
= order.
type
(result.
level
);
if
(func != Operation::
IDENTITY
) {
result.
operators
.
push_back
(op);
}
result.
arguments
=
export_arguments
(args, sub);
return
result;
}
parse_expression::expression
ExpressionExporter::export_expression
(
const
Expression &expr)
const
{
if
(
not
expr.
top
.
isExpr
()) {
return
export_expression
(Operation::
IDENTITY
, {expr.
top
});
}
vector<parse_expression::expression> sub;
for
(arithmetic::ConstUpIterator
i
(expr, {expr.
top
});
not
i.
done
(); ++i) {
if
(i->
exprIndex
>= sub.
size
()) {
sub.
resize
(i->
exprIndex
+
1
);
}
sub[i->
exprIndex
] =
export_expression
(i->
func
, i->
operands
, &sub);
}
if
(expr.
top
.
index
< sub.
size
()) {
return
sub[expr.
top
.
index
];
}
return
export_expression
(Operation::
IDENTITY
, {
Operand::undef
()});
}
parse_expression::expression
CompositionExporter::export_expression
(
const
Parallel &expr)
const
{
const
parse_expression::precedence_set &order =
precedence
();
parse_expression::expression result;
result.
valid
=
true
;
auto
op =
export_operator
(Operation::
WIRE_AND
);
auto
idx = order.
find
(-
1
, op);
result.
level
= idx.
level
;
result.
type
= order.
type
(result.
level
);
result.
operators
.
push_back
(op);
for
(
const
Action &act : expr.
actions
) {
result.
arguments
.
push_back
(
export_action
(act));
}
return
result;
}
parse_expression::expression
CompositionExporter::export_expression
(
const
Choice &expr)
const
{
const
parse_expression::precedence_set &order =
precedence
();
parse_expression::expression result;
result.
valid
=
true
;
auto
op =
export_operator
(Operation::
WIRE_OR
);
auto
idx = order.
find
(-
1
, op);
result.
level
= idx.
level
;
result.
type
= order.
type
(result.
level
);
result.
operators
.
push_back
(op);
for
(
const
Parallel ¶ : expr.
terms
) {
result.
arguments
.
push_back
({-
1
, std::shared_ptr<parse::syntax>(
export_expression
(para).
clone
())});
}
return
result;
}
}
Back
|
FazBrowse Home
|
New Git URL