| | import pexpect |
| | import json_stream |
| | import json |
| |
|
| | MACOS = 'MacOS' |
| | LINUX = 'Linux' |
| | OS = LINUX |
| | LEAN_REPL_CMD = 'lake env ../repl/.lake/build/bin/repl' |
| | SHELL_CMD = '/bin/sh' if OS == MACOS else '/bin/bash' |
| | BASE_PATH = 'LeanSrc/LeanSrc' |
| |
|
| | def make_lean_repl(repl_type='zsh'): |
| | |
| | |
| | |
| | print('making repl') |
| | if repl_type == 'icanon': |
| | lean_repl = pexpect.spawn(SHELL_CMD, cwd='LeanSrc', maxread=5000, timeout=20, echo=False) |
| | lean_repl.sendline('stty -icanon') |
| | lean_repl.sendline(LEAN_REPL_CMD) |
| | if OS == MACOS: |
| | print(lean_repl.readline()) |
| | print(lean_repl.readline()) |
| | elif repl_type == 'zsh': |
| | lean_repl = pexpect.spawn(LEAN_REPL_CMD,cwd='LeanSrc', maxread=1025, timeout=5) |
| |
|
| | return lean_repl |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | def json_stream_itr(bstr): |
| | start = 0 |
| | for end in range(len(bstr)): |
| | |
| | if bstr[end:end+1] == b'}': |
| | yield bstr[start: end + 1] |
| | start = end + 1 |
| |
|
| | def load_mult_json(jsons): |
| | |
| | |
| | |
| | itr = json_stream_itr(jsons) |
| | ps_out = {'messages': []} |
| | while True: |
| | try: |
| | data = json_stream.load(itr, persistent=True) |
| | except StopIteration: |
| | break |
| | data.read_all() |
| | data = json_stream.to_standard_types(data) |
| | for k in data: |
| | |
| | |
| | if k == 'message': |
| | if data[k].strip().startswith('Lean error:'): |
| | dct = {'severity': 'error', 'data': data[k].replace('Lean error:', '')} |
| | else: |
| | dct = {'severity': 'warning', 'data': data[k]} |
| | print('got unexpected non-error message', dct) |
| | exit() |
| | ps_out['messages'].append(dct) |
| | elif isinstance(data[k], list) and k in ps_out: |
| | ps_out[k].extend(data[k]) |
| | else: |
| | assert k not in ps_out, k +',' +str(ps_out[k]) |
| | ps_out[k] = data[k] |
| | |
| | |
| |
|
| | |
| | assert ps_out is not None, 'parsing failed: ' + jsons.decode() |
| | |
| | return ps_out |
| |
|
| | def make_repl_command(def_str, env=None): |
| | jsn = {'cmd': def_str} |
| | if env is not None: |
| | jsn['env'] = env |
| | return json.dumps(jsn) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | def send_command_zsh(repl, command, env=None, timeout=5, first=False): |
| | rpl_comm = make_repl_command(command, env=env) |
| | |
| | """ |
| | num_splits = len(rpl_comm)//1024 + 1 |
| | for split in range(num_splits): |
| | #print(rpl_comm) # NOTE: uncomment to see everything being sent to lean repl |
| | spl_comm = rpl_comm[split*1024:(split+1)*1024] |
| | print(spl_comm) |
| | |
| | #print('sent and expecting:') |
| | #print(len(rpl_comm), rpl_comm) |
| | if split < num_splits - 1: |
| | repl.sendline(spl_comm) |
| | else: |
| | repl.sendline(spl_comm) |
| | repl.expect_exact(rpl_comm+'\r\n') |
| | """ |
| | |
| | |
| | |
| | |
| | repl.sendline(rpl_comm) |
| | repl.expect_exact(rpl_comm + '\r\n') |
| |
|
| | repl.sendline() |
| | repl.expect_exact('\r\n') |
| |
|
| | _index = repl.expect('env": \d+\}', timeout=timeout) |
| | env_str = repl.match.group().decode() |
| | new_env = int(env_str.split(':')[1].strip()[:-1]) |
| | output = repl.before + repl.match.group() |
| | |
| | return load_mult_json(output), new_env |
| |
|
| |
|
| | def send_command_icanon(repl, command, env=None, timeout=20, first=False): |
| | |
| | rpl_comm = make_repl_command(command, env=env) |
| | |
| | repl.sendline(rpl_comm + '\n') |
| | if OS == MACOS: |
| | if first: |
| | repl.expect_exact('~>') |
| | first = False |
| | else: |
| | try: |
| | repl.expect_exact('\r\n\r\n') |
| | except pexpect.exceptions.TIMEOUT as e: |
| | print('did not find newlines') |
| | elif OS == LINUX and first: |
| | repl.expect_exact('\r{') |
| | try: |
| | _index = repl.expect('env": \d+\}', timeout=60) |
| | command_sent = True |
| | except pexpect.exceptions.TIMEOUT as e: |
| | raise(e) |
| | print('did not find env in output') |
| | return None, None |
| | |
| | env_str = repl.match.group().decode() |
| | new_env = int(env_str.split(':')[1].strip()[:-1]) |
| | output = repl.before + repl.match.group() |
| | if OS == LINUX and first: |
| | output = '{'.encode() + output |
| | return load_mult_json(output), new_env |
| |
|
| | def make_repl_tactic(tac, proofState): |
| | return json.dumps({'tactic': tac, 'proofState': proofState}) |
| |
|
| | def send_tactic(repl, tactic, proofState): |
| | rpl_comm = make_repl_tactic(tactic, proofState) |
| |
|
| | |
| | |
| | |
| | |
| | repl.sendline(rpl_comm+'\n') |
| | |
| | |
| | |
| | _index = repl.expect('(]})|(\"}\r\n\r\n)', timeout=60) |
| |
|
| |
|
| | |
| | |
| | output = repl.before + repl.match.group() |
| | |
| | |
| | ps_out = load_mult_json(output) |
| | |
| | if 'proofState' not in ps_out: |
| | return None, None |
| | return ps_out, ps_out['proofState'] |
| | |
| | |
| | |
| |
|
| | def get_errs(outp): |
| | return [m for m in outp.get('messages', []) if m.get('severity', 'error') == 'error'] |
| |
|