From 911b13dc4bc36ca7d3bbcbdc83ab7c9ac1c507c1 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Thu, 23 May 2019 15:59:03 -0700 Subject: [PATCH 1/5] Add basic pretty printer for types --- magma/util.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/magma/util.py b/magma/util.py index 61264e2b9a..a2a2bc0a29 100644 --- a/magma/util.py +++ b/magma/util.py @@ -7,3 +7,30 @@ def BitOrBits(width): if not isinstance(width, int): raise ValueError(f"Expected width to be None or int, got {width}") return m.Bits[width] + + +def pretty_str(t): + if isinstance(t, m.TupleKind): + args = [] + for i in range(t.N): + key_str = str(t.Ks[i]) + val_str = pretty_str(t.Ts[i]) + indent = " " * 4 + val_str = f"\n{indent}".join(val_str.splitlines()) + args.append(f"{key_str} = {val_str}") + # Pretty print by using newlines + indent + joiner = ",\n " + result = joiner.join(args) + # Insert first newline + indent and last newline + result = "\n " + result + "\n" + s = f"Tuple({result})" + elif isinstance(t, m.ArrayKind): + s = "Array[%d, %s]" % (t.N, t.T) + s = f"Array[{t.N}, {pretty_str(t.T)}]" + else: + s = str(t) + return s + + +def pretty_print_type(t): + print(pretty_str(t)) From 52c43e34a036e077a2a77537727503a4aaa7abe2 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Thu, 23 May 2019 15:59:53 -0700 Subject: [PATCH 2/5] Add test file --- tests/test_type/test_pretty_print.py | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 tests/test_type/test_pretty_print.py diff --git a/tests/test_type/test_pretty_print.py b/tests/test_type/test_pretty_print.py new file mode 100644 index 0000000000..542e63a500 --- /dev/null +++ b/tests/test_type/test_pretty_print.py @@ -0,0 +1,63 @@ +import magma as m + + +def test_pretty_print_tuple(): + t = m.Tuple(a=m.Bit, b=m.Bit, c=m.Bit) + assert m.util.pretty_str(t) == """\ +Tuple( + a = Bit, + b = Bit, + c = Bit +)\ +""" + + +def test_pretty_print_tuple_recursive(): + t = m.Tuple(a=m.Bit, b=m.Bit, c=m.Bit) + u = m.Tuple(x=t, y=t) + assert m.util.pretty_str(u) == """\ +Tuple( + x = Tuple( + a = Bit, + b = Bit, + c = Bit + ), + y = Tuple( + a = Bit, + b = Bit, + c = Bit + ) +)\ +""" + + +def test_pretty_print_array_of_tuple(): + t = m.Tuple(a=m.Bit, b=m.Bit, c=m.Bit) + u = m.Array[3, t] + assert m.util.pretty_str(u) == """\ +Array[3, Tuple( + a = Bit, + b = Bit, + c = Bit +)]\ +""" + + +def test_pretty_print_array_of_nested_tuple(): + t = m.Tuple(a=m.Bit, b=m.Bit, c=m.Bit) + u = m.Tuple(x=t, y=t) + v = m.Array[3, u] + assert m.util.pretty_str(v) == """\ +Array[3, Tuple( + x = Tuple( + a = Bit, + b = Bit, + c = Bit + ), + y = Tuple( + a = Bit, + b = Bit, + c = Bit + ) +)]\ +""" From f14f82b1a6726adae38f26b70f7199ac918f35c4 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Thu, 23 May 2019 16:04:01 -0700 Subject: [PATCH 3/5] Add cases for Bits, UInt, SInt --- magma/util.py | 7 ++++++- tests/test_type/test_pretty_print.py | 14 +++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/magma/util.py b/magma/util.py index a2a2bc0a29..9183735e63 100644 --- a/magma/util.py +++ b/magma/util.py @@ -24,8 +24,13 @@ def pretty_str(t): # Insert first newline + indent and last newline result = "\n " + result + "\n" s = f"Tuple({result})" + elif isinstance(t, m.SIntKind): + s = f"SInt[{t.N}]" + elif isinstance(t, m.UIntKind): + s = f"UInt[{t.N}]" + elif isinstance(t, m.BitsKind): + s = f"Bits[{t.N}]" elif isinstance(t, m.ArrayKind): - s = "Array[%d, %s]" % (t.N, t.T) s = f"Array[{t.N}, {pretty_str(t.T)}]" else: s = str(t) diff --git a/tests/test_type/test_pretty_print.py b/tests/test_type/test_pretty_print.py index 542e63a500..de35dbde27 100644 --- a/tests/test_type/test_pretty_print.py +++ b/tests/test_type/test_pretty_print.py @@ -44,20 +44,20 @@ def test_pretty_print_array_of_tuple(): def test_pretty_print_array_of_nested_tuple(): - t = m.Tuple(a=m.Bit, b=m.Bit, c=m.Bit) + t = m.Tuple(a=m.Bits[5], b=m.UInt[3], c=m.SInt[4]) u = m.Tuple(x=t, y=t) v = m.Array[3, u] assert m.util.pretty_str(v) == """\ Array[3, Tuple( x = Tuple( - a = Bit, - b = Bit, - c = Bit + a = Bits[5], + b = UInt[3], + c = SInt[4] ), y = Tuple( - a = Bit, - b = Bit, - c = Bit + a = Bits[5], + b = UInt[3], + c = SInt[4] ) )]\ """ From 3b9307003e9b9dc2c4820298faa72833be4284d5 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Thu, 23 May 2019 16:56:12 -0700 Subject: [PATCH 4/5] Simplify default logic --- magma/util.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/magma/util.py b/magma/util.py index 9183735e63..71ab72ed85 100644 --- a/magma/util.py +++ b/magma/util.py @@ -24,12 +24,8 @@ def pretty_str(t): # Insert first newline + indent and last newline result = "\n " + result + "\n" s = f"Tuple({result})" - elif isinstance(t, m.SIntKind): - s = f"SInt[{t.N}]" - elif isinstance(t, m.UIntKind): - s = f"UInt[{t.N}]" elif isinstance(t, m.BitsKind): - s = f"Bits[{t.N}]" + s = str(t) elif isinstance(t, m.ArrayKind): s = f"Array[{t.N}, {pretty_str(t.T)}]" else: From 5b6a2fb15fee155d33e53ac7a40f1d7a9bbb9d46 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Thu, 23 May 2019 16:58:02 -0700 Subject: [PATCH 5/5] Fixup str format --- magma/bits.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/magma/bits.py b/magma/bits.py index b4b788670d..7e00c780c9 100644 --- a/magma/bits.py +++ b/magma/bits.py @@ -179,10 +179,10 @@ def __getitem__(cls, index): def __str__(cls): if cls.isinput(): - return "In(UInt({}))".format(cls.N) + return "In(UInt[{}])".format(cls.N) if cls.isoutput(): - return "Out(UInt({}))".format(cls.N) - return "UInt({})".format(cls.N) + return "Out(UInt[{}])".format(cls.N) + return "UInt[{}]".format(cls.N) def qualify(cls, direction): if cls.T.isoriented(direction): @@ -247,10 +247,10 @@ def __getitem__(cls, index): def __str__(cls): if cls.isinput(): - return "In(SInt({}))".format(cls.N) + return "In(SInt[{}])".format(cls.N) if cls.isoutput(): - return "Out(SInt({}))".format(cls.N) - return "SInt({})".format(cls.N) + return "Out(SInt[{}])".format(cls.N) + return "SInt[{}]".format(cls.N) def qualify(cls, direction): if cls.T.isoriented(direction): @@ -321,10 +321,10 @@ def __getitem__(cls, index): def __str__(cls): if cls.isinput(): - return "In(BFloat({}))".format(cls.N) + return "In(BFloat[{}])".format(cls.N) if cls.isoutput(): - return "Out(BFloat({}))".format(cls.N) - return "BFloat({})".format(cls.N) + return "Out(BFloat[{}])".format(cls.N) + return "BFloat[{}]".format(cls.N) def qualify(cls, direction): if cls.T.isoriented(direction):