Skip to content

Commit

Permalink
Add a bool type to style format.
Browse files Browse the repository at this point in the history
  • Loading branch information
john-preston committed Aug 9, 2023
1 parent 5a11029 commit 2d03abc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
4 changes: 4 additions & 0 deletions codegen/style/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ QString Generator::typeToString(structure::Type type) const {
switch (type.tag) {
case Tag::Invalid: return QString();
case Tag::Int: return "int";
case Tag::Bool: return "bool";
case Tag::Double: return "double";
case Tag::Pixels: return "int";
case Tag::String: return "QString";
Expand All @@ -256,6 +257,7 @@ QString Generator::typeToDefaultValue(structure::Type type) const {
switch (type.tag) {
case Tag::Invalid: return QString();
case Tag::Int: return "0";
case Tag::Bool: return "false";
case Tag::Double: return "0.";
case Tag::Pixels: return "0";
case Tag::String: return "QString()";
Expand Down Expand Up @@ -292,6 +294,7 @@ QString Generator::valueAssignmentCode(
switch (value.type().tag) {
case Tag::Invalid: return QString();
case Tag::Int: return QString("%1").arg(value.Int());
case Tag::Bool: return QString(value.Bool() ? "true" : "false");
case Tag::Double: return QString("%1").arg(value.Double());
case Tag::Pixels: return pxValueName(value.Int());
case Tag::String: return QString("QString::fromUtf8(%1)").arg(stringToEncodedString(value.String()));
Expand Down Expand Up @@ -1153,6 +1156,7 @@ bool Generator::collectUniqueValues() {
switch (value.type().tag) {
case Tag::Invalid:
case Tag::Int:
case Tag::Bool:
case Tag::Double:
case Tag::String:
case Tag::Color:
Expand Down
39 changes: 27 additions & 12 deletions codegen/style/parsed_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ std::string logType(const structure::Type &type) {
}
static auto builtInTypes = new QMap<structure::TypeTag, std::string> {
{ structure::TypeTag::Int , "int" },
{ structure::TypeTag::Bool , "bool" },
{ structure::TypeTag::Double , "double" },
{ structure::TypeTag::Pixels , "pixels" },
{ structure::TypeTag::String , "string" },
Expand Down Expand Up @@ -304,6 +305,8 @@ structure::Value ParsedFile::readValue() {
return pointValue;
} else if (auto sizeValue = readSizeValue()) {
return sizeValue;
} else if (auto boolValue = readBoolValue()) {
return boolValue;
} else if (auto alignValue = readAlignValue()) {
return alignValue;
} else if (auto marginsValue = readMarginsValue()) {
Expand Down Expand Up @@ -568,8 +571,8 @@ structure::Value ParsedFile::readColorValue() {
}

structure::Value ParsedFile::readPointValue() {
if (auto font = file_.getToken(BasicType::Name)) {
if (tokenValue(font) == "point") {
if (auto name = file_.getToken(BasicType::Name)) {
if (tokenValue(name) == "point") {
assertNextToken(BasicType::LeftParenthesis);

auto x = readNumericOrNumericCopyValue(); assertNextToken(BasicType::Comma);
Expand All @@ -589,8 +592,8 @@ structure::Value ParsedFile::readPointValue() {
}

structure::Value ParsedFile::readSizeValue() {
if (auto font = file_.getToken(BasicType::Name)) {
if (tokenValue(font) == "size") {
if (auto name = file_.getToken(BasicType::Name)) {
if (tokenValue(name) == "size") {
assertNextToken(BasicType::LeftParenthesis);

auto w = readNumericOrNumericCopyValue(); assertNextToken(BasicType::Comma);
Expand All @@ -609,9 +612,21 @@ structure::Value ParsedFile::readSizeValue() {
return {};
}

structure::Value ParsedFile::readBoolValue() {
if (auto value = file_.getToken(BasicType::Name)) {
if (tokenValue(value) == "true") {
return { structure::TypeTag::Bool, true };
} else if (tokenValue(value) == "false") {
return { structure::TypeTag::Bool, false };
}
file_.putBack();
}
return {};
}

structure::Value ParsedFile::readAlignValue() {
if (auto font = file_.getToken(BasicType::Name)) {
if (tokenValue(font) == "align") {
if (auto name = file_.getToken(BasicType::Name)) {
if (tokenValue(name) == "align") {
assertNextToken(BasicType::LeftParenthesis);

auto align = tokenValue(assertNextToken(BasicType::Name));
Expand All @@ -630,8 +645,8 @@ structure::Value ParsedFile::readAlignValue() {
}

structure::Value ParsedFile::readMarginsValue() {
if (auto font = file_.getToken(BasicType::Name)) {
if (tokenValue(font) == "margins") {
if (auto name = file_.getToken(BasicType::Name)) {
if (tokenValue(name) == "margins") {
assertNextToken(BasicType::LeftParenthesis);

auto l = readNumericOrNumericCopyValue(); assertNextToken(BasicType::Comma);
Expand All @@ -655,8 +670,8 @@ structure::Value ParsedFile::readMarginsValue() {
}

structure::Value ParsedFile::readFontValue() {
if (auto font = file_.getToken(BasicType::Name)) {
if (tokenValue(font) == "font") {
if (auto name = file_.getToken(BasicType::Name)) {
if (tokenValue(name) == "font") {
assertNextToken(BasicType::LeftParenthesis);

int flags = 0;
Expand Down Expand Up @@ -701,8 +716,8 @@ structure::Value ParsedFile::readFontValue() {
}

structure::Value ParsedFile::readIconValue() {
if (auto font = file_.getToken(BasicType::Name)) {
if (tokenValue(font) == "icon") {
if (auto name = file_.getToken(BasicType::Name)) {
if (tokenValue(name) == "icon") {
std::vector<structure::data::monoicon> parts;
if (file_.getToken(BasicType::LeftBrace)) { // complex icon
do {
Expand Down
2 changes: 2 additions & 0 deletions codegen/style/parsed_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class ParsedFile {
structure::Value readColorValue();
structure::Value readPointValue();
structure::Value readSizeValue();
structure::Value readBoolValue();
structure::Value readAlignValue();
structure::Value readMarginsValue();
structure::Value readFontValue();
Expand Down Expand Up @@ -117,6 +118,7 @@ class ParsedFile {

QMap<std::string, structure::Type> typeNames_ = {
{ "int" , { structure::TypeTag::Int } },
{ "bool" , { structure::TypeTag::Bool } },
{ "double" , { structure::TypeTag::Double } },
{ "pixels" , { structure::TypeTag::Pixels } },
{ "string" , { structure::TypeTag::String } },
Expand Down
19 changes: 19 additions & 0 deletions codegen/style/structure_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ struct Value::DataTypes {

};

class TBool : public DataBase {
public:
TBool(bool value) : value_(value) {
}
bool Bool() const override { return value_; }

private:
bool value_;

};

class TDouble : public DataBase {
public:
TDouble(double value) : value_(value) {
Expand Down Expand Up @@ -166,6 +177,13 @@ Value::Value(TypeTag type, int value) : Value(type, std::make_shared<DataTypes::
}
}

Value::Value(TypeTag type, bool value) : Value(type, std::make_shared<DataTypes::TBool>(value)) {
if (type_.tag != TypeTag::Bool) {
type_.tag = TypeTag::Invalid;
data_ = std::make_shared<DataBase>();
}
}

Value::Value(TypeTag type, std::string value) : Value(type, std::make_shared<DataTypes::TString>(value)) {
if (type_.tag != TypeTag::String &&
type_.tag != TypeTag::Align) {
Expand All @@ -178,6 +196,7 @@ Value::Value(Type type, Qt::Initialization) : type_(type) {
switch (type_.tag) {
case TypeTag::Invalid: data_ = std::make_shared<DataBase>(); break;
case TypeTag::Int: data_ = std::make_shared<DataTypes::TInt>(0); break;
case TypeTag::Bool: data_ = std::make_shared<DataTypes::TBool>(false); break;
case TypeTag::Double: data_ = std::make_shared<DataTypes::TDouble>(0.); break;
case TypeTag::Pixels: data_ = std::make_shared<DataTypes::TInt>(0); break;
case TypeTag::String: data_ = std::make_shared<DataTypes::TString>(""); break;
Expand Down
6 changes: 6 additions & 0 deletions codegen/style/structure_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Value;
enum class TypeTag {
Invalid,
Int,
Bool,
Double,
Pixels,
String,
Expand Down Expand Up @@ -114,6 +115,9 @@ class Value {
// Can be int / pixels.
Value(TypeTag type, int value);

// Can be only bool.
Value(TypeTag type, bool value);

// Can be string / align.
Value(TypeTag type, std::string value);

Expand All @@ -122,6 +126,7 @@ class Value {

Type type() const { return type_; }
int Int() const { return data_->Int(); }
bool Bool() const { return data_->Bool(); }
double Double() const { return data_->Double(); }
std::string String() const { return data_->String(); }
data::point Point() const { return data_->Point(); }
Expand Down Expand Up @@ -151,6 +156,7 @@ class Value {
class DataBase {
public:
virtual int Int() const { return 0; }
virtual bool Bool() const { return false; }
virtual double Double() const { return 0.; }
virtual std::string String() const { return std::string(); }
virtual data::point Point() const { return {}; };
Expand Down

0 comments on commit 2d03abc

Please sign in to comment.