Optimized code runner and implemented basic IDE feature in app.ed
This commit is contained in:
@@ -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