Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/python-wasm
Path: blob/main/python/pylang/src/output/literals.py
1398 views
1
# vim:fileencoding=utf-8
2
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
3
from __python__ import hash_literals
4
5
from ast_types import AST_Binary, AST_Number, AST_String, is_node_type
6
7
8
def print_array(self, output):
9
output.print('ρσ_list_decorate')
10
11
def f_list_decorate():
12
def f_list_decorate0():
13
a = self.elements
14
len_ = a.length
15
if len_ > 0:
16
output.space()
17
for i, exp in enumerate(a):
18
if i:
19
output.comma()
20
exp.print(output)
21
if len_ > 0:
22
output.space()
23
24
output.with_square(f_list_decorate0)
25
26
output.with_parens(f_list_decorate)
27
28
29
def print_obj_literal_slow(self, output):
30
def f_obj_literal_slow():
31
output.print('function()')
32
33
def f_obj_literal_slow0():
34
output.indent()
35
if self.is_pydict:
36
output.spaced.apply(output, 'var ρσ_d = ρσ_dict()'.split(' '))
37
else:
38
output.spaced(
39
'var', 'ρσ_d', '=',
40
('Object.create(null)' if self.is_jshash else '{}'))
41
output.end_statement()
42
for i, prop in enumerate(self.properties):
43
output.indent()
44
if self.is_pydict:
45
output.print('ρσ_d.set')
46
47
def f_py_dict():
48
prop.key.print(output)
49
output.print(','), output.space()
50
prop.value.print(output)
51
52
output.with_parens(f_py_dict)
53
else:
54
output.print('ρσ_d')
55
output.with_square(lambda: prop.key.print(output))
56
output.space(), output.print('='), output.space()
57
prop.value.print(output)
58
output.end_statement()
59
output.indent()
60
output.spaced('return', 'ρσ_d')
61
output.end_statement()
62
63
output.with_block(f_obj_literal_slow0)
64
65
output.with_parens(f_obj_literal_slow)
66
output.print('.call(this)')
67
68
69
# This simple obj literal printer works fine for literals
70
# that aren't supposed to be dict's, I think. The function
71
# print_obj_literal_slow above was what RapydScript had,
72
# but in running the brython benchmarks, I found it was
73
# insanely slow, since e.g., {[0]:0} is 100x slower than
74
# {0:0} in javascript.
75
def print_obj_literal(self, output):
76
if self.is_pydict:
77
print_obj_literal_slow(self, output)
78
return
79
output.print("{")
80
for i, prop in enumerate(self.properties):
81
if is_node_type(prop.key, AST_Number) or is_node_type(
82
prop.key, AST_String):
83
prop.key.print(output)
84
else:
85
86
def key():
87
prop.key.print(output)
88
89
output.with_square(key)
90
output.print(":")
91
prop.value.print(output)
92
if i + 1 < len(self.properties):
93
output.print(",")
94
output.print("}")
95
96
97
def print_object(self, output):
98
if self.is_pydict:
99
if self.properties.length > 0:
100
print_obj_literal(self, output)
101
else:
102
output.print('ρσ_dict()')
103
else:
104
if self.properties.length > 0:
105
print_obj_literal(self, output)
106
else:
107
output.print("Object.create(null)" if self.is_jshash else '{}')
108
109
110
def print_set(self, output):
111
if self.items.length is 0:
112
output.print('ρσ_set()')
113
return
114
115
def f_print_set():
116
output.print('function()')
117
118
def f_print_set0():
119
output.indent()
120
output.spaced.apply(output, 'var s = ρσ_set()'.split(' '))
121
output.end_statement()
122
for item in self.items:
123
output.indent()
124
output.print('s.jsset.add')
125
output.with_parens(lambda: item.value.print(output))
126
output.end_statement()
127
output.indent()
128
output.spaced('return', 's')
129
output.end_statement()
130
131
output.with_block(f_print_set0)
132
133
output.with_parens(f_print_set)
134
output.print('()')
135
136
137
def print_regexp(self, output):
138
str_ = self.value.toString()
139
if output.options.ascii_only:
140
str_ = output.to_ascii(str_)
141
output.print(str_)
142
p = output.parent()
143
if is_node_type(p, AST_Binary) and RegExp(r"^in").test(
144
p.operator) and p.left is self:
145
output.print(" ")
146
147