• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-cqtcocoa誰得pythonphprubygameguibathyscaphec翻訳計画中(planning stage)omegatframeworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

A categorical programming language


Commit MetaInfo

修订版1f5c0f0be8af91291b969d522bcbdc25add99369 (tree)
时间2022-02-17 10:51:25
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

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.

更改概述

差异

--- /dev/null
+++ b/cammy-rpy/builder.nix
@@ -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+
--- /dev/null
+++ b/cammy-rpy/cammylib/hax.py
@@ -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
--- /dev/null
+++ b/cammy-rpy/default.nix
@@ -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
--- a/cammy-run/main.py
+++ b/cammy-rpy/main.py
@@ -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
101
112 import math, sys
123
@@ -15,7 +6,8 @@ from rpython.rlib.rbigint import rbigint
156 from rpython.rlib.rfloat import string_to_float
167 from rpython.rlib.rstring import StringBuilder, split
178
18-import stb
9+from cammylib.hax import patch_ctypes_for_ffi
10+from cammylib import stb
1911
2012 class TypeFail(Exception):
2113 def __init__(self, reason):
@@ -554,6 +546,7 @@ def main(argv):
554546
555547
556548 def target(driver, *args):
549+ patch_ctypes_for_ffi()
557550 driver.exe_name = "cammy-run"
558551 return main, None
559552
--- /dev/null
+++ b/cammy-rpy/repl.nix
@@ -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
--- /dev/null
+++ b/cammy-rpy/repl.py
@@ -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
--- a/cammy-run/default.nix
+++ /dev/null
@@ -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-}
--- a/default.nix
+++ b/default.nix
@@ -4,7 +4,8 @@ let
44 frame = import ./frame { inherit nixpkgs; };
55 jelly = (import jelly/Cargo.nix { pkgs = nixpkgs; }).rootCrate.build;
66 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; };
89 eggs = builtins.attrValues (removeAttrs (import ./eggs.nix {
910 inherit pkgs;
1011 stdenv = pkgs.stdenv;
@@ -46,5 +47,6 @@ in pkgs.stdenv.mkDerivation {
4647 makeWrapper ${movelist}/bin/movelist $out/bin/cammy-movelist
4748
4849 makeWrapper ${cammy-run}/bin/cammy-run $out/bin/cammy-draw
50+ makeWrapper ${cammy-repl}/bin/cammy-repl $out/bin/cammy-repl
4951 '';
5052 }
--- /dev/null
+++ b/hive/fun/graph.cammy
@@ -0,0 +1 @@
1+(pair id @0)
--- a/todo.txt
+++ b/todo.txt
@@ -40,3 +40,7 @@
4040 * Monad: X → [N, X]
4141 * Distributive law: [N, X] + 1 → [N, X + 1]
4242 * 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