Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Update verification of istream #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tsv
test.*
65 changes: 51 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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):
Expand All @@ -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<fence>^(?:~{3,}|`{3,}))[ ]*(\{?\.?(?P<lang>[a-zA-Z0-9_+-]*)\}?)?[ ]*\n(?P<code>.*?)(?<=\n)(?P=fence)[ ]*$',
r'((?P<title>^#+[^\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 = []
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion site
Submodule site updated 2051 files