|
| 1 | +# Developed by Scott Shawcroft (@tannewt) https://github.com/tannewt/basicpython |
| 2 | +# |
| 3 | +# This is an experiment to edit Python code like BASIC was edited. |
| 4 | +# The idea is imagining this as the default mode on a Raspberry Pi 400 |
| 5 | +# |
| 6 | +print("# Basic Python v0") |
| 7 | + |
| 8 | +program = [] |
| 9 | +top = {} |
| 10 | +no_ready = False |
| 11 | + |
| 12 | +while True: |
| 13 | + if no_ready: |
| 14 | + line = input() |
| 15 | + else: |
| 16 | + line = input("READY.\n") |
| 17 | + lineno = None |
| 18 | + if " " in line: |
| 19 | + command, remainder = line.split(" ", 1) |
| 20 | + lineno = None |
| 21 | + try: |
| 22 | + lineno = int(command, 10) |
| 23 | + except ValueError: |
| 24 | + pass |
| 25 | + else: |
| 26 | + command = line |
| 27 | + no_ready = False |
| 28 | + if lineno is not None: |
| 29 | + if lineno < 1: |
| 30 | + print("Line must be 1+") |
| 31 | + else: |
| 32 | + if lineno >= len(program): |
| 33 | + program.extend([""] * (lineno - len(program))) |
| 34 | + program[lineno - 1] = remainder |
| 35 | + no_ready = True |
| 36 | + elif command.lower() == "list": |
| 37 | + for i, line in enumerate(program): |
| 38 | + if line: |
| 39 | + print(i+1, line) |
| 40 | + elif command.lower() == "run": |
| 41 | + isolated = {} |
| 42 | + try: |
| 43 | + exec("\n".join(program), isolated, isolated) |
| 44 | + except Exception as e: |
| 45 | + print(e) |
| 46 | + elif command.lower() == "save": |
| 47 | + filename = remainder.strip(" \"") |
| 48 | + with open(filename, "w") as f: |
| 49 | + f.write("\n".join(program)) |
| 50 | + elif command.lower() == "load": |
| 51 | + filename = remainder.strip(" \"") |
| 52 | + with open(filename, "r") as f: |
| 53 | + program = f.readlines() |
| 54 | + program = [line.strip("\r\n") for line in program] |
| 55 | + elif command.lower() in ["exit","quit","dos"]: |
| 56 | + break |
| 57 | + else: |
| 58 | + try: |
| 59 | + exec(line, top, top) |
| 60 | + except Exception as e: |
| 61 | + print(e) |
0 commit comments