cython

Форк
0
/
Interpreter.py 
57 строк · 1.8 Кб
1
"""
2
This module deals with interpreting the parse tree as Python
3
would have done, in the compiler.
4

5
For now this only covers parse tree to value conversion of
6
compile-time values.
7
"""
8

9

10
from .ExprNodes import DictNode
11
from .Errors import CompileError
12

13

14
class EmptyScope:
15
    def lookup(self, name):
16
        return None
17

18
empty_scope = EmptyScope()
19

20
def interpret_compiletime_options(optlist, optdict, type_env=None, type_args=()):
21
    """
22
    Tries to interpret a list of compile time option nodes.
23
    The result will be a tuple (optlist, optdict) but where
24
    all expression nodes have been interpreted. The result is
25
    in the form of tuples (value, pos).
26

27
    optlist is a list of nodes, while optdict is a DictNode (the
28
    result optdict is a dict)
29

30
    If type_env is set, all type nodes will be analysed and the resulting
31
    type set. Otherwise only interpretateable ExprNodes
32
    are allowed, other nodes raises errors.
33

34
    A CompileError will be raised if there are problems.
35
    """
36

37
    def interpret(node, ix):
38
        if ix in type_args:
39
            if type_env:
40
                type = node.analyse_as_type(type_env)
41
                if not type:
42
                    raise CompileError(node.pos, "Invalid type.")
43
                return (type, node.pos)
44
            else:
45
                raise CompileError(node.pos, "Type not allowed here.")
46
        return (node.compile_time_value(empty_scope), node.pos)
47

48
    if optlist:
49
        optlist = [interpret(x, ix) for ix, x in enumerate(optlist)]
50
    if optdict:
51
        assert isinstance(optdict, DictNode)
52
        new_optdict = {}
53
        for item in optdict.key_value_pairs:
54
            new_key, dummy = interpret(item.key, None)
55
            new_optdict[new_key] = interpret(item.value, item.key.value)
56
        optdict = new_optdict
57
    return (optlist, new_optdict)
58

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.