Skip to content

Commit 598ae4f

Browse files
committed
AST conversion python->java
1 parent f8e2f7a commit 598ae4f

File tree

6 files changed

+1172
-15
lines changed

6 files changed

+1172
-15
lines changed

graalpython/com.oracle.graal.python.pegparser/src/com/oracle/graal/python/pegparser/sst/ConstantValue.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ public static ConstantValue ofRaw(Object o) {
150150
return new ConstantValue(o, Kind.RAW);
151151
}
152152

153+
public static ConstantValue ofArbitraryPythonObject(Object o) {
154+
assert Kind.ARBITRARY_PYTHON_OBJECT.checkValueType(o);
155+
return new ConstantValue(o, Kind.ARBITRARY_PYTHON_OBJECT);
156+
}
157+
153158
public enum Kind {
154159
ARBITRARY_PYTHON_OBJECT() {
155160
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,9 +524,21 @@ public RootCallTarget parseForBytecodeInterpreter(PythonContext context, Source
524524
RaisePythonExceptionErrorCallback errorCb = new RaisePythonExceptionErrorCallback(source, PythonOptions.isPExceptionWithJavaStacktrace(this));
525525
try {
526526
Parser parser = Compiler.createParser(source.getCharacters().toString(), errorCb, type, interactiveTerminal);
527-
Compiler compiler = new Compiler(errorCb);
528527
ModTy mod = (ModTy) parser.parse();
529528
assert mod != null;
529+
return compileForBytecodeInterpreter(context, mod, source, topLevel, optimize, argumentNames);
530+
} catch (PException e) {
531+
if (topLevel) {
532+
PythonUtils.getOrCreateCallTarget(new TopLevelExceptionHandler(this, e)).call();
533+
}
534+
throw e;
535+
}
536+
}
537+
538+
public RootCallTarget compileForBytecodeInterpreter(PythonContext context, ModTy mod, Source source, boolean topLevel, int optimize, List<String> argumentNames) {
539+
RaisePythonExceptionErrorCallback errorCb = new RaisePythonExceptionErrorCallback(source, PythonOptions.isPExceptionWithJavaStacktrace(this));
540+
try {
541+
Compiler compiler = new Compiler(errorCb);
530542
boolean hasArguments = argumentNames != null && !argumentNames.isEmpty();
531543
if (hasArguments) {
532544
mod = transformASTForExecutionWithArguments(argumentNames, mod);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,14 +1148,7 @@ Object compile(TruffleString expression, TruffleString filename, TruffleString m
11481148
} else {
11491149
ct = getCore().getLanguage().cacheCode(filename, createCode);
11501150
}
1151-
RootCallTarget rootCallTarget = (RootCallTarget) ct;
1152-
RootNode rootNode = rootCallTarget.getRootNode();
1153-
if (rootNode instanceof PBytecodeRootNode) {
1154-
((PBytecodeRootNode) rootNode).triggerDeferredDeprecationWarnings();
1155-
} else if (rootNode instanceof PRootNode) {
1156-
((PRootNode) rootNode).triggerDeprecationWarnings();
1157-
}
1158-
return factory().createCode(rootCallTarget);
1151+
return wrapRootCallTarget((RootCallTarget) ct);
11591152
}
11601153

11611154
@Specialization(limit = "3")
@@ -1214,11 +1207,29 @@ Object generic(VirtualFrame frame, Object wSource, Object wFilename, Object wMod
12141207
}
12151208
checkOptimize(optimize, kwOptimize);
12161209
}
1210+
if (getCore().isCoreInitialized() && AstModuleBuiltins.isAst(getContext(), wSource)) {
1211+
ModTy mod = AstModuleBuiltins.obj2sst(getContext(), wSource);
1212+
// TODO _PyAST_Validate
1213+
// TODO fake source
1214+
Source source = Source.newBuilder(PythonLanguage.ID, "", "").build();
1215+
RootCallTarget rootCallTarget = getLanguage().compileForBytecodeInterpreter(getContext(), mod, source, false, optimize, null);
1216+
return wrapRootCallTarget(rootCallTarget);
1217+
}
12171218
TruffleString source = sourceAsString(frame, wSource, filename, interopLib, acquireLib, bufferLib, handleDecodingErrorNode, asStrNode, switchEncodingNode);
12181219
checkSource(source);
12191220
return compile(source, filename, mode, flags, kwDontInherit, optimize);
12201221
}
12211222

1223+
private PCode wrapRootCallTarget(RootCallTarget rootCallTarget) {
1224+
RootNode rootNode = rootCallTarget.getRootNode();
1225+
if (rootNode instanceof PBytecodeRootNode) {
1226+
((PBytecodeRootNode) rootNode).triggerDeferredDeprecationWarnings();
1227+
} else if (rootNode instanceof PRootNode) {
1228+
((PRootNode) rootNode).triggerDeprecationWarnings();
1229+
}
1230+
return factory().createCode(rootCallTarget);
1231+
}
1232+
12221233
private void checkSource(TruffleString source) throws PException {
12231234
if (source.indexOfCodePointUncached(0, 0, source.codePointLengthUncached(TS_ENCODING), TS_ENCODING) > -1) {
12241235
throw raise(ValueError, ErrorMessages.SRC_CODE_CANNOT_CONTAIN_NULL_BYTES);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/ast/AstModuleBuiltins.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@
5656
import com.oracle.graal.python.builtins.objects.module.PythonModule;
5757
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5858
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
59+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5960
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
6061
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6162
import com.oracle.graal.python.pegparser.sst.ModTy;
6263
import com.oracle.graal.python.runtime.PythonContext;
64+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6365
import com.oracle.truffle.api.dsl.NodeFactory;
6466
import com.oracle.truffle.api.object.HiddenKey;
6567
import com.oracle.truffle.api.strings.TruffleString;
@@ -92,12 +94,6 @@ public void initialize(Python3Core core) {
9294
clsAst.setAttribute(T__FIELDS, emptyTuple);
9395
clsAst.setAttribute(T__ATTRIBUTES, emptyTuple);
9496
// TODO clsAst.setAttribute('__match_args__', emptyTuple);
95-
}
96-
97-
@Override
98-
@SuppressWarnings("unused")
99-
public void postInitialize(Python3Core core) {
100-
super.postInitialize(core);
10197
PythonModule astModule = core.lookupBuiltinModule(T__AST);
10298
AstTypeFactory astTypeFactory = new AstTypeFactory(core.getLanguage(), core.factory(), astModule);
10399
AstState state = new AstState(astTypeFactory, core.lookupType(PythonBuiltinClassType.AST));
@@ -108,7 +104,18 @@ private static AstState getAstState(PythonContext context) {
108104
return (AstState) ReadAttributeFromObjectNode.getUncached().execute(context.lookupBuiltinModule(T__AST), AST_STATE_KEY);
109105
}
110106

107+
@TruffleBoundary
111108
public static Object sst2Obj(PythonContext context, ModTy mod) {
112109
return mod.accept(new Sst2ObjVisitor(getAstState(context)));
113110
}
111+
112+
@TruffleBoundary
113+
public static ModTy obj2sst(PythonContext context, Object obj) {
114+
return new Obj2Sst(getAstState(context)).obj2ModTy(obj);
115+
}
116+
117+
@TruffleBoundary
118+
public static boolean isAst(PythonContext context, Object obj) {
119+
return Obj2SstBase.isInstanceOf(obj, getAstState(context).clsAst);
120+
}
114121
}

0 commit comments

Comments
 (0)