|
28 | 28 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__ADD__; |
29 | 29 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__CONTAINS__; |
30 | 30 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__; |
| 31 | +import static com.oracle.graal.python.nodes.SpecialMethodNames.__FORMAT__; |
31 | 32 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__; |
32 | 33 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GE__; |
33 | 34 | import static com.oracle.graal.python.nodes.SpecialMethodNames.__GT__; |
|
95 | 96 | import com.oracle.graal.python.nodes.SpecialMethodNames; |
96 | 97 | import com.oracle.graal.python.nodes.builtins.ListNodes.AppendNode; |
97 | 98 | import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode; |
| 99 | +import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode; |
98 | 100 | import com.oracle.graal.python.nodes.classes.IsSubtypeNode; |
99 | 101 | import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode; |
100 | 102 | import com.oracle.graal.python.nodes.function.PythonBuiltinNode; |
|
112 | 114 | import com.oracle.graal.python.nodes.util.CastToJavaStringNodeGen; |
113 | 115 | import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext; |
114 | 116 | import com.oracle.graal.python.runtime.PythonContext; |
| 117 | +import com.oracle.graal.python.runtime.PythonCore; |
115 | 118 | import com.oracle.graal.python.runtime.PythonOptions; |
116 | 119 | import com.oracle.graal.python.runtime.exception.PException; |
| 120 | +import com.oracle.graal.python.runtime.formatting.InternalFormat; |
| 121 | +import com.oracle.graal.python.runtime.formatting.InternalFormat.Spec; |
117 | 122 | import com.oracle.graal.python.runtime.formatting.StringFormatProcessor; |
| 123 | +import com.oracle.graal.python.runtime.formatting.TextFormatter; |
118 | 124 | import com.oracle.truffle.api.CompilerAsserts; |
119 | 125 | import com.oracle.truffle.api.CompilerDirectives; |
120 | 126 | import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary; |
@@ -153,6 +159,38 @@ static String doGeneric(Object self, |
153 | 159 | } |
154 | 160 | } |
155 | 161 |
|
| 162 | + @Builtin(name = __FORMAT__, minNumOfPositionalArgs = 2) |
| 163 | + @GenerateNodeFactory |
| 164 | + @TypeSystemReference(PythonArithmeticTypes.class) |
| 165 | + abstract static class FormatNode extends PythonBinaryBuiltinNode { |
| 166 | + |
| 167 | + @Specialization(guards = "formatString.isEmpty()") |
| 168 | + Object emptyFormat(VirtualFrame frame, Object self, @SuppressWarnings("unused") String formatString, |
| 169 | + @Cached("create(__STR__)") LookupAndCallUnaryNode strCall) { |
| 170 | + return strCall.executeObject(frame, self); |
| 171 | + } |
| 172 | + |
| 173 | + @Specialization(guards = "!formatString.isEmpty()") |
| 174 | + Object format(Object self, String formatString, |
| 175 | + @Cached CastToJavaStringCheckedNode castToJavaStringNode) { |
| 176 | + String str = castToJavaStringNode.cast(self, INVALID_RECEIVER, __STR__, self); |
| 177 | + return formatString(getCore(), formatString, str); |
| 178 | + } |
| 179 | + |
| 180 | + @Fallback |
| 181 | + Object other(@SuppressWarnings("unused") Object self, Object formatString) { |
| 182 | + throw raise(PythonBuiltinClassType.TypeError, ErrorMessages.ARG_D_MUST_BE_S_NOT_P, "format()", 2, "str", formatString); |
| 183 | + } |
| 184 | + |
| 185 | + @TruffleBoundary |
| 186 | + private static Object formatString(PythonCore core, String formatString, String str) { |
| 187 | + Spec spec = InternalFormat.fromText(core, formatString, __FORMAT__); |
| 188 | + TextFormatter formatter = new TextFormatter(core, spec.withDefaults(Spec.STRING)); |
| 189 | + formatter.format(str); |
| 190 | + return formatter.pad().getResult(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
156 | 194 | @Builtin(name = __REPR__, minNumOfPositionalArgs = 1) |
157 | 195 | @GenerateNodeFactory |
158 | 196 | public abstract static class ReprNode extends PythonUnaryBuiltinNode { |
|
0 commit comments