ErasLangBackend / worker.py
42Cummer's picture
Upload 8 files
02215ec verified
import sys
import traceback
import re
import resource
def limit_resources():
"""
Prevents malicious or accidental resource exhaustion.
"""
try:
# Limit CPU time to 5 seconds
resource.setrlimit(resource.RLIMIT_CPU, (5, 5))
except (ValueError, OSError):
# Resource limits may not be available on all platforms
pass
try:
# Limit memory usage to 128MB
resource.setrlimit(resource.RLIMIT_AS, (128 * 1024 * 1024, 128 * 1024 * 1024))
except (ValueError, OSError):
# Resource limits may not be available on all platforms
pass
def translate_line_number(error_msg):
"""
Search the traceback for the # ERAS_LINE_X comment
to map Python errors back to the original ErasLang line.
"""
match = re.search(r"# ERAS_LINE_(\d+)", error_msg)
return match.group(1) if match else "Unknown"
def perform_execution():
# 1. Read the transpiled Python code from the standard input pipe
try:
code = sys.stdin.read()
except EOFError:
return
# 2. Set up the "Stage" (Global Constants)
globals_dict = {
"enchanted": 1,
"exile": 0,
"thirteen": 13,
"__name__": "__main__", # Ensure __name__ is set for if __name__ checks
"__builtins__": __builtins__ # Keep basic functions like print()
}
try:
# 3. Apply SWE Guardrails
limit_resources()
# 4. Execute the performance
exec(code, globals_dict)
# 5. Catch and Translate Errors to ErasLang Terminology
except ZeroDivisionError:
print("EXILE ERROR: You tried to divide by zero. You're on your own, kid.")
sys.exit(1)
except NameError as e:
full_error = traceback.format_exc()
line = translate_line_number(full_error)
# Extract the missing variable name from the error message
var_match = re.search(r"name '(.+)' is not defined", str(e))
var_name = var_match.group(1) if var_match else "something"
print(f"VAULT ERROR: '{var_name}' was never declared at Line {line}. You left a Blank Space.")
sys.exit(1)
except SyntaxError as e:
print(f"BAD BLOOD: The bridge is broken. Syntax error near Line {e.lineno}.")
sys.exit(1)
except Exception as e:
full_error = traceback.format_exc()
line = translate_line_number(full_error)
print(f"RUNTIME ERROR at Line {line}: {str(e)}")
sys.exit(1)
if __name__ == "__main__":
perform_execution()