File size: 2,524 Bytes
02215ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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()