A categorical programming language
修订版 | 0cb2292a98b637165644cd19cf47133202a2dd79 (tree) |
---|---|
时间 | 2022-02-28 13:57:22 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Call jelly from RPython.
That was a headache, but it appears to work.
@@ -1,4 +1,5 @@ | ||
1 | -{ nixpkgs ? import <nixpkgs> {} }: | |
1 | +entrypoint: exe: enableJIT: | |
2 | +{ nixpkgs ? import <nixpkgs> {}, jelly ? null }: | |
2 | 3 | let |
3 | 4 | inherit (nixpkgs.pkgs) |
4 | 5 | fetchFromGitLab stdenv |
@@ -14,7 +15,6 @@ let | ||
14 | 15 | sha256 = "03cshgvh8qcsyac4q4vf0sbvcm1m2ikgwycwip4cc7sw9pzpw6a3"; |
15 | 16 | }; |
16 | 17 | in |
17 | -entrypoint: exe: enableJIT: | |
18 | 18 | let |
19 | 19 | opt = if enableJIT then "jit" else "2"; |
20 | 20 | in stdenv.mkDerivation { |
@@ -33,6 +33,8 @@ entrypoint: exe: enableJIT: | ||
33 | 33 | pypyPackages.pytest libffi |
34 | 34 | ]; |
35 | 35 | |
36 | + JELLY_PATH = "${jelly}/bin/jelly"; | |
37 | + | |
36 | 38 | buildPhase = '' |
37 | 39 | source $stdenv/setup |
38 | 40 | mkdir -p ./rpython/_cache |
@@ -0,0 +1,33 @@ | ||
1 | +import os | |
2 | + | |
3 | +from rpython.rlib.rfile import create_fdopen_rfile | |
4 | + | |
5 | +from cammylib.parser import parse | |
6 | + | |
7 | +jelly_path = os.environ["JELLY_PATH"] | |
8 | + | |
9 | +def jellify(sexp): | |
10 | + """ | |
11 | + Optimize a Cammy expression. | |
12 | + """ | |
13 | + | |
14 | + rin, win = os.pipe() | |
15 | + rout, wout = os.pipe() | |
16 | + # NB: We must close all four pipe FDs in both processes. | |
17 | + pid = os.fork() | |
18 | + if pid: | |
19 | + os.close(rin) | |
20 | + with create_fdopen_rfile(win, mode="w") as handle: | |
21 | + handle.write(sexp.asStr()) | |
22 | + os.close(wout) | |
23 | + with create_fdopen_rfile(rout, mode="r") as handle: | |
24 | + # NB: jelly doesn't return a trail. | |
25 | + return parse(handle.read())[0] | |
26 | + else: | |
27 | + os.dup2(rin, 0) | |
28 | + os.dup2(wout, 1) | |
29 | + os.close(rin) | |
30 | + os.close(win) | |
31 | + os.close(rout) | |
32 | + os.close(wout) | |
33 | + os.execv(jelly_path, ["jelly"]) |
@@ -1,5 +1,4 @@ | ||
1 | -{ nixpkgs ? import <nixpkgs> {} }: | |
2 | 1 | let |
3 | - makeBuilder = import ./builder.nix { inherit nixpkgs; }; | |
2 | + makeBuilder = import ./builder.nix; | |
4 | 3 | in |
5 | 4 | makeBuilder ./draw.py "cammy-draw" false # true |
@@ -1,5 +1,4 @@ | ||
1 | -{ nixpkgs ? import <nixpkgs> {} }: | |
2 | 1 | let |
3 | - makeBuilder = import ./builder.nix { inherit nixpkgs; }; | |
2 | + makeBuilder = import ./builder.nix; | |
4 | 3 | in |
5 | 4 | makeBuilder ./frame.py "cammy-frame" false |
@@ -1,5 +1,4 @@ | ||
1 | -{ nixpkgs ? import <nixpkgs> {} }: | |
2 | 1 | let |
3 | - makeBuilder = import ./builder.nix { inherit nixpkgs; }; | |
2 | + makeBuilder = import ./builder.nix; | |
4 | 3 | in |
5 | 4 | makeBuilder ./repl.py "cammy-repl" false |
@@ -7,6 +7,7 @@ from rpython.rlib.rfile import create_stdio | ||
7 | 7 | |
8 | 8 | from cammylib.arrows import buildArrow, BuildProblem |
9 | 9 | from cammylib.hive import Hive, MissingAtom |
10 | +from cammylib.jelly import jellify | |
10 | 11 | from cammylib.parser import parse |
11 | 12 | from cammylib.types import ConstraintStore, TypeExtractor, UnificationFailed |
12 | 13 |
@@ -37,6 +38,8 @@ def repl(hive, stdin, stdout): | ||
37 | 38 | print "Canonicalized:", sexp.asStr() |
38 | 39 | except MissingAtom as ma: |
39 | 40 | print "Couldn't canonicalize S-expression; missing atom:", ma.atom |
41 | + sexp = jellify(sexp) | |
42 | + print "Jellified:", sexp.asStr() | |
40 | 43 | try: |
41 | 44 | arrow = buildArrow(sexp) |
42 | 45 | print "Arrow:", arrow |
@@ -1,5 +1,4 @@ | ||
1 | -{ nixpkgs ? import <nixpkgs> {} }: | |
2 | 1 | let |
3 | - makeBuilder = import ./builder.nix { inherit nixpkgs; }; | |
2 | + makeBuilder = import ./builder.nix; | |
4 | 3 | in |
5 | 4 | makeBuilder ./weave.py "cammy-weave" false |
@@ -4,10 +4,10 @@ let | ||
4 | 4 | frame = import ./frame { inherit nixpkgs; }; |
5 | 5 | jelly = (import jelly/Cargo.nix { pkgs = nixpkgs; }).rootCrate.build; |
6 | 6 | movelist = import ./movelist { inherit nixpkgs; }; |
7 | - cammy-draw = import ./cammy-rpy/draw.nix { inherit nixpkgs; }; | |
8 | - cammy-frame = import ./cammy-rpy/frame.nix { inherit nixpkgs; }; | |
9 | - cammy-repl = import ./cammy-rpy/repl.nix { inherit nixpkgs; }; | |
10 | - cammy-weave = import ./cammy-rpy/weave.nix { inherit nixpkgs; }; | |
7 | + cammy-draw = import ./cammy-rpy/draw.nix { inherit nixpkgs jelly; }; | |
8 | + cammy-frame = import ./cammy-rpy/frame.nix { inherit nixpkgs jelly; }; | |
9 | + cammy-repl = import ./cammy-rpy/repl.nix { inherit nixpkgs jelly; }; | |
10 | + cammy-weave = import ./cammy-rpy/weave.nix { inherit nixpkgs jelly; }; | |
11 | 11 | eggs = builtins.attrValues (removeAttrs (import ./eggs.nix { |
12 | 12 | inherit pkgs; |
13 | 13 | stdenv = pkgs.stdenv; |
@@ -1,18 +1,17 @@ | ||
1 | 1 | * Literate Cammy |
2 | - * RPython modules will go in cammy-rpy/cammylib/ | |
3 | - * RPython entrypoints will go in cammy-rpy/ | |
4 | - * Write filterSource expressions to grab only one entrypoint + modules | |
5 | - * cammy-rpy/builder.nix | |
6 | - * Nix expressions also go in cammy-rpy/ next to entrypoints | |
7 | 2 | * Entrypoints: |
8 | - * cammy-repl $HIVE: REPL session with a hive | |
9 | - * cammy-weave $HIVE: document a hive | |
3 | + - cammy-repl $HIVE: REPL session with a hive | |
4 | + - cammy-weave $HIVE: document a hive | |
10 | 5 | * cammy-tangle $HIVE: take an expression on stdin, return framed optimized |
11 | 6 | expression on stdout |
12 | 7 | * cammy-frame $HIVE | jelly |
13 | - * cammy-draw ...: take an expression and canvas params, make a PNG | |
14 | - * cammy-frame $HIVE: take an expression on stdin, return framed expression | |
8 | + - cammy-draw ...: take an expression and canvas params, make a PNG | |
9 | + - cammy-frame $HIVE: take an expression on stdin, return framed expression | |
15 | 10 | on stdout |
11 | + * cammy-jelly $EXPR: print an optimized expression on stdout | |
12 | + * $EXPR is a path! | |
13 | + * This is wholly due to RPython issues | |
14 | + * When $EXPR is "-", read from stdin | |
16 | 15 | * REPL improvements |
17 | 16 | * allow mid-functor newlines |
18 | 17 | * jellification |
@@ -58,3 +57,4 @@ | ||
58 | 57 | * Moore machines: For state type Q and input type S, Q × S -> Q |
59 | 58 | * Moore transducers: [Q × S, Q] -> [Q × S, Q] |
60 | 59 | * Mealy machines: For state type Q, input type S, and output type L, Q × S -> Q × L |
60 | +* Jets |