Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
d217f1bc
Commit
d217f1bc
authored
May 18, 2015
by
Marius Wachtler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for compile('single')
parent
66cd8f53
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
7 deletions
+25
-7
src/codegen/irgen/hooks.cpp
src/codegen/irgen/hooks.cpp
+21
-7
test/tests/compile_exec.py
test/tests/compile_exec.py
+4
-0
No files found.
src/codegen/irgen/hooks.cpp
View file @
d217f1bc
...
...
@@ -360,12 +360,30 @@ CLFunction* compileForEvalOrExec(AST* source, std::vector<AST_stmt*> body, std::
// TODO: CPython parses execs as Modules, not as Suites. This is probably not too hard to change,
// but is non-trivial since we will later decide some things (ex in scoping_analysis) based off
// the type of the root ast node.
static
AST_Suite
*
parseExec
(
llvm
::
StringRef
source
)
{
static
AST_Suite
*
parseExec
(
llvm
::
StringRef
source
,
bool
interactive
=
false
)
{
// TODO error message if parse fails or if it isn't an expr
// TODO should have a cleaner interface that can parse the Expression directly
// TODO this memory leaks
const
char
*
code
=
source
.
data
();
AST_Module
*
parsedModule
=
parse_string
(
code
);
if
(
interactive
)
{
for
(
int
i
=
0
;
i
<
parsedModule
->
body
.
size
();
++
i
)
{
AST_stmt
*
s
=
parsedModule
->
body
[
i
];
if
(
s
->
type
!=
AST_TYPE
::
Expr
)
continue
;
AST_Expr
*
expr
=
(
AST_Expr
*
)
s
;
AST_Print
*
print
=
new
AST_Print
;
print
->
lineno
=
expr
->
lineno
;
print
->
col_offset
=
expr
->
col_offset
;
print
->
dest
=
NULL
;
print
->
nl
=
true
;
print
->
values
.
push_back
(
expr
->
value
);
parsedModule
->
body
[
i
]
=
print
;
}
}
AST_Suite
*
parsedSuite
=
new
AST_Suite
(
std
::
move
(
parsedModule
->
interned_strings
));
parsedSuite
->
body
=
std
::
move
(
parsedModule
->
body
);
return
parsedSuite
;
...
...
@@ -462,8 +480,7 @@ Box* compile(Box* source, Box* fn, Box* type, Box** _args) {
}
else
if
(
type_str
==
"eval"
)
{
parsed
=
parseEval
(
source_str
);
}
else
if
(
type_str
==
"single"
)
{
fatalOrError
(
NotImplemented
,
"unimplemented"
);
throwCAPIException
();
parsed
=
parseExec
(
source_str
,
true
);
}
else
{
raiseExcHelper
(
ValueError
,
"compile() arg 3 must be 'exec', 'eval' or 'single'"
);
}
...
...
@@ -473,7 +490,7 @@ Box* compile(Box* source, Box* fn, Box* type, Box** _args) {
return
boxAst
(
parsed
);
CLFunction
*
cl
;
if
(
type_str
==
"exec"
)
{
if
(
type_str
==
"exec"
||
type_str
==
"single"
)
{
// TODO: CPython parses execs as Modules
if
(
parsed
->
type
!=
AST_TYPE
::
Suite
)
raiseExcHelper
(
TypeError
,
"expected Suite node, got %s"
,
boxAst
(
parsed
)
->
cls
->
tp_name
);
...
...
@@ -482,9 +499,6 @@ Box* compile(Box* source, Box* fn, Box* type, Box** _args) {
if
(
parsed
->
type
!=
AST_TYPE
::
Expression
)
raiseExcHelper
(
TypeError
,
"expected Expression node, got %s"
,
boxAst
(
parsed
)
->
cls
->
tp_name
);
cl
=
compileEval
(
static_cast
<
AST_Expression
*>
(
parsed
),
filename_str
);
}
else
if
(
type_str
==
"single"
)
{
fatalOrError
(
NotImplemented
,
"unimplemented"
);
throwCAPIException
();
}
else
{
raiseExcHelper
(
ValueError
,
"compile() arg 3 must be 'exec', 'eval' or 'single'"
);
}
...
...
test/tests/compile_exec.py
View file @
d217f1bc
...
...
@@ -26,3 +26,7 @@ a = 0
g
=
{
'_c'
:
c
}
exec
"exec _c"
in
g
print
a
,
sorted
(
g
.
keys
())
print
c
=
compile
(
"a = 1; b = 2; a - b; b - a; c = a - b; c; print c"
,
"test.py"
,
"single"
)
exec
c
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment