A categorical programming language
修订版 | cc80fa3f9a450a2a7a586b96382519443aea2cc6 (tree) |
---|---|
时间 | 2022-02-23 16:31:12 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Port cammy-frame to RPython.
Spent more time debugging cammylib than on the main entrypoint. I
suppose that this means that cammylib is reasonably factored.
@@ -3,6 +3,13 @@ import os.path | ||
3 | 3 | from cammylib.parser import parse |
4 | 4 | |
5 | 5 | |
6 | +class MissingAtom(Exception): | |
7 | + "An atom is missing from a hive." | |
8 | + | |
9 | + def __init__(self, atom): | |
10 | + self.atom = atom | |
11 | + | |
12 | + | |
6 | 13 | class Hive(object): |
7 | 14 | "A self-referential collection of S-expressions." |
8 | 15 |
@@ -11,12 +18,15 @@ class Hive(object): | ||
11 | 18 | self.exprs = {} |
12 | 19 | |
13 | 20 | def load(self, atom): |
14 | - print "Loading", atom | |
21 | + print("Loading", atom) | |
15 | 22 | filename = atom + ".cammy" |
16 | 23 | fullpath = os.path.join(self.hivepath, filename) |
17 | 24 | with open(fullpath, "r") as handle: |
18 | - sexp, trail = parse(handle.read()) | |
19 | - sexp = sexp.canonicalize(self) | |
20 | - self.exprs[atom] = sexp | |
21 | - print "Loaded", atom, sexp | |
22 | - return sexp | |
25 | + try: | |
26 | + sexp, trail = parse(handle.read()) | |
27 | + sexp = sexp.canonicalize(self) | |
28 | + self.exprs[atom] = sexp | |
29 | + print("Loaded", atom, sexp) | |
30 | + return sexp | |
31 | + except IOError: | |
32 | + raise MissingAtom(atom) |
@@ -1,12 +1,12 @@ | ||
1 | 1 | BASIS_ATOMS = ( |
2 | 2 | "id", "ignore", "fst", "snd", "left", "right", |
3 | - "zero", "succ", "nil", "cons", "t", "f", "not", "conj", "disj", | |
3 | + "zero", "succ", "nil", "cons", "t", "f", "not", "conj", "disj", "either", | |
4 | 4 | "f-zero", "f-one", "f-pi", "f-sign", "f-floor", "f-negate", "f-recip", |
5 | 5 | "f-lt", "f-add", "f-mul", "f-sqrt", "f-sin", "f-cos", "f-atan2" |
6 | 6 | ) |
7 | 7 | |
8 | 8 | BASIS_FUNCTORS = ( |
9 | - "comp", "pair", "case", "curry", "uncurry", "pr", "fold", "either", | |
9 | + "comp", "pair", "case", "curry", "uncurry", "pr", "fold", | |
10 | 10 | ) |
11 | 11 | |
12 | 12 |
@@ -2,4 +2,4 @@ | ||
2 | 2 | let |
3 | 3 | makeBuilder = import ./builder.nix { inherit nixpkgs; }; |
4 | 4 | in |
5 | - makeBuilder ./draw.py "cammy-draw" true | |
5 | + makeBuilder ./draw.py "cammy-draw" false # true |
@@ -0,0 +1,5 @@ | ||
1 | +{ nixpkgs ? import <nixpkgs> {} }: | |
2 | +let | |
3 | + makeBuilder = import ./builder.nix { inherit nixpkgs; }; | |
4 | +in | |
5 | + makeBuilder ./frame.py "cammy-frame" false |
@@ -0,0 +1,24 @@ | ||
1 | +from rpython.rlib.rfile import create_stdio | |
2 | + | |
3 | +from cammylib.hive import Hive, MissingAtom | |
4 | +from cammylib.parser import parse | |
5 | + | |
6 | + | |
7 | +def main(argv): | |
8 | + hivepath = argv[-1] | |
9 | + hive = Hive(hivepath) | |
10 | + stdin, stdout, stderr = create_stdio() | |
11 | + try: | |
12 | + sexp, trail = parse(stdin.read()) | |
13 | + stdout.write(sexp.canonicalize(hive).asStr()) | |
14 | + stdout.write("\n") | |
15 | + return 0 | |
16 | + except MissingAtom as ma: | |
17 | + stderr.write("Missing atom in hive: ") | |
18 | + stderr.write(ma.atom) | |
19 | + stderr.write("\n") | |
20 | + return 1 | |
21 | + | |
22 | +def target(driver, *args): | |
23 | + driver.exe_name = "cammy-frame" | |
24 | + return main, None |
@@ -3,7 +3,7 @@ | ||
3 | 3 | from rpython.rlib.rfile import create_stdio |
4 | 4 | |
5 | 5 | from cammylib.arrows import buildArrow, BuildProblem |
6 | -from cammylib.hive import Hive | |
6 | +from cammylib.hive import Hive, MissingAtom | |
7 | 7 | from cammylib.parser import parse |
8 | 8 | |
9 | 9 | LINE_BUFFER_LENGTH = 1024 |
@@ -19,8 +19,11 @@ def repl(hive, stdin, stdout): | ||
19 | 19 | print "Got:", line |
20 | 20 | print "S-expression:", sexp.asStr() |
21 | 21 | print "Trail:", trail |
22 | - sexp = sexp.canonicalize(hive) | |
23 | - print "Canonicalized:", sexp.asStr() | |
22 | + try: | |
23 | + sexp = sexp.canonicalize(hive) | |
24 | + print "Canonicalized:", sexp.asStr() | |
25 | + except MissingAtom as ma: | |
26 | + print "Couldn't canonicalize S-expression; missing atom:", ma.atom | |
24 | 27 | try: |
25 | 28 | arrow = buildArrow(sexp) |
26 | 29 | print "Arrow:", arrow |
@@ -32,10 +35,7 @@ def main(argv): | ||
32 | 35 | hivepath = argv[-1] |
33 | 36 | hive = Hive(hivepath) |
34 | 37 | stdin, stdout, stderr = create_stdio() |
35 | - try: | |
36 | - return repl(hive, stdin, stdout) | |
37 | - except: | |
38 | - return 1 | |
38 | + return repl(hive, stdin, stdout) | |
39 | 39 | |
40 | 40 | def target(driver, *args): |
41 | 41 | driver.exe_name = "cammy-repl" |
@@ -5,6 +5,7 @@ let | ||
5 | 5 | jelly = (import jelly/Cargo.nix { pkgs = nixpkgs; }).rootCrate.build; |
6 | 6 | movelist = import ./movelist { inherit nixpkgs; }; |
7 | 7 | cammy-draw = import ./cammy-rpy/draw.nix { inherit nixpkgs; }; |
8 | + cammy-frame = import ./cammy-rpy/frame.nix { inherit nixpkgs; }; | |
8 | 9 | cammy-repl = import ./cammy-rpy/repl.nix { inherit nixpkgs; }; |
9 | 10 | eggs = builtins.attrValues (removeAttrs (import ./eggs.nix { |
10 | 11 | inherit pkgs; |
@@ -47,6 +48,7 @@ in pkgs.stdenv.mkDerivation { | ||
47 | 48 | makeWrapper ${movelist}/bin/movelist $out/bin/cammy-movelist |
48 | 49 | |
49 | 50 | makeWrapper ${cammy-draw}/bin/cammy-draw $out/bin/cammy-draw |
51 | + makeWrapper ${cammy-frame}/bin/cammy-frame $out/bin/cammy-frame | |
50 | 52 | makeWrapper ${cammy-repl}/bin/cammy-repl $out/bin/cammy-repl |
51 | 53 | ''; |
52 | 54 | } |