-
Notifications
You must be signed in to change notification settings - Fork 25
/
rust.py
53 lines (47 loc) · 1.42 KB
/
rust.py
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
from execute import execute, livecode
import requests
import re
from typing import Optional
def verifier_feedback(ok: str, not_ok: str) -> Optional[str]:
msg = "Consider previous issue"
if msg in ok:
return None
_, err = calculateScoreHelper(not_ok)
if err:
err = err.strip()
hint = f"\n/* {msg}: {err} */\n"
text = ok + hint
return text
return None
def calculateScore(msg: str) -> Optional[float]:
score, _ = calculateScoreHelper(msg)
return score
def calculateScoreHelper(msg: str) -> (Optional[float], Optional[str]):
v = filter_code(msg + "```").strip()
if v == "":
return None, None
r = check_code(v)
if r["status"] == 0:
return 1.0, None
log = r["log"]
print(log)
first = log[log.rindex("ex.rs:") + 6 :]
num_line_first = int(first[0 : first.index(":")])
if filter_code(msg).strip() != v and num_line_first >= v.count("\n"):
return None, None
else:
err = log
return -1.0, err
def score_func(sentence: str) -> Optional[float]:
print("TEXT")
print(sentence)
score = calculateScore(sentence)
print("SCORE")
print(score)
return score
def filter_code(msg: str) -> str:
m = re.findall("```([Rr]ust)?(.*?)```", msg, re.MULTILINE | re.DOTALL)
r = "\n".join([x[1] for x in m])
return r
def check_code(v: str) -> dict:
return execute("rustc --crate-type lib", "rs", v)