A categorical programming language
修订版 | 1f5c0f0be8af91291b969d522bcbdc25add99369 (tree) |
---|---|
时间 | 2022-02-17 10:51:25 |
作者 | Corbin <cds@corb...> |
Commiter | Corbin |
Refactor RPython Cammy modules.
We will be implementing several tools in RPython, so we need to share
the code while still keeping the Nix expressions clean and minimal. I
don't want to rebuild the JIT when I'm only changing the REPL.
@@ -0,0 +1,52 @@ | ||
1 | +{ nixpkgs ? import <nixpkgs> {} }: | |
2 | +let | |
3 | + inherit (nixpkgs.pkgs) | |
4 | + fetchFromGitLab stdenv | |
5 | + pkg-config libffi stb | |
6 | + pypy pypyPackages; | |
7 | + # https://foss.heptapod.net/pypy/pypy/ | |
8 | + pypySrc = fetchFromGitLab { | |
9 | + domain = "foss.heptapod.net"; | |
10 | + owner = "pypy"; | |
11 | + repo = "pypy"; | |
12 | + # release candidate from branch release-pypy3.8-v7.x | |
13 | + rev = "90fd9ed34d52181de59cbfff863719472b05418e"; | |
14 | + sha256 = "03cshgvh8qcsyac4q4vf0sbvcm1m2ikgwycwip4cc7sw9pzpw6a3"; | |
15 | + }; | |
16 | +in | |
17 | +entrypoint: exe: enableJIT: | |
18 | + let | |
19 | + opt = if enableJIT then "jit" else "2"; | |
20 | + in stdenv.mkDerivation { | |
21 | + name = exe; | |
22 | + | |
23 | + src = builtins.filterSource | |
24 | + (path: type: baseNameOf path == entrypoint | |
25 | + || builtins.match ".*cammylib.*" path != null) | |
26 | + ./.; | |
27 | + | |
28 | + buildInputs = [ | |
29 | + # always required | |
30 | + pypySrc pkg-config stb | |
31 | + pypy pypyPackages.py | |
32 | + # only required for JIT | |
33 | + pypyPackages.pytest libffi | |
34 | + ]; | |
35 | + | |
36 | + buildPhase = '' | |
37 | + source $stdenv/setup | |
38 | + mkdir -p ./rpython/_cache | |
39 | + cp -r ${pypySrc}/rpython . | |
40 | + chmod -R u+w rpython/ | |
41 | + # Do the actual translation. | |
42 | + ${pypy}/bin/pypy -mrpython -O${opt} ${entrypoint} | |
43 | + ''; | |
44 | + | |
45 | + installPhase = '' | |
46 | + mkdir -p $out/bin/ | |
47 | + cp ${exe} $out/bin/ | |
48 | + ''; | |
49 | + | |
50 | + separateDebugInfo = true; | |
51 | + } | |
52 | + |
@@ -0,0 +1,26 @@ | ||
1 | +from ctypes import util | |
2 | + | |
3 | +ctypes_is_patched = False | |
4 | + | |
5 | +def patch_ctypes_for_ffi(): | |
6 | + """ | |
7 | + Monkey-patch the stdlib so that ctypes uses the generic name for libc | |
8 | + instead of a full path. This is required to make rffi work. | |
9 | + | |
10 | + This code was borrowed from Typhon. | |
11 | + | |
12 | + NOT_RPYTHON | |
13 | + """ | |
14 | + | |
15 | + global ctypes_is_patched | |
16 | + if ctypes_is_patched: | |
17 | + return | |
18 | + ctypes_is_patched = True | |
19 | + | |
20 | + fl = util.find_library | |
21 | + def patched_find_library(name): | |
22 | + if name == "c": | |
23 | + return "libc.so.6" | |
24 | + else: | |
25 | + return fl(name) | |
26 | + util.find_library = patched_find_library |
@@ -0,0 +1,5 @@ | ||
1 | +{ nixpkgs ? import <nixpkgs> {} }: | |
2 | +let | |
3 | + makeBuilder = import ./builder.nix { inherit nixpkgs; }; | |
4 | +in | |
5 | + makeBuilder ./main.py "cammy-run" true |
@@ -1,12 +1,3 @@ | ||
1 | -# Monkey-patch the stdlib. Borrowed from Typhon. | |
2 | -from ctypes import util | |
3 | -fl = util.find_library | |
4 | -def patched_find_library(name): | |
5 | - if name == "c": | |
6 | - return "libc.so.6" | |
7 | - else: | |
8 | - return fl(name) | |
9 | -util.find_library = patched_find_library | |
10 | 1 | |
11 | 2 | import math, sys |
12 | 3 |
@@ -15,7 +6,8 @@ from rpython.rlib.rbigint import rbigint | ||
15 | 6 | from rpython.rlib.rfloat import string_to_float |
16 | 7 | from rpython.rlib.rstring import StringBuilder, split |
17 | 8 | |
18 | -import stb | |
9 | +from cammylib.hax import patch_ctypes_for_ffi | |
10 | +from cammylib import stb | |
19 | 11 | |
20 | 12 | class TypeFail(Exception): |
21 | 13 | def __init__(self, reason): |
@@ -554,6 +546,7 @@ def main(argv): | ||
554 | 546 | |
555 | 547 | |
556 | 548 | def target(driver, *args): |
549 | + patch_ctypes_for_ffi() | |
557 | 550 | driver.exe_name = "cammy-run" |
558 | 551 | return main, None |
559 | 552 |
@@ -0,0 +1,5 @@ | ||
1 | +{ nixpkgs ? import <nixpkgs> {} }: | |
2 | +let | |
3 | + makeBuilder = import ./builder.nix { inherit nixpkgs; }; | |
4 | +in | |
5 | + makeBuilder ./repl.py "cammy-repl" false |
@@ -0,0 +1,22 @@ | ||
1 | +# Inspired by https://www.pypy.org/posts/2018/11/guest-post-implementing-calculator-repl-6271483514675006846.html | |
2 | + | |
3 | +from rpython.rlib.rfile import create_stdio | |
4 | + | |
5 | +LINE_BUFFER_LENGTH = 1024 | |
6 | + | |
7 | +def repl(stdin, stdout): | |
8 | + while True: | |
9 | + stdout.write("> ") | |
10 | + line = stdin.readline(LINE_BUFFER_LENGTH) | |
11 | + print line | |
12 | + | |
13 | +def main(argv): | |
14 | + stdin, stdout, stderr = create_stdio() | |
15 | + try: | |
16 | + repl(stdin, stdout) | |
17 | + except: | |
18 | + return 0 | |
19 | + | |
20 | +def target(driver, *args): | |
21 | + driver.exe_name = "cammy-repl" | |
22 | + return main, None |
@@ -1,42 +0,0 @@ | ||
1 | -{ nixpkgs ? import <nixpkgs> {} }: | |
2 | -let | |
3 | - inherit (nixpkgs.pkgs) | |
4 | - fetchFromGitLab stdenv | |
5 | - pypy pypyPackages pkg-config libffi stb; | |
6 | - # https://foss.heptapod.net/pypy/pypy/ | |
7 | - pypySrc = fetchFromGitLab { | |
8 | - domain = "foss.heptapod.net"; | |
9 | - owner = "pypy"; | |
10 | - repo = "pypy"; | |
11 | - # release candidate from branch release-pypy3.8-v7.x | |
12 | - rev = "90fd9ed34d52181de59cbfff863719472b05418e"; | |
13 | - sha256 = "03cshgvh8qcsyac4q4vf0sbvcm1m2ikgwycwip4cc7sw9pzpw6a3"; | |
14 | - }; | |
15 | -in | |
16 | -stdenv.mkDerivation { | |
17 | - name = "cammy-run"; | |
18 | - | |
19 | - src = ./.; | |
20 | - | |
21 | - buildInputs = [ | |
22 | - # always required | |
23 | - pypy pypyPackages.py pypySrc pkg-config stb | |
24 | - # only required for JIT | |
25 | - pypyPackages.pytest libffi ]; | |
26 | - | |
27 | - buildPhase = '' | |
28 | - source $stdenv/setup | |
29 | - mkdir -p ./rpython/_cache | |
30 | - cp -r ${pypySrc}/rpython . | |
31 | - chmod -R u+w rpython/ | |
32 | - # Do the actual translation. | |
33 | - ${pypy}/bin/pypy -mrpython -Ojit main.py | |
34 | - ''; | |
35 | - | |
36 | - installPhase = '' | |
37 | - mkdir -p $out/bin/ | |
38 | - cp cammy-run $out/bin/ | |
39 | - ''; | |
40 | - | |
41 | - separateDebugInfo = true; | |
42 | -} |
@@ -4,7 +4,8 @@ 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-run = import ./cammy-run { inherit nixpkgs; }; | |
7 | + cammy-run = import ./cammy-rpy { inherit nixpkgs; }; | |
8 | + cammy-repl = import ./cammy-rpy/repl.nix { inherit nixpkgs; }; | |
8 | 9 | eggs = builtins.attrValues (removeAttrs (import ./eggs.nix { |
9 | 10 | inherit pkgs; |
10 | 11 | stdenv = pkgs.stdenv; |
@@ -46,5 +47,6 @@ in pkgs.stdenv.mkDerivation { | ||
46 | 47 | makeWrapper ${movelist}/bin/movelist $out/bin/cammy-movelist |
47 | 48 | |
48 | 49 | makeWrapper ${cammy-run}/bin/cammy-run $out/bin/cammy-draw |
50 | + makeWrapper ${cammy-repl}/bin/cammy-repl $out/bin/cammy-repl | |
49 | 51 | ''; |
50 | 52 | } |
@@ -0,0 +1 @@ | ||
1 | +(pair id @0) |
@@ -40,3 +40,7 @@ | ||
40 | 40 | * Monad: X → [N, X] |
41 | 41 | * Distributive law: [N, X] + 1 → [N, X + 1] |
42 | 42 | * Creates a stream+maybe monad: X → [N, X + 1] |
43 | +* Subtypes? | |
44 | + * Interval type? | |
45 | +* Cantor's type of bitstrings: N -> 2 | |
46 | +* The square type: For a type X, 2 -> X |