|
68 | 68 | import static com.oracle.graal.python.runtime.exception.PythonErrorType.ValueError; |
69 | 69 |
|
70 | 70 | import java.io.IOException; |
| 71 | +import java.io.InputStream; |
| 72 | +import java.io.PrintStream; |
71 | 73 | import java.io.PrintWriter; |
72 | 74 | import java.math.BigInteger; |
| 75 | +import java.nio.CharBuffer; |
73 | 76 | import java.util.List; |
74 | 77 | import java.util.function.Supplier; |
75 | 78 |
|
76 | 79 | import com.oracle.graal.python.PythonLanguage; |
77 | 80 | import com.oracle.graal.python.builtins.Builtin; |
78 | 81 | import com.oracle.graal.python.builtins.CoreFunctions; |
| 82 | +import com.oracle.graal.python.builtins.PythonBuiltinClassType; |
79 | 83 | import com.oracle.graal.python.builtins.PythonBuiltins; |
80 | 84 | import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.GetAttrNodeFactory; |
81 | 85 | import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory.NextNodeFactory; |
@@ -1535,4 +1539,44 @@ public Object doit(Object object) { |
1535 | 1539 | return "truffle ast dump not supported for " + object.toString(); |
1536 | 1540 | } |
1537 | 1541 | } |
| 1542 | + |
| 1543 | + @Builtin(name = "input", keywordArguments = {"prompt"}) |
| 1544 | + @GenerateNodeFactory |
| 1545 | + abstract static class InputNode extends PythonUnaryBuiltinNode { |
| 1546 | + @Specialization |
| 1547 | + @TruffleBoundary |
| 1548 | + String input(@SuppressWarnings("unused") PNone prompt) { |
| 1549 | + CharBuffer buf = CharBuffer.allocate(1000); |
| 1550 | + try { |
| 1551 | + InputStream stdin = getContext().getStandardIn(); |
| 1552 | + int read = stdin.read(); |
| 1553 | + while (read != -1 && read != '\n') { |
| 1554 | + if (buf.remaining() == 0) { |
| 1555 | + CharBuffer newBuf = CharBuffer.allocate(buf.capacity() * 2); |
| 1556 | + newBuf.put(buf); |
| 1557 | + buf = newBuf; |
| 1558 | + } |
| 1559 | + buf.put((char) read); |
| 1560 | + read = stdin.read(); |
| 1561 | + } |
| 1562 | + buf.limit(buf.position()); |
| 1563 | + buf.rewind(); |
| 1564 | + return buf.toString(); |
| 1565 | + } catch (IOException e) { |
| 1566 | + throw raise(PythonBuiltinClassType.EOFError, e.getMessage()); |
| 1567 | + } |
| 1568 | + } |
| 1569 | + |
| 1570 | + @Specialization |
| 1571 | + String inputPrompt(PString prompt) { |
| 1572 | + return inputPrompt(prompt.getValue()); |
| 1573 | + } |
| 1574 | + |
| 1575 | + @Specialization |
| 1576 | + @TruffleBoundary |
| 1577 | + String inputPrompt(String prompt) { |
| 1578 | + new PrintStream(getContext().getStandardOut()).println(prompt); |
| 1579 | + return input(null); |
| 1580 | + } |
| 1581 | + } |
1538 | 1582 | } |
0 commit comments