FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
lpython/src/lpython/pickle.cpp at Smit-create-patch-1 · kinshukdua/lpython · GitHub
kinshukdua
/
lpython
Public
forked from
lcompilers/lpython
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
lpython
/
src
/
lpython
/
pickle.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
273 lines (256 loc) · 7.53 KB
Breadcrumbs
lpython
/
src
/
lpython
/
pickle.cpp
Copy path
File metadata and controls
273 lines (256 loc) · 7.53 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#
include
<
string
>
#
include
<
lpython/pickle.h
>
#
include
<
lpython/pickle.h
>
#
include
<
lpython/parser/parser.h
>
#
include
<
lpython/parser/parser.tab.hh
>
#
include
<
libasr/asr_utils.h
>
#
include
<
libasr/string_utils.h
>
using
LFortran::
AST
::
ast_t
;
using
LFortran::
AST
::Declaration_t;
using
LFortran::
AST
::
expr_t
;
using
LFortran::
AST
::
stmt_t
;
using
LFortran::
AST
::Name_t;
using
LFortran::
AST
::Num_t;
using
LFortran::
AST
::BinOp_t;
using
LFortran::
AST
::UnaryOp_t;
using
LFortran::
AST
::Compare_t;
using
LFortran::
AST
::If_t;
using
LFortran::
AST
::Assignment_t;
using
LFortran::
AST
::WhileLoop_t;
using
LFortran::
AST
::Exit_t;
using
LFortran::
AST
::Return_t;
using
LFortran::
AST
::Cycle_t;
using
LFortran::
AST
::DoLoop_t;
using
LFortran::
AST
::Subroutine_t;
using
LFortran::
AST
::Function_t;
using
LFortran::
AST
::Program_t;
using
LFortran::
AST
::astType;
using
LFortran::
AST
::exprType;
using
LFortran::
AST
::stmtType;
using
LFortran::
AST
::operatorType;
using
LFortran::
AST
::unaryopType;
using
LFortran::
AST
::cmpopType;
using
LFortran::
AST
::TranslationUnit_t;
using
LFortran::
AST
::PickleBaseVisitor;
namespace
LFortran
{
std::string
pickle
(
int
token,
const
LFortran::
YYSTYPE
&yystype,
bool
/*
colors
*/
)
{
std::string t;
t +=
"
(
"
;
if
(token >= yytokentype::
TK_NAME
&& token <=
TK_FALSE
) {
t +=
"
TOKEN
"
;
}
else
if
(token == yytokentype::
TK_NEWLINE
) {
t +=
"
NEWLINE
"
;
t +=
"
)
"
;
return
t;
}
else
if
(token == yytokentype::
END_OF_FILE
) {
t +=
"
EOF
"
;
t +=
"
)
"
;
return
t;
}
else
{
t +=
"
KEYWORD
"
;
}
t +=
"
\"
"
;
t +=
token2text
(token);
t +=
"
\"
"
;
if
(token == yytokentype::
TK_NAME
) {
t +=
"
"
+ yystype.
string
.
str
();
}
else
if
(token == yytokentype::
TK_INTEGER
) {
t +=
"
"
+ yystype.
int_suffix
.
int_n
.
str
();
if
(yystype.
int_suffix
.
int_kind
.
p
) {
t +=
"
_
"
+ yystype.
int_suffix
.
int_kind
.
str
();
}
}
else
if
(token == yytokentype::
TK_STRING
) {
t = t +
"
"
+
"
\"
"
+ yystype.
string
.
str
() +
"
\"
"
;
}
else
if
(token == yytokentype::
TK_BOZ_CONSTANT
) {
t +=
"
"
+ yystype.
string
.
str
();
}
t +=
"
)
"
;
return
t;
}
std::string
op2str
(
const
operatorType type)
{
switch
(type) {
case
(operatorType::Add) :
return
"
+
"
;
case
(operatorType::Sub) :
return
"
-
"
;
case
(operatorType::Mul) :
return
"
*
"
;
case
(operatorType::Div) :
return
"
/
"
;
case
(operatorType::Pow) :
return
"
**
"
;
}
throw
std::runtime_error
(
"
Unknown type
"
);
}
std::string
unop2str
(
const
unaryopType type)
{
switch
(type) {
case
(unaryopType::Invert) :
return
"
inv
"
;
case
(unaryopType::Not) :
return
"
not
"
;
case
(unaryopType::UAdd) :
return
"
u+
"
;
case
(unaryopType::USub) :
return
"
u-
"
;
}
throw
std::runtime_error
(
"
Unknown type
"
);
}
std::string
compare2str
(
const
cmpopType type)
{
switch
(type) {
case
(cmpopType::Eq) :
return
"
==
"
;
case
(cmpopType::NotEq) :
return
"
/=
"
;
case
(cmpopType::Lt) :
return
"
<
"
;
case
(cmpopType::LtE) :
return
"
<=
"
;
case
(cmpopType::Gt) :
return
"
>
"
;
case
(cmpopType::GtE) :
return
"
>=
"
;
}
throw
std::runtime_error
(
"
Unknown type
"
);
}
class
PickleVisitor
:
public
PickleBaseVisitor
<PickleVisitor>
{
public:
void
visit_BinOp
(
const
BinOp_t &x) {
s.
append
(
"
(
"
);
//
We do not print BinOp +, but rather just +. It is still uniquely
//
determined that + means BinOp's +.
/*
s.append(expr2str(x.base.type));
s.append(" ");
*/
s.
append
(
op2str
(x.
m_op
));
s.
append
(
"
"
);
this
->
visit_expr
(*x.
m_left
);
s.
append
(
"
"
);
this
->
visit_expr
(*x.
m_right
);
s.
append
(
"
)
"
);
}
void
visit_UnaryOp
(
const
UnaryOp_t &x) {
s.
append
(
"
(
"
);
s.
append
(
unop2str
(x.
m_op
));
s.
append
(
"
"
);
this
->
visit_expr
(*x.
m_operand
);
s.
append
(
"
)
"
);
}
void
visit_Compare
(
const
Compare_t &x) {
s.
append
(
"
(
"
);
s.
append
(
compare2str
(x.
m_op
));
s.
append
(
"
"
);
this
->
visit_expr
(*x.
m_left
);
s.
append
(
"
"
);
this
->
visit_expr
(*x.
m_right
);
s.
append
(
"
)
"
);
}
void
visit_Name
(
const
Name_t &x) {
if
(use_colors) {
s.
append
(
color
(fg::yellow));
}
s.
append
(x.
m_id
);
if
(use_colors) {
s.
append
(
color
(fg::reset));
}
}
void
visit_Num
(
const
Num_t &x) {
if
(use_colors) {
s.
append
(
color
(fg::cyan));
}
s.
append
(
BigInt::int_to_str
(x.
m_n
));
if
(use_colors) {
s.
append
(
color
(fg::reset));
}
if
(x.
m_kind
) {
s +=
"
_
"
;
s += x.
m_kind
;
}
}
std::string
get_str
() {
return
s;
}
};
std::string
pickle
(LFortran::
AST
::
ast_t
&ast,
bool
colors,
bool
indent) {
PickleVisitor v;
v.
use_colors
= colors;
v.
indent
= indent;
v.
visit_ast
(ast);
return
v.
get_str
();
}
std::string
pickle
(
AST
::TranslationUnit_t &ast,
bool
colors,
bool
indent) {
PickleVisitor v;
v.
use_colors
= colors;
v.
indent
= indent;
v.
visit_ast
((
AST
::
ast_t
&)(ast));
return
v.
get_str
();
}
/*
-----------------------------------------------------------------------
*/
//
ASR
class
ASRPickleVisitor
:
public LFortran::
ASR
::PickleBaseVisitor<ASRPickleVisitor>
{
public:
bool
show_intrinsic_modules;
std::string
get_str
() {
return
s;
}
void
visit_symbol
(
const
ASR
::
symbol_t
&x) {
s.
append
(
LFortran::ASRUtils::symbol_parent_symtab
(&x)->
get_counter
());
s.
append
(
"
"
);
if
(use_colors) {
s.
append
(
color
(fg::yellow));
}
s.
append
(
LFortran::ASRUtils::symbol_name
(&x));
if
(use_colors) {
s.
append
(
color
(fg::reset));
}
}
void
visit_ConstantInteger
(
const
ASR
::ConstantInteger_t &x) {
s.
append
(
"
(
"
);
if
(use_colors) {
s.
append
(
color
(style::bold));
s.
append
(
color
(fg::magenta));
}
s.
append
(
"
ConstantInteger
"
);
if
(use_colors) {
s.
append
(
color
(fg::reset));
s.
append
(
color
(style::reset));
}
s.
append
(
"
"
);
if
(use_colors) {
s.
append
(
color
(fg::cyan));
}
s.
append
(
std::to_string
(x.
m_n
));
if
(use_colors) {
s.
append
(
color
(fg::reset));
}
s.
append
(
"
"
);
this
->
visit_ttype
(*x.
m_type
);
s.
append
(
"
)
"
);
}
void
visit_Module
(
const
ASR
::Module_t &x) {
if
(!show_intrinsic_modules &&
startswith
(x.
m_name
,
"
lfortran_intrinsic_
"
)) {
s.
append
(
"
(
"
);
if
(use_colors) {
s.
append
(
color
(style::bold));
s.
append
(
color
(fg::magenta));
}
s.
append
(
"
IntrinsicModule
"
);
if
(use_colors) {
s.
append
(
color
(fg::reset));
s.
append
(
color
(style::reset));
}
s.
append
(
"
"
);
s.
append
(x.
m_name
);
s.
append
(
"
)
"
);
}
else
{
LFortran::
ASR
::PickleBaseVisitor<ASRPickleVisitor>::
visit_Module
(x);
};
}
};
std::string
pickle
(LFortran::
ASR
::
asr_t
&asr,
bool
colors,
bool
indent,
bool
show_intrinsic_modules) {
ASRPickleVisitor v;
v.
use_colors
= colors;
v.
indent
= indent;
v.
show_intrinsic_modules
= show_intrinsic_modules;
v.
visit_asr
(asr);
return
v.
get_str
();
}
std::string
pickle
(LFortran::
ASR
::TranslationUnit_t &asr,
bool
colors,
bool
indent,
bool
show_intrinsic_modules) {
return
pickle
((
ASR
::
asr_t
&)asr, colors, indent, show_intrinsic_modules);
}
}
Back
|
FazBrowse Home
|
New Git URL