-
Notifications
You must be signed in to change notification settings - Fork 12
Add support for generating structs #59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| This file is part of libkode | ||
|
|
||
| Copyright (c) 2025 Miklos Marton <martonmiklosqdev@gmail.com> | ||
|
|
||
| This library is free software; you can redistribute it and/or | ||
| modify it under the terms of the GNU Library General Public | ||
| License as published by the Free Software Foundation; either | ||
| version 2 of the License, or (at your option) any later version. | ||
|
|
||
| This library is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| Library General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Library General Public License | ||
| along with this library; see the file COPYING.LIB. If not, write to | ||
| the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
| Boston, MA 02110-1301, USA. | ||
| */ | ||
| #include "struct.h" | ||
|
|
||
| #include "code.h" | ||
|
|
||
| using namespace KODE; | ||
|
|
||
| class Struct::Private | ||
| { | ||
| public: | ||
| QString mName; | ||
| bool mTypedef = false; | ||
| Variable::List mMembers; | ||
| }; | ||
|
|
||
| Struct::Struct() : d(new Private) {} | ||
|
|
||
| void Struct::setTypedef(bool typeDef) | ||
| { | ||
| d->mTypedef = typeDef; | ||
| } | ||
|
|
||
| void Struct::setName(const QString &name) | ||
| { | ||
| d->mName = name; | ||
| } | ||
|
|
||
| QString Struct::name() const | ||
| { | ||
| return d->mName; | ||
| } | ||
|
|
||
| void Struct::addMemberVariable(const Variable &variable) | ||
| { | ||
| d->mMembers.append(variable); | ||
| } | ||
|
|
||
| Variable::List Struct::memberVariables() const | ||
| { | ||
| return d->mMembers; | ||
| } | ||
|
|
||
| void Struct::printDeclaration(KODE::Code &code) const | ||
| { | ||
| if (d->mTypedef) | ||
| code.addLine(QStringLiteral("typedef struct {")); | ||
| else | ||
| code.addLine(QStringLiteral("struct %1 {").arg(d->mName)); | ||
| code.indent(); | ||
|
|
||
| for (const auto &member : d->mMembers) { | ||
| code.addLine(QString("%1 %2 %3;") | ||
| .arg(member.isStatic() ? "static" : "", member.type(), member.name())); | ||
| } | ||
| code.unindent(); | ||
| if (d->mTypedef) | ||
| code.addLine(QString("} %1;").arg(d->mName)); | ||
| else | ||
| code.addLine("};"); | ||
|
|
||
| code.newLine(); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| This file is part of libkode | ||
|
|
||
| Copyright (c) 2025 Miklos Marton <martonmiklosqdev@gmail.com> | ||
|
|
||
| This library is free software; you can redistribute it and/or | ||
| modify it under the terms of the GNU Library General Public | ||
| License as published by the Free Software Foundation; either | ||
| version 2 of the License, or (at your option) any later version. | ||
|
|
||
| This library is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| Library General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU Library General Public License | ||
| along with this library; see the file COPYING.LIB. If not, write to | ||
| the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
| Boston, MA 02110-1301, USA. | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <kode_export.h> | ||
| #include "variable.h" | ||
| #include "code.h" | ||
|
|
||
| #include <QString> | ||
|
|
||
| namespace KODE { | ||
|
|
||
| class KODE_EXPORT Struct | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it make more sense to flag a Class as being a struct, to avoid the duplication between Class and Struct?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Makes sense, let me rework the code in that way. |
||
| { | ||
| public: | ||
| typedef QList<Struct> List; | ||
|
|
||
| Struct(); | ||
|
|
||
| void setTypedef(bool typeDef = true); | ||
| /** | ||
| * Sets the @param name of the class object. | ||
| */ | ||
| void setName(const QString &name); | ||
|
|
||
| /** | ||
| * Returns the name of the class object. | ||
| */ | ||
| QString name() const; | ||
|
|
||
| /** | ||
| * Adds a member @param variable to the class object. | ||
| */ | ||
| void addMemberVariable(const KODE::Variable &variable); | ||
|
|
||
| /** | ||
| * Returns the list of all member variables. | ||
| */ | ||
| KODE::Variable::List memberVariables() const; | ||
|
|
||
| /** | ||
| * Prints the declaration of the enum to the code | ||
| */ | ||
| void printDeclaration(KODE::Code &code) const; | ||
|
|
||
| private: | ||
| class Private; | ||
| Private *d; | ||
| }; | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/enums/structs/ ?