Spaces:
Sleeping
Sleeping
Updated app.py to catch compilation errors.
Browse files
app.py
CHANGED
|
@@ -10,11 +10,9 @@ def valid(code:str) -> bool:
|
|
| 10 |
def run_code_with_inputs(code: str, inputs: str) -> str:
|
| 11 |
"""
|
| 12 |
Runs the provided Pseudocode as a console program with the given inputs.
|
| 13 |
-
|
| 14 |
Parameters:
|
| 15 |
code (str): pseudocode to execute.
|
| 16 |
inputs (str): User inputs separated by newlines.
|
| 17 |
-
|
| 18 |
Returns:
|
| 19 |
str: Combined output from the program execution.
|
| 20 |
"""
|
|
@@ -41,9 +39,9 @@ def run_code_with_inputs(code: str, inputs: str) -> str:
|
|
| 41 |
))
|
| 42 |
outputs.append(processes[-1].stdout)
|
| 43 |
except: pass
|
| 44 |
-
|
| 45 |
processes.append(subprocess.run(
|
| 46 |
-
["fpc",
|
| 47 |
text=True,
|
| 48 |
capture_output=True,
|
| 49 |
timeout=10
|
|
@@ -61,12 +59,27 @@ def run_code_with_inputs(code: str, inputs: str) -> str:
|
|
| 61 |
capture_output=True,
|
| 62 |
timeout=10
|
| 63 |
))
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if any(errors):
|
| 68 |
nl='\n'
|
| 69 |
-
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
return outputs[-1]
|
| 71 |
|
| 72 |
except Exception as e:
|
|
@@ -85,7 +98,8 @@ with gr.Blocks(theme=gr.themes.Default(text_size=gr.themes.sizes.text_lg)) as de
|
|
| 85 |
gr.Code(label="Pseudocode", language="typescript", value="""DECLARE yourname: STRING
|
| 86 |
OUTPUT "What is your name?"
|
| 87 |
INPUT yourname
|
| 88 |
-
OUTPUT "Hello, ",yourname,'!'
|
|
|
|
| 89 |
gr.Code(label="User Inputs", value='Alice\nBob', language=None, elem_id="input-box"),
|
| 90 |
],
|
| 91 |
outputs=gr.Code(
|
|
|
|
| 10 |
def run_code_with_inputs(code: str, inputs: str) -> str:
|
| 11 |
"""
|
| 12 |
Runs the provided Pseudocode as a console program with the given inputs.
|
|
|
|
| 13 |
Parameters:
|
| 14 |
code (str): pseudocode to execute.
|
| 15 |
inputs (str): User inputs separated by newlines.
|
|
|
|
| 16 |
Returns:
|
| 17 |
str: Combined output from the program execution.
|
| 18 |
"""
|
|
|
|
| 39 |
))
|
| 40 |
outputs.append(processes[-1].stdout)
|
| 41 |
except: pass
|
| 42 |
+
pasfile = file_path.removesuffix('.pseudo')+'.pas'
|
| 43 |
processes.append(subprocess.run(
|
| 44 |
+
["fpc", pasfile],
|
| 45 |
text=True,
|
| 46 |
capture_output=True,
|
| 47 |
timeout=10
|
|
|
|
| 59 |
capture_output=True,
|
| 60 |
timeout=10
|
| 61 |
))
|
| 62 |
+
ctime1 = os.path.getctime(pasfile)
|
| 63 |
+
ctime2 = os.path.getctime(binfile)
|
| 64 |
+
binfile_compiled = ctime1 < ctime2
|
| 65 |
+
# print(ctime1, ctime2, ctime1 < ctime2)
|
| 66 |
+
if binfile_compiled:
|
| 67 |
+
outputs.append(processes[-1].stdout)
|
| 68 |
+
errors.append(processes[-1].stderr)
|
| 69 |
+
else:
|
| 70 |
+
outputs.append("""Could not compile the pseudocode.
|
| 71 |
+
Please check for syntax errors and undefined or misspelled identifiers/keywords.
|
| 72 |
+
""")
|
| 73 |
|
| 74 |
if any(errors):
|
| 75 |
nl='\n'
|
| 76 |
+
return f"Error in Subprocess\nErrors:\n{nl.join(errors)}\n\nOutput:\n{nl.join(outputs)}"
|
| 77 |
+
|
| 78 |
+
# print(errors)
|
| 79 |
+
# print(outputs)
|
| 80 |
+
# ctime1 = os.path.getctime(pasfile)
|
| 81 |
+
# ctime2 = os.path.getctime(binfile)
|
| 82 |
+
# print(ctime1, ctime2, ctime1 < ctime2)
|
| 83 |
return outputs[-1]
|
| 84 |
|
| 85 |
except Exception as e:
|
|
|
|
| 98 |
gr.Code(label="Pseudocode", language="typescript", value="""DECLARE yourname: STRING
|
| 99 |
OUTPUT "What is your name?"
|
| 100 |
INPUT yourname
|
| 101 |
+
OUTPUT "Hello, ",yourname,'!'
|
| 102 |
+
OUTPUT "You''re using Pseudocode+ ",__version__,'.' """),
|
| 103 |
gr.Code(label="User Inputs", value='Alice\nBob', language=None, elem_id="input-box"),
|
| 104 |
],
|
| 105 |
outputs=gr.Code(
|