Skip to content

Commit ab3670a

Browse files
committed
Support writing operator() and operator[], fixes #315 comment thread issue
1 parent ddd6292 commit ab3670a

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

regression-tests/test-results/version

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
cppfront compiler - v0.2.1, build 8428:0755
3-
Copyright(c) Herb Sutter, all rights reserved
2+
cppfront compiler v0.2.1 Build 8429:1203
3+
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0
66
No commercial use

source/common.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -762,8 +762,8 @@ class cmdline_processor
762762
};
763763

764764
help_requested = true;
765-
print("\ncppfront compiler - v0.2.1, build " + stamp());
766-
print("\nCopyright(c) Herb Sutter, all rights reserved\n");
765+
print("\ncppfront compiler v0.2.1 Build " + stamp());
766+
print("\nCopyright(c) Herb Sutter All rights reserved\n");
767767
print("\nSPDX-License-Identifier: CC-BY-NC-ND-4.0");
768768
print("\n No commercial use");
769769
print("\n No forks/derivatives\n");

source/lex.h

+22-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ auto lex_line(
709709
});
710710
}
711711

712-
// Else if token after "operator" is an operator symbol
712+
// Else if token after "operator" is a single-token operator symbol
713713
else if (is_operator(tokens[i-1].type()))
714714
{
715715
// Merge just "operator" + the symbol into an identifier,
@@ -730,6 +730,27 @@ auto lex_line(
730730
tokens.push_back(last_token);
731731
}
732732

733+
// Else if token after "operator" is a two-token operator symbol
734+
else if (
735+
(tokens[i-1].type() == lexeme::LeftParen && tokens[i].type() == lexeme::RightParen)
736+
|| (tokens[i-1].type() == lexeme::LeftBracket && tokens[i].type() == lexeme::RightBracket)
737+
)
738+
{
739+
// Merge just "operator" + the symbols into an identifier,
740+
generated_text.push_back( "operator" + tokens[i-1].to_string(true) + tokens[i].to_string(true) );
741+
742+
tokens.pop_back();
743+
tokens.pop_back();
744+
auto pos = tokens.back().position();
745+
tokens.pop_back();
746+
tokens.push_back({
747+
&generated_text.back()[0],
748+
std::ssize(generated_text.back()),
749+
pos,
750+
lexeme::Identifier
751+
});
752+
}
753+
733754
}
734755
};
735756

source/parse.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -5856,12 +5856,12 @@ class parser
58565856
}
58575857
next();
58585858

5859-
// Next is an optional meta functions clause
5859+
// Next is an optional metafunctions clause
58605860
while (curr() == "@") {
58615861
next();
58625862
auto idx = id_expression();
58635863
if (!idx) {
5864-
error("'@' must be followed by a a meta function name", false);
5864+
error("'@' must be followed by a a metafunction name", false);
58655865
return {};
58665866
}
58675867
n->meta_functions.push_back( std::move(idx) );
@@ -5925,7 +5925,7 @@ class parser
59255925
if (!n->meta_functions.empty()) {
59265926
errors.emplace_back(
59275927
n->meta_functions.front()->position(),
5928-
"(temporary alpha limitation) meta functions are currently not supported on functions, only on types"
5928+
"(temporary alpha limitation) metafunctions are currently not supported on functions, only on types"
59295929
);
59305930
return {};
59315931
}
@@ -5941,7 +5941,7 @@ class parser
59415941
if (!n->meta_functions.empty()) {
59425942
errors.emplace_back(
59435943
n->meta_functions.front()->position(),
5944-
"(temporary alpha limitation) meta functions are currently not supported on namespaces, only on types"
5944+
"(temporary alpha limitation) metafunctions are currently not supported on namespaces, only on types"
59455945
);
59465946
return {};
59475947
}
@@ -5972,7 +5972,7 @@ class parser
59725972
if (!n->meta_functions.empty()) {
59735973
errors.emplace_back(
59745974
n->meta_functions.front()->position(),
5975-
"(temporary alpha limitation) meta functions are currently not supported on objects, only on types"
5975+
"(temporary alpha limitation) metafunctions are currently not supported on objects, only on types"
59765976
);
59775977
return {};
59785978
}
@@ -6102,11 +6102,11 @@ class parser
61026102
}
61036103
}
61046104

6105-
// If this is a type with meta functions, apply those
6105+
// If this is a type with metafunctions, apply those
61066106
if (n->is_type()) {
61076107
if (!apply_type_meta_functions(*n)) {
61086108
error(
6109-
"error encountered while applying type's meta functions",
6109+
"error encountered while applying type metafunctions",
61106110
false, {}, true
61116111
);
61126112
return {};

source/reflect.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,14 @@ auto declaration::as_type() const
410410

411411
//-----------------------------------------------------------------------
412412
//
413-
// Meta functions - these are hardwired for now until we get to the
413+
// Metafunctions - these are hardwired for now until we get to the
414414
// step of writing a Cpp2 interpreter to run inside the compiler
415415
//
416416
//-----------------------------------------------------------------------
417417
//
418418

419419
//-----------------------------------------------------------------------
420-
// Some common meta function helpers (meta functions are just functions,
420+
// Some common metafunction helpers (metafunctions are just functions,
421421
// so they can be factored as usual)
422422
//
423423
auto add_virtual_destructor(meta::type_declaration& t)
@@ -742,7 +742,7 @@ auto parser::apply_type_meta_functions( declaration_node& n )
742742
auto cs = meta::compiler_services{ &errors, generated_tokens };
743743
auto rtype = meta::type_declaration{ &n, cs };
744744

745-
// For each meta function, apply it
745+
// For each metafunction, apply it
746746
for (auto& meta : n.meta_functions)
747747
{
748748
rtype.set_meta_function_name( meta->to_string() );
@@ -775,7 +775,7 @@ auto parser::apply_type_meta_functions( declaration_node& n )
775775
struct_( rtype );
776776
}
777777
else {
778-
error( "(temporary alpha limitation) unrecognized meta function name '" + meta->to_string() + "' - currently the supported names are: interface, polymorphic_base, ordered, weakly_ordered, partially_ordered, value, weakly_ordered_value, partially_ordered_value, struct" );
778+
error( "(temporary alpha limitation) unrecognized metafunction name '" + meta->to_string() + "' - currently the supported names are: interface, polymorphic_base, ordered, weakly_ordered, partially_ordered, value, weakly_ordered_value, partially_ordered_value, struct" );
779779
return false;
780780
}
781781
}

0 commit comments

Comments
 (0)