Skip to content

Commit 586ff20

Browse files
committed
Add basic AST pretty-print visualizer and @print metafunction
Also make `unique`, `shared`, and `nonesuch` be `inline`, closes #685. And finally fix `token::to_string`'s default to never decorate, all callers passed `true` anyway except the one use in `parse_tree_printer::start(token)`.
1 parent 99d66f0 commit 586ff20

23 files changed

+1536
-250
lines changed

include/cpp2util.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,14 @@ struct {
517517
[[nodiscard]] auto cpp2_new(auto&& ...args) const -> std::unique_ptr<T> {
518518
return std::make_unique<T>(CPP2_FORWARD(args)...);
519519
}
520-
} unique;
520+
} inline unique;
521521

522522
[[maybe_unused]] struct {
523523
template<typename T>
524524
[[nodiscard]] auto cpp2_new(auto&& ...args) const -> std::shared_ptr<T> {
525525
return std::make_shared<T>(CPP2_FORWARD(args)...);
526526
}
527-
} shared;
527+
} inline shared;
528528

529529
template<typename T>
530530
[[nodiscard]] auto cpp2_new(auto&& ...args) -> std::unique_ptr<T> {
@@ -1009,7 +1009,7 @@ inline constexpr auto is( auto const& x, auto const& value ) -> bool
10091009
struct nonesuch_ {
10101010
auto operator==(auto const&) -> bool { return false; }
10111011
};
1012-
constexpr static nonesuch_ nonesuch;
1012+
constexpr inline nonesuch_ nonesuch;
10131013

10141014
// The 'as' cast functions are <To, From> so use that order here
10151015
// If it's confusing, we can switch this to <From, To>

regression-tests/pure2-intro-example-hello-2022.cpp2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ main: () -> int = {
55

66
for view do (inout str) {
77
len := decorate(str);
8-
println(str, len);
8+
print_it(str, len);
99
}
1010
}
1111

@@ -14,7 +14,7 @@ decorate: (inout thing: _ ) -> int = {
1414
return thing.ssize();
1515
}
1616

17-
println: (x: _, len: _) =
17+
print_it: (x: _, len: _) =
1818
std::cout
1919
<< ">> " << x
2020
<< " - length "

regression-tests/pure2-print.cpp2

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
// Exercise the pretty-print visualizer for the various grammar elements
3+
4+
outer: @print type = {
5+
6+
mytype: final type =
7+
{
8+
protected f: () -> int = 42;
9+
10+
g: (virtual this, i:int) -> int = {
11+
s := "string literal";
12+
ret: int = i;
13+
p: const * const int = ret&;
14+
if p* < 0 {
15+
ret = -p*;
16+
}
17+
ret += strlen(s) - 10 + s.strlen() * (16 / (3 & 2)) % 3;
18+
19+
m: std::map<const int,std::string> = ();
20+
m[0] = "har" as std::string;
21+
ret -= h("x", m).length();
22+
_ = m;
23+
24+
return ret;
25+
}
26+
27+
private h: (s: std::string, inout m: std::map<const int,std::string> ) -> std::string
28+
[[pre: m.empty() == false || false]]
29+
[[pre Bounds: 0 < m.ssize() < 100 && true != false]]
30+
= {
31+
a := :()={};
32+
b := :()={};
33+
c := :()={};
34+
35+
while s.empty() next a() { break; }
36+
37+
do { } while s.empty() next b();
38+
39+
label: for m next c() do (_) { continue label; }
40+
41+
if !s.empty() is (true) { a(); }
42+
else if !m.empty() { b(); }
43+
else { c(); }
44+
45+
[[assert: true]]
46+
47+
return :() -> std::string = (s + m[0])$; ();
48+
}
49+
50+
values: <T> (this, t: T) throws -> (offset: int, name: std::string) = {
51+
offset = 53;
52+
name = "plugh";
53+
}
54+
55+
operator=: (out this) = { }
56+
57+
operator=: (out this, that) = { }
58+
59+
operator=: (implicit out this, _: int) = { }
60+
61+
variadic: (x...: int) = { }
62+
}
63+
64+
test: () = {
65+
var: ::outer::mytype = ();
66+
std::cout << var.g(42) << "\n";
67+
68+
std::cout << inspect var.g(42) -> std::string {
69+
is 43 = "forty-and-three";
70+
is _ = "default case";
71+
} << "\n";
72+
73+
}
74+
75+
x: <Ts...: type> type = {
76+
tup: std::tuple<Ts...> = ();
77+
}
78+
79+
print: <Args...: type> (inout out: std::ostream, args...: Args) requires sizeof...(Args) >= 0 = {
80+
(out << ... << args);
81+
}
82+
83+
all: <Args...: type> (args...: Args) -> bool =
84+
(... && args);
85+
86+
}
87+
88+
main: () = {
89+
outer::test();
90+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
43
2+
forty-and-three
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
pure2-print.cpp2:50:80: warning: unused parameter 't' [-Wunused-parameter]
2+
template<typename T> [[nodiscard]] auto outer::mytype::values(T const& t) const& -> values__ret{
3+
^
4+
pure2-print.cpp2:61:53: warning: unused parameter 'x' [-Wunused-parameter]
5+
auto outer::mytype::variadic(auto const& ...x) -> void{}
6+
^
7+
2 warnings generated.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
In file included from pure2-print.cpp:7:
2+
../../../include/cpp2util.h:10005:33: error: expected initializer before ‘static_assert’
3+
pure2-print.cpp2:80:1: note: in expansion of macro ‘CPP2_REQUIRES_’
4+
pure2-print.cpp2:79:37: error: no declaration matches ‘void outer::print(std::ostream&, const Args& ...) requires cpp2::cmp_greater_eq(sizeof (Args)..., 0)’
5+
pure2-print.cpp2:79:37: note: no functions named ‘void outer::print(std::ostream&, const Args& ...) requires cpp2::cmp_greater_eq(sizeof (Args)..., 0)’
6+
pure2-print.cpp2:4:7: note: ‘class outer’ defined here
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
43
2+
forty-and-three

regression-tests/test-results/gcc-13/pure2-print.cpp.output

Whitespace-only changes.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Microsoft (R) C/C++ Optimizing Compiler Version 19.36.32538 for x86
1+
Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32824 for x86
22
Copyright (C) Microsoft Corporation. All rights reserved.
33

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
43
2+
forty-and-three

0 commit comments

Comments
 (0)