• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

A categorical programming language


Commit MetaInfo

修订版cd87073975ed14b10dc277904b2d8dc732f7fb2e (tree)
时间2023-01-13 17:49:17
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

Get new REPL to extract complete terms.

We're gonna hack a bit on new REPL, I guess.

更改概述

差异

--- a/new-repl.py
+++ b/new-repl.py
@@ -1,49 +1,88 @@
11 #!/usr/bin/env nix-shell
2-#! nix-shell -i python -p python3Packages.requests python3Packages.click-repl
2+#! nix-shell -i python -p python3Packages.requests
33
4+import readline
5+import sys
46 from textwrap import dedent
57
6-import click
7-import click_repl
8-
98 import requests
109
11-@click.group()
12-def cli():
13- pass
14-
1510 def getJSON(url):
1611 try:
1712 return requests.get(url).json()
1813 except requests.exceptions.JSONDecodeError:
19- click.echo("Server response was not JSON")
14+ print("Server response was not JSON")
15+ raise
16+
17+def fetchDippers(base): return getJSON(base + "/dippers")
18+
19+class Extractor:
20+ def __init__(self, base):
21+ # XXX should this cache be sparse?
22+ self.cache = {}
23+ self.base = base
24+
25+ def extractExpr(self, expr):
26+ if expr not in self.cache:
27+ self.cache[expr] = getJSON(base + "/extract/" + str(expr))
28+ return self.cache[expr]
2029
21-@cli.command()
22-def size():
23- dippers = getJSON("http://localhost:5000/dippers")
24- if dippers:
25- click.echo("got dippers")
26- click.echo(len(dippers))
30+ def fullyExtract(self, expr):
31+ if isinstance(expr, int): return self.fullyExtract(self.extractExpr(expr))
32+ elif isinstance(expr, list):
33+ return [expr[0]] + [self.fullyExtract(ex) for ex in expr[1:]]
34+ else: return expr
2735
2836 def sexpify(expr):
2937 if isinstance(expr, list):
3038 return "(" + " ".join(sexpify(x) for x in expr) + ")"
3139 else: return expr
3240
33-@cli.command()
34-@click.argument("name")
35-def show(name):
36- obj = getJSON("http://localhost:5000/dip/" + name)
37- if obj:
38- trail = obj["trail"].strip()
39- expr = sexpify(obj["expr"])
40- click.echo(dedent("""
41- Name: {name}
42- Trail: {trail}
43- Dipped form: {expr}
44- """).strip().format(name=name, trail=trail, expr=expr))
45-
46-click_repl.register_repl(cli)
41+def fetchDipper(base, name):
42+ return getJSON(base + "/dip/" + name)
43+
44+class DipperCompleter:
45+ def __init__(self, dippers):
46+ self.cache = {}
47+ self.dippers = dippers
48+
49+ def complete(self, text, state):
50+ if text not in self.cache:
51+ self.cache[text] = [d for d in self.dippers if d.startswith(text)]
52+ if state < len(self.cache[text]): return self.cache[text][state]
53+
54+def repl(base):
55+ print("Starting REPL with honey base", base)
56+ extractor = Extractor(base)
57+ done = False
58+ while not done:
59+ try:
60+ line = input("> ")
61+ except EOFError:
62+ print("\nSee you later~")
63+ done = True
64+ else:
65+ if line.startswith(":"):
66+ print("I think it's a command")
67+ else:
68+ print("I think it's a sugared Cammy expression")
69+ if line.startswith("("):
70+ print("I think it's an application")
71+ else:
72+ print("I'm gonna guess that it's a dip!")
73+ obj = fetchDipper(base, line)
74+ trail = obj.get("trail", "").strip()
75+ if trail: print("Trail:", trail)
76+ term = extractor.fullyExtract(obj["expr"])
77+ print("Term:", sexpify(term))
78+
79+def installCompletion(dippers):
80+ readline.parse_and_bind("tab: complete")
81+ readline.set_completer(DipperCompleter(dippers).complete)
82+ readline.set_completer_delims("() ")
4783
4884 if __name__ == "__main__":
49- cli()
85+ base = sys.argv[-1]
86+ dippers = fetchDippers(base)
87+ installCompletion(dippers)
88+ repl(base)