OILS / opy / misc / py_ast.py View on Github | oilshell.org

65 lines, 30 significant
1"""
2Wrapper for Python.asdl.
3"""
4
5import os
6import sys
7
8from asdl import py_meta
9from asdl import asdl_ as asdl
10
11# Dummy types for now
12class Identifier:
13 pass
14
15class Bytes:
16 pass
17
18class PyObject:
19 pass
20
21class Constant:
22 pass
23
24class Singleton:
25 pass
26
27
28 # TODO: Fill these in:
29 #
30 # Num(object n) -- a number as a PyObject.
31 # Bytes(bytes s)
32 # Constant(constant value)
33 # NameConstant(singleton value)
34 #
35 # singleton: None, True or False
36 # constant can be None, whereas None means "no value" for object.
37 #
38 # Hm do I want an LST? Then it shouldn't have these typed values? That
39 # comes later?
40 #
41 # identifier: this one is used a lot. Why not string?
42
43def _ParseAndMakeTypes(schema_path, root):
44 module = asdl.parse(schema_path)
45
46 app_types = {
47 'identifier': asdl.UserType(Identifier),
48 'bytes': asdl.UserType(Bytes),
49 'object': asdl.UserType(PyObject),
50 'constant': asdl.UserType(Constant),
51 'singleton': asdl.UserType(Singleton),
52 }
53
54 # Check for type errors
55 if not asdl.check(module, app_types):
56 raise AssertionError('ASDL file is invalid')
57 py_meta.MakeTypes(module, root, app_types)
58
59
60bin_dir = os.path.dirname(os.path.abspath(sys.argv[0])) # ~/git/oil/bin
61schema_path = os.path.join(bin_dir, '../foil/Python.asdl') # ~/git/oil/osh
62
63root = sys.modules[__name__]
64
65_ParseAndMakeTypes(schema_path, root)