2 This module deals with interpreting the parse tree as Python
3 would have done, in the compiler.
5 For now this only covers parse tree to value conversion of
10 from ExprNodes
import *
11 from Errors
import CompileError
14 class EmptyScope(object):
15 def lookup(self
, name
):
18 empty_scope
= EmptyScope()
20 def interpret_compiletime_options(optlist
, optdict
, type_env
=None, type_args
=()):
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).
27 optlist is a list of nodes, while optdict is a DictNode (the
28 result optdict is a dict)
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.
34 A CompileError will be raised if there are problems.
37 def interpret(node
, ix
):
40 type = node
.analyse_as_type(type_env
)
42 raise CompileError(node
.pos
, "Invalid type.")
43 return (type, node
.pos
)
45 raise CompileError(node
.pos
, "Type not allowed here.")
47 if (sys
.version_info
[0] >=3 and
48 isinstance(node
, StringNode
) and
49 node
.unicode_value
is not None):
50 return (node
.unicode_value
, node
.pos
)
51 return (node
.compile_time_value(empty_scope
), node
.pos
)
54 optlist
= [interpret(x
, ix
) for ix
, x
in enumerate(optlist
)]
56 assert isinstance(optdict
, DictNode
)
58 for item
in optdict
.key_value_pairs
:
59 new_key
, dummy
= interpret(item
.key
, None)
60 new_optdict
[new_key
] = interpret(item
.value
, item
.key
.value
)
62 return (optlist
, new_optdict
)