Skip to content

Commit

Permalink
Support writing operator() and operator[], fixes hsutter#315 comm…
Browse files Browse the repository at this point in the history
…ent thread issue
  • Loading branch information
hsutter authored and zaucy committed Dec 5, 2023
1 parent f9d9c8b commit e3cf390
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
4 changes: 2 additions & 2 deletions regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

SPDX-License-Identifier: CC-BY-NC-ND-4.0
No commercial use
Expand Down
4 changes: 2 additions & 2 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,8 @@ class cmdline_processor
};

help_requested = true;
print("\ncppfront compiler - v0.2.1, build " + stamp());
print("\nCopyright(c) Herb Sutter, all rights reserved\n");
print("\ncppfront compiler v0.2.1 Build " + stamp());
print("\nCopyright(c) Herb Sutter All rights reserved\n");
print("\nSPDX-License-Identifier: CC-BY-NC-ND-4.0");
print("\n No commercial use");
print("\n No forks/derivatives\n");
Expand Down
23 changes: 22 additions & 1 deletion source/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ auto lex_line(
});
}

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

// Else if token after "operator" is a two-token operator symbol
else if (
(tokens[i-1].type() == lexeme::LeftParen && tokens[i].type() == lexeme::RightParen)
|| (tokens[i-1].type() == lexeme::LeftBracket && tokens[i].type() == lexeme::RightBracket)
)
{
// Merge just "operator" + the symbols into an identifier,
generated_text.push_back( "operator" + tokens[i-1].to_string(true) + tokens[i].to_string(true) );

tokens.pop_back();
tokens.pop_back();
auto pos = tokens.back().position();
tokens.pop_back();
tokens.push_back({
&generated_text.back()[0],
std::ssize(generated_text.back()),
pos,
lexeme::Identifier
});
}

}
};

Expand Down
14 changes: 7 additions & 7 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -5856,12 +5856,12 @@ class parser
}
next();

// Next is an optional meta functions clause
// Next is an optional metafunctions clause
while (curr() == "@") {
next();
auto idx = id_expression();
if (!idx) {
error("'@' must be followed by a a meta function name", false);
error("'@' must be followed by a a metafunction name", false);
return {};
}
n->meta_functions.push_back( std::move(idx) );
Expand Down Expand Up @@ -5925,7 +5925,7 @@ class parser
if (!n->meta_functions.empty()) {
errors.emplace_back(
n->meta_functions.front()->position(),
"(temporary alpha limitation) meta functions are currently not supported on functions, only on types"
"(temporary alpha limitation) metafunctions are currently not supported on functions, only on types"
);
return {};
}
Expand All @@ -5941,7 +5941,7 @@ class parser
if (!n->meta_functions.empty()) {
errors.emplace_back(
n->meta_functions.front()->position(),
"(temporary alpha limitation) meta functions are currently not supported on namespaces, only on types"
"(temporary alpha limitation) metafunctions are currently not supported on namespaces, only on types"
);
return {};
}
Expand Down Expand Up @@ -5972,7 +5972,7 @@ class parser
if (!n->meta_functions.empty()) {
errors.emplace_back(
n->meta_functions.front()->position(),
"(temporary alpha limitation) meta functions are currently not supported on objects, only on types"
"(temporary alpha limitation) metafunctions are currently not supported on objects, only on types"
);
return {};
}
Expand Down Expand Up @@ -6102,11 +6102,11 @@ class parser
}
}

// If this is a type with meta functions, apply those
// If this is a type with metafunctions, apply those
if (n->is_type()) {
if (!apply_type_meta_functions(*n)) {
error(
"error encountered while applying type's meta functions",
"error encountered while applying type metafunctions",
false, {}, true
);
return {};
Expand Down
8 changes: 4 additions & 4 deletions source/reflect.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,14 @@ auto declaration::as_type() const

//-----------------------------------------------------------------------
//
// Meta functions - these are hardwired for now until we get to the
// Metafunctions - these are hardwired for now until we get to the
// step of writing a Cpp2 interpreter to run inside the compiler
//
//-----------------------------------------------------------------------
//

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

// For each meta function, apply it
// For each metafunction, apply it
for (auto& meta : n.meta_functions)
{
rtype.set_meta_function_name( meta->to_string() );
Expand Down Expand Up @@ -775,7 +775,7 @@ auto parser::apply_type_meta_functions( declaration_node& n )
struct_( rtype );
}
else {
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" );
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" );
return false;
}
}
Expand Down

0 comments on commit e3cf390

Please sign in to comment.