Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: NoIndent
IndentGotoLabels: false
IndentPPDirectives: None
IndentPPDirectives: BeforeHash
IndentWidth: 4
IndentWrappedFunctionNames: false
# clang-format-16 InsertNewlineAtEOF: true
Expand Down
6 changes: 3 additions & 3 deletions nyan/api_error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ MemberTypeError::MemberTypeError(const fqon_t &objname,
const std::string &real_type,
const std::string &wrong_type) :
APIError{(static_cast<const std::ostringstream &>(
std::ostringstream{} << "type mismatch for member " << objname + "." << member
<< ": tried to convert real type " << real_type << " to " << wrong_type))
.str()},
std::ostringstream{} << "type mismatch for member " << objname + "." << member
<< ": tried to convert real type " << real_type << " to " << wrong_type))
.str()},
objname{objname},
member{member},
real_type{real_type},
Expand Down
6 changes: 4 additions & 2 deletions nyan/api_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ class InvalidObjectError : public APIError {
*/
class MemberTypeError : public APIError {
public:
MemberTypeError(const fqon_t &objname, const memberid_t &member,
const std::string &real_type, const std::string &wrong_type);
MemberTypeError(const fqon_t &objname,
const memberid_t &member,
const std::string &real_type,
const std::string &wrong_type);

protected:
/**
Expand Down
62 changes: 27 additions & 35 deletions nyan/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,7 @@ void ASTObject::ast_parents(TokenStream &tokens) {
if (token.type != token_type::ID) {
throw ASTError{
"expected inheritance parent identifier, but there is",
token
};
token};
}

this->parents.emplace_back(token, stream);
Expand All @@ -301,9 +300,9 @@ void ASTObject::ast_members(TokenStream &tokens) {
bool object_next = false;
auto lookahead = tokens.next();

if (lookahead->type == token_type::OPERATOR // value assignment
or lookahead->type == token_type::COLON // type declaration
or lookahead->type == token_type::DOT) { // inherited member access (e.g. Parent.some_member)
if (lookahead->type == token_type::OPERATOR // value assignment
or lookahead->type == token_type::COLON // type declaration
or lookahead->type == token_type::DOT) { // inherited member access (e.g. Parent.some_member)
object_next = false;
}
else if (lookahead->type == token_type::LANGLE or lookahead->type == token_type::LBRACKET or lookahead->type == token_type::LPAREN) {
Expand Down Expand Up @@ -434,7 +433,6 @@ ASTMember::ASTMember(const Token &name,
name{IDToken{name, tokens}},
type{std::nullopt},
value{std::nullopt} {

auto token = tokens.next();
bool had_def_or_decl = false;

Expand Down Expand Up @@ -535,7 +533,6 @@ ASTMember::ASTMember(const Token &name,
ASTMemberType::ASTMemberType(const Token &name,
TokenStream &tokens) :
name{IDToken{name, tokens}} {

// now there may follow type arguments, e.g.:
// set(arg, key=val)
// optional(dict(ktype, vtype))
Expand All @@ -547,7 +544,6 @@ ASTMemberType::ASTMemberType(const Token &name,
size_t num_expected_types = member_type.expected_nested_types();

if (token->type == token_type::LPAREN) {

// TODO: if we introduce optional arguments for composite types
// we have to adjust the allowed count here.
// or just count the non-kwarg arguments, and ignored the kwarg count.
Expand All @@ -561,29 +557,28 @@ ASTMemberType::ASTMemberType(const Token &name,
num_expected_types,
[this](const Token &token, TokenStream &stream) {
this->nested_types.emplace_back(token, stream);
}
);
});

if (unlikely(num_read_types != num_expected_types)) {
throw ASTError(
std::string("expected ")
+ std::to_string(num_expected_types)
+ " arguments for "
+ composite_type_to_string(member_type.composite_type)
+ " declaration, but only "
+ std::to_string(num_read_types)
+ " could be found",
+ std::to_string(num_expected_types)
+ " arguments for "
+ composite_type_to_string(member_type.composite_type)
+ " declaration, but only "
+ std::to_string(num_read_types)
+ " could be found",
*token,
false);
}
}
else if (num_expected_types > 0) {
throw ASTError(
std::string("expected ")
+ std::to_string(num_expected_types)
+ " arguments for "
+ composite_type_to_string(member_type.composite_type)
+ " declaration",
+ std::to_string(num_expected_types)
+ " arguments for "
+ composite_type_to_string(member_type.composite_type)
+ " declaration",
*token,
false);
}
Expand Down Expand Up @@ -739,12 +734,11 @@ void ASTObject::strb(std::ostringstream &builder, int indentlevel) const {

// object parents
builder << "(";
util::strjoin(builder, ", ", this->parents,
[](auto &stream, auto &elem) {
stream << elem.str();
});
util::strjoin(builder, ", ", this->parents, [](auto &stream, auto &elem) {
stream << elem.str();
});
builder << "):"
<< std::endl;
<< std::endl;

if (this->objects.size() > 0) {
for (auto &object : this->objects) {
Expand Down Expand Up @@ -795,8 +789,8 @@ void ASTMember::strb(std::ostringstream &builder, int indentlevel) const {

if (this->value.has_value()) {
builder << " "
<< op_to_string(this->operation)
<< " ";
<< op_to_string(this->operation)
<< " ";

this->value->strb(builder);
}
Expand All @@ -810,10 +804,9 @@ void ASTMemberType::strb(std::ostringstream &builder, int /*indentlevel*/) const

if (this->args.size() > 0) {
builder << "(";
util::strjoin(builder, ", ", this->args,
[](auto &stream, auto& elem) {
elem.strb(stream);
});
util::strjoin(builder, ", ", this->args, [](auto &stream, auto &elem) {
elem.strb(stream);
});
builder << ")";
}
}
Expand Down Expand Up @@ -846,8 +839,7 @@ void ASTMemberValue::strb(std::ostringstream &builder, int /*indentlevel*/) cons
throw InternalError{"unhandled container type"};
}

util::strjoin(builder, ", ", this->values,
[](auto &stream, auto &elem) { stream << elem.str(); });
util::strjoin(builder, ", ", this->values, [](auto &stream, auto &elem) { stream << elem.str(); });

switch (this->composite_type) {
case composite_t::SET:
Expand All @@ -869,7 +861,7 @@ ASTError::ASTError(const std::string &msg,
if (add_token) {
std::ostringstream builder;
builder << msg << ": "
<< token_type_str(token.type);
<< token_type_str(token.type);
this->msg = std::move(builder).str();
}
else {
Expand All @@ -885,7 +877,7 @@ ASTError::ASTError(const std::string &msg,
if (add_token) {
std::ostringstream builder;
builder << msg << ": "
<< token_type_str(token.get_type());
<< token_type_str(token.get_type());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm this is incorrect now - i thought clang-format was able to mix indentation and alignment?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't that because << is technically a line continuation? The documentation says:

UT_AlignWithSpaces (in configuration: AlignWithSpaces) Use tabs for line continuation and indentation, and spaces for alignment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a line continuation that should align to the previous <<. i thought we found a solution for this already to format correctly.
https://clang.llvm.org/docs/ClangFormatStyleOptions.html#alignoperands

but it seems to be weird anyway, even with always-tab (which we don't have):
llvm/llvm-project#59797

this->msg = builder.str();
}
else {
Expand Down
2 changes: 1 addition & 1 deletion nyan/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ unsigned int comma_list(token_type end,
TokenStream &tokens,
size_t limit,
const std::function<void(const Token &, TokenStream &)> &func,
bool unlimited=false);
bool unlimited = false);


/**
Expand Down
13 changes: 4 additions & 9 deletions nyan/basic_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,10 @@ size_t BasicType::expected_nested_types() const {


bool BasicType::operator==(const BasicType &other) const {
return (this->primitive_type == other.primitive_type and
this->composite_type == other.composite_type);
return (this->primitive_type == other.primitive_type and this->composite_type == other.composite_type);
}



std::string BasicType::str() const {
if (this->is_fundamental()) {
return type_to_string(this->primitive_type);
Expand All @@ -130,22 +128,19 @@ BasicType BasicType::from_type_token(const IDToken &tok) {
{"text", primitive_t::TEXT},
{"file", primitive_t::FILENAME},
{"int", primitive_t::INT},
{"float", primitive_t::FLOAT}
};
{"float", primitive_t::FLOAT}};

// container type name map
static const std::unordered_map<std::string, composite_t> container_types = {
{"set", composite_t::SET},
{"orderedset", composite_t::ORDEREDSET},
{"dict", composite_t::DICT}
};
{"dict", composite_t::DICT}};

// modifier type name map
static const std::unordered_map<std::string, composite_t> modifiers = {
{"abstract", composite_t::ABSTRACT},
{"children", composite_t::CHILDREN},
{"optional", composite_t::OPTIONAL}
};
{"optional", composite_t::OPTIONAL}};

primitive_t type = primitive_t::OBJECT;
composite_t composite_type = composite_t::SINGLE;
Expand Down
21 changes: 7 additions & 14 deletions nyan/c3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ std::vector<fqon_t>
linearize_recurse(const fqon_t &name,
const objstate_fetch_t &get_obj,
std::unordered_set<fqon_t> *seen) {

using namespace std::string_literals;

// test for inheritance loops
if (seen->find(name) != std::end(*seen)) {
throw C3Error{
"recursive inheritance loop detected: '"s + name + "' already in {"
+ util::strjoin(", ", *seen)
+ "}"
};
+ "}"};
}
else {
seen->insert(name);
Expand All @@ -67,14 +65,12 @@ linearize_recurse(const fqon_t &name,
for (auto &parent : parents) {
// Recursive call to get the linearization of the parent
par_linearizations.push_back(
linearize_recurse(parent, get_obj, seen)
);
linearize_recurse(parent, get_obj, seen));
}

// And at the end, add all parents of this object to the merge-list.
par_linearizations.push_back(
{std::begin(parents), std::end(parents)}
);
{std::begin(parents), std::end(parents)});

// remove current name from the seen set
// we only needed it for the recursive call above.
Expand Down Expand Up @@ -109,7 +105,6 @@ linearize_recurse(const fqon_t &name,

// Test if the candidate is in any tail
for (size_t j = 0; j < par_linearizations.size(); j++) {

// The current list will never contain the candidate again.
if (j == i) {
continue;
Expand All @@ -121,7 +116,6 @@ linearize_recurse(const fqon_t &name,
// Start one slot after the head
// and check that the candidate is not in that tail.
for (size_t k = headpos_try + 1; k < tail.size(); k++) {

// The head is in that tail, so we fail
if (unlikely(*candidate == tail[k])) {
candidate_ok = false;
Expand All @@ -138,7 +132,8 @@ linearize_recurse(const fqon_t &name,
// The candidate was not in any tail
if (candidate_ok) {
break;
} else {
}
else {
// Try the next candidate,
// this means to select the next par_lin list.
continue;
Expand Down Expand Up @@ -171,8 +166,7 @@ linearize_recurse(const fqon_t &name,
if (not candidate_ok) {
throw C3Error{
"Can't find consistent C3 resolution order for "s
+ name + " for bases " + util::strjoin(", ", parents)
};
+ name + " for bases " + util::strjoin(", ", parents)};
}
}

Expand All @@ -181,8 +175,7 @@ linearize_recurse(const fqon_t &name,
}


C3Error::C3Error(const std::string &msg)
:
C3Error::C3Error(const std::string &msg) :
Error{msg} {}


Expand Down
6 changes: 2 additions & 4 deletions nyan/change_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ ObjectChanges &ChangeTracker::track_patch(const fqon_t &target_name) {
// else: create a new one.
auto it = this->changes.find(target_name);
if (it == std::end(this->changes)) {
return this->changes.emplace(
target_name,
ObjectChanges{}
).first->second;
auto it_new_tracker = this->changes.emplace(target_name, ObjectChanges{}).first;
return it_new_tracker->second;
}
else {
return it->second;
Expand Down
1 change: 0 additions & 1 deletion nyan/change_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class ObjectChanges {
*/
class ChangeTracker {
public:

/**
* Get the ObjectChanges for an object targeted by a patch
* from the changes map or create a new one if there doesn't
Expand Down
10 changes: 5 additions & 5 deletions nyan/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
* The expression is expected to be true (=likely) or false (=unlikely).
*/
#if defined(__GNUC__)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#else
#define likely(x) (x)
#define likely(x) (x)
#define unlikely(x) (x)
#endif

Expand All @@ -31,9 +31,9 @@
*/
#if defined(_WIN32)
#if defined(nyan_EXPORTS)
#define NYANAPI __declspec(dllexport) // library is built
#define NYANAPI __declspec(dllexport) // library is built
#else
#define NYANAPI __declspec(dllimport) // library is used
#define NYANAPI __declspec(dllimport) // library is used
#endif /* nyan_EXPORTS */
#else
#define NYANAPI __attribute__((visibility("default")))
Expand Down
6 changes: 3 additions & 3 deletions nyan/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
#pragma once

#ifdef _MSC_VER
// Allow using alternative operator representation with non-conforming compiler
#include <ciso646>
// Allow using alternative operator representation with non-conforming compiler
#include <ciso646>
#endif

#include <cstdint>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <string>

Expand Down
Loading