From 36481e398a0bc8ae741f61a8e17b6475d8a42c9b Mon Sep 17 00:00:00 2001 From: tonimaru Date: Fri, 7 Nov 2014 18:35:03 +0900 Subject: [PATCH] Update verification of istream --- .gitignore | 2 ++ main.py | 65 ++++++++++++++++++++++++++++++++++++++++++------------ site | 2 +- 3 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6c3c7fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.tsv +test.* diff --git a/main.py b/main.py index ffc644d..5a41768 100644 --- a/main.py +++ b/main.py @@ -15,21 +15,28 @@ def error_echo(string): sys.stderr.flush() class Code(object): - def __init__(self, code, result): + def __init__(self, code, result, input): self._code = code self._result = result + self._input = input def compile(self): open('test.cpp', 'w').write(self.code) subprocess.check_call([ - '/usr/local/gcc-head/bin/g++', - '-I/usr/local/gcc-head/include/c++/4.9.0', - '-L/usr/local/gcc-head/lib64', - '-Wl,-rpath,/usr/local/gcc-head/lib64', + 'g++', '-std=c++11', '-otest.exe', '-lpthread', 'test.cpp']) + # subprocess.check_call([ + # '/usr/local/gcc-head/bin/g++', + # '-I/usr/local/gcc-head/include/c++/4.9.0', + # '-L/usr/local/gcc-head/lib64', + # '-Wl,-rpath,/usr/local/gcc-head/lib64', + # '-std=c++11', + # '-otest.exe', + # '-lpthread', + # 'test.cpp']) #subprocess.check_call( #["/usr/local/llvm-3.3/bin/clang++", # "-otest.exe", @@ -43,7 +50,11 @@ def compile(self): # "test.cpp"]) def run(self): - return subprocess.check_output(['./test.exe']) + if self.input is None: + return subprocess.check_output(['./test.exe'], stderr=subprocess.STDOUT) + else: + pin = subprocess.Popen(['./test.exe'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) + return pin.communicate(input=self.input)[0] @property def code(self): @@ -53,17 +64,30 @@ def code(self): def result(self): return self._result + @property + def input(self): + return self._input + FENCED_BLOCK_RE = re.compile( - r'(?P^(?:~{3,}|`{3,}))[ ]*(\{?\.?(?P[a-zA-Z0-9_+-]*)\}?)?[ ]*\n(?P.*?)(?<=\n)(?P=fence)[ ]*$', + r'((?P^#+[^\n]*?$)[ ]*\n)?(?P<fence>^(?:~{3,}|`{3,}))[ ]*(\{?\.?(?P<lang>[a-zA-Z0-9_+-]*)\}?)?[ ]*\n(?P<code>.*?)(?<=\n)(?P=fence)[ ]*$', re.MULTILINE|re.DOTALL ) +OUTPUT_RE = re.compile('#+出力') +INPUT_RE = re.compile('#+入力') + # コードブロックがサンプルコードであるかどうかを判断する def is_sample_code(code, lang): # 最初の5行以内に #include を含んでたらサンプルコードだと判断する first_lines = code.split('\n')[:5] return any(line.find('#include') >= 0 for line in first_lines) +def is_output(code): + return OUTPUT_RE.match(code) + +def is_input(code): + return INPUT_RE.match(code) + # コードブロックと出力を探して返す def get_codes(md): codes = [] @@ -81,23 +105,36 @@ def __call__(self): next_fenced_block = NextFencedBlock(md) + current_code = None + result = None + input = None + while True: m = next_fenced_block() if not m: + if current_code is not None: + codes.append(Code(current_code, result, input)) break code = m.group('code') lang = m.group('lang') - if not is_sample_code(code, lang): + if is_sample_code(code, lang): + if current_code is not None: + codes.append(Code(current_code, result, input)) + result = None + input = None + current_code = code continue - m = next_fenced_block() - if not m: - result = None - else: - result = m.group('code') + title = m.group('title') + if title is not None: + if is_output(title): + result = code + continue - codes.append(Code(code, result)) + if is_input(title): + input = code + continue return codes diff --git a/site b/site index 7152781..ce5a0a9 160000 --- a/site +++ b/site @@ -1 +1 @@ -Subproject commit 7152781f347fc3638d7c8ddbc0ad81b863f39c65 +Subproject commit ce5a0a9cd0d077c7775a0204aeac5dcfae0b7f03