Optimized code runner and implemented basic IDE feature in app.ed
This commit is contained in:
@@ -27,6 +27,14 @@ Current installation process doesn't involve any `.mpy` module compilation, so,
|
|||||||
|
|
||||||
As of now, T-DeckARD implements the following component modules.
|
As of now, T-DeckARD implements the following component modules.
|
||||||
|
|
||||||
|
### `deck.runtime`
|
||||||
|
|
||||||
|
A library for establishing runtime compatibility across various Python environments.
|
||||||
|
|
||||||
|
Exported functions:
|
||||||
|
|
||||||
|
- `runcode(code_string)`: compile and execute a Python program expressed as a string. Used by REPL and some applets.
|
||||||
|
|
||||||
### `deck.input`
|
### `deck.input`
|
||||||
|
|
||||||
A library for single-line and multi-line (ed-like) text input.
|
A library for single-line and multi-line (ed-like) text input.
|
||||||
@@ -240,6 +248,7 @@ The editor mode supports the following subset of POSIX ed commands in the standa
|
|||||||
- `s/old/new`: replace text in the range (doesn't support regex syntax from POSIX ed)
|
- `s/old/new`: replace text in the range (doesn't support regex syntax from POSIX ed)
|
||||||
- `f`: set the current filename to write to
|
- `f`: set the current filename to write to
|
||||||
- `w`: save changes to the current filename
|
- `w`: save changes to the current filename
|
||||||
|
- `x`: interpret the range as Python code and execute it immediately
|
||||||
- `q`: quit (`q!` force-quits without prompting for saving changes)
|
- `q`: quit (`q!` force-quits without prompting for saving changes)
|
||||||
|
|
||||||
### `app.llmchat`
|
### `app.llmchat`
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
from deck.input import input
|
from deck.input import input
|
||||||
from deck.pager import print_paged
|
from deck.pager import print_paged
|
||||||
|
from deck.runtime import runcode
|
||||||
|
|
||||||
class Ed:
|
class Ed:
|
||||||
def __init__(self, filename=None):
|
def __init__(self, filename=None):
|
||||||
@@ -191,6 +192,11 @@ class Ed:
|
|||||||
self.filename = params
|
self.filename = params
|
||||||
print(self.filename if self.filename else "No current filename")
|
print(self.filename if self.filename else "No current filename")
|
||||||
|
|
||||||
|
elif cmd_char == 'x': # execute current buffer range as Python code
|
||||||
|
code = '\n'.join(self.buffer[start-1:end])
|
||||||
|
print(f'Running lines {start} to {end} as Python code...')
|
||||||
|
runcode(code)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Unknown command")
|
print("Unknown command")
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,8 @@ try:
|
|||||||
from repl.keys import show_keys
|
from repl.keys import show_keys
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
try:
|
|
||||||
from codeop import compile_command
|
from deck.runtime import runcode
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if 'tdeckboot.py' in os.listdir('/'):
|
if 'tdeckboot.py' in os.listdir('/'):
|
||||||
from tdeckboot import *
|
from tdeckboot import *
|
||||||
@@ -27,9 +25,9 @@ while True:
|
|||||||
if __line.lower() == 'exit': break
|
if __line.lower() == 'exit': break
|
||||||
__cmd = __line
|
__cmd = __line
|
||||||
try:
|
try:
|
||||||
if compile_command(__cmd):
|
status, res = runcode(__cmd)
|
||||||
exec(compile_command(__cmd))
|
if status != 1:
|
||||||
__cmd = ""
|
__cmd = ""
|
||||||
except Exception as __err:
|
except Exception as __err:
|
||||||
print("*ERROR* Exception:",str(__err))
|
print("*ERROR* Exception:", str(__err))
|
||||||
__cmd = ""
|
__cmd = ""
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
# Runtime compatibility module
|
||||||
|
# Usage: from deck.runtime import runcode
|
||||||
|
# Created by Luxferre in 2025, released into public domain
|
||||||
|
|
||||||
|
CMDCOMPILER = False
|
||||||
|
try:
|
||||||
|
from codeop import compile_command
|
||||||
|
CMDCOMPILER = True
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
def runcode(code):
|
||||||
|
"""
|
||||||
|
Compile and evaluate Python code dynamically.
|
||||||
|
Returns (statuscode, result) tuple.
|
||||||
|
Return status codes:
|
||||||
|
0 on error, 1 if the command is incomplete, 2 on success
|
||||||
|
"""
|
||||||
|
status = 0 # error status
|
||||||
|
res = None
|
||||||
|
if CMDCOMPILER:
|
||||||
|
compiled = compile_command(code)
|
||||||
|
else:
|
||||||
|
compiled = compile(code, '<string>', 'exec')
|
||||||
|
if compiled:
|
||||||
|
try:
|
||||||
|
res = exec(compiled)
|
||||||
|
status = 2 # success status
|
||||||
|
except Exception as e:
|
||||||
|
res = str(e)
|
||||||
|
else:
|
||||||
|
status = 1 # continuation status
|
||||||
|
return (status, res)
|
||||||
Reference in New Issue
Block a user