circuitpython/tests/circuitpython/codeop_compile.py
Jeff Epler 9477574dfc
Add codeop.compile_command
This function in standard Python is a building block for custom REPLs:
```python
from codeop import compile_command

print("Repl in (Circuit-)Python")
ns = {}

PS1="<<< "
PS2=",,, "
command = ""
while True:
    line = input(PS2 if command else PS1)
    if command:
        command = command + "\n" + line
    else:
        command = line
    try:
        if (code := compile_command(command)):
            command = ""
            exec(code, ns)
    except Exception as e:
        command = ""
        print(e)
```
2023-12-14 09:23:23 -06:00

26 lines
450 B
Python

try:
from codeop import compile_command
except ImportError:
print("SKIP")
raise SystemExit
def test(snippet):
result = compile_command(snippet)
print("None" if result is None else "<code>")
# Complete command
test("3+3")
# Complete command
test("if 1:\n print('hi mom')\n")
# Incomplete command
test("if 1:")
# Incomplete multiline string
test("'''I'm sure it's OK")
# Incomplete parenthesized expression
test("[1, 2")