forked from M-Labs/artiq
compiler: insert new lines into long synthesized code (#1986)
This commit is contained in:
parent
0b3c232819
commit
f7f027001e
|
@ -169,6 +169,7 @@ class EmbeddingMap:
|
||||||
class ASTSynthesizer:
|
class ASTSynthesizer:
|
||||||
def __init__(self, embedding_map, value_map, quote_function=None, expanded_from=None):
|
def __init__(self, embedding_map, value_map, quote_function=None, expanded_from=None):
|
||||||
self.source = ""
|
self.source = ""
|
||||||
|
self.source_last_new_line = 0
|
||||||
self.source_buffer = source.Buffer(self.source, "<synthesized>")
|
self.source_buffer = source.Buffer(self.source, "<synthesized>")
|
||||||
self.embedding_map = embedding_map
|
self.embedding_map = embedding_map
|
||||||
self.value_map = value_map
|
self.value_map = value_map
|
||||||
|
@ -187,6 +188,14 @@ class ASTSynthesizer:
|
||||||
return source.Range(self.source_buffer, range_from, range_to,
|
return source.Range(self.source_buffer, range_from, range_to,
|
||||||
expanded_from=self.expanded_from)
|
expanded_from=self.expanded_from)
|
||||||
|
|
||||||
|
def _add_iterable(self, fragment):
|
||||||
|
# Since DILocation points on the beginning of the piece of source
|
||||||
|
# we don't care if the fragment's end will overflow LLVM's limit.
|
||||||
|
if len(self.source) - self.source_last_new_line >= 2**16:
|
||||||
|
fragment = "\\\n" + fragment
|
||||||
|
self.source_last_new_line = len(self.source) + 2
|
||||||
|
return self._add(fragment)
|
||||||
|
|
||||||
def fast_quote_list(self, value):
|
def fast_quote_list(self, value):
|
||||||
elts = [None] * len(value)
|
elts = [None] * len(value)
|
||||||
is_T = False
|
is_T = False
|
||||||
|
@ -245,7 +254,7 @@ class ASTSynthesizer:
|
||||||
for index, elt in enumerate(value):
|
for index, elt in enumerate(value):
|
||||||
elts[index] = self.quote(elt)
|
elts[index] = self.quote(elt)
|
||||||
if index < len(value) - 1:
|
if index < len(value) - 1:
|
||||||
self._add(", ")
|
self._add_iterable(", ")
|
||||||
return elts
|
return elts
|
||||||
|
|
||||||
def quote(self, value):
|
def quote(self, value):
|
||||||
|
@ -296,28 +305,28 @@ class ASTSynthesizer:
|
||||||
loc=self._add(repr(value)))
|
loc=self._add(repr(value)))
|
||||||
elif isinstance(value, str):
|
elif isinstance(value, str):
|
||||||
return asttyped.StrT(s=value, ctx=None, type=builtins.TStr(),
|
return asttyped.StrT(s=value, ctx=None, type=builtins.TStr(),
|
||||||
loc=self._add(repr(value)))
|
loc=self._add_iterable(repr(value)))
|
||||||
elif isinstance(value, bytes):
|
elif isinstance(value, bytes):
|
||||||
return asttyped.StrT(s=value, ctx=None, type=builtins.TBytes(),
|
return asttyped.StrT(s=value, ctx=None, type=builtins.TBytes(),
|
||||||
loc=self._add(repr(value)))
|
loc=self._add_iterable(repr(value)))
|
||||||
elif isinstance(value, bytearray):
|
elif isinstance(value, bytearray):
|
||||||
quote_loc = self._add('`')
|
quote_loc = self._add_iterable('`')
|
||||||
repr_loc = self._add(repr(value))
|
repr_loc = self._add_iterable(repr(value))
|
||||||
unquote_loc = self._add('`')
|
unquote_loc = self._add_iterable('`')
|
||||||
loc = quote_loc.join(unquote_loc)
|
loc = quote_loc.join(unquote_loc)
|
||||||
|
|
||||||
return asttyped.QuoteT(value=value, type=builtins.TByteArray(), loc=loc)
|
return asttyped.QuoteT(value=value, type=builtins.TByteArray(), loc=loc)
|
||||||
elif isinstance(value, list):
|
elif isinstance(value, list):
|
||||||
begin_loc = self._add("[")
|
begin_loc = self._add_iterable("[")
|
||||||
elts = self.fast_quote_list(value)
|
elts = self.fast_quote_list(value)
|
||||||
end_loc = self._add("]")
|
end_loc = self._add_iterable("]")
|
||||||
return asttyped.ListT(elts=elts, ctx=None, type=builtins.TList(),
|
return asttyped.ListT(elts=elts, ctx=None, type=builtins.TList(),
|
||||||
begin_loc=begin_loc, end_loc=end_loc,
|
begin_loc=begin_loc, end_loc=end_loc,
|
||||||
loc=begin_loc.join(end_loc))
|
loc=begin_loc.join(end_loc))
|
||||||
elif isinstance(value, tuple):
|
elif isinstance(value, tuple):
|
||||||
begin_loc = self._add("(")
|
begin_loc = self._add_iterable("(")
|
||||||
elts = self.fast_quote_list(value)
|
elts = self.fast_quote_list(value)
|
||||||
end_loc = self._add(")")
|
end_loc = self._add_iterable(")")
|
||||||
return asttyped.TupleT(elts=elts, ctx=None,
|
return asttyped.TupleT(elts=elts, ctx=None,
|
||||||
type=types.TTuple([e.type for e in elts]),
|
type=types.TTuple([e.type for e in elts]),
|
||||||
begin_loc=begin_loc, end_loc=end_loc,
|
begin_loc=begin_loc, end_loc=end_loc,
|
||||||
|
|
Loading…
Reference in New Issue