Skip to content

Commit 11947af

Browse files
committed
add option data-format for user-specified data format IR
1 parent 0cad6af commit 11947af

File tree

7 files changed

+96
-55
lines changed

7 files changed

+96
-55
lines changed

purescripto/configure.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
- .pure-py/
1414
"""
1515
from importlib import import_module
16-
from typing import Dict, List, Iterable, cast
16+
from typing import Dict, List, Iterable
1717
from subprocess import check_call
1818
from distutils.dir_util import copy_tree
1919
from purescripto.configure_consts import *
@@ -24,11 +24,11 @@
2424
import wisepy2
2525

2626
_TEMPLATE = {
27-
CKey.CoreFnDir: CValue.CoreFnDir,
27+
CKey.CoreFnDir : CValue.CoreFnDir,
2828
CKey.EntryModule: CValue.EntryModule,
29-
CKey.BluePrint: CValue.BluePrint,
29+
CKey.BluePrint : CValue.BluePrint,
3030
CKey.IndexMirror: CValue.IndexMirror,
31-
CKey.IsPrettyPrint: CValue.IsPrettyPrint,
31+
CKey.DataFormat : CValue.DataFormat,
3232
}
3333

3434

@@ -41,7 +41,7 @@ def mk_ps_blueprint_cmd(
4141
python_pack_name: str,
4242
entry: str,
4343
ffi_deps_path: str,
44-
is_pretty_print: bool,
44+
format: str,
4545
):
4646
return [
4747
pspy_blueprint,
@@ -51,8 +51,8 @@ def mk_ps_blueprint_cmd(
5151
entry,
5252
"--ffi-dep",
5353
ffi_deps_path,
54-
"--out-pretty",
55-
str(is_pretty_print),
54+
"--out-format",
55+
format,
5656
]
5757

5858

@@ -171,15 +171,15 @@ def pspy(
171171
conf_dict.setdefault(CKey.PyPack, py_pack_name_default)
172172
conf_dict.setdefault(CKey.CoreFnDir, CValue.CoreFnDir)
173173
conf_dict.setdefault(CKey.EntryModule, CValue.EntryModule)
174-
conf_dict.setdefault(CKey.IsPrettyPrint, CValue.IsPrettyPrint)
174+
conf_dict.setdefault(CKey.DataFormat, CValue.DataFormat)
175175

176176
conf = CValue()
177177
conf.IndexMirror = conf_dict[CKey.IndexMirror]
178178
conf.BluePrint = conf_dict[CKey.BluePrint]
179179
conf.PyPack = conf_dict[CKey.PyPack]
180180
conf.CoreFnDir = conf_dict[CKey.CoreFnDir]
181181
conf.EntryModule = conf_dict[CKey.EntryModule]
182-
conf.IsPrettyPrint = cast(bool, conf_dict[CKey.IsPrettyPrint])
182+
conf.DataFormat = conf_dict[CKey.DataFormat]
183183

184184
# TODO: currently unused.
185185
# support custom corefn output path in pspy-blueprint.
@@ -209,7 +209,7 @@ def pspy(
209209
conf.PyPack,
210210
conf.EntryModule,
211211
str(ffi_deps_file),
212-
conf.IsPrettyPrint,
212+
conf.DataFormat,
213213
)
214214
try:
215215
check_call(cmd)

purescripto/configure_consts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class CKey:
77
PyPack = "python-package"
88
CoreFnDir = "corefn-dir"
99
EntryModule = "entry-module"
10-
IsPrettyPrint = "pretty-print?"
10+
DataFormat = "data-format"
1111

1212

1313
class CValue:
@@ -16,7 +16,7 @@ class CValue:
1616
PyPack = "python"
1717
CoreFnDir = "output"
1818
EntryModule = "Main"
19-
IsPrettyPrint = False
19+
DataFormat = "Compressed"
2020

2121

2222
STR_PSPY_BLUEPRINT_CMD = "pspy-blueprint"

purescripto/purescript_loader.py

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,25 @@
22
from typing import Union, Tuple
33
from types import CodeType
44
from pie import LoaderForBetterLife
5-
from importlib import import_module
6-
from purescripto import rts
7-
from purescripto.utilities import import_from_path
8-
from purescripto.workaround import suppress_cpy38_literal_is, workaround
95
from py_sexpr.stack_vm.emit import module_code
106
from purescripto.topdown import load_topdown
7+
from purescripto.rts import META_ENV, RTS_TEMPLATE
118
import marshal
129
import zipfile
1310
import io
14-
import functools
15-
16-
RES = "res"
17-
"""generated code object is stored in global variable $RES"""
18-
19-
20-
@functools.lru_cache()
21-
def _import_module_to_dict(m: str):
22-
with suppress_cpy38_literal_is():
23-
package, _, module = m.rpartition(".")
24-
entry_path = import_module(package).__file__
25-
loc = entry_path[: -len("__init__.py")] + module + ".py"
26-
return import_from_path(m, loc).__dict__
27-
28-
29-
RTS_TEMPLATE = {
30-
"zfsr32": rts.zfsr32,
31-
"Error": Exception,
32-
"import_module": _import_module_to_dict,
33-
}
34-
35-
36-
def _META_ENV():
37-
with workaround():
38-
import py_sexpr.terms as terms
39-
40-
def make_pair(a, b):
41-
return a, b
42-
43-
env = {each: getattr(terms, each) for each in terms.__all__}
44-
env[make_pair.__name__] = make_pair
45-
return env
46-
47-
48-
META_ENV = _META_ENV()
49-
11+
import sys
5012

5113
class LoadPureScriptImplCode(LoaderForBetterLife[CodeType]):
5214
def source_to_prog(self, src: bytes, path: Path) -> CodeType:
53-
15+
sys.setrecursionlimit(3000)
5416
filename = str(path.absolute())
5517
if path.name.endswith(".raw.py"):
5618
meta_code = compile(src, filename, "eval")
5719
sexpr = eval(meta_code, META_ENV)
5820
else:
5921
assert path.name.endswith(".zip.py")
6022
zip = zipfile.ZipFile(filename)
61-
file = io.StringIO(zip.read('source').decode('utf8'))
23+
file = io.StringIO(zip.read("source").decode("utf8"))
6224
sexpr = load_topdown(file, META_ENV)
6325

6426
code = module_code(sexpr, name=self.qualified_name + "$", filename=filename)

purescripto/rts.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,68 @@
1+
from purescripto.utilities import import_from_path
2+
from purescripto.workaround import suppress_cpy38_literal_is, workaround
3+
from importlib import import_module
4+
import functools
5+
from py_sexpr.terms import *
6+
7+
18
def zfsr32(val, n):
29
"""zero fill shift right for 32 bit integers"""
310
return (val >> n) if val >= 0 else ((val + 4294967296) >> n)
11+
12+
13+
__all__ = ["META_ENV", "RTS_TEMPLATE"]
14+
15+
16+
def _META_ENV():
17+
with workaround():
18+
import py_sexpr.terms as terms
19+
20+
def make_pair(a, b):
21+
return a, b
22+
23+
env = {each: getattr(terms, each) for each in terms.__all__}
24+
env[make_pair.__name__] = make_pair
25+
env[get_attr_looper.__name__] = lambda a, b, c: call(
26+
"$" + get_attr_looper.__name__, a, b, c
27+
)
28+
env[get_item_looper.__name__] = lambda a, b, c: call(
29+
"$" + get_item_looper.__name__, a, b, c
30+
)
31+
32+
return env
33+
34+
35+
@functools.lru_cache()
36+
def _import_module_to_dict(m: str):
37+
with suppress_cpy38_literal_is():
38+
package, _, module = m.rpartition(".")
39+
entry_path = import_module(package).__file__
40+
loc = entry_path[: -len("__init__.py")] + module + ".py"
41+
return import_from_path(m, loc).__dict__
42+
43+
44+
def get_item_looper(depth, base, item):
45+
while depth > 0:
46+
base = base[item]
47+
depth -= 1
48+
return base
49+
50+
51+
def get_attr_looper(depth, base, attr):
52+
# TODO: specializer here
53+
while depth > 0:
54+
base = get_attr(base, attr)
55+
depth -= 1
56+
return base
57+
58+
59+
RTS_TEMPLATE = {
60+
"zfsr32": zfsr32,
61+
"Error": Exception,
62+
"import_module": _import_module_to_dict,
63+
("$" + get_attr_looper.__name__): get_attr_looper,
64+
("$" + get_item_looper.__name__): get_item_looper,
65+
}
66+
67+
68+
META_ENV = _META_ENV()

purescripto/topdown.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def make_pair(a, b):
1717

1818
def load_topdown(file_io: TextIO, env):
1919
readline, read = file_io.readline, file_io.read
20+
2021
n_entry = int(readline()[:-1])
2122
ACTION_ATTR = 0
2223
ACTION_APP = 1
@@ -87,7 +88,7 @@ def read_seq(readline=readline, ACTION_SEQ=ACTION_SEQ):
8788
"b": read_bool,
8889
"n": read_nil,
8990
"f": read_float,
90-
"v": read_var,
91+
"v": read_var
9192
}
9293
non_term_maps = {"a": read_acc, "c": read_cons, "l": read_seq}
9394

purescripto/workaround.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ def suppress_cpy38_literal_is():
3434
"ignore", category=SyntaxWarning, message='"is" with a literal'
3535
)
3636
yield
37+
38+

runtests/test_load.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from purescripto.rts import META_ENV
2+
from purescripto.topdown import load_topdown
3+
from py_sexpr.stack_vm.emit import module_code
4+
import zipfile
5+
import io
6+
filename = "C:/Users/twshe/Desktop/mydb/com-haskell/v0.1/purescript-cyberbrain/purescript_cyberbrain/Data/Show/pure.zip.py"
7+
# filename = 'C:\\Users\\twshe\\Desktop\\mydb\\com-haskell\\v0.1\\purescript-cyberbrain\\purescript_cyberbrain\\Cyberbrain\\PyInstructions\\pure.zip.py'
8+
zip = zipfile.ZipFile(filename)
9+
file = io.StringIO(zip.read("source").decode("utf8"))
10+
sexpr = load_topdown(file, META_ENV)
11+
module_code(sexpr)

0 commit comments

Comments
 (0)