Author SHA1 Message Date
Borna Rajković ae2a3c64ef Fixed pin selection 2022-06-12 16:40:35 +02:00
Borna Rajković 09991904e9 Added dynamic color support + text display item 2022-06-12 16:02:24 +02:00
Borna Rajković e6bbaabde4 Added antialiasing 2022-06-12 13:43:35 +02:00
Borna Rajković 6dfe86335d Added cancel to dialogs 2022-06-12 11:51:57 +02:00
Borna Rajković 4d4cf136fb Added pin connection editing 2022-06-09 23:42:45 +02:00
Borna Rajković f66fc1db26 Updated translations + bugfixes 2022-06-09 20:24:27 +02:00
Borna Rajković 1759adf25a Moved connection determination to schema 2022-06-07 21:13:21 +02:00
Borna Rajković 1ec8b10ef5 Fixed schema generation 2022-06-06 01:10:11 +02:00
Borna Rajković 9b7ede933a Added single automatic connection dialog + bugfixes 2022-06-05 19:02:44 +02:00
Borna Rajković 3172d7e4cf Added component/bus deletion and rename 2022-06-02 01:37:14 +02:00
Borna Rajković f93a2afa63 Updated component and bus generation logic + bugfixes 2022-05-31 01:05:08 +02:00
Borna Rajković 86b8861533 Extracted schema and library management to Application 2022-05-30 23:11:06 +02:00
Borna Rajković 0c4afe87d9 Basic refactor 2022-05-27 08:18:17 +02:00
Borna Rajković aac972c6ca Updated bus rendering 2022-05-26 19:22:10 +02:00
Borna Rajković 5cf8235608 Added connection generation 2022-05-25 07:39:45 +02:00
Borna Rajković cbff8ff5f2 Added drag&drop to insert component or bus instances 2022-05-23 08:48:13 +02:00
Borna Rajković 0a658a4aba Added count and pin validators 2022-05-20 00:44:40 +02:00
Borna Rajković 1100a9f0b9 Added connection display 2022-05-19 21:08:00 +02:00
Borna Rajković f11551fef6 Added connection display 2022-05-18 00:14:33 +02:00
Borna Rajković 281926cb8b Added movement storing 2022-05-17 21:31:32 +02:00
Borna Rajković e2e0506041 Added support for @componentHeader 2022-05-16 01:02:34 +02:00
Borna Rajković 59b19062ff Added support for memory attribute 2022-05-15 23:55:03 +02:00
Borna Rajković bb423c2184 Added warning and error dialogs to attribute change 2022-05-15 18:38:17 +02:00
Borna Rajković 65020c2b7f Updated name dialog 2022-05-15 17:00:20 +02:00
Borna Rajković 23764776f1 Updated attribute dialog 2022-05-15 16:13:29 +02:00
Borna Rajković 05fa2abbb0 Refactored function validators 2022-05-15 11:17:05 +02:00
Borna Rajković e7fd7df154 Added display code generation 2022-05-14 15:58:44 +02:00
Borna Rajković 45a668fc46 Added comdel system generation
// todo display generation
2022-05-09 00:51:47 +02:00
Borna Rajković 74a7ef3e7a Refactored instances 2022-05-08 15:47:47 +02:00
Borna Rajković fb8aafb2a9 Updated support for single automatic bus 2022-05-08 14:50:56 +02:00
Borna Rajković 9ff6633777 Removed @wire from schema 2022-05-07 18:28:31 +02:00
Borna Rajković 5ccd9b2fa4 Cleaned up connections 2022-05-07 14:19:43 +02:00
Borna Rajković 344fe7cfa6 Refactor 2022-05-07 13:20:09 +02:00
104 changed files with 6348 additions and 2984 deletions

No files matched your search

+13 -14
View File
@@ -14,29 +14,28 @@ add_executable(SchemeEditor
comdel/domain/value.cpp comdel/domain/value.cpp
comdel/domain/schema.cpp comdel/domain/schema.cpp
comdel/domain/component.cpp comdel/domain/component.cpp
comdel/domain/connectioninstance.cpp comdel/domain/connection_instance.cpp
comdel/domain/rule.cpp comdel/domain/rule.cpp
comdel/domain/wireinstance.cpp
comdel/domain/attribute.cpp comdel/domain/attribute.cpp
comdel/domain/bus.cpp comdel/domain/bus.cpp
comdel/domain/pin.cpp comdel/domain/pin.cpp
comdel/domain/display.cpp comdel/domain/display.cpp
comdel/domain/library.cpp comdel/domain/library.cpp
comdel/domain/functionsignature.cpp comdel/domain/function_signature.cpp
comdel/domain/addressspace.cpp comdel/domain/address_space.cpp
comdel/domain/instanceattribute.cpp comdel/domain/instance_attribute.cpp
comdel/domain/connection.cpp comdel/domain/connection.cpp
comdel/domain/instance.cpp comdel/domain/instance.cpp
comdel/domain/schemacreator.cpp comdel/domain/schema_creator.cpp
comdel/parser/comdelparser.cpp comdel/parser/comdel_parser.cpp
comdel/parser/token.cpp comdel/parser/token.cpp
comdel/parser/sourceerror.cpp comdel/parser/source_error.cpp
comdel/parser/parsecontext.cpp comdel/parser/parse_context.cpp
comdel/parser/tokenstype.cpp comdel/parser/tokens_type.cpp
comdel/parser/astnode.cpp comdel/parser/ast_nodes.cpp
comdel/parser/parserutil.cpp comdel/parser/parser_util.cpp
comdel/parser/comdellexer.cpp comdel/parser/comdel_lexer.cpp
main.cpp main.cpp
mainwindow.ui mainwindow.ui
comdel/domain/comdelvalidator.cpp comdel/domain/comdelvalidator.h comdel/display/dialogmanager.cpp comdel/display/dialogmanager.h comdel/display/attribute_dialog.cpp comdel/display/attribute_dialog.h comdel/display/name_dialog.cpp comdel/display/name_dialog.h) comdel/domain/comdel_validator.cpp comdel/domain/comdel_validator.h comdel/display/attribute_dialog.cpp comdel/display/attribute_dialog.h comdel/display/name_dialog.cpp comdel/display/name_dialog.h comdel/domain/comdel_generator.cpp comdel/domain/comdel_generator.h comdel/display/library_list.cpp comdel/display/library_list.h application.cpp application.h comdel/display/single_automatic_dialog.cpp comdel/display/single_automatic_dialog.h comdel/parser/color.cpp comdel/parser/color.h)
target_link_libraries(SchemeEditor Qt5::Core Qt5::Gui Qt5::Widgets) target_link_libraries(SchemeEditor Qt5::Core Qt5::Gui Qt5::Widgets)
+37 -34
View File
@@ -9,77 +9,80 @@ CONFIG += c++17
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \ SOURCES += \
comdel/display/attribute_dialog.cpp \
comdel/display/component_display.cpp \ comdel/display/component_display.cpp \
comdel/display/library_display.cpp \ comdel/display/library_display.cpp \
comdel/display/library_list.cpp \
comdel/display/name_dialog.cpp \
comdel/display/schema_display.cpp \ comdel/display/schema_display.cpp \
comdel/domain/addressspace.cpp \ comdel/display/single_automatic_dialog.cpp \
comdel/domain/address_space.cpp \
comdel/domain/attribute.cpp \ comdel/domain/attribute.cpp \
comdel/domain/bus.cpp \ comdel/domain/bus.cpp \
comdel/domain/schemacreator.cpp \ comdel/domain/comdel_generator.cpp \
comdel/domain/comdel_validator.cpp \
comdel/domain/component.cpp \ comdel/domain/component.cpp \
comdel/domain/connection.cpp \ comdel/domain/connection.cpp \
comdel/domain/comdelvalidator.cpp \ comdel/domain/connection_instance.cpp \
comdel/domain/connectioninstance.cpp \
comdel/domain/display.cpp \ comdel/domain/display.cpp \
comdel/domain/functionsignature.cpp \ comdel/domain/function_signature.cpp \
comdel/domain/instance.cpp \ comdel/domain/instance.cpp \
comdel/domain/instanceattribute.cpp \ comdel/domain/instance_attribute.cpp \
comdel/domain/library.cpp \ comdel/domain/library.cpp \
comdel/domain/pin.cpp \ comdel/domain/pin.cpp \
comdel/domain/rule.cpp \ comdel/domain/rule.cpp \
comdel/domain/schema.cpp \ comdel/domain/schema.cpp \
comdel/domain/schema_creator.cpp \
comdel/domain/value.cpp \ comdel/domain/value.cpp \
comdel/domain/wireinstance.cpp \ comdel/parser/ast_nodes.cpp \
comdel/parser/astnode.cpp \ comdel/parser/comdel_lexer.cpp \
comdel/parser/comdellexer.cpp \ comdel/parser/comdel_parser.cpp \
comdel/parser/comdelparser.cpp \ comdel/parser/parse_context.cpp \
comdel/parser/parsecontext.cpp \ comdel/parser/parser_util.cpp \
comdel/parser/parserutil.cpp \ comdel/parser/source_error.cpp \
comdel/parser/sourceerror.cpp \
comdel/parser/token.cpp \ comdel/parser/token.cpp \
comdel/parser/tokenstype.cpp \ comdel/parser/tokens_type.cpp \
comdel/display/dialogmanager.cpp \ application.cpp \
comdel/display/attribute_dialog.cpp \
comdel/display/name_dialog.cpp \
main.cpp \ main.cpp \
mainwindow.cpp mainwindow.cpp
HEADERS += \ HEADERS += \
comdel/display/attribute_dialog.h \
comdel/display/component_display.h \ comdel/display/component_display.h \
comdel/display/library_display.h \ comdel/display/library_display.h \
comdel/display/library_list.h \
comdel/display/name_dialog.h \
comdel/display/schema_display.h \ comdel/display/schema_display.h \
comdel/domain/addressspace.h \ comdel/display/single_automatic_dialog.h \
comdel/domain/address_space.h \
comdel/domain/attribute.h \ comdel/domain/attribute.h \
comdel/domain/bus.h \ comdel/domain/bus.h \
comdel/domain/schemacreator.h \ comdel/domain/comdel_generator.h \
comdel/domain/comdel_validator.h \
comdel/domain/component.h \ comdel/domain/component.h \
comdel/domain/connection.h \ comdel/domain/connection.h \
comdel/domain/comdelvalidator.h \ comdel/domain/connection_instance.h \
comdel/domain/connectioninstance.h \
comdel/domain/display.h \ comdel/domain/display.h \
comdel/domain/functionsignature.h \ comdel/domain/function_signature.h \
comdel/domain/instance.h \ comdel/domain/instance.h \
comdel/domain/instanceattribute.h \ comdel/domain/instance_attribute.h \
comdel/domain/library.h \ comdel/domain/library.h \
comdel/domain/pin.h \ comdel/domain/pin.h \
comdel/domain/rule.h \ comdel/domain/rule.h \
comdel/domain/schema.h \ comdel/domain/schema.h \
comdel/domain/schema_creator.h \
comdel/domain/value.h \ comdel/domain/value.h \
comdel/domain/wireinstance.h \ comdel/parser/ast_nodes.h \
comdel/parser/astnode.h \ comdel/parser/comdel_lexer.h \
comdel/parser/comdellexer.h \ comdel/parser/comdel_parser.h \
comdel/parser/comdelparser.h \
comdel/parser/expected.h \ comdel/parser/expected.h \
comdel/parser/parsecontext.h \ comdel/parser/parse_context.h \
comdel/parser/parserutil.h \ comdel/parser/parser_util.h \
comdel/parser/poly.h \ comdel/parser/poly.h \
comdel/parser/presult.h \ comdel/parser/presult.h \
comdel/parser/sourceerror.h \ comdel/parser/source_error.h \
comdel/parser/token.h \ comdel/parser/token.h \
comdel/parser/tokenstype.h \ comdel/parser/tokens_type.h \
comdel/display/dialogmanager.h \
comdel/display/attribute_dialog.h \
comdel/display/name_dialog.h \
mainwindow.h mainwindow.h
FORMS += \ FORMS += \
+394
View File
@@ -0,0 +1,394 @@
//
// Created by bbr on 27.05.22..
//
#include "application.h"
#include "comdel/parser/parse_context.h"
#include "comdel/parser/parser_util.h"
#include "comdel/domain/schema_creator.h"
#include "comdel/domain/comdel_generator.h"
#include "comdel/domain/comdel_validator.h"
std::optional<domain::Library> Application::getLibrary() {
return library;
}
domain::Schema *Application::getSchema() {
return schema;
}
void Application::clear() {
if (schema != nullptr) {
delete schema;
schema = nullptr;
}
library = std::nullopt;
libraryPath = "";
}
bool Application::loadLibrary(std::string &filename, std::ostream &errorOutput) {
clear();
ParseContext parseContext;
auto libraryNode = load_library_from_file(&parseContext, filename.c_str(), errorOutput);
if (libraryNode) {
domain::SchemaCreator generator(validators);
library = generator.loadLibrary(*libraryNode);
for (auto &error: generator.getErrors()) {
parseContext.formatError(error, errorOutput, "ERROR: ");
}
if (library.has_value()) {
libraryPath = filename;
// on library load we create a new schema
schema = new domain::Schema(library.value());
} else {
errorOutput << "Failed creating library model" << std::endl;
return false;
}
} else {
errorOutput << "Failed parsing library" << std::endl;
return false;
}
return true;
}
std::pair<bool, std::vector<domain::ValidationError>> Application::loadSchema(std::string &filename, std::ostream &errorOutput) {
clear();
std::vector<domain::ValidationError> errors;
ParseContext parseContext;
auto schemaNode = load_schema_from_file(&parseContext, filename.c_str(), errorOutput);
if (schemaNode) {
domain::SchemaCreator generator(validators);
library = generator.loadLibrary(*schemaNode->library);
libraryPath = schemaNode->source->asString();
for (auto &error: generator.getErrors()) {
parseContext.formatError(error, errorOutput, "ERROR: ");
}
if (library) {
schema = generator.loadSchema(*schemaNode, *library);
for (auto &error: generator.getErrors()) {
parseContext.formatError(error, errorOutput, "ERROR: ");
}
if (schema == nullptr) {
clear();
return {false, errors};
}
} else {
clear();
return {false, errors};
}
} else {
errorOutput << "Failed parsing library" << std::endl;
return {false, errors};
}
domain::ComdelValidator validator{validators};
domain::ValidationContext context;
context.instance = nullptr;
context.attribute = nullptr;
context.addressSpaces = {};
auto memoryReferenceValidation = validator.validateMemoryReferences(*schema, *library, context);
errors.insert(errors.end(), memoryReferenceValidation.begin(), memoryReferenceValidation.end());
auto nameValidation = validator.validateInstanceNames(*schema, *library, context);
errors.insert(errors.end(), nameValidation.begin(), nameValidation.end());
if(!errors.empty()) {
clear();
return {false, errors};
}
return {true, errors};
}
bool Application::generateSchema(std::ostringstream &output) {
if (schema == nullptr) {
return false;
}
domain::generate_schema(libraryPath, schema, output);
return true;
}
std::vector<domain::ValidationError> Application::validateSchema() {
if (schema == nullptr) {
return std::vector<domain::ValidationError>{domain::ValidationError(domain::Action::ERROR, "No schema loaded")};
}
domain::ComdelValidator validator{validators};
domain::ValidationContext context;
context.instance = nullptr;
context.attribute = nullptr;
context.addressSpaces = {};
for (auto &lib: library->getAddressSpaces()) {
context.addressSpaces.insert(std::make_pair(lib.getName(), lib));
}
auto errors = validator.validateSchema(*schema, context);
auto memoryReferenceValidation = validator.validateMemoryReferences(*schema, *library, context);
errors.insert(errors.end(), memoryReferenceValidation.begin(), memoryReferenceValidation.end());
auto nameValidation = validator.validateInstanceNames(*schema, *library, context);
errors.insert(errors.end(), nameValidation.begin(), nameValidation.end());
auto countValidation = validator.validateInstanceCount(*schema, *library, context);
errors.insert(errors.end(), countValidation.begin(), countValidation.end());
auto pinValidation = validator.validatePinConnections(*schema, *library, context);
errors.insert(errors.end(), pinValidation.begin(), pinValidation.end());
return errors;
}
std::vector<domain::ValidationError> Application::generateComdel(std::ostringstream &output) {
auto errors = validateSchema();
if (!Application::hasErrors(errors)) {
// as long as all validation errors are warning we continue with build
domain::generate_comdel(schema, library.value(), output);
}
return errors;
}
bool Application::hasErrors(std::vector<domain::ValidationError> errors) {
for (auto &err: errors) {
if (err.type == domain::Action::ERROR) {
return true;
}
}
return false;
}
// static instance of application
static Application *application = nullptr;
Application *Application::instance() {
if (application == nullptr) {
application = new Application();
}
return application;
}
std::shared_ptr<domain::ComponentInstance>
Application::addComponent(domain::Component component, std::vector<domain::InstanceAttribute> attributes, int x,
int y) {
std::set<std::string> names;
for (const auto &c: schema->componentInstances) {
names.insert(c->name);
}
std::string name = generateName(names, component.getInstanceName());
schema->componentInstances.push_back(
std::make_shared<domain::ComponentInstance>(name, attributes, std::make_pair(x, y), component));
return schema->componentInstances.back();
}
std::shared_ptr<domain::BusInstance> Application::addBus(domain::Bus bus, int x, int y) {
std::set<std::string> names;
for (const auto &b: schema->busInstances) {
names.insert(b->name);
}
std::string name = generateName(names, bus.getInstanceName());
schema->busInstances.push_back(std::make_shared<domain::BusInstance>(name, std::make_pair(x, y), bus));
return schema->busInstances.back();
}
std::string Application::generateName(std::set<std::string> &names, std::string instanceName) {
if (names.find(instanceName) == names.end()) {
return instanceName;
}
char buffer[4];
for (int i = 0; i < 1000; i++) {
sprintf(buffer, "%03d", i);
auto name = instanceName + "_" + buffer;
if (names.find(name) == names.end()) {
return name;
}
}
// return default value as this should never happen
return instanceName;
}
bool Application::removeComponent(std::string componentName) {
auto component = findComponentByName(componentName);
if (component == nullptr) {
return false;
}
std::set<domain::BusInstance*> automaticBusses;
schema->connections.erase(
std::remove_if(
schema->connections.begin(),
schema->connections.end(),
[component, &automaticBusses](const std::shared_ptr<domain::ConnectionInstance> &conn) {
auto busConnection = dynamic_cast<domain::BusConnectionInstance *>(conn.get());
if (busConnection) {
if (busConnection->instance == component) {
return true;
}
}
auto directConnection = dynamic_cast<domain::DirectConnectionInstance *>(conn.get());
if (directConnection) {
if (directConnection->instance == component || directConnection->secondInstance == component) {
automaticBusses.insert(directConnection->bus);
return true;
}
}
return false;
}),
schema->connections.end()
);
schema->busInstances.erase(
std::remove_if(
schema->busInstances.begin(),
schema->busInstances.end(),
[&automaticBusses](const std::shared_ptr<domain::BusInstance> &bus) {
return automaticBusses.count(bus.get());
}),
schema->busInstances.end()
);
if(component->component.getType() == domain::Component::MEMORY) {
for(auto& comp: schema->componentInstances) {
if(comp->component.getType() == domain::Component::PROCESSOR) {
for(auto& attribute: comp->attributes) {
if(attribute.value.isType(domain::Value::MEMORY_REFERENCE) && attribute.value.asMemoryReference() == component->name) {
attribute.value = domain::Value::fromMemoryReference(std::nullopt);
}
}
}
}
}
for(auto iter = schema->componentInstances.begin(); iter != schema->componentInstances.end(); ++iter) {
if(iter->get() == component) {
schema->componentInstances.erase(iter);
break;
}
}
return true;
}
void Application::removeConnection(domain::ConnectionInstance *connectionInstance) {
if(auto directConnection = dynamic_cast<domain::DirectConnectionInstance*>(connectionInstance)) {
schema->busInstances.erase(
std::remove_if(
schema->busInstances.begin(),
schema->busInstances.end(),
[directConnection](const std::shared_ptr<domain::BusInstance> &bus) {
return directConnection->bus == bus.get();
}),
schema->busInstances.end()
);
}
schema->connections.erase(
std::remove_if(
schema->connections.begin(),
schema->connections.end(),
[connectionInstance](const std::shared_ptr<domain::ConnectionInstance> &conn) {
return connectionInstance == conn.get();
}),
schema->connections.end()
);
}
bool Application::removeBus(std::string busName) {
auto bus = findBusByName(busName);
if (bus == nullptr) {
return false;
}
schema->connections.erase(
std::remove_if(
schema->connections.begin(),
schema->connections.end(),
[bus](const std::shared_ptr<domain::ConnectionInstance> &conn) {
auto busConnection = dynamic_cast<domain::BusConnectionInstance *>(conn.get());
if (busConnection) {
if (busConnection->bus == bus) {
return true;
}
}
return false;
}),
schema->connections.end()
);
for(auto iter = schema->busInstances.begin(); iter != schema->busInstances.end(); ++iter) {
if(iter->get() == bus) {
schema->busInstances.erase(iter);
break;
}
}
return true;
}
domain::ComponentInstance *Application::findComponentByName(std::string componentName) {
for (auto &comp: schema->componentInstances) {
if (comp->name == componentName) {
return comp.get();
}
}
return nullptr;
}
domain::BusInstance *Application::findBusByName(std::string busName) {
for (auto &bus: schema->busInstances) {
if (bus->name == busName) {
return bus.get();
}
}
return nullptr;
}
void Application::renameComponent(std::string currentName, std::string newName) {
if(currentName == newName) {
return;
}
auto component = findComponentByName(currentName);
if(component) {
component->name = newName;
if(component->component.getType() == domain::Component::MEMORY) {
for(auto& comp: schema->componentInstances) {
if(comp.get()->component.getType() == domain::Component::PROCESSOR) {
for(auto& attribute: comp.get()->attributes) {
if(attribute.value.isType(domain::Value::MEMORY_REFERENCE) && attribute.value.asMemoryReference() == currentName) {
attribute.value = domain::Value::fromMemoryReference(newName);
}
}
}
}
}
}
}
void Application::renameBus(std::string currentName, std::string newName) {
if(currentName == newName) {
return;
}
auto bus = findBusByName(currentName);
if(bus) {
bus->name = newName;
}
}
+60
View File
@@ -0,0 +1,60 @@
//
// Created by bbr on 27.05.22..
//
#ifndef SCHEMEEDITOR_APPLICATION_H
#define SCHEMEEDITOR_APPLICATION_H
#include <set>
#include "comdel/domain/library.h"
#include "comdel/domain/schema.h"
#include "comdel/domain/comdel_validator.h"
class Application {
private:
std::string libraryPath;
std::optional<domain::Library> library = std::nullopt;
domain::Schema* schema = nullptr;
std::vector<domain::FunctionValidator*> validators = domain::getSupportedValidators();
std::string generateName(std::set<std::string>& names, std::string instanceName);
domain::ComponentInstance *findComponentByName(std::string componentName);
domain::BusInstance *findBusByName(std::string busName);
public:
std::optional<domain::Library> getLibrary();
domain::Schema* getSchema();
void clear();
bool loadLibrary(std::string& filename, std::ostream& errorOutput);
std::pair<bool, std::vector<domain::ValidationError>> loadSchema(std::string& filename, std::ostream& errorOutput);
static Application* instance();
bool generateSchema(std::ostringstream &output);
std::vector<domain::ValidationError> validateSchema();
std::vector<domain::ValidationError> generateComdel(std::ostringstream &output);
std::shared_ptr<domain::ComponentInstance> addComponent(domain::Component component, std::vector<domain::InstanceAttribute> attributes, int x, int y);
bool removeComponent(std::string component);
std::shared_ptr<domain::BusInstance> addBus(domain::Bus bus, int x, int y);
bool removeBus(std::string bus);
static bool hasErrors(std::vector<domain::ValidationError> empty);
~Application() = default;
void renameComponent(std::string currentName, std::string newName);
void renameBus(std::string currentName, std::string newName);
void removeConnection(domain::ConnectionInstance *pInstance);
};
#endif //SCHEMEEDITOR_APPLICATION_H
+278
View File
@@ -3,3 +3,281 @@
// //
#include "attribute_dialog.h" #include "attribute_dialog.h"
#include "mainwindow.h"
#include "application.h"
namespace display {
AttributeDialog::AttributeDialog(domain::InstanceAttribute *attribute, bool updating) {
setAttribute(Qt::WA_DeleteOnClose);
attributeValue = attribute;
auto actionType = updating ? "Izmjeni " : "Postavi ";
this->setWindowTitle(QString::fromStdString(actionType + attribute->attribute.getName()));
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
auto popup = *attribute->attribute.getPopup();
layout->addWidget(new QLabel(popup.getTitle().c_str()));
layout->addWidget(new QLabel(popup.getText().c_str()));
auto type = attribute->attribute.getDefault().getType();
value = attribute->value;
if (attribute->attribute.getPopup()->isEnumerated()) {
auto *combo = new QComboBox(this);
auto enumeration = attribute->attribute.getPopup()->getEnumeration();
for (auto entry: enumeration) {
combo->addItem(QString::fromStdString(entry.getName()));
}
connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
&AttributeDialog::onEnumerationChanged);
layout->addWidget(combo);
for (int i = 0; i < enumeration.size(); i++) {
if (attributeValue->value.equals(enumeration[i].getValue())) {
combo->setCurrentIndex(i);
break;
}
}
} else if (!(type == domain::Value::ValueType::WIRE_REFERENCE || type == domain::Value::ValueType::BOOL)) {
auto edit = new QLineEdit(this);
connect(edit, &QLineEdit::textChanged, this, &AttributeDialog::onTextChanged);
layout->addWidget(edit);
switch (attribute->attribute.getDefault().getType()) {
case domain::Value::ValueType::INT:
edit->setValidator(new QIntValidator(-10000000, 10000000, edit));
edit->insert(std::to_string(attribute->value.asInt()).c_str());
break;
case domain::Value::ValueType::STRING:
edit->insert(attribute->value.asString().c_str());
break;
default:
throw std::exception();
}
} else if (type == domain::Value::ValueType::BOOL) {
auto *group = new QGroupBox(this);
auto *radioLayout = new QHBoxLayout(group);
group->setLayout(radioLayout);
auto isTrue = new QRadioButton("da", group);
connect(isTrue, &QRadioButton::clicked, [this]() {
this->value = domain::Value::fromBool(true);
});
auto isFalse = new QRadioButton("ne", group);
connect(isFalse, &QRadioButton::clicked, [this]() {
this->value = domain::Value::fromBool(false);
});
if (attribute->value.asBool()) {
isTrue->setChecked(true);
} else {
isFalse->setChecked(true);
}
radioLayout->addWidget(isTrue);
radioLayout->addWidget(isFalse);
layout->addWidget(group);
}
auto buttonLayout = new QHBoxLayout(this);
auto okButton = new QPushButton(updating ? "Ažuriraj" : "Postavi", this);
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, this, &AttributeDialog::onUpdate);
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
}
void AttributeDialog::onUpdate() {
auto oldValue = attributeValue->value;
attributeValue->value = value;
domain::ComdelValidator validator(domain::getSupportedValidators());
domain::ValidationContext context;
for (auto &addressSpace: Application::instance()->getLibrary()->getAddressSpaces()) {
context.addressSpaces.insert(std::make_pair(addressSpace.getName(), addressSpace));
}
auto validationErrors = validator.validateAttribute(attributeValue, context);
if (validationErrors.empty()) {
accept();
} else {
bool canAccept = true;
std::vector<domain::ValidationError> errors;
std::vector<domain::ValidationError> warnings;
for (auto &err: validationErrors) {
if (err.type == domain::Action::ERROR) {
errors.push_back(err);
} else {
warnings.push_back(err);
}
}
if (!errors.empty()) {
canAccept = false;
auto errorDialog = new ErrorDialog(errors);
errorDialog->exec();
}
for (auto &warning: warnings) {
auto warningDialog = new WarningDialog(warning);
int response = warningDialog->exec();
if (response == QDialog::Rejected) {
canAccept = false;
}
}
if (canAccept) {
accept();
} else {
attributeValue->value = oldValue;
}
}
}
void AttributeDialog::onTextChanged(const QString &string) {
switch (value.getType()) {
case domain::Value::STRING:
value.setString(string.toStdString());
break;
case domain::Value::INT:
value = domain::Value::fromInt(parseInt(string.toStdString()));
break;
default:
throw std::exception();
}
}
void AttributeDialog::onEnumerationChanged(int index) {
value = attributeValue->attribute.getPopup()->getEnumeration()[index].getValue();
}
MemoryDialog::MemoryDialog(domain::InstanceAttribute *attribute,
std::vector<std::shared_ptr<domain::ComponentInstance>> instances, bool updating) {
memoryInstances = std::vector<std::string>();
setAttribute(Qt::WA_DeleteOnClose);
attributeValue = attribute;
auto actionType = updating ? "Izmjeni memoriju" : "Postavi memoriju";
this->setWindowTitle(QString::fromStdString(actionType));
for (auto &instance: instances) {
if (instance->component.getType() == domain::Component::MEMORY) {
memoryInstances.push_back(instance->name);
}
}
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
auto popup = *attribute->attribute.getPopup();
layout->addWidget(new QLabel(popup.getTitle().c_str()));
layout->addWidget(new QLabel(popup.getText().c_str()));
value = attribute->value;
auto *combo = new QComboBox(this);
for (auto &entry: memoryInstances) {
combo->addItem(QString::fromStdString(entry));
}
combo->addItem("null");
connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &MemoryDialog::onMemoryChanged);
layout->addWidget(combo);
combo->setCurrentIndex(memoryInstances.size());
for (int i = 0; i < memoryInstances.size(); i++) {
if (attributeValue->value.asMemoryReference().has_value() &&
attributeValue->value.asMemoryReference() == memoryInstances[i]) {
combo->setCurrentIndex(i);
break;
}
}
auto buttonLayout = new QHBoxLayout(this);
auto okButton = new QPushButton(updating ? "Ažuriraj" : "Postavi");
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, this, &MemoryDialog::onUpdate);
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
}
void MemoryDialog::onUpdate() {
attributeValue->value = value;
accept();
}
void MemoryDialog::onMemoryChanged(int index) {
if (index == memoryInstances.size()) {
value = domain::Value::fromMemoryReference(std::nullopt);
} else {
value = domain::Value::fromMemoryReference(memoryInstances[index]);
}
}
ErrorDialog::ErrorDialog(std::vector<domain::ValidationError> errors) {
setAttribute(Qt::WA_DeleteOnClose);
this->setWindowTitle("Greške");
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
for (auto &err: errors) {
layout->addWidget(new QLabel(QString::fromStdString(err.message), this));
}
}
WarningDialog::WarningDialog(domain::ValidationError error) {
setAttribute(Qt::WA_DeleteOnClose);
this->setWindowTitle("Upozorenje");
auto layout = new QVBoxLayout(this);
this->setLayout(layout);
layout->addWidget(new QLabel(QString::fromStdString(error.message), this));
auto buttonLayout = new QHBoxLayout(this);
auto okButton = new QPushButton("U redu", this);
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, [this]() { accept(); });
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
}
}
+53 -39
View File
@@ -2,72 +2,86 @@
#define ATTRIBUTE_DIALOG_H #define ATTRIBUTE_DIALOG_H
#include <QIntValidator> #include <QIntValidator>
#include <QPushButton>
#include <QComboBox>
#include <QGroupBox> #include <QGroupBox>
#include <QRadioButton> #include <QRadioButton>
#include <QDialog> #include <QDialog>
#include <QLineEdit> #include <QLineEdit>
#include <QLabel> #include <QLabel>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <comdel/domain/instanceattribute.h> #include <utility>
#include <comdel/domain/instance_attribute.h>
#include <comdel/domain/value.h> #include <comdel/domain/value.h>
#include "comdel/domain/comdel_validator.h"
namespace display { namespace display {
class AttributeDialog : public QDialog { class AttributeDialog : public QDialog {
domain::Value value;
long long int parseInt(std::string expression) {
try {
if (expression.size() > 2) {
if (expression.substr(0, 2) == "0x") {
return std::stoll(expression, nullptr, 16);
} else if (expression.substr(0, 2) == "0b") {
return std::stoll(expression, nullptr, 2);
} else {
return std::stoll(expression, nullptr, 10);
}
} else {
return std::stoll(expression, nullptr, 10);
}
} catch (std::exception &e) {
return 0;
}
}
domain::InstanceAttribute *attributeValue;
public: public:
AttributeDialog(domain::InstanceAttribute *attribute) { AttributeDialog(domain::InstanceAttribute *attribute, bool updating = true);
this->setWindowTitle(QString::fromStdString("Izmjeni " + attribute->attribute.getName()));
auto layout = new QVBoxLayout(this); public slots:
this->setLayout(layout);
auto popup = *attribute->attribute.getPopup();
layout->addWidget(new QLabel(popup.getTitle().c_str())); void onTextChanged(const QString &string);
layout->addWidget(new QLabel(popup.getText().c_str()));
auto type = attribute->attribute.getDefault().getType(); void onEnumerationChanged(int index);
if(attribute->attribute.getPopup()->isEnumerated()) { void onUpdate();
} else if(!(type == domain::Value::ValueType::WIRE_REFERENCE || type == domain::Value::ValueType::BOOL)) { };
auto edit = new QLineEdit(this);
layout->addWidget(edit);
switch (attribute->attribute.getDefault().getType()) {
case domain::Value::ValueType::INT:
edit->setValidator(new QIntValidator(-10000000, 10000000, edit));
edit->insert(std::to_string(attribute->value.asInt()).c_str());
break;
case domain::Value::ValueType::STRING:
edit->insert(attribute->value.asString().c_str());
break;
}
class MemoryDialog : public QDialog {
domain::Value value;
} else if(type == domain::Value::ValueType::BOOL) { domain::InstanceAttribute *attributeValue;
auto *group = new QGroupBox(this); std::vector<std::string> memoryInstances;
group->setCheckable(true);
group->setChecked(true);
auto isTrue = new QRadioButton("true", group); public:
auto isFalse = new QRadioButton("false", group); MemoryDialog(domain::InstanceAttribute *attribute,
std::vector<std::shared_ptr<domain::ComponentInstance>> instances, bool updating = true);
if(attribute->value.asBool()) { public slots:
isTrue->setChecked(true);
} else { void onMemoryChanged(int index);
isFalse->setChecked(true);
} void onUpdate();
};
class ErrorDialog : public QDialog {
public:
ErrorDialog(std::vector<domain::ValidationError> errors);
};
layout->addWidget(group); class WarningDialog : public QDialog {
} public:
} WarningDialog(domain::ValidationError error);
}; };
} }
+298 -103
View File
@@ -1,129 +1,324 @@
#include "component_display.h" #include "component_display.h"
#include "attribute_dialog.h"
#include "name_dialog.h"
#include "mainwindow.h"
#include "application.h"
#include "single_automatic_dialog.h"
#include <QMenu> #include <QMenu>
#include <QLine>
#include <QPen>
#include <QGraphicsSceneContextMenuEvent> #include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsScene> #include <set>
#include <iostream>
#include "dialogmanager.h"
namespace display { namespace display {
ComponentWrapper *ComponentWrapper::ofWire(domain::WireInstance *wire) { QPen connectionPen(QColor::fromRgb(150, 150, 250));
auto component = new ComponentWrapper();
component->wireInstance = wire;
component->redraw();
return component;
}
ComponentWrapper *ComponentWrapper::ofComponent(domain::ComponentInstance *instance) {
auto component = new ComponentWrapper();
component->componentInstance = instance;
component->setFlag(QGraphicsItem::ItemIsMovable, true);
component->componentItem = new ComponentItem(instance, component);
for(auto& pin: instance->component.getPins()) { Component::Component(const std::shared_ptr<domain::ComponentInstance> &instance): instance(instance) {
component->pinItems.push_back(new PinItem(pin, component)); setFlag(ItemSendsGeometryChanges, true);
setToolTip(QString::fromStdString(instance->name + "::" + instance->component.getName()));
instance->component.getDisplay().render(this, domain::ui::DisplayContext(instance.get()));
} }
component->redraw(); void Component::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
return component;
}
ComponentWrapper *ComponentWrapper::ofBus(domain::BusInstance *instance) {
auto component = new ComponentWrapper();
component->busInstance = instance;
component->setFlag(QGraphicsItem::ItemIsMovable, true);
component->redraw();
return component;
}
void ComponentWrapper::clear() {
for(auto item: childItems()) {
this->removeFromGroup(item);
item->scene()->removeItem(item);
delete item;
}
if(componentInstance) {
componentItem->clear();
for(auto pinItem: pinItems) {
pinItem->clear();
}
}
}
void ComponentWrapper::redraw() {
if(componentInstance) {
addToGroup(componentItem);
componentItem->redraw();
for(auto pinItem: pinItems) {
addToGroup(pinItem);
pinItem->redraw();
}
this->addToGroup(new QGraphicsSimpleTextItem(QString::fromStdString(componentInstance->name)));
}
if(busInstance && busInstance->bus.getDisplay()) {
busInstance->bus.getDisplay()->render(this);
this->addToGroup(new QGraphicsSimpleTextItem(QString::fromStdString(busInstance->name)));
}
if(wireInstance) {
wireInstance->display.render(this);
this->addToGroup(new QGraphicsSimpleTextItem(QString::fromStdString(wireInstance->name)));
}
}
ComponentItem::ComponentItem(domain::ComponentInstance *instance, QGraphicsItem *parent): componentInstance(instance) {
setParentItem(parent);
redraw();
setToolTip(QString::fromStdString(this->componentInstance->component.getTooltip()));
}
void ComponentItem::redraw() {
componentInstance->component.getDisplay().render(this);
}
void ComponentItem::clear() {
for(auto item: childItems()) {
this->removeFromGroup(item);
item->scene()->removeItem(item);
delete item;
}
}
void ComponentItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu; QMenu menu;
auto instance = this->componentInstance; menu.addAction("Izmjeni ime", [this]() {
menu.addAction("Izmjeni ime", [instance, this](){ std::set<std::string> names;
DialogManager::updateName(instance); for(const auto &component: Application::instance()->getSchema()->componentInstances) {
auto *wrapper = static_cast<ComponentWrapper*>(this->parentItem()); names.insert(component->name);
wrapper->clear(); }
wrapper->redraw(); auto dialog = new NameDialog(this->instance->name, names);
dialog->exec();
auto currentName = this->instance->name;
auto newName = dialog->getName();
Application::instance()->renameComponent(currentName, newName);
setToolTip(QString::fromStdString(instance->name + "::" + instance->component.getName()));
}); });
menu.addSeparator(); menu.addSeparator();
for(auto attr: componentInstance->attributes) { for (int i = 0; i < this->instance->attributes.size(); i++) {
auto *attr = &this->instance->attributes[i];
bool enabled = attr->attribute.getPopup().has_value(); bool enabled = attr->attribute.getPopup().has_value();
auto action = menu.addAction(QString::fromStdString("Izmjeni " + attr->name), if(attr->value.getType() == domain::Value::MEMORY_REFERENCE) {
[attr, this]() { menu.addAction("Izmjeni memoriju", [attr]() {
DialogManager::updateAttribute(attr); auto dialog = new MemoryDialog(attr,
Application::instance()->getSchema()->componentInstances);
this->redraw(); dialog->exec();
});
} else {
auto action = menu.addAction(QString::fromStdString("Izmjeni '" + attr->name + "'"),
[attr]() {
auto dialog = new AttributeDialog(attr);
dialog->exec();
}); });
action->setEnabled(enabled); action->setEnabled(enabled);
} }
}
menu.addSeparator();
menu.addAction(QString::fromStdString("Ukloni " + this->instance->name), [this]() {
Application::instance()->removeComponent(this->instance->name);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
menu.exec(event->screenPos());
}
void Pin::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
auto pinConnections = Application::instance()->getSchema()->getConnections(componentInstance->name, pin.getName());
if(isSingleAutomatic(pinConnections)) {
auto *update = menu.addMenu("Izmjeni");
auto *remove = menu.addMenu("Ukloni");
for (auto pinConnection: pinConnections) {
// this always must be true as only directConnections can be connected multiple times
if (auto directConnection = dynamic_cast<domain::DirectConnectionInstance *>(pinConnection)) {
if (directConnection->bus->bus.getType() == domain::Bus::SINGLE_AUTOMATIC) {
auto connectionName = directConnection->attributes[0].value.stringify() + "-" +
directConnection->attributes[1].value.stringify();
update->addAction(QString::fromStdString("Izmjeni " + connectionName), [directConnection]() {
auto dialog = new SingleAutomaticDialog(directConnection->attributes);
dialog->exec();
});
remove->addAction(QString::fromStdString("Ukloni " + connectionName),
[this, directConnection]() {
Application::instance()->removeConnection(directConnection);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
}
}
}
} else if(!pinConnections.empty()) {
auto pinConnection = pinConnections[0];
if(auto busConnection = dynamic_cast<domain::BusConnectionInstance*>(pinConnection)) {
menu.addSection(QString::fromStdString(busConnection->bus->name));
} else if(auto directConnection = dynamic_cast<domain::DirectConnectionInstance*>(pinConnection)) {
if(directConnection->instance == componentInstance.get()) {
menu.addSection(QString::fromStdString(directConnection->secondInstance->name + "." + directConnection->connection.getSecondComponent()->pin));
} else {
menu.addSection(QString::fromStdString(directConnection->instance->name + "." + directConnection->connection.getComponent().pin));
}
}
for (int i = 0; i < pinConnection->attributes.size(); i++) {
auto *attr = &pinConnection->attributes[i];
menu.addAction(QString::fromStdString("Izmjeni '" + attr->name + "'"),[attr]() {
auto dialog = new AttributeDialog(attr);
dialog->exec();
});
}
menu.addAction("Ukloni poveznicu", [this, pinConnection]() {
Application::instance()->removeConnection(pinConnection);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
}
menu.exec(event->screenPos()); menu.exec(event->screenPos());
} }
PinItem::PinItem(domain::Pin pin, QGraphicsItem *parent): pin(pin) { void Pin::mousePressEvent(QGraphicsSceneMouseEvent *event) {
setParentItem(parent); if (event->button() == Qt::MouseButton::LeftButton) {
redraw(); auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
setToolTip(QString::fromStdString(pin.getTooltip())); view->state = Schema::CREATING_CONNECTION;
} view->context.pin = this;
void PinItem::redraw() { view->context.startingPoint = dynamic_cast<ComponentGroup *>(this->parentItem())->pos() +
pin.getDisplay().render(this); QPointF(pin.getDisplayPin().getConnectionX(),
} pin.getDisplayPin().getConnectionY());
void PinItem::clear() { view->context.line = new QGraphicsLineItem(QLineF(view->context.startingPoint, event->scenePos()));
for(auto item: childItems()) { view->context.line->setPen(QPen(QColor::fromRgb(100, 100, 250), 1, Qt::PenStyle::DashLine));
this->removeFromGroup(item); view->context.line->setZValue(1000);
item->scene()->removeItem(item); this->scene()->addItem(view->context.line);
delete item;
view->showConnectable(this);
} }
} }
void Pin::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
if (view->state == Schema::CREATING_CONNECTION) {
auto line = view->context.line->line();
line.setP2(event->scenePos());
view->context.line->setLine(line);
}
}
void Pin::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
if (event->button() == Qt::MouseButton::LeftButton) {
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->state = Schema::DEFAULT;
this->scene()->removeItem(view->context.line);
delete view->context.line;
view->removeConnectable(event->scenePos());
}
}
domain::Pin &Pin::getPin() {
return pin;
}
domain::ComponentInstance *Pin::getComponentInstance() {
return componentInstance.get();
}
bool Pin::isSingleAutomatic(std::vector<domain::ConnectionInstance *> pinConnections) {
if(pinConnections.empty()) {
return false;
}
auto pinConnection = pinConnections[0];
if(auto directConnection = dynamic_cast<domain::DirectConnectionInstance*>(pinConnection)) {
return directConnection->bus->bus.getType() == domain::Bus::SINGLE_AUTOMATIC;
} else {
return false;
}
}
Bus::Bus(const std::shared_ptr<domain::BusInstance> &instance): busInstance(instance) {
instance->bus.getDisplayBus()->render(this, instance->size);
setToolTip(QString::fromStdString(busInstance->name + "::" + busInstance->bus.getName()));
}
void Bus::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
QMenu menu;
menu.addAction("Izmjeni ime", [this]() {
std::set<std::string> names;
for(const auto &component: Application::instance()->getSchema()->busInstances) {
names.insert(component->name);
}
auto dialog = new NameDialog(this->busInstance->name, names);
dialog->exec();
auto currentName = this->busInstance->name;
auto newName = dialog->getName();
Application::instance()->renameBus(currentName, newName);
setToolTip(QString::fromStdString(busInstance->name + "::" + busInstance->bus.getName()));
});
menu.addSeparator();
menu.addAction(QString::fromStdString("Ukloni " + this->busInstance->name), [this]() {
Application::instance()->removeBus(this->busInstance->name);
auto view = dynamic_cast<Schema *>(this->scene()->views()[0]);
view->refreshContent();
});
menu.exec(event->screenPos());
}
QVariant BusGroup::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) {
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPoint newPos = value.toPointF().toPoint();
busInstance->position.first = newPos.x();
busInstance->position.second = newPos.y();
auto view = dynamic_cast<Schema *>(scene()->views()[0]);
view->updateConnections();
}
return QGraphicsItem::itemChange(change, value);
}
BusGroup::BusGroup(const std::shared_ptr<domain::BusInstance> &instance) : busInstance(instance) {
setFlag(ItemIsMovable, true);
setFlag(ItemSendsGeometryChanges, true);
setHandlesChildEvents(false);
bus = new display::Bus(instance);
addToGroup(bus);
setPos(instance->position.first, instance->position.second);
}
QVariant ComponentGroup::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value) {
if (change == ItemPositionChange && scene()) {
// value is the new position.
QPoint newPos = value.toPointF().toPoint();
componentInstance->position.first = newPos.x();
componentInstance->position.second = newPos.y();
auto view = dynamic_cast<Schema *>(scene()->views()[0]);
view->updateConnections();
}
return QGraphicsItem::itemChange(change, value);
}
std::shared_ptr<domain::ComponentInstance> ComponentGroup::getComponentInstance() {
return componentInstance;
}
std::vector<display::Pin *> &ComponentGroup::getPins() {
return pins;
}
ComponentGroup::ComponentGroup(const std::shared_ptr<domain::ComponentInstance> &instance): componentInstance(
instance) {
setFlag(ItemIsMovable, true);
setFlag(ItemSendsGeometryChanges, true);
setHandlesChildEvents(false);
addToGroup(new display::Component(instance));
for (auto &pin: instance->component.getPins()) {
pins.push_back(new display::Pin(pin, componentInstance));
addToGroup(pins.back());
}
setPos(instance->position.first, instance->position.second);
}
BusConnection::BusConnection(domain::BusConnectionInstance *connection, ComponentGroup *component, BusGroup *bus): connection(
connection), component(component), bus(bus) {
updateConnection();
setPen(connectionPen);
setZValue(-1);
setHandlesChildEvents(false);
}
void BusConnection::updateConnection() {
auto busPosition = bus->boundingRect();
auto pin = connection->instance->component.getPin(
connection->connection.getComponent().pin).getDisplayPin();
setLine(connection->instance->position.first + pin.getConnectionX(),
connection->instance->position.second + pin.getConnectionY(),
connection->bus->position.first + busPosition.width() / 2,
connection->bus->position.second + busPosition.height() / 2);
connection->start.first = connection->instance->position.first + pin.getConnectionX();
connection->start.second = connection->instance->position.second + pin.getConnectionY();
connection->end.first = connection->bus->position.first + busPosition.width() / 2;
connection->end.second = connection->bus->position.second + busPosition.height() / 2;
}
DirectConnection::DirectConnection(domain::DirectConnectionInstance *connection, ComponentGroup *first,
ComponentGroup *second): connection(connection), first(first), second(second) {
updateConnection();
setPen(connectionPen);
setZValue(-1);
setHandlesChildEvents(false);
}
void DirectConnection::updateConnection() {
auto pin1 = connection->instance->component.getPin(
connection->connection.getComponent().pin).getDisplayPin();
auto pin2 = connection->secondInstance->component.getPin(
connection->connection.getSecondComponent()->pin).getDisplayPin();
setLine(connection->instance->position.first + pin1.getConnectionX(),
connection->instance->position.second + pin1.getConnectionY(),
connection->secondInstance->position.first + pin2.getConnectionX(),
connection->secondInstance->position.second + pin2.getConnectionY());
connection->start.first = connection->instance->position.first + pin1.getConnectionX();
connection->start.second = connection->instance->position.second + pin1.getConnectionY();
connection->end.first = connection->secondInstance->position.first + pin2.getConnectionX();
connection->end.second = connection->secondInstance->position.second + pin2.getConnectionY();
}
} // namespace display } // namespace display
+91 -49
View File
@@ -2,73 +2,115 @@
#define DISPLAY_COMPONENT_H #define DISPLAY_COMPONENT_H
#include <comdel/domain/instance.h> #include <comdel/domain/instance.h>
#include <comdel/domain/wireinstance.h>
#include <QGraphicsItemGroup> #include <QGraphicsItemGroup>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsSceneMouseEvent>
#include <utility>
#include "comdel/domain/connection_instance.h"
namespace display { namespace display {
class ComponentItem: public QGraphicsItemGroup class Pin : public QGraphicsItemGroup {
{
public:
ComponentItem(domain::ComponentInstance *instance, QGraphicsItem *parent);
void redraw();
void clear();
private: private:
domain::ComponentInstance *componentInstance; domain::Pin pin;
std::shared_ptr<domain::ComponentInstance> componentInstance;
public: public:
Pin(domain::Pin pin, std::shared_ptr<domain::ComponentInstance> componentInstance) : pin(pin), componentInstance(std::move(componentInstance)) {
pin.getDisplayPin().render(this);
this->setToolTip(QString::fromStdString(pin.getTooltip()));
}
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
domain::Pin &getPin();
domain::ComponentInstance *getComponentInstance();
bool isSingleAutomatic(std::vector<domain::ConnectionInstance *> pinConnections);
};
class Component : public QGraphicsItemGroup {
private:
std::shared_ptr<domain::ComponentInstance> instance;
public:
explicit Component(const std::shared_ptr<domain::ComponentInstance> &instance);
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
};
class Bus : public QGraphicsItemGroup {
std::shared_ptr<domain::BusInstance> busInstance;
public:
explicit Bus(const std::shared_ptr<domain::BusInstance> &instance);
void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override; void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
}; };
class PinItem: public QGraphicsItemGroup class ComponentGroup : public QGraphicsItemGroup {
{
public:
PinItem(domain::Pin pin, QGraphicsItem *parent);
void redraw();
void clear();
private: private:
domain::Pin pin; std::shared_ptr<domain::ComponentInstance> componentInstance;
std::vector<display::Pin *> pins;
public:
std::shared_ptr<domain::ComponentInstance> getComponentInstance();
std::vector<display::Pin *> &getPins();
explicit ComponentGroup(const std::shared_ptr<domain::ComponentInstance> &instance);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
}; };
class ComponentWrapper: public QGraphicsItemGroup class BusGroup : public QGraphicsItemGroup {
{
public:
static ComponentWrapper *ofComponent(domain::ComponentInstance *instance);
static ComponentWrapper *ofBus(domain::BusInstance *instance);
static ComponentWrapper *ofWire(domain::WireInstance *wire);
ComponentWrapper() {
this->setHandlesChildEvents(false);
}
void redraw();
void clear();
QVariant itemChange( GraphicsItemChange change, const QVariant &value ) override
{
auto response = QGraphicsItem::itemChange(change, value);
if (change == ItemPositionChange){
if(componentInstance != nullptr) {
componentInstance->position.first = (int)scenePos().x();
componentInstance->position.second = (int)scenePos().y();
} else if(busInstance != nullptr) {
busInstance->position.first = (int)scenePos().x();
busInstance->position.second = (int)scenePos().y();
}
}
return response;
}
private: private:
domain::ComponentInstance *componentInstance = nullptr; std::shared_ptr<domain::BusInstance> busInstance;
domain::BusInstance *busInstance = nullptr; display::Bus *bus;
domain::WireInstance *wireInstance = nullptr;
ComponentItem *componentItem; public:
std::vector<PinItem*> pinItems; explicit BusGroup(const std::shared_ptr<domain::BusInstance> &instance);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) override;
}; };
class BusConnection : public QGraphicsLineItem {
private:
domain::BusConnectionInstance *connection;
ComponentGroup *component;
BusGroup *bus;
public:
BusConnection(domain::BusConnectionInstance *connection, ComponentGroup *component, BusGroup *bus);
void updateConnection();
};
class DirectConnection : public QGraphicsLineItem {
private:
domain::DirectConnectionInstance *connection;
ComponentGroup *first;
ComponentGroup *second;
public:
DirectConnection(domain::DirectConnectionInstance *connection, ComponentGroup *first, ComponentGroup *second);
void updateConnection();
};
} // namespace display } // namespace display
#endif // DISPLAY_COMPONENT_H #endif // DISPLAY_COMPONENT_H
-21
View File
@@ -1,21 +0,0 @@
//
// Created by bbr on 18. 04. 2022..
//
#include "dialogmanager.h"
#include "attribute_dialog.h"
#include "name_dialog.h"
namespace display {
void DialogManager::updateAttribute(domain::InstanceAttribute *attribute) {
auto dialog = new AttributeDialog(attribute);
dialog->exec();
}
void DialogManager::updateName(domain::ComponentInstance *instance) {
auto dialog = new NameDialog(instance);
dialog->exec();
}
}
-18
View File
@@ -1,18 +0,0 @@
#ifndef DIALOG_MANAGER_H
#define DIALOG_MANAGER_H
#include <comdel/domain/instanceattribute.h>
#include <comdel/domain/instance.h>
namespace display {
class DialogManager {
public:
static void updateAttribute(domain::InstanceAttribute *attribute);
static void updateName(domain::ComponentInstance *instance);
};
}
#endif //DIALOG_MANAGER_H
+12 -12
View File
@@ -3,28 +3,27 @@
#include <QLabel> #include <QLabel>
#include <QListWidget> #include <QListWidget>
#include <QVBoxLayout> #include <QVBoxLayout>
#include <application.h>
namespace display { namespace display {
Library::Library() Library::Library() {
{
auto layout = new QVBoxLayout(); auto layout = new QVBoxLayout();
this->setLayout(layout); this->setLayout(layout);
componentList = new QListWidget(); componentList = new LibraryList(this);
busList = new QListWidget(); busList = new LibraryList(this);
layout->setMargin(4); layout->setContentsMargins(4, 4, 4, 4);
layout->addWidget(new QLabel("Components:")); layout->addWidget(new QLabel("Komponente:"));
layout->addWidget(componentList, 1); layout->addWidget(componentList, 1);
layout->addSpacing(8); layout->addSpacing(8);
layout->addWidget(new QLabel("Buses:")); layout->addWidget(new QLabel("Sabirnice:"));
layout->addWidget(busList, 1); layout->addWidget(busList, 1);
setLibrary(library);
} }
void Library::setLibrary(std::optional<domain::Library> library) { void Library::refreshContent() {
library = Application::instance()->getLibrary();
componentList->clear(); componentList->clear();
busList->clear(); busList->clear();
@@ -34,14 +33,15 @@ void Library::setLibrary(std::optional<domain::Library> library) {
} }
for (auto &component: library->getComponents()) { for (auto &component: library->getComponents()) {
auto item = new QListWidgetItem{QString::fromStdString(component.getName())}; auto item = new LibraryListItem{component.getName(), "comdel/component", component.getName(),
componentList};
item->setToolTip(QString::fromStdString(component.getTooltip())); item->setToolTip(QString::fromStdString(component.getTooltip()));
componentList->addItem(item); componentList->addItem(item);
} }
for (auto &bus: library->getBuses()) { for (auto &bus: library->getBuses()) {
if (bus.getType() == domain::Bus::REGULAR) { if (bus.getType() == domain::Bus::REGULAR) {
auto item = new QListWidgetItem{QString::fromStdString(bus.getName())}; auto item = new LibraryListItem{bus.getName(), "comdel/bus", bus.getName(), busList};
item->setToolTip(QString::fromStdString(bus.getTooltip())); item->setToolTip(QString::fromStdString(bus.getTooltip()));
busList->addItem(item); busList->addItem(item);
} }
+5 -5
View File
@@ -5,21 +5,21 @@
#include <QWidget> #include <QWidget>
#include <comdel/domain/library.h> #include <comdel/domain/library.h>
#include "library_list.h"
namespace display { namespace display {
class Library: public QWidget class Library : public QWidget {
{
public: public:
Library(); Library();
void setLibrary(std::optional<domain::Library> library); void refreshContent();
private: private:
std::optional<domain::Library> library; std::optional<domain::Library> library;
QListWidget *componentList; LibraryList *componentList;
QListWidget *busList; LibraryList *busList;
}; };
+32
View File
@@ -0,0 +1,32 @@
//
// Created by bbr on 22.05.22..
//
#include "library_list.h"
#include <QMouseEvent>
#include <QDrag>
#include <QMimeData>
namespace display {
LibraryList::LibraryList(QWidget *parent) : QListWidget(parent) {
setDragDropMode(DragOnly);
}
QMimeData *LibraryList::mimeData(const QList<QListWidgetItem *> items) const {
for (auto qItem: items) {
// we only allow one item to be dragged at a time
auto item = dynamic_cast<LibraryListItem *>(qItem);
auto *md = new QMimeData();
md->setData(QString::fromStdString(item->mimeType), QByteArray::fromStdString(item->value));
return md;
}
return nullptr;
}
LibraryListItem::LibraryListItem(std::string title, std::string mimeType, std::string value, QListWidget *parent)
: QListWidgetItem(parent), mimeType(mimeType), value(value) {
setText(QString::fromStdString(title));
}
} // display
+34
View File
@@ -0,0 +1,34 @@
//
// Created by bbr on 22.05.22..
//
#ifndef SCHEMEEDITOR_LIBRARY_LIST_H
#define SCHEMEEDITOR_LIBRARY_LIST_H
#include <QListWidget>
#include <QList>
#include <QMimeData>
namespace display {
class LibraryList : public QListWidget {
Q_OBJECT
public:
LibraryList(QWidget *parent);
protected:
QMimeData *mimeData(const QList<QListWidgetItem *> items) const override;
};
class LibraryListItem : public QListWidgetItem {
public:
std::string mimeType;
std::string value;
LibraryListItem(std::string title, std::string mimeType, std::string value, QListWidget *parent);
};
} // display
#endif //SCHEMEEDITOR_LIBRARY_LIST_H
+45
View File
@@ -2,4 +2,49 @@
// Created by bbr on 18. 04. 2022.. // Created by bbr on 18. 04. 2022..
// //
#include <set>
#include "name_dialog.h" #include "name_dialog.h"
display::NameDialog::NameDialog(std::string currentName, std::set<std::string> &names): currentName(currentName) {
usedNames.erase(currentName);
auto *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("Izmjeni ime", this));
edit = new QLineEdit(this);
edit->insert(currentName.c_str());
connect(edit, &QLineEdit::textChanged, this, &NameDialog::onNameUpdate);
layout->addWidget(edit);
setWindowTitle("Izmjeni ime");
setLayout(layout);
auto buttonLayout = new QHBoxLayout(this);
button = new QPushButton("Ažuriraj");
auto cancelButton = new QPushButton("Odustani", this);
connect(button, &QPushButton::clicked, this, &NameDialog::onNameChange);
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(button);
buttonLayout->addWidget(cancelButton);
layout->addLayout(buttonLayout);
}
void display::NameDialog::onNameUpdate(const QString &text) {
if(usedNames.find(text.toStdString()) == usedNames.end()) {
button->setDisabled(false);
} else {
button->setDisabled(true);
}
}
void display::NameDialog::onNameChange() {
currentName = edit->text().toStdString();
close();
}
std::string display::NameDialog::getName() {
return currentName;
}
+10 -19
View File
@@ -7,36 +7,27 @@
#include <QLineEdit> #include <QLineEdit>
#include <QPushButton> #include <QPushButton>
#include <set>
#include <comdel/domain/instance.h> #include <comdel/domain/instance.h>
namespace display { namespace display {
class NameDialog : public QDialog { class NameDialog : public QDialog {
QLineEdit *edit; std::set<std::string> usedNames;
domain::ComponentInstance *instance; QLineEdit *edit = nullptr;
std::string currentName;
QPushButton *button;
public: public:
NameDialog(std::string currentName, std::set<std::string>& names);
NameDialog(domain::ComponentInstance *instance): instance(instance) { std::string getName();
auto *layout = new QVBoxLayout(this);
layout->addWidget(new QLabel("Izmjeni ime", this));
edit = new QLineEdit(this);
edit->insert(instance->name.c_str());
layout->addWidget(edit);
this->setWindowTitle("Izmjeni ime");
auto *button = new QPushButton("Ažuriraj", this);
connect(button, &QPushButton::clicked, this, &NameDialog::onNameChange);
layout->addWidget(button);
this->setLayout(layout);
}
public slots: public slots:
void onNameChange() { void onNameUpdate(const QString& text);
instance->name = this->edit->text().toStdString(); void onNameChange();
this->close();
}
}; };
} }
+301 -18
View File
@@ -1,40 +1,323 @@
#include "component_display.h" #include "component_display.h"
#include "schema_display.h" #include "schema_display.h"
#include "application.h"
#include "attribute_dialog.h"
#include "single_automatic_dialog.h"
#include <QDrag>
#include <QDragEnterEvent>
#include <QMimeData>
namespace display { namespace display {
Schema::Schema() Schema::Schema() {
{ schema = nullptr;
library = std::nullopt;
setRenderHint(QPainter::Antialiasing);
setAlignment(Qt::AlignCenter);
this->setScene(&scene); this->setScene(&scene);
this->setAcceptDrops(true);
} }
void Schema::setSchema(domain::Schema* schema) void Schema::refreshContent() {
{ schema = Application::instance()->getSchema();
library = Application::instance()->getLibrary();
components.clear();
buses.clear();
scene.clear(); scene.clear();
this->schema = schema; pins.clear();
busConnections.clear();
directConnections.clear();
if (schema != nullptr) { if (schema != nullptr) {
for(auto &instance: schema->instances) { for (auto &instance: schema->componentInstances) {
ComponentWrapper *group = nullptr; auto group = new display::ComponentGroup(instance);
auto component = dynamic_cast<domain::ComponentInstance*>(instance); for (auto pin: group->getPins()) {
if(component) { display::Pin *p = pin;
group = ComponentWrapper::ofComponent(component); domain::ConnectionComponent connection{instance->name, p->getPin().getName()};
pins.insert(std::make_pair(connection, p));
} }
auto bus = dynamic_cast<domain::BusInstance*>(instance); components.insert(std::make_pair(instance->name, group));
if(bus) { scene.addItem(group);
group = ComponentWrapper::ofBus(bus);
} }
if(group != nullptr) { for (auto &instance: schema->busInstances) {
group->setPos(instance->position.first, instance->position.second); if (instance->bus.getDisplayBus().has_value()) {
auto group = new display::BusGroup(instance);
buses.insert(std::make_pair(instance->name, group));
scene.addItem(group); scene.addItem(group);
} }
} }
for(auto &wire: schema->wires) { for (auto &connection: schema->connections) {
auto group = ComponentWrapper::ofWire(wire); auto busInstance = dynamic_cast<domain::BusConnectionInstance *>(connection.get());
group->setPos(wire->position.first, wire->position.second); if (busInstance != nullptr) {
auto con = new display::BusConnection(busInstance, components[busInstance->instance->name],
buses[busInstance->bus->name]);
busConnections.push_back(con);
scene.addItem(con);
}
auto directInstance = dynamic_cast<domain::DirectConnectionInstance *>(connection.get());
if (directInstance != nullptr) {
auto con = new display::DirectConnection(directInstance, components[directInstance->instance->name],
components[directInstance->secondInstance->name]);
directConnections.push_back(con);
scene.addItem(con);
}
}
}
}
void Schema::updateConnections() {
if (schema != nullptr) {
for (auto conn: busConnections) {
conn->updateConnection();
}
for (auto conn: directConnections) {
conn->updateConnection();
}
}
}
void Schema::dragEnterEvent(QDragEnterEvent *event) {
if (event->mimeData()->hasFormat("comdel/component") ||
event->mimeData()->hasFormat("comdel/bus")) {
event->acceptProposedAction();
}
}
void Schema::dropEvent(QDropEvent *event) {
if (event->mimeData()->hasFormat("comdel/component")) {
auto component = library->getComponent(event->mimeData()->data("comdel/component").toStdString());
auto attributes = populateAttributes(component.getAttributes());
if(attributes.size() != component.getAttributes().size()) {
return;
}
auto currentPos = this->mapToScene(event->pos()).toPoint();
auto instance = Application::instance()->addComponent(component, attributes, currentPos.x(), currentPos.y());
auto group = new display::ComponentGroup(instance);
scene.addItem(group); scene.addItem(group);
for (auto pin: group->getPins()) {
display::Pin *p = pin;
domain::ConnectionComponent connection{instance->name, p->getPin().getName()};
pins.insert(std::make_pair(connection, p));
}
components[instance->name] = group;
event->acceptProposedAction();
}
if (event->mimeData()->hasFormat("comdel/bus")) {
auto bus = library->getBus(event->mimeData()->data("comdel/bus").toStdString());
auto currentPos = this->mapToScene(event->pos()).toPoint();
auto instance = Application::instance()->addBus(bus, currentPos.x(), currentPos.y());
auto group = new display::BusGroup(instance);
scene.addItem(group);
buses[instance->name] = group;
event->acceptProposedAction();
} }
} }
void Schema::dragMoveEvent(QDragMoveEvent *event) {
event->acceptProposedAction();
}
void Schema::removeConnectable(QPointF endPoint) {
auto instance = context.pin->getComponentInstance();
auto &pin = context.pin->getPin();
auto availableConnections = schema->availableConnections(instance->name, pin.getName(), true);
for(auto &connection: availableConnections) {
if(connection.type != domain::ConnectionEntry::BUS) {
continue;
}
auto bus = connection.busInstance.value();
if(buses[bus->name] == nullptr) {
continue;
}
auto rect = buses[bus->name]->boundingRect();
rect = QRectF(buses[bus->name]->x(), buses[bus->name]->y(), rect.width(), rect.height());
if (rect.contains(endPoint)) {
auto con = library->getConnection({instance->component.getName(), pin.getName()}, bus->bus.getName());
if (con.has_value()) {
auto attributes = populateAttributes(con->getAttributes());
if(attributes.size() != con->getAttributes().size()) {
clearSelectable();
return;
}
auto conInstance = std::make_shared<domain::BusConnectionInstance>(instance, attributes, bus, *con);
schema->connections.push_back(conInstance);
if (conInstance != nullptr) {
auto c = new display::BusConnection(conInstance.get(), components[conInstance->instance->name],
buses[conInstance->bus->name]);
busConnections.push_back(c);
scene.addItem(c);
}
break;
}
}
}
for (auto &connection: availableConnections) {
if(connection.type != domain::ConnectionEntry::COMPONENT) {
continue;
}
auto pinInstance = domain::ConnectionComponent{connection.componentInstance.value()->name, connection.pin.value().getName()};
auto pin = pins[pinInstance]->getPin().getDisplayPin();
auto position = pins[pinInstance]->getComponentInstance()->position;
auto rect = QRectF(position.first + pin.x, position.second + pin.y, pin.w, pin.h);
if (rect.contains(endPoint)) {
auto name = components[pinInstance.component]->getComponentInstance()->component.getName();
auto con = library->getConnection({instance->component.getName(), connection.pin.value().getName()},
{name, pinInstance.pin});
if (con.has_value()) {
auto busInstance = Application::instance()->addBus(library->getBus(con->getBus()), 0, 0);
std::vector<domain::InstanceAttribute> attributes;
if(library->getBus(con->getBus()).getType() == domain::Bus::SINGLE_AUTOMATIC) {
attributes = populateSingleAutomaticConnection(*con);
} else {
attributes = populateAttributes(con->getAttributes());
}
if(attributes.size() != con->getAttributes().size()) {
clearSelectable();
return;
}
auto conInstance = std::make_shared<domain::DirectConnectionInstance>(instance,
components[pinInstance.component]->getComponentInstance().get(),
attributes, busInstance.get(),
*con);
schema->connections.push_back(conInstance);
if (conInstance != nullptr) {
auto c = new display::DirectConnection(conInstance.get(),
components[conInstance->instance->name],
components[conInstance->secondInstance->name]);
directConnections.push_back(c);
scene.addItem(c);
}
break;
}
}
}
clearSelectable();
}
void Schema::showConnectable(Pin *domainPin) {
auto &pin = domainPin->getPin();
auto availableConnections = schema->availableConnections(domainPin->getComponentInstance()->name, pin.getName(), true);
for(auto &connection: availableConnections) {
if(connection.type != domain::ConnectionEntry::BUS) {
continue;
}
auto bus = connection.busInstance.value();
auto &group = buses[bus->name];
if(group == nullptr) {
continue;
}
auto rect = new QGraphicsRectItem(group->boundingRect());
rect->setBrush(selectedBrush);
rect->setPos(group->scenePos());
auto _rect = rect->rect();
_rect.setWidth(_rect.width() + 1);
_rect.setHeight(_rect.height() + 1);
rect->setRect(_rect);
context.selectable.push_back(rect);
scene.addItem(rect);
}
for (auto &connection: availableConnections) {
if(connection.type != domain::ConnectionEntry::COMPONENT) {
continue;
}
auto pinInstance = domain::ConnectionComponent{connection.componentInstance.value()->name, connection.pin.value().getName()};
auto &instance = pins[pinInstance];
auto rect = new QGraphicsRectItem(instance->boundingRect());
rect->setBrush(selectedBrush);
rect->setPen(selectedPen);
rect->setPos(instance->scenePos());
auto _rect = rect->rect();
_rect.setWidth(_rect.width() + 1);
_rect.setHeight(_rect.height() + 1);
rect->setRect(_rect);
context.selectable.push_back(rect);
scene.addItem(rect);
}
}
std::vector<domain::InstanceAttribute> Schema::populateAttributes(std::vector<domain::Attribute>& attributes) {
std::vector<domain::InstanceAttribute> instanceAttributes;
for (auto attr: attributes) {
domain::InstanceAttribute attribute(attr.getName(), attr.getDefault(), attr);
if(attr.getPopup().has_value() && attr.getPopup()->getType() == domain::Popup::AUTOMATIC) {
if(attr.getDefault().isType(domain::Value::MEMORY_REFERENCE)) {
auto dialog = new MemoryDialog(&attribute, schema->componentInstances, false);
if(dialog->exec() == QDialog::Rejected) {
// if any dialog isn't set, whole creation is rejected
return {};
}
} else {
auto dialog = new AttributeDialog(&attribute, false);
if(dialog->exec() == QDialog::Rejected) {
// if any dialog isn't set, whole creation is rejected
return {};
}
}
}
instanceAttributes.push_back(attribute);
}
return instanceAttributes;
}
std::vector<domain::InstanceAttribute> Schema::populateSingleAutomaticConnection(domain::Connection connection) {
std::vector<domain::InstanceAttribute> instanceAttributes;
for (auto attr: connection.getAttributes()) {
instanceAttributes.emplace_back(attr.getName(), attr.getDefault(), attr);
}
auto dialog = new display::SingleAutomaticDialog(instanceAttributes, false);
if(dialog->exec() == QDialog::Rejected) {
// if dialog is rejected, whole creation is rejected
return {};
}
return instanceAttributes;
}
void Schema::clearSelectable() {
updateConnections();
for (auto &item: this->context.selectable) {
this->scene.removeItem(item);
delete item;
}
this->context.selectable.clear();
} }
} // namespace display } // namespace display
+58 -4
View File
@@ -4,23 +4,77 @@
#include <QGraphicsView> #include <QGraphicsView>
#include <QWidget> #include <QWidget>
#include <QColor>
#include <QGraphicsLineItem>
#include <comdel/domain/schema.h> #include <comdel/domain/schema.h>
#include <comdel/domain/library.h>
#include "component_display.h"
namespace display { namespace display {
class Schema: public QGraphicsView class BusConnection;
{
class Schema : public QGraphicsView {
Q_OBJECT
public: public:
QBrush selectedBrush = QBrush(QColor::fromRgb(50, 50, 150, 100), Qt::BrushStyle::SolidPattern);
QPen selectedPen = QPen(QColor::fromRgb(50, 50, 150), 1, Qt::PenStyle::SolidLine);
QPen activeLinePen = QPen(QColor::fromRgb(250, 100, 100));
enum State {
DEFAULT,
CREATING_CONNECTION
};
struct Context {
display::Pin *pin = nullptr;
QPointF startingPoint;
QGraphicsLineItem *line = nullptr;
std::vector<QGraphicsRectItem *> selectable;
};
std::map<std::string, display::ComponentGroup *> components;
std::map<std::string, display::BusGroup *> buses;
std::map<domain::ConnectionComponent, display::Pin *> pins;
State state = DEFAULT;
Context context;
Schema(); Schema();
void setSchema(domain::Schema* schema); std::vector<BusConnection *> busConnections;
std::vector<DirectConnection *> directConnections;
void updateConnections();
void removeConnectable(QPointF f);
void showConnectable(Pin *pin);
void refreshContent();
protected:
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
private: private:
QGraphicsScene scene; QGraphicsScene scene;
domain::Schema* schema; domain::Schema *schema{};
std::optional<domain::Library> library;
std::vector<domain::InstanceAttribute> populateAttributes(std::vector<domain::Attribute>& attributes);
std::vector<domain::InstanceAttribute> populateSingleAutomaticConnection(domain::Connection connection);
void clearSelectable();
}; };
} // namespace display } // namespace display
@@ -0,0 +1,84 @@
//
// Created by bbr on 05.06.22..
//
#include "single_automatic_dialog.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QComboBox>
#include <QPushButton>
namespace display {
SingleAutomaticDialog::SingleAutomaticDialog(std::vector<domain::InstanceAttribute> &values, bool updating): attributes(values) {
setAttribute(Qt::WA_DeleteOnClose);
setWindowTitle(QString::fromStdString(updating ? "Ažuriraj poveznicu" : "Postavi poveznicu"));
firstValue = values[0].value;
secondValue = values[1].value;
auto *parentLayout = new QVBoxLayout(this);
auto *contentLayout = new QHBoxLayout(this);
auto *firstLayout = new QVBoxLayout(this);
auto *secondLayout = new QVBoxLayout(this);
parentLayout->addLayout(contentLayout);
contentLayout->addLayout(firstLayout);
contentLayout->addLayout(secondLayout);
this->setLayout(parentLayout);
setupValues(firstLayout, values[0], &SingleAutomaticDialog::onFirstEnumerationChanged);
setupValues(secondLayout, values[1], &SingleAutomaticDialog::onSecondEnumerationChanged);
auto buttonLayout = new QHBoxLayout(this);
auto okButton = new QPushButton("U redu", this);
auto cancelButton = new QPushButton("Odustani", this);
connect(okButton, &QPushButton::clicked, [this]() { accept(); });
connect(cancelButton, &QPushButton::clicked, [this]() { reject(); });
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
parentLayout->addLayout(buttonLayout);
}
void SingleAutomaticDialog::setupValues(QVBoxLayout *layout, domain::InstanceAttribute &attribute, void (display::SingleAutomaticDialog::* handler)(int)) {
auto popup = *attribute.attribute.getPopup();
layout->addWidget(new QLabel(popup.getTitle().c_str()));
layout->addWidget(new QLabel(popup.getText().c_str()));
auto *combo = new QComboBox(this);
auto enumeration = attribute.attribute.getPopup()->getEnumeration();
for (auto entry: enumeration) {
combo->addItem(QString::fromStdString(entry.getName()));
}
connect(combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,handler);
layout->addWidget(combo);
for (int i = 0; i < enumeration.size(); i++) {
if (attribute.value.equals(enumeration[i].getValue())) {
combo->setCurrentIndex(i);
break;
}
}
}
void SingleAutomaticDialog::onFirstEnumerationChanged(int index) {
firstValue = attributes[0].attribute.getPopup()->getEnumeration()[index].getValue();
}
void SingleAutomaticDialog::onSecondEnumerationChanged(int index) {
secondValue = attributes[1].attribute.getPopup()->getEnumeration()[index].getValue();
}
void SingleAutomaticDialog::onUpdate() {
attributes[0].value = firstValue;
attributes[1].value = secondValue;
accept();
}
} // display
+35
View File
@@ -0,0 +1,35 @@
//
// Created by bbr on 05.06.22..
//
#ifndef SCHEMEEDITOR_SINGLE_AUTOMATIC_DIALOG_H
#define SCHEMEEDITOR_SINGLE_AUTOMATIC_DIALOG_H
#include <vector>
#include <QDialog>
#include <QVBoxLayout>
#include "comdel/domain/value.h"
#include "comdel/domain/instance_attribute.h"
namespace display {
class SingleAutomaticDialog: public QDialog {
domain::Value firstValue;
domain::Value secondValue;
std::vector<domain::InstanceAttribute> &attributes;
public:
explicit SingleAutomaticDialog(std::vector<domain::InstanceAttribute>& values, bool updating = true);
void setupValues(QVBoxLayout *layout, domain::InstanceAttribute &attribute, void (display::SingleAutomaticDialog::* handler)(int));
public slots:
void onFirstEnumerationChanged(int index);
void onSecondEnumerationChanged(int index);
void onUpdate();
};
} // display
#endif //SCHEMEEDITOR_SINGLE_AUTOMATIC_DIALOG_H
+29
View File
@@ -0,0 +1,29 @@
#include <algorithm>
#include "address_space.h"
namespace domain {
AddressSpace::AddressSpace(std::string name, long long start, long long end) :
name(name), start(start), end(end) {}
std::string AddressSpace::getName() {
return name;
}
long long AddressSpace::getStart() const {
return start;
}
long long AddressSpace::getEnd() const {
return end;
}
bool AddressSpace::contains(long long int address) {
return address >= start && address < end;
}
bool AddressSpace::contains(long long int pstart, long long int pend) {
return pstart >= start && pend < end;
}
} // namespace domain
+29
View File
@@ -0,0 +1,29 @@
#ifndef DOMAIN_ADDRESS_SPACE_H
#define DOMAIN_ADDRESS_SPACE_H
#include <string>
namespace domain {
class AddressSpace {
std::string name;
long long start;
long long end;
public:
AddressSpace(std::string name, long long start, long long end);
std::string getName();
long long getStart() const;
long long getEnd() const;
bool contains(long long int address);
bool contains(long long int start, long long int end);
};
} // namespace domain
#endif // DOMAIN_ADDRESS_SPACE_H
-27
View File
@@ -1,27 +0,0 @@
#include "addressspace.h"
namespace domain {
AddressSpace::AddressSpace(std::string name, long long start, long long end):
name(name), start(start), end(end)
{}
std::string AddressSpace::getName() {
return name;
}
long long AddressSpace::getStart() const {
return start;
}
long long AddressSpace::getEnd() const {
return end;
}
bool AddressSpace::contains(long long int address) {
return address >= start && address < end;
}
bool AddressSpace::contains(long long int pstart, long long int pend) {
return pstart >= start && pend < end;
}
} // namespace domain
-27
View File
@@ -1,27 +0,0 @@
#ifndef DOMAIN_ADDRESS_SPACE_H
#define DOMAIN_ADDRESS_SPACE_H
#include <string>
namespace domain {
class AddressSpace
{
std::string name;
long long start;
long long end;
public:
AddressSpace(std::string name, long long start, long long end);
std::string getName();
long long getStart() const;
long long getEnd() const;
bool contains(long long int address);
bool contains(long long int start, long long int end);
};
} // namespace domain
#endif // DOMAIN_ADDRESS_SPACE_H
+18 -8
View File
@@ -3,30 +3,34 @@
namespace domain { namespace domain {
Enumeration::Enumeration(std::string name, Value value) Enumeration::Enumeration(std::string name, Value value)
: name(name), value(value) : name(name), value(value) {}
{}
std::string Enumeration::getName() { std::string &Enumeration::getName() {
return name; return name;
} }
Value Enumeration::getValue() { Value Enumeration::getValue() {
return value; return value;
} }
Popup::Popup(std::string title, std::string text, PopupType type, std::vector<Rule> rules, std::vector<Enumeration> enumeration) Popup::Popup(std::string title, std::string text, PopupType type, std::vector<Rule> rules,
: title(title), text(text), type(type), rules(rules), enumerated(enumeration.size() > 0), enumeration(enumeration) std::vector<Enumeration> enumeration)
{} : title(title), text(text), type(type), rules(rules), enumerated(enumeration.size() > 0),
enumeration(enumeration) {}
std::string Popup::getTitle() { std::string Popup::getTitle() {
return title; return title;
} }
std::string Popup::getText() { std::string Popup::getText() {
return text; return text;
} }
Popup::PopupType Popup::getType() { Popup::PopupType Popup::getType() {
return type; return type;
} }
std::vector<Rule> Popup::getRules() { std::vector<Rule> Popup::getRules() {
return rules; return rules;
} }
@@ -34,23 +38,29 @@ std::vector<Rule> Popup::getRules() {
bool Popup::isEnumerated() { bool Popup::isEnumerated() {
return enumerated; return enumerated;
} }
std::vector<Enumeration> &Popup::getEnumeration() { std::vector<Enumeration> &Popup::getEnumeration() {
return enumeration; return enumeration;
} }
Attribute::Attribute(std::string name, Value defaultValue, std::optional<Popup> popup) Attribute::Attribute(std::string name, Value defaultValue, std::optional<Popup> popup)
: name(name), defaultValue(defaultValue), popup(popup) : name(name), defaultValue(defaultValue), popup(popup) {}
{}
std::string Attribute::getName() { std::string Attribute::getName() {
return name; return name;
} }
Value Attribute::getDefault() { Value Attribute::getDefault() {
return defaultValue; return defaultValue;
} }
std::optional<Popup> Attribute::getPopup() { std::optional<Popup> Attribute::getPopup() {
return popup; return popup;
} }
void Attribute::setPupup(std::optional<Popup> popup) {
this->popup = popup;
}
} // namespace domain } // namespace domain
+18 -4
View File
@@ -1,6 +1,7 @@
#ifndef DOMAIN_ATTRIBUTE_H #ifndef DOMAIN_ATTRIBUTE_H
#define DOMAIN_ATTRIBUTE_H #define DOMAIN_ATTRIBUTE_H
#include <utility>
#include <vector> #include <vector>
#include "rule.h" #include "rule.h"
@@ -14,7 +15,8 @@ class Enumeration {
public: public:
Enumeration(std::string name, Value value); Enumeration(std::string name, Value value);
std::string getName(); std::string &getName();
Value getValue(); Value getValue();
}; };
@@ -35,20 +37,29 @@ private:
std::vector<Rule> rules; std::vector<Rule> rules;
public: public:
Popup(std::string title, std::string text, PopupType type, std::vector<Rule> rules, std::vector<Enumeration> enumeration); Popup(std::string title, std::string text, PopupType type, std::vector<Rule> rules,
std::vector<Enumeration> enumeration);
std::string getTitle(); std::string getTitle();
std::string getText(); std::string getText();
PopupType getType(); PopupType getType();
std::vector<Rule> getRules(); std::vector<Rule> getRules();
bool isEnumerated(); bool isEnumerated();
std::vector<Enumeration> &getEnumeration(); std::vector<Enumeration> &getEnumeration();
void setEnumeration(std::vector<Enumeration> enums) {
enumerated = true;
enumeration = enums;
}
}; };
class Attribute class Attribute {
{
std::string name; std::string name;
Value defaultValue; Value defaultValue;
std::optional<Popup> popup; std::optional<Popup> popup;
@@ -56,8 +67,11 @@ public:
Attribute(std::string name, Value defaultValue, std::optional<Popup> popup = std::nullopt); Attribute(std::string name, Value defaultValue, std::optional<Popup> popup = std::nullopt);
std::string getName(); std::string getName();
Value getDefault(); Value getDefault();
std::optional<Popup> getPopup(); std::optional<Popup> getPopup();
void setPupup(std::optional<Popup> popup);
}; };
} // namespace domain } // namespace domain
+21 -9
View File
@@ -3,15 +3,22 @@
namespace domain { namespace domain {
Wire::Wire(std::string name, WireType type, int width, bool hidden, bool hasTerminate, Value terminateWith) Wire::Wire(std::string name, WireType type, int width, bool hidden, bool hasTerminate, Value terminateWith)
: name(name), type(type), width(width), hidden(hidden), hasTerminate(hasTerminate), terminateWith(terminateWith) : name(name), type(type), width(width), hidden(hidden), hasTerminate(hasTerminate),
{} terminateWith(terminateWith) {}
std::string Wire::getName() { std::string Wire::getName() {
return name; return name;
} }
std::string Bus::getInstanceName() {
return instanceName;
}
int Wire::getWidth() { int Wire::getWidth() {
return width; return width;
} }
bool Wire::isHidden() { bool Wire::isHidden() {
return hidden; return hidden;
} }
@@ -19,35 +26,40 @@ bool Wire::isHidden() {
bool Wire::hasTerminateWith() { bool Wire::hasTerminateWith() {
return hasTerminate; return hasTerminate;
} }
Value Wire::getTerminateWith() { Value Wire::getTerminateWith() {
return terminateWith; return terminateWith;
} }
Wire::WireType Wire::getType() { Wire::WireType Wire::getType() {
return type; return type;
} }
Bus::Bus(std::string name, std::string tooltip, BusType type, std::pair<int, int> count, std::vector<Wire> wires, std::optional<Display> display) Bus::Bus(std::string name, std::string instanceName, std::string tooltip, BusType type, std::pair<int, int> count, std::vector<Wire> wires,
: name(name), tooltip(tooltip), type(type), count(count), wires(wires), display(display) std::optional<ui::Bus> displayBus)
{} : name(name), instanceName(instanceName), tooltip(tooltip), type(type), count(count), wires(wires), displayBus(displayBus) {}
std::string Bus::getName() { std::string Bus::getName() {
return name; return name;
} }
std::string Bus::getTooltip() { std::string Bus::getTooltip() {
return tooltip; return tooltip;
} }
Bus::BusType Bus::getType() { Bus::BusType Bus::getType() {
return type; return type;
} }
std::pair<int, int> Bus::getCount() { std::pair<int, int> Bus::getCount() {
return count; return count;
} }
std::optional<Display> Bus::getDisplay() {
return display;
}
std::vector<Wire> Bus::getWires() { std::vector<Wire> Bus::getWires() {
return wires; return wires;
} }
std::optional<ui::Bus> Bus::getDisplayBus() {
return displayBus;
}
} // namespace domain } // namespace domain
+20 -8
View File
@@ -11,8 +11,7 @@
namespace domain { namespace domain {
class Wire class Wire {
{
public: public:
enum WireType { enum WireType {
WIRE_DEFAULT, WIRE_DEFAULT,
@@ -34,39 +33,52 @@ public:
Wire(std::string name, WireType type, int width, bool hidden, bool hasTerminate, Value terminateWith); Wire(std::string name, WireType type, int width, bool hidden, bool hasTerminate, Value terminateWith);
std::string getName(); std::string getName();
int getWidth(); int getWidth();
bool isHidden(); bool isHidden();
bool hasTerminateWith(); bool hasTerminateWith();
Value getTerminateWith(); Value getTerminateWith();
WireType getType(); WireType getType();
}; };
class Bus class Bus {
{
public: public:
enum BusType { enum BusType {
AUTOMATIC, AUTOMATIC,
REGULAR, REGULAR,
AUTOMATIC_SINGLE SINGLE_AUTOMATIC
}; };
private: private:
std::string name; std::string name;
std::string instanceName;
std::string tooltip; std::string tooltip;
BusType type; BusType type;
std::pair<int, int> count; std::pair<int, int> count;
std::optional<Display> display; std::optional<ui::Bus> displayBus;
std::vector<Wire> wires; std::vector<Wire> wires;
public: public:
Bus(std::string name, std::string tooltip, BusType type, std::pair<int, int> count, std::vector<Wire> wires, std::optional<Display> display = std::nullopt); Bus(std::string name, std::string instanceName, std::string tooltip, BusType type, std::pair<int, int> count, std::vector<Wire> wires,
std::optional<ui::Bus> display = std::nullopt);
std::string getName(); std::string getName();
std::string getInstanceName();
std::string getTooltip(); std::string getTooltip();
BusType getType(); BusType getType();
std::pair<int, int> getCount(); std::pair<int, int> getCount();
std::vector<Wire> getWires(); std::vector<Wire> getWires();
std::optional<Display> getDisplay();
std::optional<ui::Bus> getDisplayBus();
}; };
} // namespace domain } // namespace domain
+375
View File
@@ -0,0 +1,375 @@
//
// Created by bbr on 08.05.22..
//
#include <set>
#include "comdel_generator.h"
namespace domain {
void generate_schema(std::string &librarySource, Schema *schema, std::ostream &buffer) {
buffer << "@source \"" << librarySource << "\"" << std::endl << std::endl;
buffer << "@schema {" << std::endl;
for (auto &componentInstance: schema->componentInstances) {
buffer << "\t" << "@instance " << componentInstance->name << " " << componentInstance->component.getName()
<< " {" << std::endl;
buffer << "\t\t" << "@position (" << componentInstance->position.first << ", "
<< componentInstance->position.second << ")" << std::endl;
for (auto &attribute: componentInstance->attributes) {
buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify() << std::endl;
}
buffer << "\t}" << std::endl << std::endl;
}
for (auto &busInstance: schema->busInstances) {
buffer << "\t" << "@instance " << busInstance->name << " " << busInstance->bus.getName() << " {"
<< std::endl;
buffer << "\t\t" << "@position (" << busInstance->position.first << ", " << busInstance->position.second
<< ")" << std::endl;
buffer << "\t\t" << "@size " << busInstance->size << std::endl;
buffer << "\t}" << std::endl << std::endl;
}
for (auto &conn: schema->connections) {
auto busConn = dynamic_cast<domain::BusConnectionInstance *>(conn.get());
if (busConn) {
buffer << "\t" << "@connection (" << busConn->instance->name << "."
<< busConn->connection.getComponent().pin << ", " << busConn->bus->name << ") {" << std::endl;
for (auto attribute: busConn->attributes) {
buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify()
<< std::endl;
}
buffer << "\t" << "}" << std::endl;
}
auto dirConn = dynamic_cast<domain::DirectConnectionInstance *>(conn.get());
if (dirConn) {
buffer << "\t" << "@connection (" << dirConn->instance->name << "."
<< dirConn->connection.getComponent().pin << ", "
<< dirConn->bus->name << ", "
<< dirConn->secondInstance->name << "." << dirConn->connection.getSecondComponent()->pin
<< ") {" << std::endl;
for (auto attribute: dirConn->attributes) {
buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify()
<< std::endl;
}
buffer << "\t" << "}" << std::endl;
}
}
buffer << "}" << std::endl;
}
std::set<std::string> createImports(Schema *schema);
std::map<string, string> generateWires(Schema *schema, ostream &ostream);
void generateSubComponents(Schema *schema, map<string, string> &wires, ostream &buffer);
void generateDisplay(Schema *schema, ostream &buffer);
void generate_comdel(Schema *schema, Library &library, std::ostream &buffer) {
buffer << library.getHeader() << std::endl;
std::set<std::string> imports = createImports(schema);
for (auto &import: imports) {
buffer << "#include \"" << library.getComponentDirectory() << "\\" << import << "\"" << std::endl;
}
buffer << "\n\n" << std::endl;
buffer << "component System" << std::endl << "{" << std::endl;
if (library.getComponentHeader().has_value()) {
buffer << library.getComponentHeader().value() << endl;
}
auto wires = generateWires(schema, buffer);
generateSubComponents(schema, wires, buffer);
generateDisplay(schema, buffer);
buffer << "}";
}
void generateBus(BusInstance *bus, ostream &buffer);
void generateConnection(ConnectionInstance *connection, ostream &buffer);
void generateDisplay(Schema *schema, ostream &buffer) {
buffer << "\n\tdisplay {\n";
for (auto &component: schema->componentInstances) {
buffer << "\t\tcomponent { x: " << component->position.first << "; y: " << component->position.second
<< "; ref: \"" << component->name << "\"; }" << std::endl;
}
for (auto &bus: schema->busInstances) {
generateBus(bus.get(), buffer);
}
buffer << "\n";
for (auto &connection: schema->connections) {
generateConnection(connection.get(), buffer);
}
buffer << "\t}\n";
}
void generateConnection(ConnectionInstance *connection, ostream &buffer) {
buffer << "\t\tline {x1:" << connection->start.first << "; y1:" << connection->start.second << "; " <<
"x2:" << connection->end.first << "; y2:" << connection->end.second << ";}" << "\n";
}
void generateBus(BusInstance *bus, ostream &buffer) {
buffer << "\n";
buffer << "\t\t// " << bus->name << " bus\n\n";
if (bus->bus.getDisplayBus().has_value()) {
bus->bus.getDisplayBus()->comdel(buffer, bus->position.first, bus->position.second, bus->size);
}
}
void generateWire(std::string &name, Wire &wire, ostream &buffer);
std::map<string, string> generateWires(Schema *schema, ostream &buffer) {
std::set<string> usedNames;
std::map<string, string> usedMappings;
for (auto &bus: schema->busInstances) {
buffer << "\t//" << bus->name << std::endl;
for (auto &wire: bus->bus.getWires()) {
std::string name = wire.getName();
if (usedNames.count(wire.getName()) > 0) {
name = bus->name + "__" + wire.getName();
}
if (wire.isHidden()) {
name = "--" + name;
}
usedNames.insert(name);
usedMappings.insert(std::make_pair(bus->name + "." + wire.getName(), name));
generateWire(name, wire, buffer);
}
buffer << std::endl << std::endl;
}
return usedMappings;
}
void generateWire(std::string &name, Wire &wire, ostream &buffer) {
buffer << "\t";
switch (wire.getType()) {
case Wire::R_WIRE:
buffer << "r_wire";
break;
case Wire::WIRED_AND:
buffer << "wired_and";
break;
case Wire::WIRED_OR:
buffer << "wired_or";
break;
default:
buffer << "wire";
}
if (wire.getWidth() != 1) {
buffer << "<" << wire.getWidth() << ">";
}
buffer << " " << name << ";" << std::endl;
}
std::set<std::string> createImports(Schema *schema) {
std::set<std::string> importSet;
for (auto &component: schema->componentInstances) {
importSet.insert(component->component.getSource());
}
return importSet;
}
void
generateAutomaticPin(DirectConnectionInstance *connection, string name, string pin, map<string, string> wireNames,
stringstream &buffer);
void generateSingleAutomaticPin(DirectConnectionInstance *instance, string name, string pin,
map<string, string> &wireNames,
stringstream &buffer);
void generateComponent(Schema *schema, map<string, string> &wires, ostream &buffer,
shared_ptr<ComponentInstance> &component);
void generateSubComponents(Schema *schema, map<string, string> &wires, ostream &buffer) {
buffer << "\t// components --------------------------------------------" << std::endl;
for (auto &component: schema->componentInstances) {
if (component->component.getType() == Component::MEMORY) {
generateComponent(schema, wires, buffer, component);
}
}
for (auto &component: schema->componentInstances) {
if (component->component.getType() != Component::MEMORY) {
generateComponent(schema, wires, buffer, component);
}
}
}
void generateComponent(Schema *schema, map<string, string> &wires, ostream &buffer,
shared_ptr<ComponentInstance> &component) {
buffer << "\tsubcomponent " << component->component.getName() << " " << component->name;
std::optional<InstanceAttribute> memory;
std::vector<InstanceAttribute> attributes;
for (auto &attr: component->attributes) {
if (attr.name != "_memory") {
attributes.push_back(attr);
} else if (attr.name == "_memory" && attr.value.asMemoryReference().has_value()) {
memory = attr;
}
}
if (!attributes.empty()) {
buffer << "<";
buffer << attributes[0].value.stringify();
for (int i = 1; i < attributes.size(); i++) {
buffer << ", " << attributes[i].value.stringify();
}
buffer << ">";
}
stringstream tempOutput;
for (auto &pin: component->component.getPins()) {
if (schema->hasConnection(component->name, pin.getName())) {
auto conn = schema->getConnection(component->name, pin.getName());
auto busConn = dynamic_cast<BusConnectionInstance *>(conn);
if (busConn != nullptr) {
for (auto wire: busConn->connection.getWires()) {
if (wire.isType(Value::ATTRIBUTE_REFERENCE)) {
auto attribute = busConn->getAttribute(wire.asReference());
if (wire.isType(Value::WIRE_REFERENCE)) {
tempOutput << wires[busConn->bus->name + "." + attribute.value.asReference()] << ", ";
} else if (wire.isType(Value::NIL)) {
tempOutput << "*, ";
} else {
tempOutput << attribute.value.stringify() << ", ";
}
} else {
if (wire.isType(Value::WIRE_REFERENCE)) {
tempOutput << wires[busConn->bus->name + "." + wire.asReference()] << ", ";
} else if (wire.isType(Value::NIL)) {
tempOutput << "*, ";
} else {
tempOutput << wire.stringify() << ", ";
}
}
}
}
auto dirConn = dynamic_cast<DirectConnectionInstance *>(conn);
if (dirConn != nullptr) {
if (dirConn->bus->bus.getType() == Bus::AUTOMATIC) {
generateAutomaticPin(dirConn, component->name, pin.getName(), wires, tempOutput);
} else {
generateSingleAutomaticPin(dirConn, component->name, pin.getName(), wires, tempOutput);
}
}
} else {
// if no connection exists than defaults must exist
for (auto &wire: *pin.getWires()) {
if(wire.isType(Value::NIL)) {
tempOutput << "*, ";
} else {
tempOutput << wire.stringify() << ", ";
}
}
}
}
auto wireList = tempOutput.str();
wireList.pop_back();
wireList.pop_back(); // remove last COMMA(", ")
buffer << "(" << wireList << ")";
if (memory.has_value()) {
buffer << " uses " << *memory->value.asMemoryReference();
}
buffer << ";" << std::endl;
}
void generateSingleAutomaticPin(DirectConnectionInstance *connection, string name, string pin,
map<string, string> &wireNames, stringstream &buffer) {
std::vector<Value> wires;
std::string selected;
std::vector<Value> defaults;
if (connection->instance->name == name && connection->connection.getComponent().pin == pin) {
wires = connection->connection.getWires();
selected = connection->attributes[0].value.asString();
defaults = *connection->instance->component.getPin(pin).getWires();
} else {
wires = *connection->connection.getSecondWires();
selected = connection->attributes[1].value.asString();
defaults = *connection->secondInstance->component.getPin(pin).getWires();
}
for (int i = 0; i < wires.size(); i++) {
if (wires[i].isType(Value::STRING)) {
if (wires[i].asString() == selected) {
auto wireName = connection->bus->name + "." + connection->bus->bus.getWires()[0].getName();
buffer << wireNames[wireName] << ", ";
} else if(defaults[i].isType(Value::NIL)) {
buffer << "*, ";
} else {
buffer << defaults[i].stringify() << ", ";
}
} else if(defaults[i].isType(Value::NIL)) {
buffer << "*, ";
} else {
buffer << wires[i].stringify() << ", ";
}
}
}
void
generateAutomaticPin(DirectConnectionInstance *connection, string name, string pin, map<string, string> wireNames,
stringstream &buffer) {
std::vector<Value> wires;
if (connection->instance->name == name && connection->connection.getComponent().pin == pin) {
wires = connection->connection.getWires();
} else {
wires = *connection->connection.getSecondWires();
}
for (auto &wire: wires) {
if (wire.isType(Value::ATTRIBUTE_REFERENCE)) {
auto attribute = connection->getAttribute(wire.asReference());
if (wire.isType(Value::WIRE_REFERENCE)) {
buffer << wireNames[connection->bus->name + "." + attribute.value.asReference()] << ", ";
} else if (wire.isType(Value::NIL)) {
buffer << "*, ";
} else {
buffer << attribute.value.stringify() << ", ";
}
} else {
if (wire.isType(Value::WIRE_REFERENCE)) {
buffer << wireNames[connection->bus->name + "." + wire.asReference()] << ", ";
} else if (wire.isType(Value::NIL)) {
buffer << "*, ";
} else {
buffer << wire.stringify() << ", ";
}
}
}
}
} // domain
+16
View File
@@ -0,0 +1,16 @@
#ifndef COMDEL_GENERATOR_H
#define COMDEL_GENERATOR_H
#include <sstream>
#include "schema.h"
#include "library.h"
namespace domain {
void generate_schema(std::string &librarySource, Schema *schema, std::ostream &buffer);
void generate_comdel(Schema *schema, Library &library, std::ostream &buffer);
} // domain
#endif //COMDEL_GENERATOR_H
+255
View File
@@ -0,0 +1,255 @@
#include <set>
#include "comdel_validator.h"
#include "library.h"
namespace domain {
std::vector<ValidationError> ComdelValidator::validateSchema(Schema &schema, ValidationContext context) {
std::vector<ValidationError> errors;
context.instance = nullptr;
context.attribute = nullptr;
for (auto &instance: schema.componentInstances) {
auto result = validateComponent(instance.get(), context);
errors.insert(errors.end(), result.begin(), result.end());
}
return errors;
}
std::vector<ValidationError>
ComdelValidator::validateInstanceCount(Schema &schema, Library &library, ValidationContext context) {
std::vector<ValidationError> errors;
// validate instance count
std::map<std::string, int> instanceMap;
for (auto &inst: schema.componentInstances) {
instanceMap[inst->component.getName()]++;
}
for (auto comp: library.getComponents()) {
int count = instanceMap[comp.getName()];
context.attributes["componentName"] = Value::fromString(comp.getName());
context.attributes["min"] = Value::fromInt(comp.getCount().first);
context.attributes["max"] = Value::fromInt(comp.getCount().second);
context.attributes["count"] = Value::fromInt(count);
if (count < comp.getCount().first) {
auto message = populateMessage(
"Nedovoljno instanci komponente '{componentName}' potrebno barem {min}, pronađeno {count}",
context);
errors.emplace_back(Action::ERROR, message);
} else if (count > comp.getCount().second) {
auto message = populateMessage(
"Previše insanci komponente '{componentName}' dozvoljeno najviše {max}, pronađeno {count}", context);
errors.emplace_back(Action::ERROR, message);
}
}
// validate bus instance count
std::map<std::string, int> busInstanceMap;
for (auto &inst: schema.busInstances) {
busInstanceMap[inst->bus.getName()]++;
}
for (auto bus: library.getBuses()) {
int count = busInstanceMap[bus.getName()];
context.attributes["busName"] = Value::fromString(bus.getName());
context.attributes["min"] = Value::fromInt(bus.getCount().first);
context.attributes["max"] = Value::fromInt(bus.getCount().second);
context.attributes["count"] = Value::fromInt(count);
if (count < bus.getCount().first) {
auto message = populateMessage(
"Nedovoljno instanci sabirnice '{busName}' potrebna barem jedna {min}, pronađeno {count}", context);
errors.emplace_back(Action::ERROR, message);
} else if (count > bus.getCount().second) {
auto message = populateMessage(
"Previše instanci sabirnice '{busName}' dozvoljeno najviše {max}, pronašeno {count}", context);
errors.emplace_back(Action::ERROR, message);
}
}
return errors;
}
std::vector<ValidationError>
ComdelValidator::validatePinConnections(Schema &schema, Library &library, ValidationContext context) {
std::vector<ValidationError> errors;
for (auto &inst: schema.componentInstances) {
for (auto &pin: inst->component.getPins()) {
if (pin.getConnection().getType() == PinConnection::REQUIRED) {
if (!connectionExists(schema, inst, pin)) {
context.instance = inst.get();
context.attributes["instanceName"] = Value::fromString(inst->name);
auto message = populateMessage(pin.getConnection().getMessage(), context);
errors.emplace_back(Action::ERROR, message);
}
}
}
}
return errors;
}
std::vector<ValidationError>
ComdelValidator::validateComponent(ComponentInstance *instance, ValidationContext context) {
std::vector<ValidationError> errors;
context.instance = instance;
context.attributes.clear();
for (auto &attribute: instance->attributes) {
context.attributes[attribute.name] = attribute.value;
}
for (auto &rule: instance->component.getRules()) {
auto result = validateRule(rule, context);
if (result) {
errors.push_back(*result);
}
}
for (auto &attribute: instance->attributes) {
auto result = validateAttribute(&attribute, context);
errors.insert(errors.end(), result.begin(), result.end());
}
return errors;
}
std::vector<ValidationError>
ComdelValidator::validateAttribute(InstanceAttribute *attribute, ValidationContext context) {
std::vector<ValidationError> errors;
if (attribute->attribute.getPopup()) {
Popup popup = *attribute->attribute.getPopup();
context.attribute = attribute;
context.attributes = std::map<std::string, Value>{{attribute->name, attribute->value}};
for (auto &rule: popup.getRules()) {
auto result = validateRule(rule, context);
if (result) {
errors.push_back(*result);
}
}
}
return errors;
}
std::optional<ValidationError> ComdelValidator::validateRule(Rule rule, ValidationContext context) {
RuleContext ruleContext;
ruleContext.addressSpaces = context.addressSpaces;
ruleContext.attributes = context.attributes;
ruleContext.function = validators;
auto action = rule.evaluate(ruleContext);
if (action) {
std::string message = this->populateMessage(action->getMessage(), context);
return ValidationError{context.instance, context.attribute, action->getType(), message};
}
return nullopt;
}
std::vector<ValidationError>
ComdelValidator::validateMemoryReferences(Schema &schema, Library &library, ValidationContext context) {
std::set<std::string> memoryInstances;
for(auto& component: schema.componentInstances) {
if(component->component.getType() == Component::MEMORY) {
memoryInstances.insert(component->name);
}
}
std::vector<ValidationError> errors;
for(auto& component: schema.componentInstances) {
if(component->component.getType() == Component::PROCESSOR) {
for(auto& attribute: component->attributes) {
if(attribute.value.isType(Value::MEMORY_REFERENCE)) {
auto memoryReference = attribute.value.asMemoryReference();
if(memoryReference != nullopt) {
if(memoryInstances.count(*memoryReference) == 0) {
context.attributes["memoryReference"] = domain::Value::fromString(memoryReference.value());
auto message = populateMessage("Ne postoji memorijska komponenta '{memoryReference}'", context);
errors.emplace_back(component.get(), nullptr, Action::ERROR, message);
}
}
}
}
}
}
return errors;
}
std::vector<ValidationError>
ComdelValidator::validateInstanceNames(Schema &schema, Library &library, ValidationContext context) {
std::set<std::string> names;
std::vector<ValidationError> errors;
for(auto& component: schema.componentInstances) {
if(names.find(component->name) != names.end()) {
context.attributes["componentName"] = Value::fromString(component->name);
auto message = populateMessage(
"Pronađeno više instanci sa imenom '{componentName}'", context);
errors.emplace_back(Action::ERROR, message);
}
names.insert(component->name);
}
return errors;
}
std::string ComdelValidator::populateMessage(string source, ValidationContext context) {
for (auto &[key, value]: context.attributes) {
source = replacePlaceholder(source, key, value);
}
return source;
}
string ComdelValidator::replacePlaceholder(string source, string key, Value value) {
key = "{" + key + "}";
auto placeholderValue = value.string();
auto found = source.find(key);
while (found != string::npos) {
source.replace(found, key.length(), placeholderValue);
found = source.find(key);
}
return source;
}
bool ComdelValidator::connectionExists(Schema &schema, shared_ptr<ComponentInstance> &component, Pin &pin) {
for (auto conn: schema.connections) {
auto busConnection = dynamic_cast<BusConnectionInstance *>(conn.get());
if (busConnection != nullptr) {
if (busConnection->instance->name == component->name &&
busConnection->connection.getComponent().pin == pin.getName()) {
return true;
}
}
auto directConnection = dynamic_cast<DirectConnectionInstance *>(conn.get());
if (directConnection != nullptr) {
if (directConnection->instance->name == component->name &&
directConnection->connection.getComponent().pin == pin.getName()) {
return true;
}
if (directConnection->secondInstance->name == component->name &&
directConnection->connection.getSecondComponent()->pin == pin.getName()) {
return true;
}
}
}
return false;
}
ComdelValidator::ComdelValidator(std::vector<FunctionValidator *> validators) {
for (auto *validator: validators) {
validator->clear();
this->validators.insert(std::make_pair(validator->getName(), validator));
}
}
}
+60
View File
@@ -0,0 +1,60 @@
#ifndef COMDEL_VALIDATOR_H
#define COMDEL_VALIDATOR_H
#include "instance.h"
#include "schema.h"
#include "library.h"
namespace domain {
struct ValidationError {
ComponentInstance *instance;
InstanceAttribute *attribute;
Action::ActionType type;
std::string message;
ValidationError(Action::ActionType type, std::string message): instance(nullptr), attribute(nullptr), type(type), message(message) {}
ValidationError(ComponentInstance* instance, InstanceAttribute* attribute, Action::ActionType type, std::string message): instance(instance), attribute(attribute), type(type), message(message) {}
};
struct ValidationContext {
ComponentInstance *instance;
InstanceAttribute *attribute;
std::map<std::string, AddressSpace> addressSpaces;
std::map<std::string, Value> attributes;
};
class ComdelValidator {
public:
std::vector<ValidationError> validateSchema(Schema &schema, ValidationContext context);
std::vector<ValidationError> validateComponent(ComponentInstance *instance, ValidationContext context);
std::vector<ValidationError> validateAttribute(InstanceAttribute *attribute, ValidationContext context);
std::optional<ValidationError> validateRule(Rule rule, ValidationContext context);
std::vector<ValidationError> validateMemoryReferences(Schema &schema, Library &library, ValidationContext context);
std::vector<ValidationError> validateInstanceNames(Schema &schema, Library &library, ValidationContext context);
std::vector<ValidationError> validateInstanceCount(Schema &schema, Library &library, ValidationContext context);
std::vector<ValidationError>
validatePinConnections(Schema &schema, Library &library, ValidationContext context);
ComdelValidator(std::vector<FunctionValidator *> validators);
private:
std::map<std::string, FunctionValidator *> validators;
std::string populateMessage(string message, ValidationContext context);
string replacePlaceholder(string message, const string name, Value value);
bool connectionExists(Schema &schema, shared_ptr<ComponentInstance> &component, Pin &pin);
};
}
#endif //COMDEL_VALIDATOR_H
-95
View File
@@ -1,95 +0,0 @@
#include "comdelvalidator.h"
namespace domain {
std::vector<ValidationError> ComdelValidator::validateSchema(Schema &schema, ValidationContext context) {
std::vector<ValidationError> errors;
context.instance = nullptr;
context.attribute = nullptr;
for(auto &inst: schema.instances) {
auto *instance = dynamic_cast<ComponentInstance*>(inst);
if(instance) {
auto result = validateComponent(instance, context);
errors.insert(errors.end(), result.begin(), result.end());
}
}
return errors;
}
std::vector<ValidationError> ComdelValidator::validateComponent(ComponentInstance *instance, ValidationContext context) {
std::vector<ValidationError> errors;
context.instance = instance;
context.attributes.clear();
for(auto &attribute: instance->attributes) {
context.attributes[attribute->name] = attribute->value;
}
for(auto &rule: instance->component.getRules()) {
auto result = validateRule(rule, context);
if(result) {
errors.push_back(*result);
}
}
for(auto &attribute: instance->attributes) {
auto result = validateAttribute(attribute, context);
errors.insert(errors.end(), result.begin(), result.end());
}
return errors;
}
std::vector<ValidationError> ComdelValidator::validateAttribute(InstanceAttribute *attribute, ValidationContext context) {
std::vector<ValidationError> errors;
if(attribute->attribute.getPopup()) {
Popup popup = *attribute->attribute.getPopup();
context.attribute = attribute;
context.attributes = std::map<std::string, Value>{{attribute->name, attribute->value}};
for(auto &rule: popup.getRules()) {
auto result = validateRule(rule, context);
if(result) {
errors.push_back(*result);
}
}
}
return errors;
}
std::optional<ValidationError> ComdelValidator::validateRule(Rule rule, ValidationContext context) {
RuleContext ruleContext;
ruleContext.addressSpaces = context.addressSpaces;
ruleContext.attributes = context.attributes;
ruleContext.function = callbacks;
auto action = rule.evaluate(ruleContext);
if (action) {
std::string message = this->populateMessage(action->getMessage(), context);
return ValidationError{context.instance, context.attribute, action->getType(), message};
}
return nullopt;
}
std::string ComdelValidator::populateMessage(string source, ValidationContext context) {
for(auto &[key, value]: context.attributes) {
source = replacePlaceholder(source, key, value);
}
return source;
}
string ComdelValidator::replacePlaceholder(string source, string key, Value value) {
key = "{" + key + "}";
auto placeholderValue = value.string();
auto found = source.find(key);
while(found != string::npos) {
source.replace(found, key.length(), placeholderValue);
found = source.find(key);
}
return source;
}
}
-43
View File
@@ -1,43 +0,0 @@
#ifndef COMDEL_VALIDATOR_H
#define COMDEL_VALIDATOR_H
#include "instance.h"
#include "schema.h"
namespace domain {
struct ValidationError
{
Instance *instance;
InstanceAttribute *attribute;
Action::ActionType type;
std::string message;
};
struct ValidationContext {
Instance *instance;
InstanceAttribute *attribute;
std::map<std::string, AddressSpace> addressSpaces;
std::map<std::string, Value> attributes;
};
class ComdelValidator
{
public:
std::vector<ValidationError> validateSchema(Schema& schema, ValidationContext context);
std::vector<ValidationError> validateComponent(ComponentInstance *instance, ValidationContext context);
std::vector<ValidationError> validateAttribute(InstanceAttribute *attribute, ValidationContext context);
std::optional<ValidationError> validateRule(Rule rule, ValidationContext context);
ComdelValidator(std::map<std::string, FunctionCallback> callbacks): callbacks(callbacks) {}
private:
std::map<std::string, FunctionCallback> callbacks;
std::string populateMessage(string basicString, ValidationContext context);
string replacePlaceholder(string basicString, const string basicString1, Value value);
};
}
#endif //COMDEL_VALIDATOR_H
+31 -12
View File
@@ -5,30 +5,37 @@ namespace domain {
Component::Component(string name, string tooltip, string source, ComponentType type, Component::Component(string name, string tooltip, string source, ComponentType type,
vector<Rule> rules, string instanceName, pair<int, int> count, Display display, vector<Rule> rules, string instanceName, pair<int, int> count, Display display,
vector<Pin> pins, vector<Attribute> attributes) vector<Pin> pins, vector<Attribute> attributes)
: name(name), tooltip(tooltip), source(source), type(type), rules(rules), instanceName(instanceName), count(count), display(display), pins(pins), attributes(attributes) : name(name), tooltip(tooltip), source(source), type(type), rules(rules), instanceName(instanceName),
{} count(count), display(display), pins(pins), attributes(attributes) {}
std::string Component::getName() { std::string Component::getName() {
return name; return name;
} }
std::string Component::getTooltip() { std::string Component::getTooltip() {
return tooltip; return tooltip;
} }
std::string Component::getSource() { std::string Component::getSource() {
return source; return source;
} }
Component::ComponentType Component::getType() { Component::ComponentType Component::getType() {
return type; return type;
} }
std::vector<Rule> Component::getRules() { std::vector<Rule> Component::getRules() {
return rules; return rules;
} }
std::string Component::getInstanceName() { std::string Component::getInstanceName() {
return instanceName; return instanceName;
} }
std::pair<int, int> Component::getCount() { std::pair<int, int> Component::getCount() {
return count; return count;
} }
Display Component::getDisplay() { Display Component::getDisplay() {
return display; return display;
} }
@@ -36,10 +43,11 @@ Display Component::getDisplay() {
std::vector<Pin> Component::getPins() { std::vector<Pin> Component::getPins() {
return pins; return pins;
} }
Pin Component::getPin(std::string pin) { Pin Component::getPin(std::string pin) {
for(uint i=0; i<pins.size(); i++) { for (auto & p: pins) {
if(pins[i].getName() == pin) { if (p.getName() == pin) {
return pins[i]; return p;
} }
} }
throw std::exception(); throw std::exception();
@@ -54,20 +62,32 @@ bool Component::hasPin(std::string name) {
return false; return false;
} }
std::vector<Attribute> Component::getAttributes() { std::vector<Attribute>& Component::getAttributes() {
return attributes; return attributes;
} }
Attribute Component::getAttribute(std::string attribute) { Attribute Component::getAttribute(std::string attribute) {
for(uint i=0; i<attributes.size(); i++) { for (auto & attr : attributes) {
if(attributes[i].getName() == attribute) { if (attr.getName() == attribute) {
return attributes[i]; return attr;
} }
} }
throw std::exception(); throw std::exception();
} }
bool Component::hasAttribute(std::string name, Value::ValueType type) { bool Component::hasAttribute(std::string name, Value::ValueType type) {
for(uint i=0; i<attributes.size(); i++) { for (auto & attribute : attributes) {
if(attributes[i].getName() == name && attributes[i].getDefault().getType() == type) { if (attribute.getName() == name && attribute.getDefault().getType() == type) {
return true;
}
if (attribute.getName() == name && (type == Value::NIL &&
(attribute.getDefault().getType() == Value::MEMORY_REFERENCE ||
attribute.getDefault().getType() == Value::WIRE_REFERENCE))) {
return true;
}
if (attribute.getName() == name && (type == Value::UNDEFINED &&
(attribute.getDefault().getType() == Value::MEMORY_REFERENCE ||
attribute.getDefault().getType() == Value::WIRE_REFERENCE))) {
return true; return true;
} }
} }
@@ -75,5 +95,4 @@ bool Component::hasAttribute(std::string name, Value::ValueType type) {
} }
} // namespace domain } // namespace domain
+12 -3
View File
@@ -13,8 +13,7 @@ namespace domain {
using namespace std; using namespace std;
class Component class Component {
{
public: public:
enum ComponentType { enum ComponentType {
OTHER, OTHER,
@@ -42,19 +41,29 @@ public:
vector<Pin> pins, vector<Attribute> attributes); vector<Pin> pins, vector<Attribute> attributes);
std::string getName(); std::string getName();
std::string getTooltip(); std::string getTooltip();
std::string getSource(); std::string getSource();
ComponentType getType(); ComponentType getType();
std::vector<Rule> getRules(); std::vector<Rule> getRules();
std::string getInstanceName(); std::string getInstanceName();
std::pair<int, int> getCount(); std::pair<int, int> getCount();
Display getDisplay(); Display getDisplay();
std::vector<Pin> getPins(); std::vector<Pin> getPins();
Pin getPin(std::string pin); Pin getPin(std::string pin);
std::vector<Attribute> getAttributes(); std::vector<Attribute>& getAttributes();
Attribute getAttribute(std::string attribute); Attribute getAttribute(std::string attribute);
bool hasAttribute(std::string name, Value::ValueType type); bool hasAttribute(std::string name, Value::ValueType type);
bool hasPin(std::string name); bool hasPin(std::string name);
+17 -4
View File
@@ -5,26 +5,29 @@ namespace domain {
Connection::Connection(ConnectionComponent first, std::optional<ConnectionComponent> second, Connection::Connection(ConnectionComponent first, std::optional<ConnectionComponent> second,
std::string bus, std::vector<Attribute> attributes, std::string bus, std::vector<Attribute> attributes,
std::vector<Value> firstWires, std::optional<std::vector<Value>> secondWires) std::vector<Value> firstWires, std::optional<std::vector<Value>> secondWires)
: first(first), second(second), bus(bus), attributes(attributes), firstWires(firstWires), secondWires(secondWires) : first(first), second(second), bus(bus), attributes(attributes), firstWires(firstWires),
{} secondWires(secondWires) {}
ConnectionComponent Connection::getComponent() { ConnectionComponent Connection::getComponent() {
return first; return first;
} }
std::optional<ConnectionComponent> Connection::getSecondComponent() { std::optional<ConnectionComponent> Connection::getSecondComponent() {
return second; return second;
} }
std::string Connection::getBus() { std::string &Connection::getBus() {
return bus; return bus;
} }
std::vector<Attribute> Connection::getAttributes() { std::vector<Attribute>& Connection::getAttributes() {
return attributes; return attributes;
} }
std::vector<Value> Connection::getWires() { std::vector<Value> Connection::getWires() {
return firstWires; return firstWires;
} }
std::optional<std::vector<Value>> Connection::getSecondWires() { std::optional<std::vector<Value>> Connection::getSecondWires() {
return secondWires; return secondWires;
} }
@@ -63,4 +66,14 @@ bool Connection::isConnecting(ConnectionComponent component, std::string bus, Co
(this->first == secondComponent && this->bus == bus && this->second == component); (this->first == secondComponent && this->bus == bus && this->second == component);
} }
bool Connection::isConnecting(ConnectionComponent component, ConnectionComponent secondComponent) {
if (!second.has_value()) {
return false;
}
return (this->first == component && this->second == secondComponent) ||
(this->first == secondComponent && this->second == component);
}
} // namespace domain } // namespace domain
+28 -10
View File
@@ -8,23 +8,31 @@
namespace domain { namespace domain {
struct ConnectionComponent struct ConnectionComponent {
{
std::string component; std::string component;
std::string pin; std::string pin;
bool operator==(const ConnectionComponent& rhs) const bool operator==(const ConnectionComponent &rhs) const {
{
return (component == rhs.component) && (pin == rhs.pin); return (component == rhs.component) && (pin == rhs.pin);
} }
bool operator!=(const ConnectionComponent& rhs) const
{ bool operator!=(const ConnectionComponent &rhs) const {
return !operator==(rhs); return !operator==(rhs);
} }
bool operator<(const ConnectionComponent &rhs) const {
if (component < rhs.component) {
return true;
} else if (component == rhs.component) {
if (pin < rhs.pin) {
return true;
}
}
return false;
}
}; };
class Connection class Connection {
{
ConnectionComponent first; ConnectionComponent first;
std::optional<ConnectionComponent> second; std::optional<ConnectionComponent> second;
std::string bus; std::string bus;
@@ -38,21 +46,31 @@ public:
std::vector<Value> firstWires, std::optional<std::vector<Value>> secondWires); std::vector<Value> firstWires, std::optional<std::vector<Value>> secondWires);
bool isConnecting(ConnectionComponent first); bool isConnecting(ConnectionComponent first);
bool isConnecting(ConnectionComponent first, std::string bus); bool isConnecting(ConnectionComponent first, std::string bus);
bool isConnecting(ConnectionComponent first, std::string bus, ConnectionComponent second); bool isConnecting(ConnectionComponent first, std::string bus, ConnectionComponent second);
bool isConnecting(ConnectionComponent first, ConnectionComponent second);
bool operator==(const Connection& connection) {
return (first == connection.first && bus == connection.bus && second == connection.second);
}
ConnectionComponent getComponent(); ConnectionComponent getComponent();
std::optional<ConnectionComponent> getSecondComponent(); std::optional<ConnectionComponent> getSecondComponent();
std::string getBus(); std::string &getBus();
std::vector<Attribute> getAttributes(); std::vector<Attribute>& getAttributes();
std::vector<Value> getWires(); std::vector<Value> getWires();
std::optional<std::vector<Value>> getSecondWires(); std::optional<std::vector<Value>> getSecondWires();
Attribute getAttribute(std::string name); Attribute getAttribute(std::string name);
bool hasAttribute(std::string name); bool hasAttribute(std::string name);
}; };
+29
View File
@@ -0,0 +1,29 @@
#include "connection_instance.h"
namespace domain {
ConnectionInstance::ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes,
Connection connection)
: instance(instance), attributes(attributes), connection(connection) {}
InstanceAttribute ConnectionInstance::getAttribute(string attribute) {
for (auto &attr: attributes) {
if (attr.name == attribute) {
return attr;
}
}
throw std::exception();
}
BusConnectionInstance::BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes,
BusInstance *bus, Connection connection)
: ConnectionInstance(instance, attributes, connection), bus(bus) {}
DirectConnectionInstance::DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondInstance,
std::vector<InstanceAttribute> attributes, BusInstance *bus,
Connection connection)
: ConnectionInstance(instance, attributes, connection), secondInstance(secondInstance), bus(bus) {}
} // namespace domain
+49
View File
@@ -0,0 +1,49 @@
#ifndef DOMAIN_CONNECTION_INSTANCE_H
#define DOMAIN_CONNECTION_INSTANCE_H
#include "connection.h"
#include "instance.h"
namespace domain {
class ConnectionInstance {
public:
ComponentInstance *instance;
Connection connection;
virtual ~ConnectionInstance() = default;
std::vector<InstanceAttribute> attributes;
std::pair<int, int> start;
std::pair<int, int> end;
ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes,
Connection connection);
InstanceAttribute getAttribute(string attribute);
};
class BusConnectionInstance : public ConnectionInstance {
public:
BusInstance *bus;
BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes, BusInstance *bus,
Connection connection);
};
class DirectConnectionInstance : public ConnectionInstance {
public:
BusInstance *bus;
ComponentInstance *secondInstance;
DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondsInstance,
std::vector<InstanceAttribute> attributes, BusInstance *bus, Connection connection);
};
} // namespace domain
#endif // DOMAIN_CONNECTIONINSTANCE_H
-18
View File
@@ -1,18 +0,0 @@
#include "connectioninstance.h"
namespace domain {
ConnectionInstance::ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute*> attributes, WireInstance *wire, Connection connection)
: instance(instance), attributes(attributes), connection(connection), wire(wire)
{}
BusConnectionInstance::BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute*> attributes, BusInstance *bus, WireInstance *wire, Connection connection)
: ConnectionInstance(instance, attributes, wire, connection), bus(bus)
{}
DirectConnectionInstance::DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondInstance, std::vector<InstanceAttribute*> attributes, BusInstance *bus, WireInstance *wire, Connection connection)
: ConnectionInstance(instance, attributes, wire, connection), secondInstance(secondInstance), bus(bus)
{}
} // namespace domain
-48
View File
@@ -1,48 +0,0 @@
#ifndef DOMAIN_CONNECTION_INSTANCE_H
#define DOMAIN_CONNECTION_INSTANCE_H
#include "connection.h"
#include "instance.h"
#include "wireinstance.h"
namespace domain {
class ConnectionInstance
{
public:
ComponentInstance *instance;
Connection connection;
WireInstance *wire;
virtual ~ConnectionInstance() = default;
std::vector<InstanceAttribute*> attributes;
ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute*> attributes, WireInstance *wire, Connection connection);
};
class BusConnectionInstance: public ConnectionInstance
{
public:
BusInstance *bus;
BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute*> attributes, BusInstance *bus, WireInstance *wire, Connection connection);
};
class DirectConnectionInstance: public ConnectionInstance
{
public:
BusInstance *bus;
ComponentInstance *secondInstance;
DirectConnectionInstance(ComponentInstance *instance, ComponentInstance *secondsInstance, std::vector<InstanceAttribute*> attributes, BusInstance *bus, WireInstance *wire, Connection connection);
};
} // namespace domain
#endif // DOMAIN_CONNECTIONINSTANCE_H
+236
View File
@@ -1,7 +1,243 @@
#include "display.h" #include "display.h"
#include <QBrush>
#include <QPen>
#include <QFont>
#include "comdel/domain/instance.h"
namespace domain { namespace domain {
Display::Display(std::vector<ui::Item> items) : items(items) {} Display::Display(std::vector<ui::Item> items) : items(items) {}
void Display::render(QGraphicsItemGroup *group, ui::DisplayContext context) {
for (auto &item: items) {
item.render(group, context);
}
}
void Display::comdel(std::ostream &buffer, int x, int y, int size) {
for (auto &item: items) {
item.comdel(buffer, x, y, size);
}
}
void ui::Text::render(QGraphicsItemGroup *group, ui::DisplayContext context) {
auto formattedText = context.populateMessage(text);
auto content = new QGraphicsTextItem(QString::fromStdString(formattedText));
content->setDefaultTextColor(color);
content->setPos(x, y);
content->setFont(QFont("arial", 8));
group->addToGroup(content);
}
void ui::Text::comdel(std::ostream &buffer, int x, int y) {
// TODO
}
void ui::Rect::render(QGraphicsItemGroup *group) {
auto rect = new QGraphicsRectItem(x, y, w, h);
rect->setPen(QPen(config.lineColor));
rect->setBrush(QBrush(config.fillColor));
group->addToGroup(rect);
}
void ui::Rect::comdel(std::ostream &buffer, int x, int y) {
buffer << "\t\trectangle {\n";
buffer << "\t\t\tx: " << (this->x + x) << "; y: " << (this->y + y) << ";\n";
buffer << "\t\t\tw: " << w << "; h: " << h << ";\n";
buffer << "\t\t}\n\n";
}
void ui::Line::render(QGraphicsItemGroup *group) {
auto line = new QGraphicsLineItem(x1, y1, x2, y2);
line->setPen(QPen(config.lineColor));
group->addToGroup(line);
}
void ui::Line::comdel(std::ostream &buffer, int x, int y) {
buffer << "\t\tline {\n";
buffer << "\t\t\tx1: " << (x1 + x) << "; y1: " << (y1 + y) << ";\n";
buffer << "\t\t\tx2: " << (x2 + x) << "; y2: " << (y2 + y) << ";\n";
buffer << "\t\t}\n\n";
}
void ui::Bus::render(QGraphicsItemGroup *group, int size) {
int _w = w, _h = h;
if (orientation == HORIZONTAL) {
_w = size;
} else {
_h = size;
}
auto rect = new QGraphicsRectItem(x, y, _w, _h);
rect->setBrush(QBrush(config.fillColor));
rect->setPen(QPen(config.lineColor));
group->addToGroup(rect);
}
int ui::Bus::getDefaultSize() {
if (orientation == HORIZONTAL) {
return w;
} else {
return h;
}
}
void ui::Bus::comdel(std::ostream &buffer, int x, int y, int size) {
buffer << "\t\trectangle {\n";
buffer << "\t\t\tx: " << (this->x + x) << "; y: " << (this->y + y) << ";\n";
if (orientation == HORIZONTAL) {
buffer << "\t\t\tw: " << size << "; h: " << h << ";\n";
} else {
buffer << "\t\t\tw: " << w << "; h: " << size << ";\n";
}
buffer << "\t\t}\n\n";
}
void ui::Pin::renderIn(QGraphicsItemGroup *group) {
QPolygon polygon;
switch (orientation) {
case PinOrientation::TOP:
polygon << QPoint(x, y) << QPoint(x, y + h) << QPoint(x + w, y + h) << QPoint(x + w, y)
<< QPoint(x + w / 2, y + h / 2);
break;
case PinOrientation::BOTTOM:
polygon << QPoint(x, y) << QPoint(x, y + h) << QPoint(x + w / 2, y + h / 2)
<< QPoint(x + w, y + h) << QPoint(x + w, y);
break;
case PinOrientation::LEFT:
polygon << QPoint(x, y) << QPoint(x + w / 2, y + h / 2) << QPoint(x, y + h)
<< QPoint(x + w, y + h) << QPoint(x + w, y);
break;
case PinOrientation::RIGHT:
polygon << QPoint(x, y) << QPoint(x, y + h) << QPoint(x + w, y + h)
<< QPoint(x + w / 2, y + h / 2) << QPoint(x + w, y);
break;
}
auto item = new QGraphicsPolygonItem(polygon);
item->setFillRule(Qt::OddEvenFill);
item->setBrush(QBrush(config.fillColor));
item->setPen(QPen(config.lineColor));
group->addToGroup(item);
}
void ui::Pin::renderOut(QGraphicsItemGroup *group) {
QPolygon polygon;
switch (orientation) {
case PinOrientation::TOP:
polygon << QPoint(x, y + h / 2) << QPoint(x, y + h) << QPoint(x + w, y + h)
<< QPoint(x + w, y + h / 2) << QPoint(x + w / 2, y);
break;
case PinOrientation::BOTTOM:
polygon << QPoint(x, y) << QPoint(x, y + h / 2) << QPoint(x + w / 2, y + h)
<< QPoint(x + w, y + h / 2) << QPoint(x + w, y);
break;
case PinOrientation::LEFT:
polygon << QPoint(x + w, y) << QPoint(x + w / 2, y) << QPoint(x, y + h / 2)
<< QPoint(x + w / 2, y + h) << QPoint(x + w, y + w);
break;
case PinOrientation::RIGHT:
polygon << QPoint(x, y) << QPoint(x, y + h) << QPoint(x + w / 2, y + h)
<< QPoint(x + w, y + h / 2) << QPoint(x + w / 2, y);
break;
}
auto item = new QGraphicsPolygonItem(polygon);
item->setFillRule(Qt::OddEvenFill);
item->setBrush(QBrush(config.fillColor));
item->setPen(QPen(config.lineColor));
group->addToGroup(item);
}
void ui::Pin::renderInOut(QGraphicsItemGroup *group) {
auto rect = new QGraphicsRectItem(x, y, w, h);
rect->setBrush(QBrush(config.fillColor));
rect->setPen(QPen(config.lineColor));
group->addToGroup(rect);
}
void ui::Pin::render(QGraphicsItemGroup *group) {
switch (pinType) {
case PinType::IN:
renderIn(group);
break;
case PinType::OUT:
renderOut(group);
break;
case PinType::IN_OUT:
renderInOut(group);
break;
}
}
int ui::Pin::getConnectionX() {
switch (orientation) {
case ui::PinOrientation::TOP:
case ui::PinOrientation::BOTTOM:
return x + w / 2;
case ui::PinOrientation::LEFT:
return x;
case ui::PinOrientation::RIGHT:
return x + w;
}
return 0;
}
int ui::Pin::getConnectionY() {
switch (orientation) {
case ui::PinOrientation::LEFT:
case ui::PinOrientation::RIGHT:
return y + h / 2;
case ui::PinOrientation::TOP:
return y;
case ui::PinOrientation::BOTTOM:
return y + h;
}
return 0;
}
void ui::Item::render(QGraphicsItemGroup *group, ui::DisplayContext context, int size) {
if (rect) rect->render(group);
if (line) line->render(group);
if (pin) pin->render(group);
if (bus) bus->render(group, size);
if (text) text->render(group, context);
}
void ui::Item::comdel(std::ostream &buffer, int x, int y, int size) {
if (rect) rect->comdel(buffer, x, y);
if (line) line->comdel(buffer, x, y);
// pins aren't exported
if (bus) bus->comdel(buffer, x, y, size);
// text currently isn't exported TODO
}
ui::DisplayContext::DisplayContext(ComponentInstance *instance) {
for(auto attr: instance->attributes) {
this->values[attr.name] = attr.value;
}
this->values["instanceName"] = Value::fromString(instance->name);
}
string replacePlaceholder(string source, string key, Value value) {
key = "{" + key + "}";
auto placeholderValue = value.string();
auto found = source.find(key);
while (found != string::npos) {
source.replace(found, key.length(), placeholderValue);
found = source.find(key);
}
return source;
}
std::string ui::DisplayContext::populateMessage(std::string source) {
for (auto &[key, value]: values) {
source = replacePlaceholder(source, key, value);
}
return source;
}
} // namespace domain } // namespace domain
+97 -30
View File
@@ -4,82 +4,149 @@
#include <QGraphicsItem> #include <QGraphicsItem>
#include <optional> #include <optional>
#include <ostream>
#include "comdel/parser/color.h"
#include "value.h"
namespace domain { namespace domain {
namespace ui { namespace ui {
class DisplayContext {
std::map<std::string, domain::Value> values;
public:
explicit DisplayContext(ComponentInstance *instance);
std::string populateMessage(std::string message);
};
struct DisplayConfig {
QColor lineColor;
QColor fillColor;
DisplayConfig(Color lineColor, Color fillColor) {
this->lineColor = QColor::fromRgb(lineColor.r, lineColor.g, lineColor.b, lineColor.a);
this->fillColor = QColor::fromRgb(fillColor.r, fillColor.g, fillColor.b, fillColor.a);
}
};
class Text {
public:
int x, y, w, h;
QColor color;
std::string text;
Text(int x, int y, int w, int h, std::string text, Color color) : x(x), y(y), w(w), h(h), text(text),
color(QColor::fromRgb(color.r, color.g, color.b, color.a)) {}
void render(QGraphicsItemGroup *group, ui::DisplayContext context);
void comdel(std::ostream &buffer, int x, int y);
};
class Rect { class Rect {
public: public:
int x, y, w, h; int x, y, w, h;
DisplayConfig config;
Rect(int x, int y, int w, int h): x(x), y(y), w(w), h(h) {} Rect(int x, int y, int w, int h, DisplayConfig config) : x(x), y(y), w(w), h(h), config(config) {}
void render(QGraphicsItemGroup *group);
void comdel(std::ostream &buffer, int x, int y);
void render(QGraphicsItemGroup *group) {
group->addToGroup(new QGraphicsRectItem(x,y,w,h));
}
}; };
class Line { class Line {
public: public:
int x1, y1, x2, y2; int x1, y1, x2, y2;
DisplayConfig config;
Line(int x1, int y1, int x2, int y2): x1(x1), y1(y1), x2(x2), y2(y2) {} Line(int x1, int y1, int x2, int y2, DisplayConfig config) : x1(x1), y1(y1), x2(x2), y2(y2), config(config) {}
void render(QGraphicsItemGroup *group) { void render(QGraphicsItemGroup *group);
group->addToGroup(new QGraphicsLineItem(x1, y1, x2, y2));
}
};
enum Orientation { void comdel(std::ostream &buffer, int x, int y);
LEFT, RIGHT, TOP, BOTTOM
}; };
enum PinType { enum PinType {
IN, OUT, IN_OUT IN, OUT, IN_OUT
}; };
enum PinOrientation {
LEFT, RIGHT, TOP, BOTTOM
};
enum BusOrientation {
VERTICAL, HORIZONTAL
};
class Bus {
public:
int x, y, w, h;
BusOrientation orientation;
DisplayConfig config;
Bus(int x, int y, int w, int h, BusOrientation orientation, DisplayConfig config) : x(x), y(y), w(w), h(h),
orientation(orientation),
config(config) {}
void render(QGraphicsItemGroup *group, int size);
int getDefaultSize();
void comdel(std::ostream &buffer, int x, int y, int size);
};
class Pin { class Pin {
public: public:
Orientation orientation; PinOrientation orientation;
PinType pinType; PinType pinType;
int x, y, w, h; int x, y, w, h;
DisplayConfig config;
Pin(int x, int y, int w, int h): x(x), y(y), w(w), h(h) {} Pin(int x, int y, int w, int h, PinOrientation orientation, PinType pinType, DisplayConfig config)
: x(x), y(y), w(w), h(h), orientation(orientation), pinType(pinType), config(config) {}
void render(QGraphicsItemGroup *group) { public:
group->addToGroup(new QGraphicsRectItem(x,y,w,h)); void render(QGraphicsItemGroup *group);
}
int getConnectionX();
int getConnectionY();
private:
void renderIn(QGraphicsItemGroup *group);
void renderOut(QGraphicsItemGroup *group);
void renderInOut(QGraphicsItemGroup *group);
}; };
class Item { class Item {
public: public:
Item(): rect(std::nullopt), line(std::nullopt), pin(std::nullopt) {} Item() : rect(std::nullopt), line(std::nullopt), pin(std::nullopt), bus(std::nullopt) {}
void render(QGraphicsItemGroup *group) {
if(rect) rect->render(group);
if(line) line->render(group);
if(pin) pin->render(group);
}
std::optional<Rect> rect = std::nullopt; std::optional<Rect> rect = std::nullopt;
std::optional<Line> line = std::nullopt; std::optional<Line> line = std::nullopt;
std::optional<Pin> pin = std::nullopt; std::optional<Pin> pin = std::nullopt;
std::optional<Bus> bus = std::nullopt;
std::optional<Text> text = std::nullopt;
void render(QGraphicsItemGroup *group, ui::DisplayContext context, int size = 0);
void comdel(std::ostream &buffer, int x, int y, int size = 0);
}; };
} }
class Display class Display {
{
public: public:
Display(std::vector<ui::Item> items); Display(std::vector<ui::Item> items);
void render(QGraphicsItemGroup *group) { void render(QGraphicsItemGroup *group, ui::DisplayContext context);
for(auto &item: items) {
item.render(group); void comdel(std::ostream &buffer, int x, int y, int size = 0);
}
} std::vector<ui::Item> &getItems() { return items; }
private: private:
std::vector<ui::Item> items; std::vector<ui::Item> items;
+166
View File
@@ -0,0 +1,166 @@
#include <map>
#include <utility>
#include "function_signature.h"
namespace domain {
class DivisibleValidator : public FunctionValidator {
public:
DivisibleValidator() : FunctionValidator("divisible", {Value::INT, Value::INT}) {}
bool validate(std::vector<Value> values) override {
if (validateSignature(values)) {
long long first = values[0].asInt();
long long second = values[1].asInt();
return (first % second) == 0;
}
return false;
}
void clear() override {}
};
class LessThenValidator : public FunctionValidator {
public:
LessThenValidator() : FunctionValidator("less_then", {Value::INT, Value::INT}) {}
bool validate(std::vector<Value> values) override {
if (validateSignature(values)) {
long long first = values[0].asInt();
long long second = values[1].asInt();
return first < second;
}
return false;
}
void clear() override {}
};
class GreaterThenValidator : public FunctionValidator {
public:
GreaterThenValidator() : FunctionValidator("greater_then", {Value::INT, Value::INT}) {}
bool validate(std::vector<Value> values) override {
if (validateSignature(values)) {
long long first = values[0].asInt();
long long second = values[1].asInt();
return first > second;
}
return false;
}
void clear() override {}
};
class ContainsAddressValidator : public FunctionValidator {
public:
ContainsAddressValidator() : FunctionValidator("contains_address", {Value::ADDRESS_SPACE, Value::INT}) {}
bool validate(std::vector<Value> values) override {
if (validateSignature(values)) {
AddressSpace space = values[0].asAddressSpace();
long long address = values[1].asInt();
return space.contains(address);
}
return false;
}
void clear() override {}
};
class ContainsValidator : public FunctionValidator {
public:
ContainsValidator() : FunctionValidator("contains", {Value::ADDRESS_SPACE, Value::INT, Value::INT}) {}
bool validate(std::vector<Value> values) override {
if (validateSignature(values)) {
AddressSpace space = values[0].asAddressSpace();
long long start = values[1].asInt();
long long size = values[1].asInt();
return space.contains(start, start + size);
}
return false;
}
void clear() override {}
};
class UniqueValidator : public FunctionValidator {
private:
std::map<std::string, std::vector<std::pair<long long int, long long int>>> spaces;
bool overlaps(long long int start1, long long int end1, long long int start2, long long int end2) {
return std::max((long long int) 0, std::min(end1, end2) - std::max(start1, start2)) > 0;
}
public:
UniqueValidator() : FunctionValidator("unique", {Value::ADDRESS_SPACE, Value::INT, Value::INT}) {}
bool validate(std::vector<Value> values) override {
if (validateSignature(values)) {
std::string space = values[0].asAddressSpace().getName();
long long int start = values[1].asInt();
long long int end = start + values[2].asInt();
if (spaces.count(space) == 0) {
spaces.insert(std::make_pair(space, std::vector<std::pair<long long int, long long int>>{}));
}
for (auto &s: spaces[space]) {
if (overlaps(s.first, s.second, start, end)) {
return false;
}
}
spaces[space].push_back(std::make_pair(start, end));
return true;
}
return false;
}
void clear() override {
spaces.clear();
}
};
std::vector<FunctionValidator *> getSupportedValidators() {
std::vector<FunctionValidator *> validators;
validators.push_back(new DivisibleValidator());
validators.push_back(new LessThenValidator());
validators.push_back(new GreaterThenValidator());
validators.push_back(new ContainsAddressValidator());
validators.push_back(new ContainsValidator());
validators.push_back(new UniqueValidator());
return validators;
}
FunctionValidator::FunctionValidator(std::string name, std::vector<Value::ValueType> signature)
: name(std::move(name)), signature(std::move(signature)) {}
std::string FunctionValidator::getName() {
return name;
}
std::vector<Value::ValueType> FunctionValidator::getSignature() {
return signature;
}
bool FunctionValidator::validateSignature(std::vector<Value> _signature) {
if (this->signature.size() != _signature.size()) {
return false;
}
for (int i = 0; i < this->signature.size(); i++) {
if (this->signature[i] != _signature[i].getType()) {
return false;
}
}
return true;
}
} // namespace domain
+36
View File
@@ -0,0 +1,36 @@
#ifndef DOMAIN_FUNCTION_VALIDATOR_H
#define DOMAIN_FUNCTION_VALIDATOR_H
#include<functional>
#include<vector>
#include "value.h"
namespace domain {
class FunctionValidator {
private:
std::string name;
std::vector<Value::ValueType> signature;
protected:
FunctionValidator(std::string name, std::vector<Value::ValueType> signature);
public:
std::string getName();
std::vector<Value::ValueType> getSignature();
bool validateSignature(std::vector<Value> signature);
virtual bool validate(std::vector<Value> values) = 0;
virtual void clear() = 0;
};
std::vector<FunctionValidator *> getSupportedValidators();
} // namespace domain
#endif // DOMAIN_FUNCTION_VALIDATOR_H
-45
View File
@@ -1,45 +0,0 @@
#include "functionsignature.h"
namespace domain {
FunctionSignature add(std::string name, std::vector<Value::ValueType> types, FunctionCallback callback) {
return {name, types, callback};
}
std::vector<FunctionSignature> getSupportedFunctions() {
std::vector<FunctionSignature> s;
s.push_back(add("divisible", std::vector<Value::ValueType>{Value::INT, Value::INT},[](std::vector<Value> values) {
long long first = values[0].asInt();
long long second = values[1].asInt();
return (first % second) == 0;
}));
s.push_back(add("less_then", std::vector<Value::ValueType>{Value::INT, Value::INT}, [](std::vector<Value> values) {
long long first = values[0].asInt();
long long second = values[1].asInt();
return first < second;
}));
s.push_back(add("greater_then", std::vector<Value::ValueType>{Value::INT, Value::INT}, [](std::vector<Value> values) {
long long first = values[0].asInt();
long long second = values[1].asInt();
return first > second;
}));
s.push_back(add("contains_address", std::vector<Value::ValueType>{Value::ADDRESS_SPACE, Value::INT}, [](std::vector<Value> values) {
AddressSpace space = values[0].asAddressSpace();
long long address = values[1].asInt();
return space.contains(address);
}));
s.push_back(add("contains", std::vector<Value::ValueType>{Value::ADDRESS_SPACE, Value::INT, Value::INT}, [](std::vector<Value> values) {
AddressSpace space = values[0].asAddressSpace();
long long start = values[1].asInt();
long long size = values[1].asInt();
return space.contains(start, start + size);
}));
s.push_back(add("unique", std::vector<Value::ValueType>{Value::ADDRESS_SPACE, Value::INT, Value::INT}, [](std::vector<Value> values) {
return true;
}));
return s;
}
} // namespace domain
-25
View File
@@ -1,25 +0,0 @@
#ifndef DOMAIN_FUNCTION_SIGNATURE_H
#define DOMAIN_FUNCTION_SIGNATURE_H
#include<functional>
#include<vector>
#include "value.h"
namespace domain {
typedef std::function<bool (std::vector<Value>)> FunctionCallback;
struct FunctionSignature {
std::string name;
std::vector<Value::ValueType> params;
FunctionCallback callback;
FunctionSignature(std::string name, std::vector<Value::ValueType> params, FunctionCallback callback): name(name), params(params), callback(callback) {}
};
std::vector<FunctionSignature> getSupportedFunctions();
} // namespace domain
#endif // DOMAIN_FUNCTIONSIGNATURE_H
+11 -9
View File
@@ -2,16 +2,18 @@
namespace domain { namespace domain {
Instance::Instance(std::string name, std::vector<InstanceAttribute*> attributes, std::pair<int, int> position)
: name(name), attributes(attributes), position(position)
{}
BusInstance::BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size) BusInstance::BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size)
: Instance(name, vector<InstanceAttribute*>(), position), bus(bus), size(size) : name(name), position(position), bus(bus), size(size) {
{} if (size < 0 && bus.getDisplayBus().has_value()) {
ComponentInstance::ComponentInstance(std::string name, std::vector<InstanceAttribute*> attributes, std::pair<int, int> position, Component component) this->size = bus.getDisplayBus()->getDefaultSize();
: Instance(name, attributes, position), component(component) }
{} }
BusInstance::BusInstance(std::string name, Bus bus) : name(name), bus(bus), position(0, 0), size(0) {}
ComponentInstance::ComponentInstance(std::string name, std::vector<InstanceAttribute> attributes,
std::pair<int, int> position, Component component)
: name(name), attributes(std::move(attributes)), position(position), component(component) {}
} // namespace domain } // namespace domain
+13 -23
View File
@@ -3,50 +3,40 @@
#include "bus.h" #include "bus.h"
#include "component.h" #include "component.h"
#include "instanceattribute.h" #include "instance_attribute.h"
#include <string> #include <string>
#include <vector> #include <vector>
namespace domain { namespace domain {
class Instance class BusInstance {
{
public: public:
std::string name; std::string name;
std::vector<InstanceAttribute*> attributes;
std::pair<int, int> position; std::pair<int, int> position;
virtual ~Instance() {};
Instance(std::string name, std::vector<InstanceAttribute*> attributes, std::pair<int, int> position);
};
class BusInstance: public Instance
{
public:
Bus bus; Bus bus;
int size; int size;
BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size); BusInstance(std::string name, Bus bus);
virtual ~BusInstance() { BusInstance(std::string name, std::pair<int, int> position, Bus bus, int size = -1);
Instance::~Instance();
} virtual ~BusInstance() = default;
}; };
class ComponentInstance: public Instance class ComponentInstance {
{
public: public:
std::string name;
std::vector<InstanceAttribute> attributes;
std::pair<int, int> position;
Component component; Component component;
ComponentInstance(std::string name, std::vector<InstanceAttribute*> attributes, std::pair<int, int> position, Component component); ComponentInstance(std::string name, std::vector<InstanceAttribute> attributes, std::pair<int, int> position, Component component);
virtual ~ComponentInstance() { virtual ~ComponentInstance() = default;
Instance::~Instance();
}
}; };
} // namespace domain } // namespace domain
+8
View File
@@ -0,0 +1,8 @@
#include "instance_attribute.h"
namespace domain {
InstanceAttribute::InstanceAttribute(std::string name, Value value, Attribute attribute)
: name(name), value(value), attribute(attribute) {}
} // namespace domain
+25
View File
@@ -0,0 +1,25 @@
#ifndef DOMAIN_INSTANCE_ATTRIBUTE_H
#define DOMAIN_INSTANCE_ATTRIBUTE_H
#include "attribute.h"
#include "value.h"
#include <string>
namespace domain {
class InstanceAttribute {
public:
InstanceAttribute(std::string name, Value value, Attribute attribute);
std::string name;
Value value;
Attribute attribute;
~InstanceAttribute() = default;
};
} // namespace domain
#endif // DOMAIN_INSTANCE_ATTRIBUTE_H
-9
View File
@@ -1,9 +0,0 @@
#include "instanceattribute.h"
namespace domain {
InstanceAttribute::InstanceAttribute(std::string name, Value value, Attribute attribute)
: name(name), value(value), attribute(attribute)
{}
} // namespace domain
-26
View File
@@ -1,26 +0,0 @@
#ifndef DOMAIN_INSTANCE_ATTRIBUTE_H
#define DOMAIN_INSTANCE_ATTRIBUTE_H
#include "attribute.h"
#include "value.h"
#include <string>
namespace domain {
class InstanceAttribute
{
public:
InstanceAttribute(std::string name, Value value, Attribute attribute);
std::string name;
Value value;
Attribute attribute;
};
} // namespace domain
#endif // DOMAIN_INSTANCE_ATTRIBUTE_H
+37 -5
View File
@@ -3,33 +3,45 @@
namespace domain { namespace domain {
Library::Library(string name, string libraryInfo, string header, string componentDirectory, Library::Library(string name, string libraryInfo, string header, string componentDirectory,
vector<AddressSpace> addressSpaces, vector<Component> components, vector<Bus> buses, vector<Connection> connections, map<string, string> messages) std::optional<std::string> componentHeader,
: name(name), libraryInfo(libraryInfo), header(header), componentDirectory(componentDirectory), addressSpaces(addressSpaces), vector<AddressSpace> addressSpaces, vector<Component> components, vector<Bus> buses,
components(components), buses(buses), connections(connections), messages(messages) vector<Connection> connections, map<string, string> messages)
{} : name(name), libraryInfo(libraryInfo), header(header), componentDirectory(componentDirectory),
componentHeader(componentHeader), addressSpaces(addressSpaces),
components(components), buses(buses), connections(connections), messages(messages) {}
std::string Library::getName() { std::string Library::getName() {
return name; return name;
} }
std::string Library::getLibraryInfo() { std::string Library::getLibraryInfo() {
return libraryInfo; return libraryInfo;
} }
std::string Library::getHeader() { std::string Library::getHeader() {
return header; return header;
} }
std::string Library::getComponentDirectory() { std::string Library::getComponentDirectory() {
return componentDirectory; return componentDirectory;
} }
std::optional<std::string> Library::getComponentHeader() {
return componentHeader;
}
std::vector<AddressSpace> Library::getAddressSpaces() { std::vector<AddressSpace> Library::getAddressSpaces() {
return addressSpaces; return addressSpaces;
} }
std::vector<Component> Library::getComponents() { std::vector<Component> Library::getComponents() {
return components; return components;
} }
std::vector<Bus> Library::getBuses() { std::vector<Bus> Library::getBuses() {
return buses; return buses;
} }
std::vector<Connection> Library::getConnections() { std::vector<Connection> Library::getConnections() {
return connections; return connections;
} }
@@ -46,6 +58,7 @@ bool Library::hasComponent(std::string name) {
} }
return false; return false;
} }
bool Library::hasBus(std::string name) { bool Library::hasBus(std::string name) {
for (uint i = 0; i < buses.size(); i++) { for (uint i = 0; i < buses.size(); i++) {
if (buses[i].getName() == name) { if (buses[i].getName() == name) {
@@ -64,6 +77,7 @@ AddressSpace &Library::getAddressSpace(std::string addressSpace) {
} }
throw std::exception(); throw std::exception();
} }
Component &Library::getComponent(std::string component) { Component &Library::getComponent(std::string component) {
for (uint i = 0; i < components.size(); i++) { for (uint i = 0; i < components.size(); i++) {
if (components[i].getName() == component) { if (components[i].getName() == component) {
@@ -72,6 +86,7 @@ Component &Library::getComponent(std::string component) {
} }
throw std::exception(); throw std::exception();
} }
Bus &Library::getBus(std::string bus) { Bus &Library::getBus(std::string bus) {
for (uint i = 0; i < buses.size(); i++) { for (uint i = 0; i < buses.size(); i++) {
if (buses[i].getName() == bus) { if (buses[i].getName() == bus) {
@@ -85,6 +100,7 @@ Bus &Library::getBus(std::string bus) {
bool Library::hasConnection(ConnectionComponent component, std::string bus) { bool Library::hasConnection(ConnectionComponent component, std::string bus) {
return getConnection(component, bus).has_value(); return getConnection(component, bus).has_value();
} }
std::optional<Connection> Library::getConnection(ConnectionComponent component, std::string bus) { std::optional<Connection> Library::getConnection(ConnectionComponent component, std::string bus) {
for (uint i = 0; i < connections.size(); i++) { for (uint i = 0; i < connections.size(); i++) {
if (connections[i].isConnecting(component, bus)) { if (connections[i].isConnecting(component, bus)) {
@@ -97,7 +113,9 @@ std::optional<Connection> Library::getConnection(ConnectionComponent component,
bool Library::hasConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent) { bool Library::hasConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent) {
return getConnection(component, bus, secondComponent).has_value(); return getConnection(component, bus, secondComponent).has_value();
} }
std::optional<Connection> Library::getConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent) {
std::optional<Connection>
Library::getConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent) {
for (uint i = 0; i < connections.size(); i++) { for (uint i = 0; i < connections.size(); i++) {
if (connections[i].isConnecting(component, bus, secondComponent)) { if (connections[i].isConnecting(component, bus, secondComponent)) {
return connections[i]; return connections[i];
@@ -110,4 +128,18 @@ std::string Library::getMessage(std::string key) {
return messages[key]; return messages[key];
} }
bool Library::hasConnection(ConnectionComponent component, ConnectionComponent secondComponent) {
return getConnection(component, secondComponent).has_value();
}
std::optional<Connection>
Library::getConnection(ConnectionComponent component, ConnectionComponent secondComponent) {
for (auto &connection: connections) {
if (connection.isConnecting(component, secondComponent)) {
return connection;
}
}
return nullopt;
}
} // namespace domain } // namespace domain
+25 -5
View File
@@ -1,7 +1,7 @@
#ifndef DOMAIN_LIBRARY_H #ifndef DOMAIN_LIBRARY_H
#define DOMAIN_LIBRARY_H #define DOMAIN_LIBRARY_H
#include "addressspace.h" #include "address_space.h"
#include "bus.h" #include "bus.h"
#include "component.h" #include "component.h"
#include "connection.h" #include "connection.h"
@@ -10,17 +10,16 @@
#include <vector> #include <vector>
namespace domain { namespace domain {
using namespace std; using namespace std;
class Library class Library {
{
std::string name; std::string name;
std::string libraryInfo; std::string libraryInfo;
std::string header; std::string header;
std::string componentDirectory; std::string componentDirectory;
std::optional<std::string> componentHeader;
std::vector<AddressSpace> addressSpaces; std::vector<AddressSpace> addressSpaces;
std::vector<Component> components; std::vector<Component> components;
@@ -31,33 +30,54 @@ class Library
public: public:
Library(string name, string libraryInfo, string header, string componentDirectory, Library(string name, string libraryInfo, string header, string componentDirectory,
vector<AddressSpace> addressSpaces, vector<Component> components, vector<Bus> buses, vector<Connection> connection, map<string, string> messages); std::optional<string> componentHeader,
vector<AddressSpace> addressSpaces, vector<Component> components, vector<Bus> buses,
vector<Connection> connection, map<string, string> messages);
std::string getName(); std::string getName();
std::string getLibraryInfo(); std::string getLibraryInfo();
std::string getHeader(); std::string getHeader();
std::string getComponentDirectory(); std::string getComponentDirectory();
std::optional<std::string> getComponentHeader();
std::vector<AddressSpace> getAddressSpaces(); std::vector<AddressSpace> getAddressSpaces();
std::vector<Component> getComponents(); std::vector<Component> getComponents();
std::vector<Bus> getBuses(); std::vector<Bus> getBuses();
std::vector<Connection> getConnections(); std::vector<Connection> getConnections();
bool hasComponent(std::string name); bool hasComponent(std::string name);
bool hasBus(std::string name); bool hasBus(std::string name);
std::map<std::string, std::string> getMessages(); std::map<std::string, std::string> getMessages();
AddressSpace &getAddressSpace(std::string name); AddressSpace &getAddressSpace(std::string name);
Component &getComponent(std::string name); Component &getComponent(std::string name);
Bus &getBus(std::string bus); Bus &getBus(std::string bus);
std::string getMessage(std::string key); std::string getMessage(std::string key);
bool hasConnection(ConnectionComponent component, std::string bus); bool hasConnection(ConnectionComponent component, std::string bus);
std::optional<Connection> getConnection(ConnectionComponent component, std::string bus); std::optional<Connection> getConnection(ConnectionComponent component, std::string bus);
bool hasConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent); bool hasConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent);
std::optional<Connection> getConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent); std::optional<Connection> getConnection(ConnectionComponent component, std::string bus, ConnectionComponent secondComponent);
bool hasConnection(ConnectionComponent component, ConnectionComponent secondComponent);
std::optional<Connection> getConnection(ConnectionComponent component, ConnectionComponent secondComponent);
}; };
} // namespace domain } // namespace domain
+13 -8
View File
@@ -3,36 +3,41 @@
namespace domain { namespace domain {
PinConnection::PinConnection(std::string message, ConnectionType type) PinConnection::PinConnection(std::string message, ConnectionType type)
: message(message), type(type) : message(message), type(type) {}
{}
PinConnection::ConnectionType PinConnection::getType() { PinConnection::ConnectionType PinConnection::getType() {
return type; return type;
} }
std::string PinConnection::getMessage() { std::string PinConnection::getMessage() {
return message; return message;
} }
Pin::Pin(std::string name, PinType type, std::string tooltip, PinConnection connection, Display display, std::optional<std::vector<Value>> wires) Pin::Pin(std::string name, PinType type, std::string tooltip, PinConnection connection, domain::ui::Pin pin,
: name(name), type(type), tooltip(tooltip), connection(connection), display(display), wires(wires) std::optional<std::vector<Value>> wires)
{} : name(name), type(type), tooltip(tooltip), connection(connection), displayPin(pin), wires(wires) {}
std::string Pin::getName() { std::string &Pin::getName() {
return name; return name;
} }
Pin::PinType Pin::getType() { Pin::PinType Pin::getType() {
return type; return type;
} }
std::string Pin::getTooltip() { std::string Pin::getTooltip() {
return tooltip; return tooltip;
} }
Display &Pin::getDisplay() {
return display; ui::Pin &Pin::getDisplayPin() {
return displayPin;
} }
PinConnection &Pin::getConnection() { PinConnection &Pin::getConnection() {
return connection; return connection;
} }
std::optional<std::vector<Value>> &Pin::getWires() { std::optional<std::vector<Value>> &Pin::getWires() {
return wires; return wires;
} }
+15 -11
View File
@@ -7,15 +7,13 @@
#include <string> #include <string>
namespace domain { namespace domain {
class PinConnection class PinConnection {
{
public: public:
enum ConnectionType { enum ConnectionType {
CHECK_ONLY, REQUIRED,
AUTOMATICALLY OPTIONAL
}; };
private: private:
@@ -26,11 +24,11 @@ public:
PinConnection(std::string message, ConnectionType type); PinConnection(std::string message, ConnectionType type);
ConnectionType getType(); ConnectionType getType();
std::string getMessage(); std::string getMessage();
}; };
class Pin class Pin {
{
public: public:
enum PinType { enum PinType {
IN_OUT, IN_OUT,
@@ -43,18 +41,24 @@ private:
PinType type; PinType type;
std::string tooltip; std::string tooltip;
PinConnection connection; PinConnection connection;
Display display; domain::ui::Pin displayPin;
std::optional<std::vector<Value>> wires; std::optional<std::vector<Value>> wires;
public: public:
Pin(std::string name, PinType type, std::string tooltip, PinConnection connection, Display display, std::optional<std::vector<Value>> wires); Pin(std::string name, PinType type, std::string tooltip, PinConnection connection, domain::ui::Pin pin,
std::optional<std::vector<Value>> wires);
std::string &getName();
std::string getName();
PinType getType(); PinType getType();
std::string getTooltip(); std::string getTooltip();
Display &getDisplay();
ui::Pin &getDisplayPin();
PinConnection &getConnection(); PinConnection &getConnection();
std::optional<std::vector<Value>> &getWires(); std::optional<std::vector<Value>> &getWires();
}; };
+16 -17
View File
@@ -1,42 +1,42 @@
#include "rule.h" #include "rule.h"
#include <utility>
namespace domain { namespace domain {
Condition::Condition(std::string function, std::vector<Value> params, bool negated) Condition::Condition(std::string function, std::vector<Value> params, bool negated)
: negated(negated), function(function), params(params) : negated(negated), function(std::move(function)), params(std::move(params)) {}
{}
bool Condition::evaluate(RuleContext &context) { bool Condition::evaluate(RuleContext &context) {
std::vector<Value> request; std::vector<Value> request;
for(unsigned int i=0; i<params.size(); i++) { for (auto & param : params) {
if(params[i].isType(Value::ADDRESS_SPACE_REFERENCE)) { if (param.isType(Value::ADDRESS_SPACE_REFERENCE)) {
request.push_back(Value::fromAddressSpace(context.addressSpaces.at(params[i].asReference()))); request.push_back(Value::fromAddressSpace(context.addressSpaces.at(param.asReference())));
} else if(params[i].isType(Value::ATTRIBUTE_REFERENCE)) { } else if (param.isType(Value::ATTRIBUTE_REFERENCE)) {
request.push_back(context.attributes[params[i].asReference()]); request.push_back(context.attributes[param.asReference()]);
} else { } else {
request.push_back(params[i]); request.push_back(param);
} }
} }
bool result = context.function[function](request); bool result = context.function[function]->validate(request);
return negated ? !result : result; return negated ? !result : result;
} }
Action::Action(ActionType type, std::string message) Action::Action(ActionType type, std::string message)
: type(type), message(message) : type(type), message(std::move(message)) {}
{}
Action::ActionType Action::getType() { Action::ActionType Action::getType() {
return type; return type;
} }
std::string Action::getMessage() { std::string Action::getMessage() {
return message; return message;
} }
IfStatement::IfStatement(Condition condition, Action action) IfStatement::IfStatement(Condition condition, Action action)
: condition(condition), action(action) : condition(std::move(condition)), action(std::move(action)) {}
{}
std::optional<Action> IfStatement::evaluate(RuleContext &context) { std::optional<Action> IfStatement::evaluate(RuleContext &context) {
if (condition.evaluate(context)) { if (condition.evaluate(context)) {
@@ -46,12 +46,11 @@ std::optional<Action> IfStatement::evaluate(RuleContext &context) {
} }
Rule::Rule(std::vector<IfStatement> statements) Rule::Rule(std::vector<IfStatement> statements)
: statements(statements) : statements(std::move(statements)) {}
{}
std::optional<Action> Rule::evaluate(RuleContext &context) { std::optional<Action> Rule::evaluate(RuleContext &context) {
for(unsigned int i=0; i<statements.size(); i++) { for (auto & statement : statements) {
auto response = statements[i].evaluate(context); auto response = statement.evaluate(context);
if (response) { if (response) {
return response; return response;
} }
+6 -6
View File
@@ -1,9 +1,9 @@
#ifndef DOMAIN_RULE_H #ifndef DOMAIN_RULE_H
#define DOMAIN_RULE_H #define DOMAIN_RULE_H
#include "addressspace.h" #include "address_space.h"
#include "value.h" #include "value.h"
#include "functionsignature.h" #include "function_signature.h"
#include <map> #include <map>
#include <string> #include <string>
@@ -15,7 +15,7 @@ namespace domain {
struct RuleContext { struct RuleContext {
std::map<std::string, AddressSpace> addressSpaces; std::map<std::string, AddressSpace> addressSpaces;
std::map<std::string, Value> attributes; std::map<std::string, Value> attributes;
std::map<std::string, FunctionCallback> function; std::map<std::string, FunctionValidator *> function;
}; };
class Condition { class Condition {
@@ -44,6 +44,7 @@ public:
Action(ActionType type, std::string message); Action(ActionType type, std::string message);
ActionType getType(); ActionType getType();
std::string getMessage(); std::string getMessage();
}; };
@@ -56,11 +57,10 @@ public:
std::optional<Action> evaluate(RuleContext &context); std::optional<Action> evaluate(RuleContext &context);
}; };
class Rule class Rule {
{
std::vector<IfStatement> statements; std::vector<IfStatement> statements;
public: public:
Rule(std::vector<IfStatement> statements); explicit Rule(std::vector<IfStatement> statements);
std::optional<Action> evaluate(RuleContext &context); std::optional<Action> evaluate(RuleContext &context);
}; };
+113 -2
View File
@@ -2,8 +2,119 @@
namespace domain { namespace domain {
Schema::Schema() bool Schema::hasConnection(string &component, string &pin) {
{ return getConnection(component, pin) != nullptr;
}
ConnectionInstance *Schema::getConnection(string &component, string &pin) {
auto pinConnections = getConnections(component, pin);
if(pinConnections.empty()) {
return nullptr;
} else {
return pinConnections[0];
}
}
std::vector<ConnectionInstance*> Schema::getConnections(string &component, string &pin) {
std::vector<ConnectionInstance*> pinConnections;
for (auto &conn: connections) {
if (conn->instance->name == component && conn->connection.getComponent().pin == pin) {
pinConnections.push_back(conn.get());
}
auto dirConn = dynamic_cast<DirectConnectionInstance *>(conn.get());
if (dirConn != nullptr) {
if (dirConn->secondInstance->name == component && conn->connection.getSecondComponent()->pin == pin) {
pinConnections.push_back(conn.get());
}
}
}
return pinConnections;
}
BusInstance *Schema::getBusInstance(string &name) {
for (auto &instance: busInstances) {
if (instance->name == name) {
return instance.get();
}
}
return nullptr;
}
ComponentInstance *Schema::getComponentInstance(string &name) {
for (auto &instance: componentInstances) {
if (instance->name == name) {
return instance.get();
}
}
return nullptr;
}
std::vector<ConnectionEntry> Schema::availableConnections(std::string instanceName, std::string pinName, bool onlyConnectable) {
std::vector<ConnectionEntry> entries;
auto instance = getComponentInstance(instanceName);
ConnectionComponent connectionComponent{instance->component.getName(), pinName};
for(auto &conn: library.getConnections()) {
if(conn.isConnecting(connectionComponent)) {
// if bus connection
if(library.getBus(conn.getBus()).getType() == Bus::REGULAR) {
for(auto& bus: busInstances) {
if(bus->bus.getName() == conn.getBus()) {
ConnectionEntry entry{ConnectionEntry::BUS, bus.get(), nullopt, nullopt, conn};
entries.emplace_back(entry);
}
}
} else {
for(auto& component: componentInstances) {
for(auto& pin: component->component.getPins()) {
if(conn.isConnecting(connectionComponent, {component->component.getName(), pin.getName()})) {
ConnectionEntry entry{ConnectionEntry::COMPONENT, nullopt, component.get(), pin, conn};
entries.emplace_back(entry);
}
}
}
}
}
}
if(onlyConnectable) {
entries.erase(
std::remove_if(
entries.begin(),
entries.end(),
[this, instance](ConnectionEntry &entry) {
auto& bus = this->library.getBus(entry.connection.getBus());
// we allow duplicates of single automatic connections
if(bus.getType() == Bus::SINGLE_AUTOMATIC) {
return false;
}
for(auto& conn: this->connections) {
if(conn->connection == entry.connection) {
if(bus.getType() == Bus::REGULAR) {
if(entry.busInstance.value()->bus.getName() == conn->connection.getBus()) {
return true;
}
} else {
auto *directInstance = dynamic_cast<DirectConnectionInstance*>(conn.get());
ComponentInstance* secondInstance;
if(directInstance->instance == instance) {
secondInstance = directInstance->secondInstance;
} else {
secondInstance = directInstance->instance;
}
return entry.componentInstance == secondInstance;
}
}
}
return false;
}),
entries.end()
);
}
return entries;
} }
} // namespace domain } // namespace domain
+31 -27
View File
@@ -1,45 +1,49 @@
#ifndef DOMAIN_SCHEMA_H #ifndef DOMAIN_SCHEMA_H
#define DOMAIN_SCHEMA_H #define DOMAIN_SCHEMA_H
#include "connectioninstance.h" #include "connection_instance.h"
#include "instance.h" #include "instance.h"
#include "wireinstance.h" #include "library.h"
#include <utility>
#include <vector> #include <vector>
namespace domain { namespace domain {
class Schema struct ConnectionEntry {
{ enum Type {
BUS,
COMPONENT
};
Type type;
std::optional<BusInstance*> busInstance;
std::optional<ComponentInstance*> componentInstance;
std::optional<Pin> pin;
Connection connection;
};
class Schema {
private:
Library library;
public: public:
Schema(); Schema(Library library): library(std::move(library)) {}
std::vector<Instance*> instances; std::vector<shared_ptr<BusInstance>> busInstances;
std::vector<ConnectionInstance*> connections; std::vector<shared_ptr<ComponentInstance>> componentInstances;
std::vector<WireInstance*> wires;
WireInstance *getWire(std::string name) { std::vector<shared_ptr<ConnectionInstance>> connections;
for(auto wire: wires) {
if (wire->name == name) {
return wire;
}
}
return nullptr;
}
bool hasWire(std::string name) { BusInstance *getBusInstance(std::string &name);
return getWire(name) != NULL; ComponentInstance *getComponentInstance(std::string &name);
} bool hasConnection(string &component, string &pin);
ConnectionInstance *getConnection(string &component, string &pin);
std::vector<ConnectionEntry> availableConnections(std::string instance, std::string pin, bool onlyConnectable);
Instance *getInstance(std::string name) { vector<ConnectionInstance *> getConnections(string &component, string &pin);
for(auto instance: instances) {
if (instance->name == name) {
return instance;
}
}
return nullptr;
}
}; };
} // namespace domain } // namespace domain
File diff suppressed because it is too large. Load diff
+93
View File
@@ -0,0 +1,93 @@
#ifndef DOMAIN_COMDEL_GENERATOR_H
#define DOMAIN_COMDEL_GENERATOR_H
#include "library.h"
#include "schema.h"
#include <set>
#include <utility>
#include <comdel/parser/ast_nodes.h>
#include <comdel/parser/parse_context.h>
#include <comdel/parser/presult.h>
namespace domain {
struct ComdelContext {
std::vector<Attribute> attributes;
std::vector<std::string> wires;
std::string name;
bool inComponent;
bool inConnection;
bool inSingleAutomaticConnection;
bool inBus;
ComdelContext(std::string name, bool inComponent, bool inConnection, bool inSingleAutomaticConnection, bool inBus);
bool doesAttributeExists(std::string name, Value::ValueType type);
bool doesWireExists(std::string name);
};
class SchemaCreator {
std::vector<ComdelContext> context;
std::string name;
std::string libraryInfo;
std::string header;
std::string componentDirectory;
std::optional<std::string> componentHeader = nullopt;
std::vector<AddressSpace> addressSpaces;
std::vector<Component> components;
std::vector<Bus> buses;
std::vector<Connection> connections;
std::map<std::string, std::string> messages;
std::vector<SourceError> errors;
std::vector<FunctionValidator *> validators;
std::optional<AddressSpace> loadAddressSpace(AddressSpaceNode node);
std::optional<Component> loadComponent(ComponentNode node);
std::optional<Attribute> loadAttribute(AttributeNode node);
std::optional<Rule> loadRule(RuleNode node);
std::optional<Condition> loadCondition(ConditionNode node);
std::optional<Popup> loadPopup(PopupNode node, std::string name, Value::ValueType type);
std::optional<Display> loadDisplay(DisplayNode node);
std::optional<Wire> loadWire(WireNode node);
std::optional<Pin> loadPin(PinNode pins);
PinConnection loadPinConnection(PinConnectionNode node);
std::optional<Connection> loadConnection(ConnectionNode node);
std::optional<Bus> loadBus(BusNode node);
std::shared_ptr<ComponentInstance> loadComponentInstance(InstanceNode instance, Library &library);
std::shared_ptr<BusInstance> loadBusInstance(InstanceNode instance, Library &library);
std::optional<Bus> getBus(std::string name);
std::optional<Pin> getComponentPin(std::string name, std::string pin);
bool hasAddressSpace(std::string name);
void push(ComdelContext context);
void pushAdditional(std::string name);
ComdelContext &current();
void pop();
std::optional<Attribute> createMemoryAttribute();
vector<Enumeration> createWireEnumeration(vector<Value> enumeration);
std::optional<Popup> createMemoryPopup();
public:
explicit SchemaCreator(std::vector<FunctionValidator *> validators);
std::vector<SourceError> getErrors();
std::optional<Library> loadLibrary(LibraryNode node);
Schema *loadSchema(SchemaNode node, Library &library);
};
} // namespace domain
#endif // DOMAIN_COMDEL_GENERATOR_H
-902
View File
@@ -1,902 +0,0 @@
#include "schemacreator.h"
#include<set>
namespace domain {
Component::ComponentType toType(ComponentNode::ComponentType type) {
if(type == ComponentNode::MEMORY) {
return Component::MEMORY;
} else if(type == ComponentNode::PROCESSOR) {
return Component::PROCESSOR;
}
return Component::OTHER;
}
Action::ActionType toType(ActionNode::ActionType type) {
if(type == ActionNode::ERROR) {
return Action::ERROR;
}
return Action::WARNING;
}
Wire::WireType toType(WireNode::WireType type) {
switch (type) {
case WireNode::R_WIRE:
return Wire::R_WIRE;
case WireNode::WIRE:
return Wire::WIRE_DEFAULT;
case WireNode::WIRED_AND:
return Wire::WIRED_AND;
case WireNode::WIRED_OR:
return Wire::WIRED_OR;
default:
return Wire::WIRE_DEFAULT;
}
}
Value::ValueType toType(ValueNode::ValueType type) {
switch (type) {
case ValueNode::BOOL:
return Value::BOOL;
case ValueNode::WIRE:
return Value::WIRE_REFERENCE;
case ValueNode::STRING:
return Value::STRING;
case ValueNode::INT:
return Value::INT;
default:
return Value::UNDEFINED;
}
}
Value toType(ValueNode node) {
if(node.getType() == ValueNode::BOOL) {
return Value::fromBool(node.asBool());
} else if(node.getType() == ValueNode::INT) {
return Value::fromInt(node.asInt());
} else if(node.getType() == ValueNode::STRING) {
return Value::fromString(node.asString());
} else if(node.getType() == ValueNode::NIL) {
return Value::fromNull();
}
return Value::fromReference(node.asIdentifier(), Value::UNDEFINED);
}
Bus::BusType toType(BusNode::BusType type) {
if(type == BusNode::AUTOMATIC) {
return Bus::AUTOMATIC;
}
return Bus::REGULAR;
}
Pin::PinType toType(PinNode::PinType type) {
if(type == PinNode::IN) {
return Pin::IN;
} else if(type == PinNode::OUT) {
return Pin::OUT;
}
return Pin::IN_OUT;
}
PinConnection::ConnectionType toType(PinConnectionNode::ConnectionType type)
{
if(type == PinConnectionNode::AUTOMATICALLY) {
return PinConnection::AUTOMATICALLY;
}
return PinConnection::CHECK_ONLY;
}
Popup::PopupType toType(PopupNode::PopupType type)
{
if(type == PopupNode::AUTOMATIC) {
return Popup::AUTOMATIC;
}
return Popup::ON_DEMAND;
}
SchemaCreator::SchemaCreator(std::vector<FunctionSignature> signatures)
: signatures(signatures)
{}
std::optional<Library> SchemaCreator::loadLibrary(LibraryNode node)
{
// library fields
if(!node.name) {
errors.push_back(SourceError{node.span, "missing @name"});
return nullopt;
} else {
name = node.name->asString();
}
if(!node.componentDirectory) {
errors.push_back(SourceError{node.span, "missing @componentDirectory"});
return nullopt;
} else {
componentDirectory = node.componentDirectory->asString();
}
header = node.header ? node.header->asString() : "";
libraryInfo = node.libraryInfo ? node.libraryInfo->asString() : "";
for(auto& as: node.addressSpaces) {
addressSpaces.push_back(*loadAddressSpace(as));
}
for(auto& comp: node.components) {
std::optional<Component> component;
component = loadComponent(comp);
if(component) {
components.push_back(*component);
}
}
for(uint i=0; i<node.buses.size(); i++) {
auto bus = loadBus(node.buses[i]);
if(bus) {
buses.push_back(*bus);
}
}
for(uint i=0; i<node.connections.size(); i++) {
auto conn = loadConnection(node.connections[i]);
if(conn) {
connections.push_back(*conn);
}
}
for(uint i=0; i<node.messages.size(); i++) {
if(!node.messages[i].value.is(ValueNode::STRING)) {
errors.push_back(SourceError{node.messages[i].value.span, "expected `string`"});
} else {
messages[node.messages[i].key.value] = node.messages[i].value.asString();
}
}
return Library(name, libraryInfo, header, componentDirectory, addressSpaces, components, buses, connections, messages);
}
std::optional<Bus> SchemaCreator::loadBus(BusNode node)
{
std::string name = node.name.value;
auto count = std::make_pair<int, int>(1, 1);
if(node.count) {
count = std::make_pair<int, int>(node.count->first.value, node.count->second.value);
}
if(count.first > count.second || count.first < 0) {
errors.push_back(SourceError{node.count->span, "invalid @size"});
return nullopt;
}
auto type = toType(node.type.value);
if(!node.tooltip && type == Bus::REGULAR) {
errors.push_back(SourceError{node.span, "missing @tooltip"});
return nullopt;
}
std::string tooltip = node.tooltip->asString();
if(!node.display && type == Bus::REGULAR) {
errors.push_back(SourceError{node.span, "missing @display"});
return nullopt;
}
if(node.display && (type == Bus::AUTOMATIC || type == Bus::AUTOMATIC_SINGLE)) {
errors.push_back(SourceError{node.span, "automatic bus cannot have a @display"});
return nullopt;
}
optional<Display> display;
if(type == Bus::REGULAR) {
display = loadDisplay(*node.display);
if(!display) {
return nullopt;
}
}
if(node.wires.size() == 0) {
errors.push_back(SourceError{node.span, "missing @wires"});
return nullopt;
}
std::vector<Wire> wires;
for(auto& _wire: node.wires) {
auto wire = loadWire(_wire);
if(wire) {
wires.push_back(*wire);
}
}
return Bus(name, tooltip, type, count, wires, display);
}
std::optional<AddressSpace> SchemaCreator::loadAddressSpace(AddressSpaceNode node)
{
return AddressSpace(node.name.value, node.start.value, node.end.value);
}
std::optional<Connection> SchemaCreator::loadConnection(ConnectionNode node)
{
push(ComdelContext("connection", false, true, false));
std::string bus = node.bus.value;
auto busInstance = getBus(bus);
if(!busInstance) {
errors.emplace_back(node.span, "bus does not exist");
}
if(busInstance->getType() == Bus::REGULAR) {
ConnectionComponent first{node.first.component.value, node.first.pin.value};
auto componentInstance = getComponentPin(first.component, first.pin);
if(!componentInstance) {
errors.emplace_back(node.span, "pin does not exist");
}
if(node.second.has_value()) {
errors.emplace_back(node.span, "regular bus doesn't allow direct connections");
}
std::set<std::string> wireNames;
for(auto &wire: busInstance->getWires()) {
wireNames.insert(wire.getName());
current().wires.push_back(wire.getName());
}
std::vector<Attribute> attributes;
for(auto & attribute : node.attributes) {
auto attr = loadAttribute(attribute);
if(!attr) {
return nullopt;
}
attributes.push_back(*attr);
}
std::set<std::string> attributeNames;
for(auto attribute: attributes) {
attributeNames.insert(attribute.getName());
}
std::vector<Value> wires;
for(auto & firstWire : node.firstWires) {
if(firstWire.is(ValueNode::NIL)) {
wires.push_back(Value::fromNull());
} else if(firstWire.is(ValueNode::INT)) {
wires.push_back(Value::fromInt(firstWire.asInt()));
} else if(firstWire.is(ValueNode::IDENTIFIER)) {
if(attributeNames.count(firstWire.asIdentifier())) {
wires.push_back(Value::fromReference(firstWire.asIdentifier(), Value::ATTRIBUTE_REFERENCE));
} else if(wireNames.count(firstWire.asIdentifier())) {
wires.push_back(Value::fromReference(firstWire.asIdentifier(), Value::WIRE_REFERENCE));
} else {
errors.emplace_back(firstWire.span, "unknown identifier");
}
} else {
errors.emplace_back(firstWire.span, "unknown value type");
}
}
pop();
return Connection(first, nullopt, bus, attributes, wires, nullopt);
} else if(busInstance->getType() == Bus::AUTOMATIC || busInstance->getType() == Bus::AUTOMATIC_SINGLE) {
ConnectionComponent first{node.first.component.value, node.first.pin.value};
if(!node.second.has_value()) {
errors.emplace_back(node.span, "missing second component");
}
ConnectionComponent second{node.second->component.value, node.second->pin.value};
auto firstComponentInstance = getComponentPin(first.component, first.pin);
if(!firstComponentInstance) {
errors.emplace_back(node.span, "pin does not exist");
}
auto secondComponentInstance = getComponentPin(second.component, second.pin);
if(!secondComponentInstance) {
errors.emplace_back(node.span, "pin does not exist");
}
std::set<std::string> wireNames;
for(auto &wire: busInstance->getWires()) {
wireNames.insert(wire.getName());
current().wires.push_back(wire.getName());
}
std::vector<Attribute> attributes;
for(auto & attribute : node.attributes) {
auto attr = loadAttribute(attribute);
if(!attr) {
return nullopt;
}
attributes.push_back(*attr);
}
std::set<std::string> attributeNames;
for(auto attribute: attributes) {
attributeNames.insert(attribute.getName());
}
std::vector<Value> firstWires;
for(auto & firstWire : node.firstWires) {
if(firstWire.is(ValueNode::NIL)) {
firstWires.push_back(Value::fromNull());
} else if(firstWire.is(ValueNode::INT)) {
firstWires.push_back(Value::fromInt(firstWire.asInt()));
} else if(firstWire.is(ValueNode::IDENTIFIER)) {
if(attributeNames.count(firstWire.asIdentifier())) {
firstWires.push_back(Value::fromReference(firstWire.asIdentifier(), Value::ATTRIBUTE_REFERENCE));
} else if(wireNames.count(firstWire.asIdentifier())) {
firstWires.push_back(Value::fromReference(firstWire.asIdentifier(), Value::WIRE_REFERENCE));
} else {
errors.emplace_back(firstWire.span, "unknown identifier");
}
} else {
errors.emplace_back(firstWire.span, "unknown value type");
}
}
std::vector<Value> secondWires;
for(auto & secondWire : *node.secondWires) {
if(secondWire.is(ValueNode::NIL)) {
secondWires.push_back(Value::fromNull());
} else if(secondWire.is(ValueNode::INT)) {
secondWires.push_back(Value::fromInt(secondWire.asInt()));
} else if(secondWire.is(ValueNode::IDENTIFIER)) {
if(attributeNames.count(secondWire.asIdentifier())) {
secondWires.push_back(Value::fromReference(secondWire.asIdentifier(), Value::ATTRIBUTE_REFERENCE));
} else if(wireNames.count(secondWire.asIdentifier())) {
secondWires.push_back(Value::fromReference(secondWire.asIdentifier(), Value::WIRE_REFERENCE));
} else {
errors.emplace_back(secondWire.span, "unknown identifier");
}
} else {
errors.emplace_back(secondWire.span, "unknown value type");
}
}
pop();
return Connection(first, second, bus, attributes, firstWires, secondWires);
}
pop();
errors.emplace_back(node.span, "unsupported connection type");
return std::nullopt;
}
std::optional<Component> SchemaCreator::loadComponent(ComponentNode node)
{
push(ComdelContext(node.name.value, true, false, false));
std::string name = node.name.value;
if(!node.tooltip) {
errors.push_back(SourceError{node.span, "missing @tooltip"});
pop();
return nullopt;
}
std::string tooltip = node.tooltip->asString();
if(!node.source) {
errors.push_back(SourceError{node.span, "missing @source"});
pop();
return nullopt;
}
std::string source = node.source->asString();
Component::ComponentType type = toType(node.type.value);
std::vector<Attribute> attributes;
for(auto& a: node.attributes) {
std::optional<Attribute> attribute = loadAttribute(a);
if(attribute) {
attributes.push_back(*attribute);
}
}
context[context.size() -1 ].attributes = attributes;
std::vector<Rule> rules;
for(auto& r: node.rules) {
std::optional<Rule> rule = loadRule(r);
if(rule) {
rules.push_back(*rule);
}
}
if(!node.instanceName) {
errors.push_back(SourceError{node.span, "missing @instanceName"});
pop();
return nullopt;
}
std::string instanceName = node.instanceName->asString();
auto count = std::make_pair<int, int>(1, 1);
if(node.count) {
count = std::make_pair<int, int>(node.count->first.value, node.count->second.value);
}
if(count.first > count.second || count.first < 0) {
errors.push_back(SourceError{node.count->first.span, "invalid @size"});
pop();
return nullopt;
}
if(!node.display) {
errors.push_back(SourceError{node.span, "missing @display"});
pop();
return nullopt;
}
optional<Display> display = loadDisplay(*node.display);
if(!display) {
pop();
return nullopt;
}
std::vector<Pin> pins;
for(uint i=0; i<node.pins.size(); i++) {
auto pin = loadPin(node.pins[i]);
if(!pin) {
pop();
return nullopt;
}
pins.push_back(*pin);
}
pop();
return Component(name, tooltip, source, type, rules, instanceName, count, *display, pins, attributes);
}
std::optional<Wire> SchemaCreator::loadWire(WireNode node) {
return Wire(
node.name.value,
toType(node.type.value),
node.size.value,
node.hidden,
node.hasTerminateWith,
toType(node.terminateWith)
);
}
optional<Pin> SchemaCreator::loadPin(PinNode node)
{
std::string name = node.name.value;
Pin::PinType type = toType(node.type.value);
if(!node.tooltip) {
errors.push_back(SourceError{node.span, "missing @tooltip"});
return nullopt;
}
std::string tooltip = node.tooltip->asString();
if(!node.display) {
errors.push_back(SourceError{node.span, "missing @display"});
return nullopt;
}
std::vector<Attribute> attributes;
optional<Display> display = loadDisplay(*node.display);
if(!display) {
return nullopt;
}
if(!node.connection) {
errors.push_back(SourceError{node.span, "missing @connection"});
return nullopt;
}
auto connection = loadPinConnection(*node.connection);
std::optional<std::vector<Value>> wiresOpt = std::nullopt;
if(node.wires.has_value()) {
auto nodeWires = node.wires.value();
std::vector<Value> wires;
for(auto &nodeWire : nodeWires) {
if(nodeWire.is(ValueNode::NIL)) {
wires.push_back(Value::fromNull());
} else if(nodeWire.is(ValueNode::INT)) {
wires.push_back(Value::fromInt(nodeWire.asInt()));
} else {
errors.emplace_back(node.span, "unknown value type");
}
}
wiresOpt = wires;
}
return Pin(name, type, tooltip, connection, *display, wiresOpt);
}
int getIntProperty(DisplayItemNode &node, std::string property) {
for(auto& prop: node.values) {
if(prop.key.value == property) {
return prop.value.asInt();
}
}
throw std::exception();
}
std::optional<Display> SchemaCreator::loadDisplay(DisplayNode node)
{
std::vector<ui::Item> items;
for(auto &item: node.items) {
ui::Item displayItem;
std::string type = item.type.value;
if(type == "rect") {
int x, y, w, h;
x = getIntProperty(item, "x");
y = getIntProperty(item, "y");
w = getIntProperty(item, "w");
h = getIntProperty(item, "h");
displayItem.rect = ui::Rect(x, y, w, h);
} else if(type == "line") {
int x1, y1, x2, y2;
x1 = getIntProperty(item, "x1");
y1 = getIntProperty(item, "y1");
x2 = getIntProperty(item, "x2");
y2 = getIntProperty(item, "y2");
displayItem.line = ui::Line(x1, y1, x2, y2);
} else if(type == "pin") {
int x, y, w, h;
x = getIntProperty(item, "x");
y = getIntProperty(item, "y");
w = getIntProperty(item, "w");
h = getIntProperty(item, "h");
displayItem.pin = ui::Pin(x, y, w, h);
} else {
errors.push_back(SourceError{item.type.span, "unsupported display type"});
}
items.push_back(displayItem);
}
return Display(items);
}
PinConnection SchemaCreator::loadPinConnection(PinConnectionNode node)
{
std::string message = node.message.asString();
PinConnection::ConnectionType type = toType(node.type.value);
return PinConnection(message, type);
}
std::optional<Attribute> SchemaCreator::loadAttribute(AttributeNode node)
{
std::string name = node.name.value;
pushAdditional(name);
Value value;
if(current().inComponent) {
if(node.type == ValueNode::INT) {
value = Value::fromInt(node.defaultValue->asInt());
} else if (node.type == ValueNode::BOOL) {
value = Value::fromBool(node.defaultValue->asBool());
} else if (node.type == ValueNode::STRING) {
value = Value::fromString(node.defaultValue->asString());
} else {
errors.push_back(SourceError{node.name.span, "unsupported type"});
}
}
if(current().inConnection) { // TODO remove identifier
if (node.type == ValueNode::WIRE || node.type == ValueNode::IDENTIFIER) {
if(current().doesWireExists(node.defaultValue->asIdentifier())) {
value = Value::fromReference(node.defaultValue->asIdentifier(), Value::WIRE_REFERENCE);
} else {
value = Value::fromReference("", Value::WIRE_REFERENCE);
errors.push_back(SourceError{node.span, "unknown identifier"});
}
} else {
errors.push_back(SourceError{node.name.span, "unsupported type"});
}
}
current().attributes.push_back(Attribute(name, value));
std::optional<Popup> popup;
if(node.popup) {
popup = loadPopup(*node.popup, name, value.getType());
}
pop();
return Attribute(name, value, popup);
}
std::optional<Popup> SchemaCreator::loadPopup(PopupNode node, std::string name, Value::ValueType type)
{
auto popupType = toType(node.type.value().value);
pushAdditional(name);
current().attributes.clear();
current().attributes.push_back(Attribute(name, Value::ofType(type)));
if(!node.title) {
errors.push_back(SourceError{node.span, "missing @title"});
return nullopt;
}
std::string title = node.title->asString();
if(!node.text) {
errors.push_back(SourceError{node.span, "missing @text"});
return nullopt;
}
std::string text = node.text->asString();
std::vector<Rule> rules;
for(auto& r: node.rules) {
std::optional<Rule> rule = loadRule(r);
if(rule) {
rules.push_back(*rule);
}
}
std::vector<Enumeration> enumeration;
if(node.enumerated) {
for(uint i=0; i<node.enumeration.size(); i++) {
if(type == Value::INT || type == Value::STRING || type == Value::BOOL) {
auto value = toType(node.enumeration[i].value);
if(value.getType() == type) {
enumeration.push_back(Enumeration(node.enumeration[i].key.asString(), value));
} else {
errors.push_back(SourceError{node.enumeration[i].span, "wrong type"});
}
} else if(type == Value::WIRE_REFERENCE) {
auto value = toType(node.enumeration[i].value);
if(value.isType(Value::UNDEFINED)) {
if(current().doesWireExists(value.asReference())) {
value = Value::fromReference(value.asReference(), Value::WIRE_REFERENCE);
} else {
errors.push_back(SourceError{node.enumeration[i].span, "unknown wire"});
}
}
if(value.isType(Value::WIRE_REFERENCE) || value.isType(Value::INT) || value.isType(Value::NIL)) {
enumeration.push_back(Enumeration(node.enumeration[i].key.asString(), value));
} else {
errors.push_back(SourceError{node.enumeration[i].span, "wrong type"});
}
}
}
} else {
if(type == Value::WIRE_REFERENCE && !current().inConnection) {
errors.push_back(SourceError{node.span, "@enumeration is required for attributes of type wire"});
}
}
pop();
return Popup(title, text, popupType, rules, enumeration);
}
std::optional<Rule> SchemaCreator::loadRule(RuleNode node)
{
std::vector<IfStatement> statements;
for(auto& stmt: node.statements) {
auto condition = loadCondition(stmt.condition);
if(condition) {
statements.push_back(IfStatement(*condition, Action(toType(stmt.action.type.value), stmt.action.message.asString())));
} else {
return nullopt;
}
}
return Rule(statements);
}
std::optional<Condition> SchemaCreator::loadCondition(ConditionNode node)
{
std::string function = node.functionName.value;
for(uint i=0; i<signatures.size(); i++) {
if(signatures[i].name == function) {
if(signatures[i].params.size() == node.params.size()) {
std::vector<Value> params;
for(uint j=0; j<signatures[i].params.size(); j++) {
bool exists = false;
auto type = toType(node.params[j]);
if (type.getType() == Value::UNDEFINED) {
if(current().doesAttributeExists(type.asReference(), signatures[i].params[j])) {
exists = true;
type = Value::fromReference(type.asReference(), Value::ATTRIBUTE_REFERENCE);
}
if(signatures[i].params[j] == Value::ADDRESS_SPACE) {
if(hasAddressSpace(type.asReference())) {
exists = true;
type = Value::fromReference(type.asReference(), Value::ADDRESS_SPACE_REFERENCE);
}
}
if(!exists) {
errors.push_back(SourceError{node.functionName.span, "unknown reference " + type.asReference()});
}
}
params.push_back(type);
}
return Condition(function, params, node.negated);
} else {
errors.push_back(SourceError{node.functionName.span, "wrong number of parameters"});
}
}
}
errors.push_back(SourceError{node.functionName.span, "unknown function name"});
return nullopt;
}
Schema* SchemaCreator::loadSchema(SchemaNode node, Library &library)
{
Schema *schema = new Schema();
for(auto &instance: node.instances) {
if(library.hasComponent(instance.component.value)) {
schema->instances.push_back(loadComponentInstance(instance, library));
}
if(library.hasBus(instance.component.value)) {
schema->instances.push_back(loadBusInstance(instance, library));
}
}
for(auto &wire: node.wires) {
auto w = loadWireInstance(wire);
if(w) {
schema->wires.push_back(*w);
}
}
for(auto &conn: node.connections) {
auto firstComponent = dynamic_cast<ComponentInstance*>(schema->getInstance(conn.first.instance.value));
if(firstComponent == NULL) {
errors.push_back(SourceError{conn.first.instance.span, "unknown component"});
continue;
}
if(!firstComponent->component.hasPin(conn.first.pin.value)) {
errors.push_back(SourceError{conn.first.pin.span, "unknown pin"});
continue;
}
ComponentInstance *secondComponent = NULL;
if(conn.second.has_value()) {
secondComponent = dynamic_cast<ComponentInstance*>(schema->getInstance(conn.second->instance.value));
if(secondComponent == NULL) {
errors.push_back(SourceError{conn.second->instance.span, "unknown component"});
continue;
}
if(!secondComponent->component.hasPin(conn.second->pin.value)) {
errors.push_back(SourceError{conn.second->pin.span, "unknown pin"});
continue;
}
}
auto bus = dynamic_cast<BusInstance*>(schema->getInstance(conn.bus.value));
if(bus == NULL) {
errors.push_back(SourceError{conn.bus.span, "unknown bus"});
continue;
}
std::optional<Connection> connection = std::nullopt;
if(secondComponent != NULL) {
ConnectionComponent firstConn{firstComponent->component.getName(), conn.first.pin.value};
ConnectionComponent secondConn{secondComponent->component.getName(), conn.second->pin.value};
if(library.hasConnection(firstConn,
bus->bus.getName(),
secondConn)) {
connection = *library.getConnection(firstConn, bus->bus.getName(), secondConn);
} else {
errors.push_back(SourceError{conn.span, "unknown connection"});
continue;
}
} else {
ConnectionComponent firstConn{firstComponent->component.getName(), conn.first.pin.value};
if(library.hasConnection(firstConn,
bus->bus.getName())) {
connection = *library.getConnection(firstConn, bus->bus.getName());
} else {
errors.push_back(SourceError{conn.span, "unknown connection"});
continue;
}
}
if(!conn.wire) {
errors.push_back(SourceError{conn.span, "missing @wire"});
continue;
}
if(!schema->hasWire(conn.wire->value)) {
errors.push_back(SourceError{conn.wire->span, "unknown wire"});
continue;
}
auto wire = schema->getWire(conn.wire->value);
std::vector<InstanceAttribute*> attributes;
for(auto& attr: conn.attributes) {
if(connection->hasAttribute(attr.name.value)) {
auto attribute = connection->getAttribute(attr.name.value);
auto value = toType(attr.value);
for(auto& en: attribute.getPopup()->getEnumeration()) {
if(en.getValue().asReference() == value.asReference()) {
value = Value::fromReference(value.asReference(), Value::WIRE_REFERENCE);
}
}
if(value.isType(Value::UNDEFINED)) {
errors.push_back(SourceError{attr.span, "invalid value"});
}
attributes.push_back(new InstanceAttribute(attribute.getName(), toType(attr.value), attribute));
} else {
errors.push_back(SourceError(attr.name.span, "unknown attribute"));
}
}
if(secondComponent == NULL) {
schema->connections.push_back(new BusConnectionInstance(firstComponent, attributes, bus, wire, *connection));
} else {
schema->connections.push_back(new DirectConnectionInstance(firstComponent, secondComponent, attributes, bus, wire, *connection));
}
}
return schema;
}
ComponentInstance *SchemaCreator::loadComponentInstance(InstanceNode instance, Library &library) {
auto name = instance.name.value;
auto position = std::make_pair(instance.position->first.value, instance.position->second.value);
auto component = library.getComponent(instance.component.value);
// validate attributes
std::vector<InstanceAttribute*> attributes;
for(auto& attr: instance.attributes) {
if(component.hasAttribute(attr.name.value, toType(attr.value.getType()))) {
auto attribute = component.getAttribute(attr.name.value);
attributes.push_back(new InstanceAttribute(attribute.getName(), toType(attr.value), attribute));
} else {
errors.push_back(SourceError(attr.name.span, "unknown attribute"));
}
}
return new ComponentInstance(name, attributes, position, component);
}
BusInstance *SchemaCreator::loadBusInstance(InstanceNode instance, Library &library) {
auto name = instance.name.value;
auto position = std::make_pair(instance.position->first.value, instance.position->second.value);
auto bus = library.getBus(instance.component.value);
long long size = 0;
if(instance.size) {
size = instance.size->value;
}
return new BusInstance(name, position, bus, size);
}
std::optional<WireInstance*> SchemaCreator::loadWireInstance(WireInstanceNode node) {
if(!node.display) {
errors.push_back(SourceError{node.span, "missing @text"});
return nullopt;
}
auto display = loadDisplay(*node.display);
if(!display) {
return nullopt;
}
std::pair<int, int> position = std::make_pair(0, 0);
if(node.position) {
position = std::make_pair(node.position->first.value, node.position->second.value);
}
return std::optional<WireInstance*>(new WireInstance(node.name.value, *display, position));
}
} // namespace domain
-146
View File
@@ -1,146 +0,0 @@
#ifndef DOMAIN_COMDEL_GENERATOR_H
#define DOMAIN_COMDEL_GENERATOR_H
#include "library.h"
#include "schema.h"
#include <set>
#include <comdel/parser/astnode.h>
#include <comdel/parser/parsecontext.h>
#include <comdel/parser/presult.h>
namespace domain {
struct ComdelContext {
std::vector<Attribute> attributes;
std::vector<std::string> wires;
std::string name;
bool inComponent;
bool inConnection;
bool inBus;
ComdelContext(std::string name, bool inComponent, bool inConnection, bool inBus)
: name(name), inComponent(inComponent), inConnection(inConnection), inBus(inBus)
{}
bool doesAttributeExists(std::string name, Value::ValueType type) {
for(auto &attribute: attributes) {
if(attribute.getDefault().getType() == type && attribute.getName() == name) {
return true;
}
}
return false;
}
bool doesWireExists(std::string name) {
for(auto &w: wires) {
if(w == name) {
return true;
}
}
return false;
}
};
class SchemaCreator
{
std::vector<ComdelContext> context;
std::string name;
std::string libraryInfo;
std::string header;
std::string componentDirectory;
std::vector<AddressSpace> addressSpaces;
std::vector<Component> components;
std::vector<Bus> buses;
std::vector<Connection> connections;
std::map<std::string, std::string> messages;
std::vector<SourceError> errors;
std::vector<FunctionSignature> signatures;
std::optional<AddressSpace> loadAddressSpace(AddressSpaceNode node);
std::optional<Component> loadComponent(ComponentNode node);
std::optional<Attribute> loadAttribute(AttributeNode node);
std::optional<Rule> loadRule(RuleNode node);
std::optional<Condition> loadCondition(ConditionNode node);
std::optional<Popup> loadPopup(PopupNode node, std::string name, Value::ValueType type);
std::optional<Display> loadDisplay(DisplayNode node);
std::optional<Wire> loadWire(WireNode node);
std::optional<Pin> loadPin(PinNode pins);
PinConnection loadPinConnection(PinConnectionNode node);
std::optional<Connection> loadConnection(ConnectionNode node);
std::optional<Bus> loadBus(BusNode node);
ComponentInstance *loadComponentInstance(InstanceNode instance, Library &library);
BusInstance *loadBusInstance(InstanceNode instance, Library &library);
std::optional<WireInstance*> loadWireInstance(WireInstanceNode node);
std::optional<Bus> getBus(std::string name) {
for(auto &bus: buses) {
if(bus.getName() == name) {
return bus;
}
}
return std::nullopt;
}
std::optional<Pin> getComponentPin(std::string name, std::string pin) {
for(auto &c: components) {
if(c.getName() == name) {
for(auto &p: c.getPins()) {
if(p.getName() == pin) {
return p;
}
}
}
}
return nullopt;
}
bool hasAddressSpace(std::string name) {
for(auto &as: addressSpaces) {
if(as.getName() == name) {
return true;
}
}
return false;
}
void push(ComdelContext context) {
this->context.push_back(context);
}
void pushAdditional(std::string name) {
if(this->context.size() > 0) {
this->context.push_back(current());
current().name = name;
} else {
ComdelContext con(name, false, false, false);
push(con);
}
}
ComdelContext &current() {
return this->context[this->context.size() - 1];
}
void pop() {
this->context.pop_back();
}
public:
SchemaCreator(std::vector<FunctionSignature> signatures);
std::vector<SourceError> getErrors() {
return errors;
}
std::optional<Library> loadLibrary(LibraryNode node);
Schema* loadSchema(SchemaNode node, Library &library);
};
} // namespace domain
#endif // DOMAIN_COMDEL_GENERATOR_H
+50 -9
View File
@@ -24,6 +24,12 @@ std::string Value::string() {
return "AddressSpace::" + reference; return "AddressSpace::" + reference;
case ATTRIBUTE_REFERENCE: case ATTRIBUTE_REFERENCE:
return "Attribute::" + reference; return "Attribute::" + reference;
case MEMORY_REFERENCE:
if (memoryReference.has_value()) {
return "Memory::" + *memoryReference;
} else {
return "Memory::null";
}
default: default:
return "unknown"; return "unknown";
} }
@@ -33,8 +39,9 @@ std::string Value::string() {
Value::ValueType Value::getType() { Value::ValueType Value::getType() {
return type; return type;
} }
bool Value::isType(Value::ValueType type) {
return this->type == type; bool Value::isType(Value::ValueType _type) {
return type == _type;
} }
long long Value::asInt() { long long Value::asInt() {
@@ -43,49 +50,66 @@ long long Value::asInt() {
} }
throw std::exception(); throw std::exception();
} }
std::string Value::asString() { std::string Value::asString() {
if (isType(Value::STRING)) { if (isType(Value::STRING)) {
return stringValue; return stringValue;
} }
throw std::exception(); throw std::exception();
} }
bool Value::asBool() { bool Value::asBool() {
if (isType(Value::BOOL)) { if (isType(Value::BOOL)) {
return boolValue; return boolValue;
} }
throw std::exception(); throw std::exception();
} }
AddressSpace Value::asAddressSpace() { AddressSpace Value::asAddressSpace() {
if (isType(Value::ADDRESS_SPACE)) { if (isType(Value::ADDRESS_SPACE)) {
return *addressSpace; return *addressSpace;
} }
throw std::exception(); throw std::exception();
} }
std::string Value::asReference() { std::string Value::asReference() {
if(isType(Value::WIRE_REFERENCE) || isType(Value::ADDRESS_SPACE_REFERENCE) || isType(Value::ATTRIBUTE_REFERENCE) || isType(Value::UNDEFINED)) { if (isType(Value::WIRE_REFERENCE) || isType(Value::ADDRESS_SPACE_REFERENCE) ||
isType(Value::ATTRIBUTE_REFERENCE) || isType(Value::UNDEFINED)) {
return reference; return reference;
} }
throw std::exception(); throw std::exception();
} }
std::optional<std::string> Value::asMemoryReference() {
return memoryReference;
}
domain::ComponentInstance *Value::asMemory() {
return memory;
}
void Value::setInt(long long value) { void Value::setInt(long long value) {
if (isType(Value::INT)) { if (isType(Value::INT)) {
this->intValue = value; this->intValue = value;
} }
throw std::exception(); throw std::exception();
} }
void Value::setString(std::string value) { void Value::setString(std::string value) {
if (isType(Value::STRING)) { if (isType(Value::STRING)) {
this->stringValue = value; this->stringValue = value;
} }
throw std::exception(); throw std::exception();
} }
void Value::setBool(bool value) { void Value::setBool(bool value) {
if (isType(Value::BOOL)) { if (isType(Value::BOOL)) {
this->boolValue = value; this->boolValue = value;
} }
throw std::exception(); throw std::exception();
} }
void Value::setReference(std::string value) { void Value::setReference(std::string value) {
if (isType(Value::WIRE_REFERENCE)) { if (isType(Value::WIRE_REFERENCE)) {
this->reference = value; this->reference = value;
@@ -100,36 +124,34 @@ Value Value::fromInt(long long value) {
val.intValue = value; val.intValue = value;
return val; return val;
} }
Value Value::fromString(std::string value) { Value Value::fromString(std::string value) {
Value val; Value val;
val.type = Value::STRING; val.type = Value::STRING;
val.stringValue = value; val.stringValue = value;
return val; return val;
} }
Value Value::fromBool(bool value) { Value Value::fromBool(bool value) {
Value val; Value val;
val.type = Value::BOOL; val.type = Value::BOOL;
val.boolValue = value; val.boolValue = value;
return val; return val;
} }
Value Value::fromAddressSpace(AddressSpace addressSpace) { Value Value::fromAddressSpace(AddressSpace addressSpace) {
Value val; Value val;
val.type = Value::ADDRESS_SPACE; val.type = Value::ADDRESS_SPACE;
val.addressSpace = addressSpace; val.addressSpace = addressSpace;
return val; return val;
} }
Value Value::fromReference(std::string value, Value::ValueType type) { Value Value::fromReference(std::string value, Value::ValueType type) {
Value val; Value val;
val.type = type; val.type = type;
val.reference = value; val.reference = value;
return val; return val;
} }
Value Value::ofType(Value::ValueType type) {
Value val;
val.type = type;
return val;
}
Value Value::fromNull() { Value Value::fromNull() {
Value val; Value val;
@@ -137,6 +159,13 @@ Value Value::fromNull() {
return val; return val;
} }
Value Value::fromMemoryReference(std::optional<std::string> value) {
Value val;
val.type = MEMORY_REFERENCE;
val.memoryReference = value;
return val;
}
std::string Value::stringify() { std::string Value::stringify() {
switch (type) { switch (type) {
case INT: case INT:
@@ -151,9 +180,21 @@ std::string Value::stringify() {
case ADDRESS_SPACE_REFERENCE: case ADDRESS_SPACE_REFERENCE:
case ATTRIBUTE_REFERENCE: case ATTRIBUTE_REFERENCE:
return reference; return reference;
case MEMORY_REFERENCE:
if (memoryReference.has_value()) {
return memoryReference.value();
} else {
return "null";
}
default: default:
throw std::exception(); throw std::exception();
} }
} }
Value Value::ofType(Value::ValueType type) {
Value val;
val.type = type;
return val;
}
} // namespace domain } // namespace domain
+60 -9
View File
@@ -1,7 +1,7 @@
#ifndef DOMAIN_VALUE_H #ifndef DOMAIN_VALUE_H
#define DOMAIN_VALUE_H #define DOMAIN_VALUE_H
#include "addressspace.h" #include "address_space.h"
#include <string> #include <string>
#include <optional> #include <optional>
@@ -9,8 +9,9 @@
namespace domain { namespace domain {
class Value class ComponentInstance;
{
class Value {
public: public:
enum ValueType { enum ValueType {
INT, INT,
@@ -19,50 +20,100 @@ public:
ADDRESS_SPACE, ADDRESS_SPACE,
ADDRESS_SPACE_REFERENCE, ADDRESS_SPACE_REFERENCE,
ATTRIBUTE_REFERENCE, ATTRIBUTE_REFERENCE,
MEMORY_REFERENCE,
MEMORY,
WIRE_REFERENCE, WIRE_REFERENCE,
NIL, NIL,
UNDEFINED, UNDEFINED,
}; };
private: private:
long long intValue; long long intValue = 0;
std::string stringValue; std::string stringValue;
bool boolValue; bool boolValue = false;
std::optional<AddressSpace> addressSpace; std::optional<AddressSpace> addressSpace = std::nullopt;
std::string reference; std::string reference;
domain::ComponentInstance *memory = nullptr;
ValueType type; std::optional<std::string> memoryReference = std::nullopt;
ValueType type = UNDEFINED;
public: public:
Value() { Value() = default;
this->type = UNDEFINED;
bool equals(Value value) {
if (value.getType() == type) {
switch (type) {
case INT:
return value.asInt() == intValue;
case STRING:
return value.asString() == stringValue;
case NIL:
case UNDEFINED:
return true;
case WIRE_REFERENCE:
case ATTRIBUTE_REFERENCE:
case ADDRESS_SPACE_REFERENCE:
return value.asReference() == reference;
case MEMORY_REFERENCE:
return value.asMemoryReference() == memoryReference;
case MEMORY:
return value.asMemory() == memory;
case BOOL:
return value.asBool() == boolValue;
default:
return false;
}
}
return false;
} }
std::string string(); std::string string();
ValueType getType(); ValueType getType();
bool isType(ValueType type); bool isType(ValueType type);
long long asInt(); long long asInt();
std::string asString(); std::string asString();
bool asBool(); bool asBool();
std::string asReference(); std::string asReference();
std::optional<std::string> asMemoryReference();
domain::ComponentInstance *asMemory();
AddressSpace asAddressSpace(); AddressSpace asAddressSpace();
void setInt(long long intValue); void setInt(long long intValue);
void setString(std::string value); void setString(std::string value);
void setBool(bool value); void setBool(bool value);
void setReference(std::string value); void setReference(std::string value);
std::string stringify(); std::string stringify();
static Value fromInt(long long value); static Value fromInt(long long value);
static Value fromString(std::string value); static Value fromString(std::string value);
static Value fromBool(bool value); static Value fromBool(bool value);
static Value fromNull(); static Value fromNull();
static Value fromAddressSpace(AddressSpace addressSpace); static Value fromAddressSpace(AddressSpace addressSpace);
static Value fromReference(std::string value, ValueType type); static Value fromReference(std::string value, ValueType type);
static Value fromMemoryReference(std::optional<std::string> memoryReference);
static Value ofType(ValueType type); static Value ofType(ValueType type);
}; };
-10
View File
@@ -1,10 +0,0 @@
#include "wireinstance.h"
namespace domain {
WireInstance::WireInstance(std::string name, Display display, std::pair<int, int> position)
: name(name), display(display), position(position)
{}
} // namespace domain
-24
View File
@@ -1,24 +0,0 @@
#ifndef DOMAIN_WIRE_INSTANCE_H
#define DOMAIN_WIRE_INSTANCE_H
#include "display.h"
#include <string>
namespace domain {
class WireInstance
{
public:
std::string name;
Display display;
std::pair<int, int> position;
WireInstance(std::string name, Display display, std::pair<int, int> position);
};
} // namespace domain
#endif // DOMAIN_WIREINSTANCE_H
@@ -1,4 +1,4 @@
#include "astnode.h" #include "ast_nodes.h"
/*************************** AST NODE ********************************/ /*************************** AST NODE ********************************/
@@ -9,14 +9,27 @@ AstNode::~AstNode() = default;
NumberNode::NumberNode(const std::string &expression) { NumberNode::NumberNode(const std::string &expression) {
if (expression.size() > 2) { if (expression.size() > 2) {
if (expression.substr(0, 2) == "0x") { if (expression.substr(0, 2) == "0x") {
this->value = std::stoll(expression, 0, 16); this->value = std::stoll(expression, nullptr, 16);
} else if (expression.substr(0, 2) == "0b") { } else if (expression.substr(0, 2) == "0b") {
this->value = std::stoll(expression, 0, 2); this->value = std::stoll(expression, nullptr, 2);
} else { } else {
this->value = std::stoll(expression, 0, 10); this->value = std::stoll(expression, nullptr, 10);
} }
} else { } else {
this->value = std::stoll(expression, 0, 10); this->value = std::stoll(expression, nullptr, 10);
}
}
/*************************** COLOR NODE *********************************/
ColorNode::ColorNode(const std::string &expression) {
auto value = expression.substr(1);
color.r = std::stoul(value.substr(0, 2), nullptr, 16);
color.g = std::stoul(value.substr(2, 4), nullptr, 16);
color.b = std::stoul(value.substr(4, 6), nullptr, 16);
color.a = 255;
if(value.length() == 8) {
color.a = std::stoul(value.substr(6,8), nullptr, 16);
} }
} }
@@ -49,6 +62,14 @@ std::string ValueNode::asIdentifier() {
return ""; return "";
} }
Color ValueNode::asColor() {
if (is(COLOR)) {
return colorValue.value();
}
return {};
}
bool ValueNode::asBool() { bool ValueNode::asBool() {
if (is(BOOL)) { if (is(BOOL)) {
return boolValue.value(); return boolValue.value();
@@ -94,9 +115,23 @@ ValueNode ValueNode::ofNull() {
return value; return value;
} }
ValueNode ValueNode::ofWire(std::string _value) { ValueNode ValueNode::ofColor(Color color) {
ValueNode value; ValueNode value;
value.type = EnumNode(COLOR);
value.colorValue = color;
return value;
}
ValueNode ValueNode::ofWire(std::optional<std::string> _value) {
ValueNode value;
value.type = EnumNode(WIRE); value.type = EnumNode(WIRE);
value.identifierValue = std::optional<std::string>(_value); value.identifierValue = _value;
return value;
}
ValueNode ValueNode::ofMemory(std::optional<std::string> _value) {
ValueNode value;
value.type = EnumNode(MEMORY);
value.identifierValue = _value;
return value; return value;
} }
@@ -2,7 +2,10 @@
#define AST_NODE_H #define AST_NODE_H
#include "token.h" #include "token.h"
#include "color.h"
#include "source_error.h"
#include <optional> #include <optional>
#include <utility>
#include <vector> #include <vector>
/** /**
@@ -19,57 +22,67 @@ public:
virtual ~AstNode(); virtual ~AstNode();
AstNode(AstNode &&) = default; AstNode(AstNode &&) = default;
AstNode &operator=(AstNode &&) = default; AstNode &operator=(AstNode &&) = default;
AstNode(const AstNode &) = default; AstNode(const AstNode &) = default;
AstNode &operator=(const AstNode &) = default; AstNode &operator=(const AstNode &) = default;
}; };
template<typename T> template<typename T>
struct EnumNode: public AstNode struct EnumNode : public AstNode {
{
EnumNode() = default; EnumNode() = default;
explicit EnumNode(T value) : value(value) {} explicit EnumNode(T value) : value(value) {}
T value; T value;
}; };
struct StringNode: public AstNode struct StringNode : public AstNode {
{
std::string value; std::string value;
std::string asString(); std::string asString();
}; };
struct IdentifierNode: public AstNode struct IdentifierNode : public AstNode {
{
std::string value; std::string value;
}; };
struct NumberNode : public AstNode { struct NumberNode : public AstNode {
long long int value; long long int value;
explicit NumberNode(const std::string &expression); explicit NumberNode(const std::string &expression);
NumberNode() : value(0) {} NumberNode() : value(0) {}
}; };
struct ColorNode : public AstNode {
Color color;
struct CountNode: public AstNode explicit ColorNode(const std::string &expression);
{
ColorNode() {}
};
struct CountNode : public AstNode {
NumberNode first; NumberNode first;
NumberNode second; NumberNode second;
CountNode(NumberNode first, NumberNode second): first(first), second(second) {} CountNode(NumberNode first, NumberNode second) : first(std::move(first)), second(std::move(second)) {}
CountNode() = default; CountNode() = default;
}; };
struct AddressSpaceNode: public AstNode struct AddressSpaceNode : public AstNode {
{
IdentifierNode name; IdentifierNode name;
NumberNode start; NumberNode start;
NumberNode end; NumberNode end;
}; };
class ValueNode: public AstNode class ValueNode : public AstNode {
{
public: public:
enum ValueType { enum ValueType {
INT, INT,
@@ -77,7 +90,9 @@ public:
BOOL, BOOL,
WIRE, WIRE,
IDENTIFIER, IDENTIFIER,
MEMORY,
NIL, NIL,
COLOR,
}; };
private: private:
@@ -86,30 +101,46 @@ private:
std::optional<std::string> stringValue; std::optional<std::string> stringValue;
std::optional<bool> boolValue; std::optional<bool> boolValue;
std::optional<std::string> identifierValue; std::optional<std::string> identifierValue;
std::optional<Color> colorValue;
public: public:
ValueNode() = default;; ValueNode() = default;
ValueType getType() { ValueType getType() const {
return type.value; return type.value;
} }
long long asInt(); long long asInt();
std::string asString(); std::string asString();
std::string asIdentifier(); std::string asIdentifier();
Color asColor();
bool asBool(); bool asBool();
bool is(ValueType valueType); bool is(ValueType valueType);
static ValueNode ofBool(bool _value); static ValueNode ofBool(bool _value);
static ValueNode ofInt(long long _value); static ValueNode ofInt(long long _value);
static ValueNode ofString(std::string _value); static ValueNode ofString(std::string _value);
static ValueNode ofIdentifier(std::string _value); static ValueNode ofIdentifier(std::string _value);
static ValueNode ofMemory(std::optional<std::string> _value);
static ValueNode ofNull(); static ValueNode ofNull();
static ValueNode ofWire(std::string _value);
static ValueNode ofColor(Color color);
static ValueNode ofWire(std::optional<std::string> _value);
}; };
struct ConditionNode struct ConditionNode {
{
bool negated; bool negated;
IdentifierNode functionName; IdentifierNode functionName;
std::vector<ValueNode> params; std::vector<ValueNode> params;
@@ -127,25 +158,21 @@ public:
}; };
struct IfStatementNode: public AstNode struct IfStatementNode : public AstNode {
{
ConditionNode condition; ConditionNode condition;
ActionNode action; ActionNode action;
}; };
struct RuleNode: public AstNode struct RuleNode : public AstNode {
{
std::vector<IfStatementNode> statements; std::vector<IfStatementNode> statements;
}; };
struct EnumerationNode: public AstNode struct EnumerationNode : public AstNode {
{
StringNode key; StringNode key;
ValueNode value; ValueNode value;
}; };
struct PopupNode: public AstNode struct PopupNode : public AstNode {
{
enum PopupType { enum PopupType {
AUTOMATIC, AUTOMATIC,
ON_DEMAND ON_DEMAND
@@ -161,36 +188,92 @@ struct PopupNode: public AstNode
std::vector<RuleNode> rules; std::vector<RuleNode> rules;
}; };
struct PropertyNode: public AstNode struct PropertyNode : public AstNode {
{
IdentifierNode key; IdentifierNode key;
ValueNode value; ValueNode value;
}; };
struct DisplayItemNode: public AstNode struct DisplayItemNode : public AstNode {
{
IdentifierNode type; IdentifierNode type;
std::vector<PropertyNode> values; std::vector<PropertyNode> values;
long long int asInt(std::vector<SourceError> *errors, const std::string &property, long long int _default = 0) {
for (auto &prop: values) {
if (prop.key.value == property) {
if (prop.value.is(ValueNode::INT)) {
return prop.value.asInt();
} else {
if (errors != nullptr) {
errors->emplace_back(prop.value.span, "expected number");
}
}
}
}
return _default;
}
Color asColor(std::vector<SourceError> *errors, const std::string &property, Color _default = Color(0, 0, 0)) {
for (auto &prop: values) {
if (prop.key.value == property) {
if (prop.value.is(ValueNode::COLOR)) {
return prop.value.asColor();
} else {
if (errors != nullptr) {
errors->emplace_back(prop.value.span, "expected number");
}
}
}
}
return _default;
}
std::string asIdentifier(std::vector<SourceError> *errors, const std::string &property, std::string _default = "") {
for (auto &prop: values) {
if (prop.key.value == property) {
if (prop.value.is(ValueNode::IDENTIFIER)) {
return prop.value.asIdentifier();
} else {
if (errors != nullptr) {
errors->emplace_back(prop.value.span, "expected identifier");
}
}
}
}
return _default;
}
std::string asString(std::vector<SourceError> *errors, const std::string &property, std::string _default = "") {
for (auto &prop: values) {
if (prop.key.value == property) {
if (prop.value.is(ValueNode::STRING)) {
return prop.value.asString();
} else {
if (errors != nullptr) {
errors->emplace_back(prop.value.span, "expected string");
}
}
}
}
return _default;
}
}; };
struct DisplayNode: public AstNode struct DisplayNode : public AstNode {
{
std::vector<DisplayItemNode> items; std::vector<DisplayItemNode> items;
}; };
struct PinConnectionNode: public AstNode struct PinConnectionNode : public AstNode {
{
enum ConnectionType { enum ConnectionType {
CHECK_ONLY, REQUIRED,
AUTOMATICALLY OPTIONAL
}; };
StringNode message; StringNode message;
EnumNode<ConnectionType> type; EnumNode<ConnectionType> type;
}; };
struct PinNode: public AstNode struct PinNode : public AstNode {
{
enum PinType { enum PinType {
IN_OUT, IN_OUT,
IN, IN,
@@ -205,8 +288,7 @@ struct PinNode: public AstNode
std::optional<std::vector<ValueNode>> wires; std::optional<std::vector<ValueNode>> wires;
}; };
struct WireNode: public AstNode struct WireNode : public AstNode {
{
enum WireType { enum WireType {
WIRE, WIRE,
WIRED_AND, WIRED_AND,
@@ -217,28 +299,25 @@ struct WireNode: public AstNode
IdentifierNode name; IdentifierNode name;
NumberNode size; NumberNode size;
bool hidden; bool hidden = false;
bool hasTerminateWith; bool hasTerminateWith;
ValueNode terminateWith; ValueNode terminateWith;
}; };
struct AttributeNode: public AstNode struct AttributeNode : public AstNode {
{
ValueNode::ValueType type; ValueNode::ValueType type;
IdentifierNode name; IdentifierNode name;
std::optional<ValueNode> defaultValue; std::optional<ValueNode> defaultValue;
std::optional<PopupNode> popup; std::optional<PopupNode> popup;
}; };
struct ConnectionComponentNode: public AstNode struct ConnectionComponentNode : public AstNode {
{
IdentifierNode component; IdentifierNode component;
IdentifierNode pin; IdentifierNode pin;
}; };
struct ConnectionNode: public AstNode struct ConnectionNode : public AstNode {
{
ConnectionComponentNode first; ConnectionComponentNode first;
std::optional<ConnectionComponentNode> second; std::optional<ConnectionComponentNode> second;
@@ -249,8 +328,7 @@ struct ConnectionNode: public AstNode
std::optional<std::vector<ValueNode>> secondWires; std::optional<std::vector<ValueNode>> secondWires;
}; };
struct ComponentNode: public AstNode struct ComponentNode : public AstNode {
{
enum ComponentType { enum ComponentType {
OTHER, OTHER,
PROCESSOR, PROCESSOR,
@@ -262,22 +340,23 @@ struct ComponentNode: public AstNode
std::optional<StringNode> source; std::optional<StringNode> source;
EnumNode<ComponentType> type; EnumNode<ComponentType> type;
std::vector<RuleNode> rules; std::vector<RuleNode> rules;
std::optional<StringNode> instanceName; std::optional<IdentifierNode> instanceName;
std::optional<CountNode> count; std::optional<CountNode> count;
std::optional<DisplayNode> display; std::optional<DisplayNode> display;
std::vector<PinNode> pins; std::vector<PinNode> pins;
std::vector<AttributeNode> attributes; std::vector<AttributeNode> attributes;
}; };
struct BusNode: public AstNode struct BusNode : public AstNode {
{
enum BusType { enum BusType {
AUTOMATIC, AUTOMATIC,
REGULAR REGULAR,
SINGLE_AUTOMATIC
}; };
EnumNode<BusType> type; EnumNode<BusType> type;
IdentifierNode name; IdentifierNode name;
std::optional<IdentifierNode> instanceName;
std::optional<StringNode> tooltip; std::optional<StringNode> tooltip;
std::optional<CountNode> count; std::optional<CountNode> count;
std::optional<DisplayNode> display; std::optional<DisplayNode> display;
@@ -285,12 +364,12 @@ struct BusNode: public AstNode
std::vector<WireNode> wires; std::vector<WireNode> wires;
}; };
struct LibraryNode: public AstNode struct LibraryNode : public AstNode {
{
std::optional<StringNode> name; std::optional<StringNode> name;
std::optional<StringNode> libraryInfo; std::optional<StringNode> libraryInfo;
std::optional<StringNode> header; std::optional<StringNode> header;
std::optional<StringNode> componentDirectory; std::optional<StringNode> componentDirectory;
std::optional<StringNode> componentHeader;
std::vector<AddressSpaceNode> addressSpaces; std::vector<AddressSpaceNode> addressSpaces;
@@ -302,21 +381,12 @@ struct LibraryNode: public AstNode
}; };
// SCHEMA models // SCHEMA models
struct WireInstanceNode: public AstNode struct InstanceAttributeNode : public AstNode {
{
IdentifierNode name;
std::optional<CountNode> position;
std::optional<DisplayNode> display;
};
struct InstanceAttributeNode: public AstNode
{
IdentifierNode name; IdentifierNode name;
ValueNode value; ValueNode value;
}; };
struct InstanceNode: public AstNode struct InstanceNode : public AstNode {
{
IdentifierNode name; IdentifierNode name;
IdentifierNode component; IdentifierNode component;
@@ -326,30 +396,25 @@ struct InstanceNode: public AstNode
std::optional<NumberNode> size; std::optional<NumberNode> size;
}; };
struct ConnectionComponentInstance: public AstNode struct ConnectionComponentInstance : public AstNode {
{
IdentifierNode instance; IdentifierNode instance;
IdentifierNode pin; IdentifierNode pin;
}; };
struct ConnectionInstanceNode: public AstNode struct ConnectionInstanceNode : public AstNode {
{
ConnectionComponentInstance first; ConnectionComponentInstance first;
std::optional<ConnectionComponentInstance> second; std::optional<ConnectionComponentInstance> second;
IdentifierNode bus; IdentifierNode bus;
std::optional<IdentifierNode> wire;
std::vector<InstanceAttributeNode> attributes; std::vector<InstanceAttributeNode> attributes;
}; };
struct SchemaNode: public AstNode struct SchemaNode : public AstNode {
{
std::optional<StringNode> source; std::optional<StringNode> source;
std::vector<InstanceNode> instances; std::vector<InstanceNode> instances;
std::vector<WireInstanceNode> wires;
std::vector<ConnectionInstanceNode> connections; std::vector<ConnectionInstanceNode> connections;
std::optional<LibraryNode> library; std::optional<LibraryNode> library;
+5
View File
@@ -0,0 +1,5 @@
//
// Created by bbr on 12.06.22..
//
#include "color.h"
+21
View File
@@ -0,0 +1,21 @@
//
// Created by bbr on 12.06.22..
//
#ifndef SCHEMEEDITOR_COLOR_H
#define SCHEMEEDITOR_COLOR_H
struct Color {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
Color(): r(0), g(0), b(0), a(0) {}
Color(unsigned char r, unsigned char g, unsigned char b, unsigned char a = 0): r(r), g(g), b(b), a(a) {}
};
#endif //SCHEMEEDITOR_COLOR_H
@@ -1,6 +1,6 @@
#include "comdellexer.h" #include "comdel_lexer.h"
#include "token.h" #include "token.h"
#include "tokenstype.h" #include "tokens_type.h"
#include <fstream> #include <fstream>
#include <cctype> #include <cctype>
@@ -42,8 +42,7 @@ ComdelLexer::ComdelLexer(std::string fileName, std::string source,
// TODO Update this // TODO Update this
fileId(this->parseContext->addFile(fileName, this->source)), fileId(this->parseContext->addFile(fileName, this->source)),
position(this->fileId, 1, 1, 0), position(this->fileId, 1, 1, 0),
ch(this->source[0]) ch(this->source[0]) {}
{}
LexerResult ComdelLexer::tokenize() { LexerResult ComdelLexer::tokenize() {
@@ -181,8 +180,7 @@ PResult<TokenType> ComdelLexer::takeString() {
} }
PResult<TokenType> ComdelLexer::takeRawString() PResult<TokenType> ComdelLexer::takeRawString() {
{
Position lo = position; Position lo = position;
if (ch != '`') if (ch != '`')
@@ -237,20 +235,15 @@ bool ComdelLexer::skipMultilineComment() {
PResult<TokenType> ComdelLexer::nextTokenType() { PResult<TokenType> ComdelLexer::nextTokenType() {
if (isWhitespace(ch)) if (isWhitespace(ch)) {
{
skipWhitespace(); skipWhitespace();
return TokenType::WHITESPACE; return TokenType::WHITESPACE;
} } else if (identifierStart(ch)) {
else if (identifierStart(ch))
{
bump(); bump();
while (identifierContinue(ch)) while (identifierContinue(ch))
bump(); bump();
return TokenType::IDENTIFIER; return TokenType::IDENTIFIER;
} } else if (numberStart(ch)) {
else if (numberStart(ch))
{
if (ch == '-') { if (ch == '-') {
bump(); bump();
} }
@@ -266,27 +259,16 @@ PResult<TokenType> ComdelLexer::nextTokenType() {
"illegal digit or letter found at the end of number"}); "illegal digit or letter found at the end of number"});
} }
return TokenType::NUMBER; return TokenType::NUMBER;
} } else if (ch == '!') {
else if (ch == '!')
{
bump(); bump();
return TokenType::NOT; return TokenType::NOT;
} } else if (ch == '<') {
else if (ch == '<')
{
bump(); bump();
return TokenType::LT; return TokenType::LT;
} } else if (ch == '>') {
else if (ch == '>')
{
bump(); bump();
return TokenType::GT; return TokenType::GT;
} } else if (ch == '#') {
else if (ch == '#')
{
bump(); bump();
if (digitIsValid(ch, Radix::HEX_NUMBER)) { if (digitIsValid(ch, Radix::HEX_NUMBER)) {
@@ -304,65 +286,42 @@ PResult<TokenType> ComdelLexer::nextTokenType() {
return PError({Span(tokenBegin, position), return PError({Span(tokenBegin, position),
"unexpected #"}); "unexpected #"});
} }
} } else if (ch == '@') {
else if (ch == '@')
{
bump(); bump();
while (identifierContinue(ch)) while (identifierContinue(ch))
bump(); bump();
return TokenType::KEYWORD; return TokenType::KEYWORD;
} } else if (ch == '"') {
else if (ch == '"')
{
return takeString(); return takeString();
} } else if (ch == '`') {
else if (ch == '`')
{
return takeRawString(); return takeRawString();
} } else if (ch == '(') {
else if (ch == '(')
{
bump(); bump();
return TokenType::LPAREN; return TokenType::LPAREN;
} } else if (ch == ')') {
else if (ch == ')')
{
bump(); bump();
return TokenType::RPAREN; return TokenType::RPAREN;
} } else if (ch == '[') {
else if (ch == '[')
{
bump(); bump();
return TokenType::LBRACKET; return TokenType::LBRACKET;
} } else if (ch == ']') {
else if (ch == ']')
{
bump(); bump();
return TokenType::RBRACKET; return TokenType::RBRACKET;
} } else if (ch == '{') {
else if (ch == '{')
{
bump(); bump();
return TokenType::LBRACE; return TokenType::LBRACE;
} } else if (ch == '}') {
else if (ch == '}')
{
bump(); bump();
return TokenType::RBRACE; return TokenType::RBRACE;
} } else if (ch == '/') {
else if (ch == '/')
{
bump(); bump();
if (ch == '/') if (ch == '/') {
{
bump(); bump();
skipComment(); skipComment();
return TokenType::COMMENT; return TokenType::COMMENT;
} else if (ch == '*') } else if (ch == '*') {
{
bump(); bump();
if (!skipMultilineComment()) if (!skipMultilineComment()) {
{
return PError({Span(tokenBegin, position), return PError({Span(tokenBegin, position),
"unterminated multiline comment"}); "unterminated multiline comment"});
} }
@@ -370,34 +329,22 @@ PResult<TokenType> ComdelLexer::nextTokenType() {
} }
return PError({Span(tokenBegin, position), return PError({Span(tokenBegin, position),
"unexpected /"}); "unexpected /"});
} } else if (ch == '.') {
else if (ch == '.')
{
bump(); bump();
return TokenType::DOT; return TokenType::DOT;
} } else if (ch == ':') {
else if (ch == ':')
{
bump(); bump();
return TokenType::COLON; return TokenType::COLON;
} } else if (ch == ';') {
else if (ch == ';')
{
bump(); bump();
return TokenType::SEMICOLON; return TokenType::SEMICOLON;
} } else if (ch == ',') {
else if (ch == ',')
{
bump(); bump();
return TokenType::COMMA; return TokenType::COMMA;
} } else if (ch == '=') {
else if (ch == '=')
{
bump(); bump();
return TokenType::EQUALS; return TokenType::EQUALS;
} } else {
else
{
std::stringstream message; std::stringstream message;
message << "unexpected character `" << ch << "`"; message << "unexpected character `" << ch << "`";
bump(); bump();
@@ -1,9 +1,9 @@
#ifndef COMDEL_LEXER_H #ifndef COMDEL_LEXER_H
#define COMDEL_LEXER_H #define COMDEL_LEXER_H
#include "parsecontext.h" #include "parse_context.h"
#include "presult.h" #include "presult.h"
#include "sourceerror.h" #include "source_error.h"
#include "token.h" #include "token.h"
@@ -16,8 +16,7 @@ struct LexerResult {
std::vector<SourceError> errors; std::vector<SourceError> errors;
}; };
class ComdelLexer class ComdelLexer {
{
enum Radix { enum Radix {
BIN_NUMBER = 2, BIN_NUMBER = 2,
@@ -33,14 +32,18 @@ class ComdelLexer
public: public:
ComdelLexer(std::string fileName, std::string source, ParseContext *parseContext); ComdelLexer(std::string fileName, std::string source, ParseContext *parseContext);
LexerResult tokenize(); LexerResult tokenize();
private: private:
void skipWhitespace(); void skipWhitespace();
unsigned takeNumberInRadix(Radix radix); unsigned takeNumberInRadix(Radix radix);
unsigned takeHexColor(); unsigned takeHexColor();
bool digitIsValid(char ch, Radix radix); bool digitIsValid(char ch, Radix radix);
Radix takeRadix(); Radix takeRadix();
PResult<TokenType> nextTokenType(); PResult<TokenType> nextTokenType();
@@ -50,13 +53,17 @@ private:
char ch; char ch;
void skipComment(); void skipComment();
bool skipMultilineComment(); bool skipMultilineComment();
PResult<TokenType> takeString(); PResult<TokenType> takeString();
PResult<TokenType> takeRawString(); PResult<TokenType> takeRawString();
void bump(unsigned count = 1); void bump(unsigned count = 1);
char peek(); char peek();
bool eof(); bool eof();
}; };
@@ -1,7 +1,7 @@
#include "comdelparser.h" #include "comdel_parser.h"
#include "poly.h" #include "poly.h"
#include "tokenstype.h" #include "tokens_type.h"
#include <utility> #include <utility>
#include <algorithm> #include <algorithm>
@@ -9,9 +9,7 @@
#include <sstream> #include <sstream>
ComdelParser::ComdelParser(std::vector<Token> tokens) ComdelParser::ComdelParser(std::vector<Token> tokens)
: tokens(std::move(tokens)) : tokens(std::move(tokens)), position(0) {}
, position(0)
{}
// Tries to consume a token, returns an error otherwise. // Tries to consume a token, returns an error otherwise.
// Unlike the regular consume() call this should be used // Unlike the regular consume() call this should be used
@@ -27,8 +25,7 @@ ComdelParser::ComdelParser(std::vector<Token> tokens)
// Checks if the current token matches the argument. // Checks if the current token matches the argument.
// Otherwise, adds the token to the list of expected tokens (used in // Otherwise, adds the token to the list of expected tokens (used in
// error message). // error message).
bool ComdelParser::check(TokenType tokenType) bool ComdelParser::check(TokenType tokenType) {
{
if (current().type == tokenType) if (current().type == tokenType)
return true; return true;
@@ -53,12 +50,11 @@ bool ComdelParser::consume(TokenType tokenType) {
do{}while(0) do{}while(0)
void ComdelParser::bump() { void ComdelParser::bump() {
if (tokens[position].type == TokenType::END_OF_FILE) { if (tokens[position].type != TokenType::END_OF_FILE) {
ATLAS_ASSERT_FAIL("Internal parser error, called bump after EOF"); ++position;
} else { } else {
tokens[++position]; ATLAS_ASSERT_FAIL("Internal parser error, called bump after EOF");
} }
expectedTokens.clear(); expectedTokens.clear();
} }
@@ -94,7 +90,7 @@ PError ComdelParser::unexpected() {
else else
ss << "."; ss << ".";
} }
return PError({current().span, ss.str()}); return {{current().span, ss.str()}};
} }
// Parses a list of 'nodes' separated by 'separator'. Parsed nodes // Parses a list of 'nodes' separated by 'separator'. Parsed nodes
@@ -107,18 +103,18 @@ PError ComdelParser::unexpected() {
// //
// 'closeDelim' is mandatory, and it will be consumed. // 'closeDelim' is mandatory, and it will be consumed.
// //
// Function 'parse_f' should only parse a single node of type T. // Function 'parse_f' should only parseLibrary a single node of type T.
// //
// Parameter allowTrailing==true means that the list should end with // Parameter allowTrailing==true means that the list should end with
// 'separator' and otherwise the list should end with 'node'. This // 'separator' and otherwise the list should end with 'node'. This
// parameter is false in every call-site! // parameter is false in every call-site!
template<typename T> PResult<std::vector<T>> template<typename T>
PResult<std::vector<T>>
ComdelParser::parseList(std::optional<TokenType> openDelim, ComdelParser::parseList(std::optional<TokenType> openDelim,
TokenType closeDelim, TokenType closeDelim,
std::optional<TokenType> separator, std::optional<TokenType> separator,
bool allowTrailing, bool allowTrailing,
const std::function<PResult<T>()>& parse_f) const std::function<PResult<T>()> &parse_f) {
{
std::vector<T> vec; std::vector<T> vec;
if (openDelim) { if (openDelim) {
@@ -169,8 +165,7 @@ void ComdelParser::skipUntilNextKeyword() {
} }
} }
const std::vector<SourceError> &ComdelParser::getErrors() const std::vector<SourceError> &ComdelParser::getErrors() {
{
return errors; return errors;
} }
@@ -184,9 +179,8 @@ Span &ComdelParser::getPreviousSpan() {
} }
// See comment for Spanner // See comment for Spanner
Spanner ComdelParser::getSpanner() Spanner ComdelParser::getSpanner() {
{ return {current().span, getPreviousSpan()};
return Spanner(current().span, getPreviousSpan());
} }
// utility for reading strings // utility for reading strings
@@ -196,7 +190,7 @@ Spanner ComdelParser::getSpanner()
if (!assign_or_return_if_err_temp_) { \ if (!assign_or_return_if_err_temp_) { \
err = PError(assign_or_return_if_err_temp_.error()); \ err = PError(assign_or_return_if_err_temp_.error()); \
} else { \ } else { \
var = *assign_or_return_if_err_temp_; \ (var) = *assign_or_return_if_err_temp_; \
} \ } \
} while (0) } while (0)
@@ -207,7 +201,7 @@ Spanner ComdelParser::getSpanner()
if (!assign_or_return_if_err_temp_) { \ if (!assign_or_return_if_err_temp_) { \
err = PError(assign_or_return_if_err_temp_.error()); \ err = PError(assign_or_return_if_err_temp_.error()); \
} else { \ } else { \
var.push_back(*assign_or_return_if_err_temp_); \ (var).push_back(*assign_or_return_if_err_temp_); \
} \ } \
} while (0) } while (0)
@@ -218,7 +212,7 @@ Spanner ComdelParser::getSpanner()
if (!assign_or_return_if_err_temp_) { \ if (!assign_or_return_if_err_temp_) { \
return PError(assign_or_return_if_err_temp_.error()); \ return PError(assign_or_return_if_err_temp_.error()); \
} else { \ } else { \
var.push_back(*assign_or_return_if_err_temp_); \ (var).push_back(*assign_or_return_if_err_temp_); \
} \ } \
} while (0) } while (0)
@@ -229,8 +223,7 @@ Spanner ComdelParser::getSpanner()
* *
****************************************************************************/ ****************************************************************************/
std::optional<LibraryNode> ComdelParser::parse() std::optional<LibraryNode> ComdelParser::parseLibrary() {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
LibraryNode library{}; LibraryNode library{};
@@ -243,6 +236,9 @@ std::optional<LibraryNode> ComdelParser::parse()
} else if (check(TokenType::KW_HEADER)) { } else if (check(TokenType::KW_HEADER)) {
bump(); bump();
ASSIGN_OR_SET_ERR(library.header, parseString()); ASSIGN_OR_SET_ERR(library.header, parseString());
} else if (check(TokenType::KW_COMPONENT_HEADER)) {
bump();
ASSIGN_OR_SET_ERR(library.componentHeader, parseString());
} else if (check(TokenType::KW_DIRECTORY)) { } else if (check(TokenType::KW_DIRECTORY)) {
bump(); bump();
ASSIGN_OR_SET_ERR(library.componentDirectory, parseString()); ASSIGN_OR_SET_ERR(library.componentDirectory, parseString());
@@ -259,11 +255,12 @@ std::optional<LibraryNode> ComdelParser::parse()
APPEND_OR_SET_ERR(library.connections, parseConnection()); APPEND_OR_SET_ERR(library.connections, parseConnection());
} else if (check(TokenType::KW_MESSAGES)) { } else if (check(TokenType::KW_MESSAGES)) {
bump(); bump();
ASSIGN_OR_SET_ERR(library.messages, parseList<PropertyNode>(std::optional<TokenType>(TokenType::LBRACE), ASSIGN_OR_SET_ERR(library.messages, parseList<PropertyNode>(TokenType::LBRACE,
TokenType::RBRACE, TokenType::RBRACE,
std::nullopt, std::nullopt,
false, false,
[this]{return parseProperty(std::optional<TokenType>(TokenType::STRING));} [this] { return parseProperty(TokenType::STRING);
}
)); ));
if (!err.has_value()) { if (!err.has_value()) {
skipUntilNextKeyword(); skipUntilNextKeyword();
@@ -279,7 +276,7 @@ std::optional<LibraryNode> ComdelParser::parse()
} }
} }
if (errors.size()) if (!errors.empty())
return std::nullopt; return std::nullopt;
return spanner(library); return spanner(library);
@@ -290,8 +287,7 @@ std::optional<LibraryNode> ComdelParser::parse()
* StringNode := "\"" + CONTENT + "\"" * StringNode := "\"" + CONTENT + "\""
* *
****************************************************************************/ ****************************************************************************/
PResult<StringNode> ComdelParser::parseString() PResult<StringNode> ComdelParser::parseString() {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
if (check(TokenType::STRING)) { if (check(TokenType::STRING)) {
StringNode node; StringNode node;
@@ -304,13 +300,29 @@ PResult<StringNode> ComdelParser::parseString()
} }
/****************************************************************************
*
* ColorNode := #RRGGBB[AA]
*
****************************************************************************/
PResult<ColorNode> ComdelParser::parseColor() {
auto spanner = getSpanner();
if (check(TokenType::COLOR)) {
ColorNode node{current().text};
node.span = current().span;
bump();
return spanner(node);
}
return unexpected();
}
/**************************************************************************** /****************************************************************************
* *
* IdentifierNode := IDENTIFIER * IdentifierNode := IDENTIFIER
* *
****************************************************************************/ ****************************************************************************/
PResult<IdentifierNode> ComdelParser::parseIdentifier() PResult<IdentifierNode> ComdelParser::parseIdentifier() {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
if (check(TokenType::IDENTIFIER)) { if (check(TokenType::IDENTIFIER)) {
IdentifierNode node; IdentifierNode node;
@@ -327,8 +339,7 @@ PResult<IdentifierNode> ComdelParser::parseIdentifier()
* NumberNode := ('0x' | '0b'){0,1}[0-9]* * NumberNode := ('0x' | '0b'){0,1}[0-9]*
* *
****************************************************************************/ ****************************************************************************/
PResult<NumberNode> ComdelParser::parseNumber() PResult<NumberNode> ComdelParser::parseNumber() {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
if (check(TokenType::NUMBER)) { if (check(TokenType::NUMBER)) {
NumberNode node{current().text}; NumberNode node{current().text};
@@ -345,8 +356,7 @@ PResult<NumberNode> ComdelParser::parseNumber()
* CountNode := "@size (" + NumberNode + "," + NumberNode + ")" * CountNode := "@size (" + NumberNode + "," + NumberNode + ")"
* *
****************************************************************************/ ****************************************************************************/
PResult<CountNode> ComdelParser::parseCount() PResult<CountNode> ComdelParser::parseCount() {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
RETURN_IF_NOT_TOKEN(TokenType::KW_COUNT); RETURN_IF_NOT_TOKEN(TokenType::KW_COUNT);
@@ -370,8 +380,7 @@ PResult<CountNode> ComdelParser::parseCount()
* PropertyNode := key: value; * PropertyNode := key: value;
* *
****************************************************************************/ ****************************************************************************/
PResult<PropertyNode> ComdelParser::parseProperty(std::optional<TokenType> valueType = std::nullopt) PResult<PropertyNode> ComdelParser::parseProperty(std::optional<TokenType> valueType = std::nullopt) {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
IdentifierNode key; IdentifierNode key;
ASSIGN_OR_RETURN_IF_ERR(key, parseIdentifier()); ASSIGN_OR_RETURN_IF_ERR(key, parseIdentifier());
@@ -404,8 +413,7 @@ PResult<PropertyNode> ComdelParser::parseProperty(std::optional<TokenType> value
* AddressSpaceNode := "@address" + IDENTIFIER "(" + NUMBER + "," + NUMBER + ")" * AddressSpaceNode := "@address" + IDENTIFIER "(" + NUMBER + "," + NUMBER + ")"
* *
****************************************************************************/ ****************************************************************************/
PResult<AddressSpaceNode> ComdelParser::parseAddress() PResult<AddressSpaceNode> ComdelParser::parseAddress() {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
AddressSpaceNode addressSpace{}; AddressSpaceNode addressSpace{};
@@ -428,8 +436,7 @@ PResult<AddressSpaceNode> ComdelParser::parseAddress()
* ComponentNode := "@component" + IDENTIFIER + ("processor" | "memory") { COMPONENT_BLOCK } * ComponentNode := "@component" + IDENTIFIER + ("processor" | "memory") { COMPONENT_BLOCK }
* *
****************************************************************************/ ****************************************************************************/
PResult<ComponentNode> ComdelParser::parseComponent() PResult<ComponentNode> ComdelParser::parseComponent() {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
ComponentNode component{}; ComponentNode component{};
@@ -437,8 +444,7 @@ PResult<ComponentNode> ComdelParser::parseComponent()
RETURN_IF_NOT_TOKEN(TokenType::KW_COMPONENT); RETURN_IF_NOT_TOKEN(TokenType::KW_COMPONENT);
ASSIGN_OR_RETURN_IF_ERR(component.name, parseIdentifier()); ASSIGN_OR_RETURN_IF_ERR(component.name, parseIdentifier());
ASSIGN_OR_RETURN_IF_ERR(component.type, parseComponentType());
auto type = parseComponentType();
RETURN_IF_NOT_TOKEN(TokenType::LBRACE); RETURN_IF_NOT_TOKEN(TokenType::LBRACE);
@@ -446,7 +452,7 @@ PResult<ComponentNode> ComdelParser::parseComponent()
PResult<poly<AstNode>> err; PResult<poly<AstNode>> err;
if (check(TokenType::KW_INSTANCE_NAME)) { if (check(TokenType::KW_INSTANCE_NAME)) {
bump(); bump();
ASSIGN_OR_SET_ERR(component.instanceName, parseString()); ASSIGN_OR_SET_ERR(component.instanceName, parseIdentifier());
} else if (check(TokenType::KW_TOOLTIP)) { } else if (check(TokenType::KW_TOOLTIP)) {
bump(); bump();
ASSIGN_OR_SET_ERR(component.tooltip, parseString()); ASSIGN_OR_SET_ERR(component.tooltip, parseString());
@@ -465,6 +471,7 @@ PResult<ComponentNode> ComdelParser::parseComponent()
APPEND_OR_SET_ERR(component.rules, parseRule()); APPEND_OR_SET_ERR(component.rules, parseRule());
} else { } else {
err = unexpected(); err = unexpected();
bump();
} }
if (!err.has_value()) { if (!err.has_value()) {
errors.push_back(err.error()); errors.push_back(err.error());
@@ -553,11 +560,7 @@ PResult<BusNode> ComdelParser::parseBus() {
RETURN_IF_NOT_TOKEN(TokenType::KW_BUS); RETURN_IF_NOT_TOKEN(TokenType::KW_BUS);
ASSIGN_OR_RETURN_IF_ERR(bus.name, parseIdentifier()); ASSIGN_OR_RETURN_IF_ERR(bus.name, parseIdentifier());
ASSIGN_OR_RETURN_IF_ERR(bus.type, parseBusType());
auto type = parseBusType();
RETURN_IF_ERR(type);
bus.type = *type;
RETURN_IF_NOT_TOKEN(TokenType::LBRACE); RETURN_IF_NOT_TOKEN(TokenType::LBRACE);
@@ -565,14 +568,17 @@ PResult<BusNode> ComdelParser::parseBus() {
PResult<poly<AstNode>> err; PResult<poly<AstNode>> err;
if (check(TokenType::KW_TOOLTIP)) { if (check(TokenType::KW_TOOLTIP)) {
bump(); bump();
ASSIGN_OR_RETURN_IF_ERR(bus.tooltip, parseString()); ASSIGN_OR_SET_ERR(bus.tooltip, parseString());
} else if (check(TokenType::KW_INSTANCE_NAME)) {
bump();
ASSIGN_OR_SET_ERR(bus.instanceName, parseIdentifier());
} else if (check(TokenType::KW_COUNT)) { } else if (check(TokenType::KW_COUNT)) {
ASSIGN_OR_RETURN_IF_ERR(bus.count, parseCount()); ASSIGN_OR_SET_ERR(bus.count, parseCount());
} else if (check(TokenType::KW_DISPLAY)) { } else if (check(TokenType::KW_DISPLAY)) {
ASSIGN_OR_RETURN_IF_ERR(bus.display, parseDisplay()); ASSIGN_OR_SET_ERR(bus.display, parseDisplay());
} else if (check(TokenType::KW_WIRES)) { } else if (check(TokenType::KW_WIRES)) {
bump(); bump();
RETURN_IF_NOT_TOKEN(TokenType::LBRACE); if(consume(TokenType::LBRACE)) {
while (check(TokenType::IDENTIFIER)) { while (check(TokenType::IDENTIFIER)) {
APPEND_OR_RETURN_IF_ERR(bus.wires, parseWire()); APPEND_OR_RETURN_IF_ERR(bus.wires, parseWire());
@@ -580,10 +586,16 @@ PResult<BusNode> ComdelParser::parseBus() {
RETURN_IF_NOT_TOKEN(TokenType::COMMA); RETURN_IF_NOT_TOKEN(TokenType::COMMA);
} }
} }
RETURN_IF_NOT_TOKEN(TokenType::RBRACE);
} else { } else {
err = unexpected(); err = unexpected();
} }
if(!consume(TokenType::RBRACE)) {
err = unexpected();
}
} else {
err = unexpected();
bump();
}
if (!err.has_value()) { if (!err.has_value()) {
errors.push_back(err.error()); errors.push_back(err.error());
skipUntilNextKeyword(); skipUntilNextKeyword();
@@ -607,11 +619,13 @@ PResult<EnumNode<BusNode::BusType>> ComdelParser::parseBusType() {
type = EnumNode(BusNode::AUTOMATIC); type = EnumNode(BusNode::AUTOMATIC);
} else if (tokenType.value().value == "regular") { } else if (tokenType.value().value == "regular") {
type = EnumNode(BusNode::REGULAR); type = EnumNode(BusNode::REGULAR);
} else if (tokenType.value().value == "singleAutomatic") {
type = EnumNode(BusNode::SINGLE_AUTOMATIC);
} else { } else {
type = PError(SourceError{current().span, "expected 'automatic' or 'regular'"}); type = PError(SourceError{current().span, "expected 'automatic', 'singleAutomatic' or 'regular'"});
} }
} else { } else {
type = PError(SourceError{current().span, "expected 'automatic' or 'regular'"}); type = PError(SourceError{current().span, "expected 'automatic', 'singleAutomatic' or 'regular'"});
} }
return type; return type;
} }
@@ -722,12 +736,16 @@ PResult<PinNode> ComdelParser::parsePin() {
ASSIGN_OR_SET_ERR(pin.connection, parsePinConnection()); ASSIGN_OR_SET_ERR(pin.connection, parsePinConnection());
} else if (check(TokenType::KW_WIRES)) { } else if (check(TokenType::KW_WIRES)) {
bump(); bump();
auto wires = parseList<ValueNode>(std::optional<TokenType>(TokenType::LBRACE), TokenType::RBRACE, std::optional<TokenType>(TokenType::COMMA), false, auto wires = parseList<ValueNode>(TokenType::LBRACE,
TokenType::RBRACE,
TokenType::COMMA,
false,
[this] { return parseConnectionWire(); }); [this] { return parseConnectionWire(); });
RETURN_IF_ERR(wires); RETURN_IF_ERR(wires);
pin.wires = *wires; pin.wires = *wires;
} else { } else {
err = unexpected(); err = unexpected();
bump();
} }
if (!err.has_value()) { if (!err.has_value()) {
errors.push_back(err.error()); errors.push_back(err.error());
@@ -771,15 +789,15 @@ PResult<EnumNode<PinConnectionNode::ConnectionType>> ComdelParser::parseConnecti
if (check(TokenType::IDENTIFIER)) { if (check(TokenType::IDENTIFIER)) {
auto identifier = parseIdentifier(); auto identifier = parseIdentifier();
if(identifier.value().value == "check_only") { if (identifier.value().value == "required") {
type = EnumNode(PinConnectionNode::CHECK_ONLY); type = EnumNode(PinConnectionNode::REQUIRED);
} else if(identifier.value().value == "automatically") { } else if (identifier.value().value == "optional") {
type = EnumNode(PinConnectionNode::AUTOMATICALLY); type = EnumNode(PinConnectionNode::OPTIONAL);
} else { } else {
return PError(SourceError{current().span, "expected identifiers 'check_only' or 'automatically'"}); return PError(SourceError{current().span, "expected connection type 'required' or 'optional'"});
} }
} else { } else {
return PError(SourceError{current().span, "expected identifiers 'check_only' or 'automatically'"}); return PError(SourceError{current().span, "expected connection type 'required' or 'optional'"});
} }
return spanner(type); return spanner(type);
@@ -848,13 +866,23 @@ PResult<AttributeNode> ComdelParser::parseAttribute() {
} else { } else {
return unexpected(); return unexpected();
} }
} else if (attribute.type == ValueNode::MEMORY) {
if (check(TokenType::IDENTIFIER)) {
auto identifier = parseIdentifier();
attribute.defaultValue = ValueNode::ofMemory(identifier->value);
} else if (check(TokenType::NIL)) {
bump();
attribute.defaultValue = ValueNode::ofMemory(std::nullopt);
} else {
return unexpected();
}
} else if (attribute.type == ValueNode::WIRE) { } else if (attribute.type == ValueNode::WIRE) {
if (check(TokenType::IDENTIFIER)) { if (check(TokenType::IDENTIFIER)) {
auto identifier = parseIdentifier(); auto identifier = parseIdentifier();
attribute.defaultValue = ValueNode::ofWire(identifier->value); attribute.defaultValue = ValueNode::ofWire(identifier->value);
} else if (check(TokenType::NIL)) { } else if (check(TokenType::NIL)) {
bump(); bump();
attribute.defaultValue = ValueNode::ofNull(); attribute.defaultValue = ValueNode::ofWire(std::nullopt);
} else { } else {
return unexpected(); return unexpected();
} }
@@ -939,15 +967,16 @@ PResult<PopupNode> ComdelParser::parsePopup() {
popup.enumerated = true; popup.enumerated = true;
ASSIGN_OR_SET_ERR(popup.enumeration, ASSIGN_OR_SET_ERR(popup.enumeration,
parseList<EnumerationNode>( parseList<EnumerationNode>(
std::optional<TokenType>(TokenType::LBRACE), TokenType::LBRACE,
TokenType::RBRACE, TokenType::RBRACE,
std::optional<TokenType>(TokenType::COMMA), TokenType::COMMA,
true, true,
[this] { return parseEnumeration(); } [this] { return parseEnumeration(); }
) )
); );
} else { } else {
err = unexpected(); err = unexpected();
bump();
} }
if (!err.has_value()) { if (!err.has_value()) {
errors.push_back(err.error()); errors.push_back(err.error());
@@ -965,7 +994,7 @@ PResult<PopupNode> ComdelParser::parsePopup() {
/**************************************************************************** /****************************************************************************
* *
* ConnectionNode := "@connection (" + COMPONENT + "." + PIN + "," + BUS) {" + CONNECTION + "}" * ConnectionNode := "@connection (" + COMPONENT + "." + PIN + "," + BUS [ + "," + COMPONENT2 + "." + "PIN2"] + ") {" + CONNECTION + "}"
* *
****************************************************************************/ ****************************************************************************/
PResult<ConnectionNode> ComdelParser::parseConnection() { PResult<ConnectionNode> ComdelParser::parseConnection() {
@@ -984,7 +1013,6 @@ PResult<ConnectionNode> ComdelParser::parseConnection() {
if (check(TokenType::COMMA)) { if (check(TokenType::COMMA)) {
auto conn = ConnectionComponentNode{}; auto conn = ConnectionComponentNode{};
RETURN_IF_NOT_TOKEN(TokenType::COMMA); RETURN_IF_NOT_TOKEN(TokenType::COMMA);
RETURN_IF_NOT_TOKEN(TokenType::LPAREN);
ASSIGN_OR_RETURN_IF_ERR(conn.component, parseIdentifier()); ASSIGN_OR_RETURN_IF_ERR(conn.component, parseIdentifier());
RETURN_IF_NOT_TOKEN(TokenType::DOT); RETURN_IF_NOT_TOKEN(TokenType::DOT);
ASSIGN_OR_RETURN_IF_ERR(conn.pin, parseIdentifier()); ASSIGN_OR_RETURN_IF_ERR(conn.pin, parseIdentifier());
@@ -1016,6 +1044,7 @@ PResult<ConnectionNode> ComdelParser::parseConnection() {
wireCount++; wireCount++;
} else { } else {
err = unexpected(); err = unexpected();
bump();
} }
if (!err.has_value()) { if (!err.has_value()) {
errors.push_back(err.error()); errors.push_back(err.error());
@@ -1081,7 +1110,7 @@ PResult<IfStatementNode> ComdelParser::parseIfStatement() {
ASSIGN_OR_RETURN_IF_ERR(ifStatement.condition.functionName, parseIdentifier()); ASSIGN_OR_RETURN_IF_ERR(ifStatement.condition.functionName, parseIdentifier());
ASSIGN_OR_RETURN_IF_ERR(ifStatement.condition.params, parseList<ValueNode>(std::optional<TokenType>(TokenType::LPAREN), ASSIGN_OR_RETURN_IF_ERR(ifStatement.condition.params, parseList<ValueNode>(TokenType::LPAREN,
TokenType::RPAREN, TokenType::RPAREN,
TokenType::COMMA, TokenType::COMMA,
false, false,
@@ -1130,6 +1159,8 @@ PResult<ValueNode> ComdelParser::parseValue() {
} else if (check(TokenType::NIL)) { } else if (check(TokenType::NIL)) {
bump(); bump();
value = ValueNode::ofNull(); value = ValueNode::ofNull();
} else if(check(TokenType::COLOR)) {
value = ValueNode::ofColor(parseColor()->color);
} else { } else {
return unexpected(); return unexpected();
} }
@@ -1170,8 +1201,6 @@ std::optional<SchemaNode> ComdelParser::parseSchema() {
PResult<poly<AstNode>> err; PResult<poly<AstNode>> err;
if (check(TokenType::KW_INSTANCE)) { if (check(TokenType::KW_INSTANCE)) {
APPEND_OR_SET_ERR(schema.instances, parseInstance()); APPEND_OR_SET_ERR(schema.instances, parseInstance());
} else if(check(TokenType::KW_WIRE)) {
APPEND_OR_SET_ERR(schema.wires, parseWireInstance());
} else if (check(TokenType::KW_CONNECTION)) { } else if (check(TokenType::KW_CONNECTION)) {
APPEND_OR_SET_ERR(schema.connections, parseConnectionInstance()); APPEND_OR_SET_ERR(schema.connections, parseConnectionInstance());
} else { } else {
@@ -1181,6 +1210,10 @@ std::optional<SchemaNode> ComdelParser::parseSchema() {
if (!err.has_value()) { if (!err.has_value()) {
errors.push_back(err.error()); errors.push_back(err.error());
skipUntilNextKeyword(); skipUntilNextKeyword();
if (check(TokenType::END_OF_FILE)) {
errors.emplace_back(current().span, "expected `}` reached EOF");
return std::nullopt;
}
} }
} }
if (!check(TokenType::RBRACE)) { if (!check(TokenType::RBRACE)) {
@@ -1194,51 +1227,18 @@ std::optional<SchemaNode> ComdelParser::parseSchema() {
errors.emplace_back(current().span, "expected `EOF`"); errors.emplace_back(current().span, "expected `EOF`");
} }
if (errors.size()) if (!errors.empty())
return std::nullopt; return std::nullopt;
return spanner(schema); return spanner(schema);
} }
PResult<WireInstanceNode> ComdelParser::parseWireInstance() {
auto spanner = getSpanner();
WireInstanceNode wireInstance;
RETURN_IF_NOT_TOKEN(TokenType::KW_WIRE);
ASSIGN_OR_RETURN_IF_ERR(wireInstance.name, parseIdentifier());
RETURN_IF_NOT_TOKEN(TokenType::LBRACE);
while(!check(TokenType::RBRACE)) {
PResult<poly<AstNode>> err;
if(check(TokenType::KW_POSITION)) {
ASSIGN_OR_SET_ERR(wireInstance.position, parsePosition());
} else if(check(TokenType::KW_DISPLAY)) {
ASSIGN_OR_SET_ERR(wireInstance.display, parseDisplay());
} else {
err = unexpected();
}
if(!err.has_value()) {
errors.push_back(err.error());
skipUntilNextKeyword();
if(check(TokenType::END_OF_FILE)) {
return PError({Span(spanner.lo), "Reached EOF"});
}
}
}
RETURN_IF_NOT_TOKEN(TokenType::RBRACE);
return spanner(wireInstance);
}
/**************************************************************************** /****************************************************************************
* *
* CountNode := "@position (" + NumberNode + "," + NumberNode + ")" * CountNode := "@position (" + NumberNode + "," + NumberNode + ")"
* *
****************************************************************************/ ****************************************************************************/
PResult<CountNode> ComdelParser::parsePosition() PResult<CountNode> ComdelParser::parsePosition() {
{
auto spanner = getSpanner(); auto spanner = getSpanner();
RETURN_IF_NOT_TOKEN(TokenType::KW_POSITION); RETURN_IF_NOT_TOKEN(TokenType::KW_POSITION);
@@ -1281,6 +1281,7 @@ PResult<InstanceNode> ComdelParser::parseInstance() {
instance.size = *number; instance.size = *number;
} else { } else {
err = unexpected(); err = unexpected();
bump();
} }
if (!err.has_value()) { if (!err.has_value()) {
errors.push_back(err.error()); errors.push_back(err.error());
@@ -1295,6 +1296,7 @@ PResult<InstanceNode> ComdelParser::parseInstance() {
return spanner(instance); return spanner(instance);
} }
PResult<InstanceAttributeNode> ComdelParser::parseInstanceAttribute() { PResult<InstanceAttributeNode> ComdelParser::parseInstanceAttribute() {
auto spanner = getSpanner(); auto spanner = getSpanner();
RETURN_IF_NOT_TOKEN(TokenType::KW_ATTRIBUTE); RETURN_IF_NOT_TOKEN(TokenType::KW_ATTRIBUTE);
@@ -1305,6 +1307,7 @@ PResult<InstanceAttributeNode> ComdelParser::parseInstanceAttribute() {
return spanner(attribute); return spanner(attribute);
} }
PResult<ConnectionInstanceNode> ComdelParser::parseConnectionInstance() { PResult<ConnectionInstanceNode> ComdelParser::parseConnectionInstance() {
auto spanner = getSpanner(); auto spanner = getSpanner();
RETURN_IF_NOT_TOKEN(TokenType::KW_CONNECTION); RETURN_IF_NOT_TOKEN(TokenType::KW_CONNECTION);
@@ -1333,13 +1336,11 @@ PResult<ConnectionInstanceNode> ComdelParser::parseConnectionInstance() {
while (!check(TokenType::RBRACE)) { while (!check(TokenType::RBRACE)) {
PResult<poly<AstNode>> err; PResult<poly<AstNode>> err;
if(check(TokenType::KW_WIRE)) { if (check(TokenType::KW_ATTRIBUTE)) {
bump();
ASSIGN_OR_SET_ERR(connection.wire, parseIdentifier());
} else if(check(TokenType::KW_ATTRIBUTE)) {
APPEND_OR_SET_ERR(connection.attributes, parseInstanceAttribute()); APPEND_OR_SET_ERR(connection.attributes, parseInstanceAttribute());
} else { } else {
err = unexpected(); err = unexpected();
bump();
} }
if (!err.has_value()) { if (!err.has_value()) {
errors.push_back(err.error()); errors.push_back(err.error());
@@ -1356,13 +1357,16 @@ PResult<ConnectionInstanceNode> ComdelParser::parseConnectionInstance() {
} }
PResult<ValueNode> ComdelParser::parseConnectionWire() { PResult<ValueNode> ComdelParser::parseConnectionWire() {
auto spanner = getSpanner();
if (check(TokenType::NUMBER)) { if (check(TokenType::NUMBER)) {
return ValueNode::ofInt(parseNumber()->value); return spanner(ValueNode::ofInt(parseNumber()->value));
} else if (check(TokenType::NIL)) { } else if (check(TokenType::NIL)) {
bump(); bump();
return ValueNode::ofNull(); return spanner(ValueNode::ofNull());
} else if (check(TokenType::STRING)) {
return spanner(ValueNode::ofString(parseString()->asString()));
} else if (check(TokenType::IDENTIFIER)) { } else if (check(TokenType::IDENTIFIER)) {
return ValueNode::ofIdentifier(parseIdentifier()->value); return spanner(ValueNode::ofIdentifier(parseIdentifier()->value));
} else { } else {
return unexpected(); return unexpected();
} }
@@ -1,10 +1,10 @@
#ifndef COMDEL_PARSER_H #ifndef COMDEL_PARSER_H
#define COMDEL_PARSER_H #define COMDEL_PARSER_H
#include "sourceerror.h" #include "source_error.h"
#include "presult.h" #include "presult.h"
#include "token.h" #include "token.h"
#include "astnode.h" #include "ast_nodes.h"
#include <optional> #include <optional>
#include <set> #include <set>
@@ -23,18 +23,16 @@ class Spanner {
public: public:
const Span lo; // the low end of the span, beginning of the node const Span lo; // the low end of the span, beginning of the node
Spanner(Span lo, Span &prevSpan) : lo(lo), prevSpan(prevSpan) {} Spanner(Span lo, Span &prevSpan) : lo(lo), prevSpan(prevSpan) {}
template<typename T, typename = std::enable_if<std::is_base_of<AstNode, T>::value>> template<typename T, typename = std::enable_if<std::is_base_of<AstNode, T>::value>>
typename std::remove_reference_t<T> operator()(T &&astNode) const { typename std::remove_reference_t<T> operator()(T &&astNode) const {
astNode.span = lo.to(prevSpan); astNode.span = lo.to(prevSpan);
return std::move(astNode); return std::forward<T>(astNode);
} }
}; };
class ComdelParser class ComdelParser {
{
private: private:
std::vector<Token> tokens; std::vector<Token> tokens;
std::set<TokenType> expectedTokens; std::set<TokenType> expectedTokens;
@@ -47,6 +45,7 @@ private:
void bump(); void bump();
bool consume(TokenType tokenType); bool consume(TokenType tokenType);
bool check(TokenType tokenType); bool check(TokenType tokenType);
void skipUntilNextKeyword(); void skipUntilNextKeyword();
@@ -64,47 +63,74 @@ private:
Spanner getSpanner(); Spanner getSpanner();
// used to parse library and schema
PResult<StringNode> parseString(); PResult<StringNode> parseString();
PResult<ColorNode> parseColor();
PResult<IdentifierNode> parseIdentifier(); PResult<IdentifierNode> parseIdentifier();
PResult<NumberNode> parseNumber(); PResult<NumberNode> parseNumber();
PResult<CountNode> parseCount(); PResult<CountNode> parseCount();
PResult<PropertyNode> parseProperty(std::optional<TokenType> valueType); PResult<PropertyNode> parseProperty(std::optional<TokenType> valueType);
PResult<EnumerationNode> parseEnumeration(); PResult<EnumerationNode> parseEnumeration();
PResult<ValueNode> parseConnectionWire(); PResult<ValueNode> parseConnectionWire();
PResult<ComponentNode> parseComponent(); PResult<ComponentNode> parseComponent();
PResult<AddressSpaceNode> parseAddress(); PResult<AddressSpaceNode> parseAddress();
PResult<PinNode> parsePin(); PResult<PinNode> parsePin();
PResult<DisplayNode> parseDisplay(); PResult<DisplayNode> parseDisplay();
PResult<PinConnectionNode> parsePinConnection(); PResult<PinConnectionNode> parsePinConnection();
PResult<AttributeNode> parseAttribute(); PResult<AttributeNode> parseAttribute();
PResult<PopupNode> parsePopup(); PResult<PopupNode> parsePopup();
PResult<RuleNode> parseRule(); PResult<RuleNode> parseRule();
PResult<BusNode> parseBus(); PResult<BusNode> parseBus();
PResult<WireNode> parseWire(); PResult<WireNode> parseWire();
PResult<ConnectionNode> parseConnection(); PResult<ConnectionNode> parseConnection();
PResult<DisplayItemNode> parseDisplayItem(); PResult<DisplayItemNode> parseDisplayItem();
PResult<IfStatementNode> parseIfStatement(); PResult<IfStatementNode> parseIfStatement();
PResult<ValueNode> parseValue(); PResult<ValueNode> parseValue();
// used to parse schema
PResult<WireInstanceNode> parseWireInstance();
PResult<CountNode> parsePosition(); PResult<CountNode> parsePosition();
PResult<InstanceNode> parseInstance();
PResult<InstanceAttributeNode> parseInstanceAttribute();
PResult<ConnectionInstanceNode> parseConnectionInstance();
public: PResult<InstanceNode> parseInstance();
ComdelParser(std::vector<Token> tokens);
std::optional<SchemaNode> parseSchema(); PResult<InstanceAttributeNode> parseInstanceAttribute();
std::optional<LibraryNode> parse();
const std::vector<SourceError>& getErrors(); PResult<ConnectionInstanceNode> parseConnectionInstance();
PResult<EnumNode<ComponentNode::ComponentType>> parseComponentType(); PResult<EnumNode<ComponentNode::ComponentType>> parseComponentType();
PResult<EnumNode<BusNode::BusType>> parseBusType(); PResult<EnumNode<BusNode::BusType>> parseBusType();
PResult<EnumNode<PinConnectionNode::ConnectionType>> parseConnectionType(); PResult<EnumNode<PinConnectionNode::ConnectionType>> parseConnectionType();
public:
explicit ComdelParser(std::vector<Token> tokens);
std::optional<SchemaNode> parseSchema();
std::optional<LibraryNode> parseLibrary();
const std::vector<SourceError> &getErrors();
}; };
#endif // COMDEL_PARSER_H #endif // COMDEL_PARSER_H
+230 -44
View File
@@ -128,27 +128,33 @@ namespace tl {
#endif #endif
namespace tl { namespace tl {
template <class T, class E> class expected; template<class T, class E>
class expected;
#ifndef TL_MONOSTATE_INPLACE_MUTEX #ifndef TL_MONOSTATE_INPLACE_MUTEX
#define TL_MONOSTATE_INPLACE_MUTEX #define TL_MONOSTATE_INPLACE_MUTEX
/// \brief Used to represent an expected with no data /// \brief Used to represent an expected with no data
class monostate {}; class monostate {
};
/// \brief A tag type to tell expected to construct its value in-place /// \brief A tag type to tell expected to construct its value in-place
struct in_place_t { struct in_place_t {
explicit in_place_t() = default; explicit in_place_t() = default;
}; };
/// \brief A tag to tell expected to construct its value in-place /// \brief A tag to tell expected to construct its value in-place
static constexpr in_place_t in_place{}; static constexpr in_place_t in_place{};
#endif #endif
/// Used as a wrapper to store the unexpected value /// Used as a wrapper to store the unexpected value
template <class E> class unexpected { template<class E>
class unexpected {
public: public:
static_assert(!std::is_same<E, void>::value, "E must not be void"); static_assert(!std::is_same<E, void>::value, "E must not be void");
unexpected() = delete; unexpected() = delete;
constexpr unexpected(const E &e) : m_val(e) {} constexpr unexpected(const E &e) : m_val(e) {}
constexpr unexpected(E &&e) : m_val(std::move(e)) {} constexpr unexpected(E &&e) : m_val(std::move(e)) {}
@@ -160,6 +166,7 @@ public:
TL_EXPECTED_11_CONSTEXPR E &value() &{ return m_val; } TL_EXPECTED_11_CONSTEXPR E &value() &{ return m_val; }
/// \group unexpected_value /// \group unexpected_value
TL_EXPECTED_11_CONSTEXPR E &&value() &&{ return std::move(m_val); } TL_EXPECTED_11_CONSTEXPR E &&value() &&{ return std::move(m_val); }
/// \exclude /// \exclude
constexpr const E &&value() const &&{ return std::move(m_val); } constexpr const E &&value() const &&{ return std::move(m_val); }
@@ -174,26 +181,31 @@ template <class E>
constexpr bool operator==(const unexpected<E> &lhs, const unexpected<E> &rhs) { constexpr bool operator==(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() == rhs.value(); return lhs.value() == rhs.value();
} }
/// \group unexpected_relop /// \group unexpected_relop
template<class E> template<class E>
constexpr bool operator!=(const unexpected<E> &lhs, const unexpected<E> &rhs) { constexpr bool operator!=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() != rhs.value(); return lhs.value() != rhs.value();
} }
/// \group unexpected_relop /// \group unexpected_relop
template<class E> template<class E>
constexpr bool operator<(const unexpected<E> &lhs, const unexpected<E> &rhs) { constexpr bool operator<(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() < rhs.value(); return lhs.value() < rhs.value();
} }
/// \group unexpected_relop /// \group unexpected_relop
template<class E> template<class E>
constexpr bool operator<=(const unexpected<E> &lhs, const unexpected<E> &rhs) { constexpr bool operator<=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() <= rhs.value(); return lhs.value() <= rhs.value();
} }
/// \group unexpected_relop /// \group unexpected_relop
template<class E> template<class E>
constexpr bool operator>(const unexpected<E> &lhs, const unexpected<E> &rhs) { constexpr bool operator>(const unexpected<E> &lhs, const unexpected<E> &rhs) {
return lhs.value() > rhs.value(); return lhs.value() > rhs.value();
} }
/// \group unexpected_relop /// \group unexpected_relop
template<class E> template<class E>
constexpr bool operator>=(const unexpected<E> &lhs, const unexpected<E> &rhs) { constexpr bool operator>=(const unexpected<E> &lhs, const unexpected<E> &rhs) {
@@ -214,6 +226,7 @@ unexpected<typename std::decay<E>::type> make_unexpected(E &&e) {
struct unexpect_t { struct unexpect_t {
unexpect_t() = default; unexpect_t() = default;
}; };
/// \brief A tag to tell expected to construct the unexpected value /// \brief A tag to tell expected to construct the unexpected value
static constexpr unexpect_t unexpect{}; static constexpr unexpect_t unexpect{};
@@ -246,11 +259,16 @@ template <bool B, class T, class F>
using conditional_t = typename std::conditional<B, T, F>::type; using conditional_t = typename std::conditional<B, T, F>::type;
// std::conjunction from C++17 // std::conjunction from C++17
template <class...> struct conjunction : std::true_type {}; template<class...>
template <class B> struct conjunction<B> : B {}; struct conjunction : std::true_type {
};
template<class B>
struct conjunction<B> : B {
};
template<class B, class... Bs> template<class B, class... Bs>
struct conjunction<B, Bs...> struct conjunction<B, Bs...>
: std::conditional<bool(B::value), conjunction<Bs...>, B>::type {}; : std::conditional<bool(B::value), conjunction<Bs...>, B>::type {
};
// std::invoke from C++17 // std::invoke from C++17
// https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround // https://stackoverflow.com/questions/38288042/c11-14-invoke-workaround
@@ -272,7 +290,8 @@ constexpr auto invoke(Fn &&f, Args &&... args) noexcept(
} }
// std::invoke_result from C++17 // std::invoke_result from C++17
template <class F, class, class... Us> struct invoke_result_impl; template<class F, class, class... Us>
struct invoke_result_impl;
template<class F, class... Us> template<class F, class... Us>
struct invoke_result_impl< struct invoke_result_impl<
@@ -289,9 +308,12 @@ using invoke_result_t = typename invoke_result<F, Us...>::type;
#endif #endif
// Trait for checking if a type is a tl::expected // Trait for checking if a type is a tl::expected
template <class T> struct is_expected_impl : std::false_type {}; template<class T>
struct is_expected_impl : std::false_type {
};
template<class T, class E> template<class T, class E>
struct is_expected_impl<expected<T, E>> : std::true_type {}; struct is_expected_impl<expected<T, E>> : std::true_type {
};
template<class T> using is_expected = is_expected_impl<decay_t<T>>; template<class T> using is_expected = is_expected_impl<decay_t<T>>;
template<class T, class E, class U> template<class T, class E, class U>
@@ -339,7 +361,8 @@ using is_move_assignable_or_void =
/// \exclude /// \exclude
namespace detail { namespace detail {
struct no_init_t {}; struct no_init_t {
};
static constexpr no_init_t no_init{}; static constexpr no_init_t no_init{};
// Implements the storage of the values, and ensures that the destructor is // Implements the storage of the values, and ensures that the destructor is
@@ -352,6 +375,7 @@ template <class T, class E, bool = std::is_trivially_destructible<T>::value,
bool = std::is_trivially_destructible<E>::value> bool = std::is_trivially_destructible<E>::value>
struct expected_storage_base { struct expected_storage_base {
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {} constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {} constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {}
template<class... Args, template<class... Args,
@@ -366,6 +390,7 @@ struct expected_storage_base {
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il, constexpr expected_storage_base(in_place_t, std::initializer_list<U> il,
Args &&... args) Args &&... args)
: m_val(il, std::forward<Args>(args)...), m_has_val(true) {} : m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
template<class... Args, template<class... Args,
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
nullptr> nullptr>
@@ -387,6 +412,7 @@ struct expected_storage_base {
m_unexpect.~unexpected<E>(); m_unexpect.~unexpected<E>();
} }
} }
union { union {
T m_val; T m_val;
unexpected<E> m_unexpect; unexpected<E> m_unexpect;
@@ -397,8 +423,10 @@ struct expected_storage_base {
// This specialization is for when both `T` and `E` are trivially-destructible, // This specialization is for when both `T` and `E` are trivially-destructible,
// so the destructor of the `expected` can be trivial. // so the destructor of the `expected` can be trivial.
template <class T, class E> struct expected_storage_base<T, E, true, true> { template<class T, class E>
struct expected_storage_base<T, E, true, true> {
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {} constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {} constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {}
template<class... Args, template<class... Args,
@@ -413,6 +441,7 @@ template <class T, class E> struct expected_storage_base<T, E, true, true> {
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il, constexpr expected_storage_base(in_place_t, std::initializer_list<U> il,
Args &&... args) Args &&... args)
: m_val(il, std::forward<Args>(args)...), m_has_val(true) {} : m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
template<class... Args, template<class... Args,
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
nullptr> nullptr>
@@ -428,6 +457,7 @@ template <class T, class E> struct expected_storage_base<T, E, true, true> {
: m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {} : m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
~expected_storage_base() = default; ~expected_storage_base() = default;
union { union {
T m_val; T m_val;
unexpected<E> m_unexpect; unexpected<E> m_unexpect;
@@ -437,8 +467,10 @@ template <class T, class E> struct expected_storage_base<T, E, true, true> {
}; };
// T is trivial, E is not. // T is trivial, E is not.
template <class T, class E> struct expected_storage_base<T, E, true, false> { template<class T, class E>
struct expected_storage_base<T, E, true, false> {
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {} constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
TL_EXPECTED_MSVC2015_CONSTEXPR expected_storage_base(no_init_t) TL_EXPECTED_MSVC2015_CONSTEXPR expected_storage_base(no_init_t)
: m_no_init(), m_has_val(false) {} : m_no_init(), m_has_val(false) {}
@@ -454,6 +486,7 @@ template <class T, class E> struct expected_storage_base<T, E, true, false> {
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il, constexpr expected_storage_base(in_place_t, std::initializer_list<U> il,
Args &&... args) Args &&... args)
: m_val(il, std::forward<Args>(args)...), m_has_val(true) {} : m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
template<class... Args, template<class... Args,
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
nullptr> nullptr>
@@ -483,8 +516,10 @@ template <class T, class E> struct expected_storage_base<T, E, true, false> {
}; };
// E is trivial, T is not. // E is trivial, T is not.
template <class T, class E> struct expected_storage_base<T, E, false, true> { template<class T, class E>
struct expected_storage_base<T, E, false, true> {
constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {} constexpr expected_storage_base() : m_val(T{}), m_has_val(true) {}
constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {} constexpr expected_storage_base(no_init_t) : m_no_init(), m_has_val(false) {}
template<class... Args, template<class... Args,
@@ -499,6 +534,7 @@ template <class T, class E> struct expected_storage_base<T, E, false, true> {
constexpr expected_storage_base(in_place_t, std::initializer_list<U> il, constexpr expected_storage_base(in_place_t, std::initializer_list<U> il,
Args &&... args) Args &&... args)
: m_val(il, std::forward<Args>(args)...), m_has_val(true) {} : m_val(il, std::forward<Args>(args)...), m_has_val(true) {}
template<class... Args, template<class... Args,
detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * = detail::enable_if_t<std::is_constructible<E, Args &&...>::value> * =
nullptr> nullptr>
@@ -518,6 +554,7 @@ template <class T, class E> struct expected_storage_base<T, E, false, true> {
m_val.~T(); m_val.~T();
} }
} }
union { union {
T m_val; T m_val;
unexpected<E> m_unexpect; unexpected<E> m_unexpect;
@@ -527,8 +564,10 @@ template <class T, class E> struct expected_storage_base<T, E, false, true> {
}; };
// `T` is `void`, `E` is trivially-destructible // `T` is `void`, `E` is trivially-destructible
template <class E> struct expected_storage_base<void, E, false, true> { template<class E>
struct expected_storage_base<void, E, false, true> {
TL_EXPECTED_MSVC2015_CONSTEXPR expected_storage_base() : m_has_val(true) {} TL_EXPECTED_MSVC2015_CONSTEXPR expected_storage_base() : m_has_val(true) {}
constexpr expected_storage_base(no_init_t) : m_val(), m_has_val(false) {} constexpr expected_storage_base(no_init_t) : m_val(), m_has_val(false) {}
constexpr expected_storage_base(in_place_t) : m_has_val(true) {} constexpr expected_storage_base(in_place_t) : m_has_val(true) {}
@@ -548,7 +587,9 @@ template <class E> struct expected_storage_base<void, E, false, true> {
: m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {} : m_unexpect(il, std::forward<Args>(args)...), m_has_val(false) {}
~expected_storage_base() = default; ~expected_storage_base() = default;
struct dummy {};
struct dummy {
};
union { union {
unexpected<E> m_unexpect; unexpected<E> m_unexpect;
dummy m_val; dummy m_val;
@@ -557,8 +598,10 @@ template <class E> struct expected_storage_base<void, E, false, true> {
}; };
// `T` is `void`, `E` is not trivially-destructible // `T` is `void`, `E` is not trivially-destructible
template <class E> struct expected_storage_base<void, E, false, false> { template<class E>
struct expected_storage_base<void, E, false, false> {
constexpr expected_storage_base() : m_dummy(), m_has_val(true) {} constexpr expected_storage_base() : m_dummy(), m_has_val(true) {}
constexpr expected_storage_base(no_init_t) : m_dummy(), m_has_val(false) {} constexpr expected_storage_base(no_init_t) : m_dummy(), m_has_val(false) {}
constexpr expected_storage_base(in_place_t) : m_dummy(), m_has_val(true) {} constexpr expected_storage_base(in_place_t) : m_dummy(), m_has_val(true) {}
@@ -596,17 +639,20 @@ template <class T, class E>
struct expected_operations_base : expected_storage_base<T, E> { struct expected_operations_base : expected_storage_base<T, E> {
using expected_storage_base<T, E>::expected_storage_base; using expected_storage_base<T, E>::expected_storage_base;
template <class... Args> void construct(Args &&... args) noexcept { template<class... Args>
void construct(Args &&... args) noexcept {
new(std::addressof(this->m_val)) T(std::forward<Args>(args)...); new(std::addressof(this->m_val)) T(std::forward<Args>(args)...);
this->m_has_val = true; this->m_has_val = true;
} }
template <class Rhs> void construct_with(Rhs &&rhs) noexcept { template<class Rhs>
void construct_with(Rhs &&rhs) noexcept {
new(std::addressof(this->m_val)) T(std::forward<Rhs>(rhs).get()); new(std::addressof(this->m_val)) T(std::forward<Rhs>(rhs).get());
this->m_has_val = true; this->m_has_val = true;
} }
template <class... Args> void construct_error(Args &&... args) noexcept { template<class... Args>
void construct_error(Args &&... args) noexcept {
new(std::addressof(this->m_unexpect)) new(std::addressof(this->m_unexpect))
unexpected<E>(std::forward<Args>(args)...); unexpected<E>(std::forward<Args>(args)...);
this->m_has_val = false; this->m_has_val = false;
@@ -728,7 +774,8 @@ struct expected_operations_base : expected_storage_base<T, E> {
#endif #endif
// The common part of move/copy assigning // The common part of move/copy assigning
template <class Rhs> void assign_common(Rhs &&rhs) { template<class Rhs>
void assign_common(Rhs &&rhs) {
if (this->m_has_val) { if (this->m_has_val) {
if (rhs.m_has_val) { if (rhs.m_has_val) {
get() = std::forward<Rhs>(rhs).get(); get() = std::forward<Rhs>(rhs).get();
@@ -746,23 +793,33 @@ struct expected_operations_base : expected_storage_base<T, E> {
bool has_value() const { return this->m_has_val; } bool has_value() const { return this->m_has_val; }
TL_EXPECTED_11_CONSTEXPR T &get() &{ return this->m_val; } TL_EXPECTED_11_CONSTEXPR T &get() &{ return this->m_val; }
constexpr const T &get() const &{ return this->m_val; } constexpr const T &get() const &{ return this->m_val; }
TL_EXPECTED_11_CONSTEXPR T &&get() &&{ return std::move(this->m_val); } TL_EXPECTED_11_CONSTEXPR T &&get() &&{ return std::move(this->m_val); }
#ifndef TL_EXPECTED_NO_CONSTRR #ifndef TL_EXPECTED_NO_CONSTRR
constexpr const T &&get() const &&{ return std::move(this->m_val); } constexpr const T &&get() const &&{ return std::move(this->m_val); }
#endif #endif
TL_EXPECTED_11_CONSTEXPR unexpected<E> &geterr() &{ TL_EXPECTED_11_CONSTEXPR unexpected<E> &geterr() &{
return this->m_unexpect; return this->m_unexpect;
} }
constexpr const unexpected<E> &geterr() const &{ return this->m_unexpect; } constexpr const unexpected<E> &geterr() const &{ return this->m_unexpect; }
TL_EXPECTED_11_CONSTEXPR unexpected<E> &&geterr() &&{ TL_EXPECTED_11_CONSTEXPR unexpected<E> &&geterr() &&{
return std::move(this->m_unexpect); return std::move(this->m_unexpect);
} }
#ifndef TL_EXPECTED_NO_CONSTRR #ifndef TL_EXPECTED_NO_CONSTRR
constexpr const unexpected<E> &&geterr() const &&{ constexpr const unexpected<E> &&geterr() const &&{
return std::move(this->m_unexpect); return std::move(this->m_unexpect);
} }
#endif #endif
TL_EXPECTED_11_CONSTEXPR void destroy_val() { TL_EXPECTED_11_CONSTEXPR void destroy_val() {
@@ -776,21 +833,25 @@ template <class E>
struct expected_operations_base<void, E> : expected_storage_base<void, E> { struct expected_operations_base<void, E> : expected_storage_base<void, E> {
using expected_storage_base<void, E>::expected_storage_base; using expected_storage_base<void, E>::expected_storage_base;
template <class... Args> void construct() noexcept { this->m_has_val = true; } template<class... Args>
void construct() noexcept { this->m_has_val = true; }
// This function doesn't use its argument, but needs it so that code in // This function doesn't use its argument, but needs it so that code in
// levels above this can work independently of whether T is void // levels above this can work independently of whether T is void
template <class Rhs> void construct_with(Rhs &&) noexcept { template<class Rhs>
void construct_with(Rhs &&) noexcept {
this->m_has_val = true; this->m_has_val = true;
} }
template <class... Args> void construct_error(Args &&... args) noexcept { template<class... Args>
void construct_error(Args &&... args) noexcept {
new(std::addressof(this->m_unexpect)) new(std::addressof(this->m_unexpect))
unexpected<E>(std::forward<Args>(args)...); unexpected<E>(std::forward<Args>(args)...);
this->m_has_val = false; this->m_has_val = false;
} }
template <class Rhs> void assign(Rhs &&rhs) noexcept { template<class Rhs>
void assign(Rhs &&rhs) noexcept {
if (!this->m_has_val) { if (!this->m_has_val) {
if (rhs.m_has_val) { if (rhs.m_has_val) {
geterr().~unexpected<E>(); geterr().~unexpected<E>();
@@ -810,14 +871,19 @@ struct expected_operations_base<void, E> : expected_storage_base<void, E> {
TL_EXPECTED_11_CONSTEXPR unexpected<E> &geterr() &{ TL_EXPECTED_11_CONSTEXPR unexpected<E> &geterr() &{
return this->m_unexpect; return this->m_unexpect;
} }
constexpr const unexpected<E> &geterr() const &{ return this->m_unexpect; } constexpr const unexpected<E> &geterr() const &{ return this->m_unexpect; }
TL_EXPECTED_11_CONSTEXPR unexpected<E> &&geterr() &&{ TL_EXPECTED_11_CONSTEXPR unexpected<E> &&geterr() &&{
return std::move(this->m_unexpect); return std::move(this->m_unexpect);
} }
#ifndef TL_EXPECTED_NO_CONSTRR #ifndef TL_EXPECTED_NO_CONSTRR
constexpr const unexpected<E> &&geterr() const &&{ constexpr const unexpected<E> &&geterr() const &&{
return std::move(this->m_unexpect); return std::move(this->m_unexpect);
} }
#endif #endif
TL_EXPECTED_11_CONSTEXPR void destroy_val() { TL_EXPECTED_11_CONSTEXPR void destroy_val() {
@@ -840,6 +906,7 @@ struct expected_copy_base<T, E, false> : expected_operations_base<T, E> {
using expected_operations_base<T, E>::expected_operations_base; using expected_operations_base<T, E>::expected_operations_base;
expected_copy_base() = default; expected_copy_base() = default;
expected_copy_base(const expected_copy_base &rhs) expected_copy_base(const expected_copy_base &rhs)
: expected_operations_base<T, E>(no_init) { : expected_operations_base<T, E>(no_init) {
if (rhs.has_value()) { if (rhs.has_value()) {
@@ -850,7 +917,9 @@ struct expected_copy_base<T, E, false> : expected_operations_base<T, E> {
} }
expected_copy_base(expected_copy_base &&rhs) = default; expected_copy_base(expected_copy_base &&rhs) = default;
expected_copy_base &operator=(const expected_copy_base &rhs) = default; expected_copy_base &operator=(const expected_copy_base &rhs) = default;
expected_copy_base &operator=(expected_copy_base &&rhs) = default; expected_copy_base &operator=(expected_copy_base &&rhs) = default;
}; };
@@ -869,11 +938,13 @@ struct expected_move_base : expected_copy_base<T, E> {
#else #else
template <class T, class E, bool = false> struct expected_move_base; template <class T, class E, bool = false> struct expected_move_base;
#endif #endif
template<class T, class E> template<class T, class E>
struct expected_move_base<T, E, false> : expected_copy_base<T, E> { struct expected_move_base<T, E, false> : expected_copy_base<T, E> {
using expected_copy_base<T, E>::expected_copy_base; using expected_copy_base<T, E>::expected_copy_base;
expected_move_base() = default; expected_move_base() = default;
expected_move_base(const expected_move_base &rhs) = default; expected_move_base(const expected_move_base &rhs) = default;
expected_move_base(expected_move_base &&rhs) noexcept( expected_move_base(expected_move_base &&rhs) noexcept(
@@ -885,7 +956,9 @@ struct expected_move_base<T, E, false> : expected_copy_base<T, E> {
this->construct_error(std::move(rhs.geterr())); this->construct_error(std::move(rhs.geterr()));
} }
} }
expected_move_base &operator=(const expected_move_base &rhs) = default; expected_move_base &operator=(const expected_move_base &rhs) = default;
expected_move_base &operator=(expected_move_base &&rhs) = default; expected_move_base &operator=(expected_move_base &&rhs) = default;
}; };
@@ -907,13 +980,16 @@ struct expected_copy_assign_base<T, E, false> : expected_move_base<T, E> {
using expected_move_base<T, E>::expected_move_base; using expected_move_base<T, E>::expected_move_base;
expected_copy_assign_base() = default; expected_copy_assign_base() = default;
expected_copy_assign_base(const expected_copy_assign_base &rhs) = default; expected_copy_assign_base(const expected_copy_assign_base &rhs) = default;
expected_copy_assign_base(expected_copy_assign_base &&rhs) = default; expected_copy_assign_base(expected_copy_assign_base &&rhs) = default;
expected_copy_assign_base &operator=(const expected_copy_assign_base &rhs) { expected_copy_assign_base &operator=(const expected_copy_assign_base &rhs) {
this->assign(rhs); this->assign(rhs);
return *this; return *this;
} }
expected_copy_assign_base & expected_copy_assign_base &
operator=(expected_copy_assign_base &&rhs) = default; operator=(expected_copy_assign_base &&rhs) = default;
}; };
@@ -945,6 +1021,7 @@ struct expected_move_assign_base<T, E, false>
using expected_copy_assign_base<T, E>::expected_copy_assign_base; using expected_copy_assign_base<T, E>::expected_copy_assign_base;
expected_move_assign_base() = default; expected_move_assign_base() = default;
expected_move_assign_base(const expected_move_assign_base &rhs) = default; expected_move_assign_base(const expected_move_assign_base &rhs) = default;
expected_move_assign_base(expected_move_assign_base &&rhs) = default; expected_move_assign_base(expected_move_assign_base &&rhs) = default;
@@ -970,10 +1047,14 @@ template <class T, class E,
std::is_move_constructible<E>::value)> std::is_move_constructible<E>::value)>
struct expected_delete_ctor_base { struct expected_delete_ctor_base {
expected_delete_ctor_base() = default; expected_delete_ctor_base() = default;
expected_delete_ctor_base(const expected_delete_ctor_base &) = default; expected_delete_ctor_base(const expected_delete_ctor_base &) = default;
expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = default; expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = default;
expected_delete_ctor_base & expected_delete_ctor_base &
operator=(const expected_delete_ctor_base &) = default; operator=(const expected_delete_ctor_base &) = default;
expected_delete_ctor_base & expected_delete_ctor_base &
operator=(expected_delete_ctor_base &&) noexcept = default; operator=(expected_delete_ctor_base &&) noexcept = default;
}; };
@@ -981,10 +1062,14 @@ struct expected_delete_ctor_base {
template<class T, class E> template<class T, class E>
struct expected_delete_ctor_base<T, E, true, false> { struct expected_delete_ctor_base<T, E, true, false> {
expected_delete_ctor_base() = default; expected_delete_ctor_base() = default;
expected_delete_ctor_base(const expected_delete_ctor_base &) = default; expected_delete_ctor_base(const expected_delete_ctor_base &) = default;
expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = delete; expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = delete;
expected_delete_ctor_base & expected_delete_ctor_base &
operator=(const expected_delete_ctor_base &) = default; operator=(const expected_delete_ctor_base &) = default;
expected_delete_ctor_base & expected_delete_ctor_base &
operator=(expected_delete_ctor_base &&) noexcept = default; operator=(expected_delete_ctor_base &&) noexcept = default;
}; };
@@ -992,10 +1077,14 @@ struct expected_delete_ctor_base<T, E, true, false> {
template<class T, class E> template<class T, class E>
struct expected_delete_ctor_base<T, E, false, true> { struct expected_delete_ctor_base<T, E, false, true> {
expected_delete_ctor_base() = default; expected_delete_ctor_base() = default;
expected_delete_ctor_base(const expected_delete_ctor_base &) = delete; expected_delete_ctor_base(const expected_delete_ctor_base &) = delete;
expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = default; expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = default;
expected_delete_ctor_base & expected_delete_ctor_base &
operator=(const expected_delete_ctor_base &) = default; operator=(const expected_delete_ctor_base &) = default;
expected_delete_ctor_base & expected_delete_ctor_base &
operator=(expected_delete_ctor_base &&) noexcept = default; operator=(expected_delete_ctor_base &&) noexcept = default;
}; };
@@ -1003,10 +1092,14 @@ struct expected_delete_ctor_base<T, E, false, true> {
template<class T, class E> template<class T, class E>
struct expected_delete_ctor_base<T, E, false, false> { struct expected_delete_ctor_base<T, E, false, false> {
expected_delete_ctor_base() = default; expected_delete_ctor_base() = default;
expected_delete_ctor_base(const expected_delete_ctor_base &) = delete; expected_delete_ctor_base(const expected_delete_ctor_base &) = delete;
expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = delete; expected_delete_ctor_base(expected_delete_ctor_base &&) noexcept = delete;
expected_delete_ctor_base & expected_delete_ctor_base &
operator=(const expected_delete_ctor_base &) = default; operator=(const expected_delete_ctor_base &) = default;
expected_delete_ctor_base & expected_delete_ctor_base &
operator=(expected_delete_ctor_base &&) noexcept = default; operator=(expected_delete_ctor_base &&) noexcept = default;
}; };
@@ -1025,11 +1118,15 @@ template <class T, class E,
std::is_move_assignable<E>::value)> std::is_move_assignable<E>::value)>
struct expected_delete_assign_base { struct expected_delete_assign_base {
expected_delete_assign_base() = default; expected_delete_assign_base() = default;
expected_delete_assign_base(const expected_delete_assign_base &) = default; expected_delete_assign_base(const expected_delete_assign_base &) = default;
expected_delete_assign_base(expected_delete_assign_base &&) noexcept = expected_delete_assign_base(expected_delete_assign_base &&) noexcept =
default; default;
expected_delete_assign_base & expected_delete_assign_base &
operator=(const expected_delete_assign_base &) = default; operator=(const expected_delete_assign_base &) = default;
expected_delete_assign_base & expected_delete_assign_base &
operator=(expected_delete_assign_base &&) noexcept = default; operator=(expected_delete_assign_base &&) noexcept = default;
}; };
@@ -1037,11 +1134,15 @@ struct expected_delete_assign_base {
template<class T, class E> template<class T, class E>
struct expected_delete_assign_base<T, E, true, false> { struct expected_delete_assign_base<T, E, true, false> {
expected_delete_assign_base() = default; expected_delete_assign_base() = default;
expected_delete_assign_base(const expected_delete_assign_base &) = default; expected_delete_assign_base(const expected_delete_assign_base &) = default;
expected_delete_assign_base(expected_delete_assign_base &&) noexcept = expected_delete_assign_base(expected_delete_assign_base &&) noexcept =
default; default;
expected_delete_assign_base & expected_delete_assign_base &
operator=(const expected_delete_assign_base &) = default; operator=(const expected_delete_assign_base &) = default;
expected_delete_assign_base & expected_delete_assign_base &
operator=(expected_delete_assign_base &&) noexcept = delete; operator=(expected_delete_assign_base &&) noexcept = delete;
}; };
@@ -1049,11 +1150,15 @@ struct expected_delete_assign_base<T, E, true, false> {
template<class T, class E> template<class T, class E>
struct expected_delete_assign_base<T, E, false, true> { struct expected_delete_assign_base<T, E, false, true> {
expected_delete_assign_base() = default; expected_delete_assign_base() = default;
expected_delete_assign_base(const expected_delete_assign_base &) = default; expected_delete_assign_base(const expected_delete_assign_base &) = default;
expected_delete_assign_base(expected_delete_assign_base &&) noexcept = expected_delete_assign_base(expected_delete_assign_base &&) noexcept =
default; default;
expected_delete_assign_base & expected_delete_assign_base &
operator=(const expected_delete_assign_base &) = delete; operator=(const expected_delete_assign_base &) = delete;
expected_delete_assign_base & expected_delete_assign_base &
operator=(expected_delete_assign_base &&) noexcept = default; operator=(expected_delete_assign_base &&) noexcept = default;
}; };
@@ -1061,11 +1166,15 @@ struct expected_delete_assign_base<T, E, false, true> {
template<class T, class E> template<class T, class E>
struct expected_delete_assign_base<T, E, false, false> { struct expected_delete_assign_base<T, E, false, false> {
expected_delete_assign_base() = default; expected_delete_assign_base() = default;
expected_delete_assign_base(const expected_delete_assign_base &) = default; expected_delete_assign_base(const expected_delete_assign_base &) = default;
expected_delete_assign_base(expected_delete_assign_base &&) noexcept = expected_delete_assign_base(expected_delete_assign_base &&) noexcept =
default; default;
expected_delete_assign_base & expected_delete_assign_base &
operator=(const expected_delete_assign_base &) = delete; operator=(const expected_delete_assign_base &) = delete;
expected_delete_assign_base & expected_delete_assign_base &
operator=(expected_delete_assign_base &&) noexcept = delete; operator=(expected_delete_assign_base &&) noexcept = delete;
}; };
@@ -1084,12 +1193,16 @@ template <class T, class E,
std::is_default_constructible<T>::value || std::is_void<T>::value> std::is_default_constructible<T>::value || std::is_void<T>::value>
struct expected_default_ctor_base { struct expected_default_ctor_base {
constexpr expected_default_ctor_base() noexcept = default; constexpr expected_default_ctor_base() noexcept = default;
constexpr expected_default_ctor_base( constexpr expected_default_ctor_base(
expected_default_ctor_base const &) noexcept = default; expected_default_ctor_base const &) noexcept = default;
constexpr expected_default_ctor_base(expected_default_ctor_base &&) noexcept = constexpr expected_default_ctor_base(expected_default_ctor_base &&) noexcept =
default; default;
expected_default_ctor_base & expected_default_ctor_base &
operator=(expected_default_ctor_base const &) noexcept = default; operator=(expected_default_ctor_base const &) noexcept = default;
expected_default_ctor_base & expected_default_ctor_base &
operator=(expected_default_ctor_base &&) noexcept = default; operator=(expected_default_ctor_base &&) noexcept = default;
@@ -1097,14 +1210,19 @@ struct expected_default_ctor_base {
}; };
// This specialization is for when T is not default constructible // This specialization is for when T is not default constructible
template <class T, class E> struct expected_default_ctor_base<T, E, false> { template<class T, class E>
struct expected_default_ctor_base<T, E, false> {
constexpr expected_default_ctor_base() noexcept = delete; constexpr expected_default_ctor_base() noexcept = delete;
constexpr expected_default_ctor_base( constexpr expected_default_ctor_base(
expected_default_ctor_base const &) noexcept = default; expected_default_ctor_base const &) noexcept = default;
constexpr expected_default_ctor_base(expected_default_ctor_base &&) noexcept = constexpr expected_default_ctor_base(expected_default_ctor_base &&) noexcept =
default; default;
expected_default_ctor_base & expected_default_ctor_base &
operator=(expected_default_ctor_base const &) noexcept = default; operator=(expected_default_ctor_base const &) noexcept = default;
expected_default_ctor_base & expected_default_ctor_base &
operator=(expected_default_ctor_base &&) noexcept = default; operator=(expected_default_ctor_base &&) noexcept = default;
@@ -1112,7 +1230,8 @@ template <class T, class E> struct expected_default_ctor_base<T, E, false> {
}; };
} // namespace detail } // namespace detail
template <class E> class bad_expected_access : public std::exception { template<class E>
class bad_expected_access : public std::exception {
public: public:
explicit bad_expected_access(E e) : m_val(std::move(e)) {} explicit bad_expected_access(E e) : m_val(std::move(e)) {}
@@ -1121,8 +1240,11 @@ public:
} }
const E &error() const &{ return m_val; } const E &error() const &{ return m_val; }
E &error() &{ return m_val; } E &error() &{ return m_val; }
const E &&error() const &&{ return std::move(m_val); } const E &&error() const &&{ return std::move(m_val); }
E &&error() &&{ return std::move(m_val); } E &&error() &&{ return std::move(m_val); }
private: private:
@@ -1151,8 +1273,11 @@ class expected : private detail::expected_move_assign_base<T, E>,
static_assert(!std::is_reference<E>::value, "E must not be a reference"); static_assert(!std::is_reference<E>::value, "E must not be a reference");
T *valptr() { return std::addressof(this->m_val); } T *valptr() { return std::addressof(this->m_val); }
const T *valptr() const { return std::addressof(this->m_val); } const T *valptr() const { return std::addressof(this->m_val); }
unexpected<E> *errptr() { return std::addressof(this->m_unexpect); } unexpected<E> *errptr() { return std::addressof(this->m_unexpect); }
const unexpected<E> *errptr() const { return std::addressof(this->m_unexpect); } const unexpected<E> *errptr() const { return std::addressof(this->m_unexpect); }
template<class U = T, template<class U = T,
@@ -1160,6 +1285,7 @@ class expected : private detail::expected_move_assign_base<T, E>,
U &val() { U &val() {
return this->m_val; return this->m_val;
} }
unexpected<E> &err() { return this->m_unexpect; } unexpected<E> &err() { return this->m_unexpect; }
template<class U = T, template<class U = T,
@@ -1167,6 +1293,7 @@ class expected : private detail::expected_move_assign_base<T, E>,
const U &val() const { const U &val() const {
return this->m_val; return this->m_val;
} }
const unexpected<E> &err() const { return this->m_unexpect; } const unexpected<E> &err() const { return this->m_unexpect; }
using impl_base = detail::expected_move_assign_base<T, E>; using impl_base = detail::expected_move_assign_base<T, E>;
@@ -1179,6 +1306,7 @@ public:
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
!defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55)
/// \group and_then /// \group and_then
/// Carries out some operation which returns an expected on the stored object /// Carries out some operation which returns an expected on the stored object
/// if there is one. \requires `std::invoke(std::forward<F>(f), value())` /// if there is one. \requires `std::invoke(std::forward<F>(f), value())`
@@ -1188,28 +1316,34 @@ public:
/// otherwise the return value of `std::invoke(std::forward<F>(f), value())` /// otherwise the return value of `std::invoke(std::forward<F>(f), value())`
/// is returned. /// is returned.
/// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &; /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &;
template <class F> TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) & { template<class F>
TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) &{
return and_then_impl(*this, std::forward<F>(f)); return and_then_impl(*this, std::forward<F>(f));
} }
/// \group and_then /// \group and_then
/// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &&; /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) &&;
template <class F> TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) && { template<class F>
TL_EXPECTED_11_CONSTEXPR auto and_then(F &&f) &&{
return and_then_impl(std::move(*this), std::forward<F>(f)); return and_then_impl(std::move(*this), std::forward<F>(f));
} }
/// \group and_then /// \group and_then
/// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &; /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &;
template <class F> constexpr auto and_then(F &&f) const & { template<class F>
constexpr auto and_then(F &&f) const &{
return and_then_impl(*this, std::forward<F>(f)); return and_then_impl(*this, std::forward<F>(f));
} }
#ifndef TL_EXPECTED_NO_CONSTRR #ifndef TL_EXPECTED_NO_CONSTRR
/// \group and_then /// \group and_then
/// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &&; /// \synopsis template <class F>\nconstexpr auto and_then(F &&f) const &&;
template <class F> constexpr auto and_then(F &&f) const && { template<class F>
constexpr auto and_then(F &&f) const &&{
return and_then_impl(std::move(*this), std::forward<F>(f)); return and_then_impl(std::move(*this), std::forward<F>(f));
} }
#endif #endif
#else #else
@@ -1257,6 +1391,7 @@ public:
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
!defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55)
/// \brief Carries out some operation on the stored object if there is one. /// \brief Carries out some operation on the stored object if there is one.
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f), /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
/// value())`. If `U` is `void`, returns an `expected<monostate,E>, otherwise /// value())`. If `U` is `void`, returns an `expected<monostate,E>, otherwise
@@ -1267,27 +1402,32 @@ public:
/// ///
/// \group map /// \group map
/// \synopsis template <class F> constexpr auto map(F &&f) &; /// \synopsis template <class F> constexpr auto map(F &&f) &;
template <class F> TL_EXPECTED_11_CONSTEXPR auto map(F &&f) & { template<class F>
TL_EXPECTED_11_CONSTEXPR auto map(F &&f) &{
return expected_map_impl(*this, std::forward<F>(f)); return expected_map_impl(*this, std::forward<F>(f));
} }
/// \group map /// \group map
/// \synopsis template <class F> constexpr auto map(F &&f) &&; /// \synopsis template <class F> constexpr auto map(F &&f) &&;
template <class F> TL_EXPECTED_11_CONSTEXPR auto map(F &&f) && { template<class F>
TL_EXPECTED_11_CONSTEXPR auto map(F &&f) &&{
return expected_map_impl(std::move(*this), std::forward<F>(f)); return expected_map_impl(std::move(*this), std::forward<F>(f));
} }
/// \group map /// \group map
/// \synopsis template <class F> constexpr auto map(F &&f) const &; /// \synopsis template <class F> constexpr auto map(F &&f) const &;
template <class F> constexpr auto map(F &&f) const & { template<class F>
constexpr auto map(F &&f) const &{
return expected_map_impl(*this, std::forward<F>(f)); return expected_map_impl(*this, std::forward<F>(f));
} }
/// \group map /// \group map
/// \synopsis template <class F> constexpr auto map(F &&f) const &&; /// \synopsis template <class F> constexpr auto map(F &&f) const &&;
template <class F> constexpr auto map(F &&f) const && { template<class F>
constexpr auto map(F &&f) const &&{
return expected_map_impl(std::move(*this), std::forward<F>(f)); return expected_map_impl(std::move(*this), std::forward<F>(f));
} }
#else #else
/// \brief Carries out some operation on the stored object if there is one. /// \brief Carries out some operation on the stored object if there is one.
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f), /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
@@ -1338,6 +1478,7 @@ public:
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
!defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55)
/// \brief Carries out some operation on the stored unexpected object if there /// \brief Carries out some operation on the stored unexpected object if there
/// is one. /// is one.
/// \returns Let `U` be the result of `std::invoke(std::forward<F>(f), /// \returns Let `U` be the result of `std::invoke(std::forward<F>(f),
@@ -1349,27 +1490,32 @@ public:
/// ///
/// \group map_error /// \group map_error
/// \synopsis template <class F> constexpr auto map_error(F &&f) &; /// \synopsis template <class F> constexpr auto map_error(F &&f) &;
template <class F> TL_EXPECTED_11_CONSTEXPR auto map_error(F &&f) & { template<class F>
TL_EXPECTED_11_CONSTEXPR auto map_error(F &&f) &{
return map_error_impl(*this, std::forward<F>(f)); return map_error_impl(*this, std::forward<F>(f));
} }
/// \group map_error /// \group map_error
/// \synopsis template <class F> constexpr auto map_error(F &&f) &&; /// \synopsis template <class F> constexpr auto map_error(F &&f) &&;
template <class F> TL_EXPECTED_11_CONSTEXPR auto map_error(F &&f) && { template<class F>
TL_EXPECTED_11_CONSTEXPR auto map_error(F &&f) &&{
return map_error_impl(std::move(*this), std::forward<F>(f)); return map_error_impl(std::move(*this), std::forward<F>(f));
} }
/// \group map_error /// \group map_error
/// \synopsis template <class F> constexpr auto map_error(F &&f) const &; /// \synopsis template <class F> constexpr auto map_error(F &&f) const &;
template <class F> constexpr auto map_error(F &&f) const & { template<class F>
constexpr auto map_error(F &&f) const &{
return map_error_impl(*this, std::forward<F>(f)); return map_error_impl(*this, std::forward<F>(f));
} }
/// \group map_error /// \group map_error
/// \synopsis template <class F> constexpr auto map_error(F &&f) const &&; /// \synopsis template <class F> constexpr auto map_error(F &&f) const &&;
template <class F> constexpr auto map_error(F &&f) const && { template<class F>
constexpr auto map_error(F &&f) const &&{
return map_error_impl(std::move(*this), std::forward<F>(f)); return map_error_impl(std::move(*this), std::forward<F>(f));
} }
#else #else
/// \brief Carries out some operation on the stored unexpected object if there /// \brief Carries out some operation on the stored unexpected object if there
/// is one. /// is one.
@@ -1426,27 +1572,38 @@ public:
/// `std::nullopt`. Otherwise, returns `std::forward<F>(f)(E)`. /// `std::nullopt`. Otherwise, returns `std::forward<F>(f)(E)`.
/// ///
/// \group or_else /// \group or_else
template <class F> expected TL_EXPECTED_11_CONSTEXPR or_else(F &&f) & { template<class F>
expected TL_EXPECTED_11_CONSTEXPR or_else(F &&f) &{
return or_else_impl(*this, std::forward<F>(f)); return or_else_impl(*this, std::forward<F>(f));
} }
template <class F> expected TL_EXPECTED_11_CONSTEXPR or_else(F &&f) && { template<class F>
expected TL_EXPECTED_11_CONSTEXPR or_else(F &&f) &&{
return or_else_impl(std::move(*this), std::forward<F>(f)); return or_else_impl(std::move(*this), std::forward<F>(f));
} }
template <class F> expected constexpr or_else(F &&f) const & { template<class F>
expected constexpr or_else(F &&f) const &{
return or_else_impl(*this, std::forward<F>(f)); return or_else_impl(*this, std::forward<F>(f));
} }
#ifndef TL_EXPECTED_NO_CONSTRR #ifndef TL_EXPECTED_NO_CONSTRR
template <class F> expected constexpr or_else(F &&f) const && {
template<class F>
expected constexpr or_else(F &&f) const &&{
return or_else_impl(std::move(*this), std::forward<F>(f)); return or_else_impl(std::move(*this), std::forward<F>(f));
} }
#endif #endif
constexpr expected() = default; constexpr expected() = default;
constexpr expected(const expected &rhs) = default; constexpr expected(const expected &rhs) = default;
constexpr expected(expected &&rhs) = default; constexpr expected(expected &&rhs) = default;
expected &operator=(const expected &rhs) = default; expected &operator=(const expected &rhs) = default;
expected &operator=(expected &&rhs) = default; expected &operator=(expected &&rhs) = default;
template<class... Args, template<class... Args,
@@ -1805,18 +1962,21 @@ public:
constexpr const U &operator*() const &{ constexpr const U &operator*() const &{
return val(); return val();
} }
/// \group deref /// \group deref
template<class U = T, template<class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
TL_EXPECTED_11_CONSTEXPR U &operator*() &{ TL_EXPECTED_11_CONSTEXPR U &operator*() &{
return val(); return val();
} }
/// \group deref /// \group deref
template<class U = T, template<class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
constexpr const U &&operator*() const &&{ constexpr const U &&operator*() const &&{
return std::move(val()); return std::move(val());
} }
/// \group deref /// \group deref
template<class U = T, template<class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
@@ -1827,6 +1987,7 @@ public:
/// \returns whether or not the optional has a value /// \returns whether or not the optional has a value
/// \group has_value /// \group has_value
constexpr bool has_value() const noexcept { return this->m_has_val; } constexpr bool has_value() const noexcept { return this->m_has_val; }
/// \group has_value /// \group has_value
constexpr explicit operator bool() const noexcept { return this->m_has_val; } constexpr explicit operator bool() const noexcept { return this->m_has_val; }
@@ -1841,6 +2002,7 @@ public:
detail::throw_exception(bad_expected_access<E>(err().value())); detail::throw_exception(bad_expected_access<E>(err().value()));
return val(); return val();
} }
/// \group value /// \group value
template<class U = T, template<class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
@@ -1849,6 +2011,7 @@ public:
detail::throw_exception(bad_expected_access<E>(err().value())); detail::throw_exception(bad_expected_access<E>(err().value()));
return val(); return val();
} }
/// \group value /// \group value
template<class U = T, template<class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
@@ -1857,6 +2020,7 @@ public:
detail::throw_exception(bad_expected_access<E>(err().value())); detail::throw_exception(bad_expected_access<E>(err().value()));
return std::move(val()); return std::move(val());
} }
/// \group value /// \group value
template<class U = T, template<class U = T,
detail::enable_if_t<!std::is_void<U>::value> * = nullptr> detail::enable_if_t<!std::is_void<U>::value> * = nullptr>
@@ -1872,6 +2036,7 @@ public:
constexpr const E &error() const &{ return err().value(); } constexpr const E &error() const &{ return err().value(); }
/// \group error /// \group error
TL_EXPECTED_11_CONSTEXPR E &error() &{ return err().value(); } TL_EXPECTED_11_CONSTEXPR E &error() &{ return err().value(); }
/// \group error /// \group error
constexpr const E &&error() const &&{ return std::move(err().value()); } constexpr const E &&error() const &&{ return std::move(err().value()); }
/// \group error /// \group error
@@ -1879,14 +2044,17 @@ public:
/// \returns the stored value if there is one, otherwise returns `u` /// \returns the stored value if there is one, otherwise returns `u`
/// \group value_or /// \group value_or
template <class U> constexpr T value_or(U &&v) const & { template<class U>
constexpr T value_or(U &&v) const &{
static_assert(std::is_copy_constructible<T>::value && static_assert(std::is_copy_constructible<T>::value &&
std::is_convertible<U &&, T>::value, std::is_convertible<U &&, T>::value,
"T must be copy-constructible and convertible to from U&&"); "T must be copy-constructible and convertible to from U&&");
return bool(*this) ? **this : static_cast<T>(std::forward<U>(v)); return bool(*this) ? **this : static_cast<T>(std::forward<U>(v));
} }
/// \group value_or /// \group value_or
template <class U> TL_EXPECTED_11_CONSTEXPR T value_or(U &&v) && { template<class U>
TL_EXPECTED_11_CONSTEXPR T value_or(U &&v) &&{
static_assert(std::is_move_constructible<T>::value && static_assert(std::is_move_constructible<T>::value &&
std::is_convertible<U &&, T>::value, std::is_convertible<U &&, T>::value,
"T must be move-constructible and convertible to from U&&"); "T must be move-constructible and convertible to from U&&");
@@ -1901,6 +2069,7 @@ template <class Exp> using err_t = typename detail::decay_t<Exp>::error_type;
template<class Exp, class Ret> using ret_t = expected<Ret, err_t<Exp>>; template<class Exp, class Ret> using ret_t = expected<Ret, err_t<Exp>>;
#ifdef TL_EXPECTED_CXX14 #ifdef TL_EXPECTED_CXX14
template<class Exp, class F, template<class Exp, class F,
detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr, detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr,
class Ret = decltype(detail::invoke(std::declval<F>(), class Ret = decltype(detail::invoke(std::declval<F>(),
@@ -1922,6 +2091,7 @@ constexpr auto and_then_impl(Exp &&exp, F &&f) {
return exp.has_value() ? detail::invoke(std::forward<F>(f)) return exp.has_value() ? detail::invoke(std::forward<F>(f))
: Ret(unexpect, std::forward<Exp>(exp).error()); : Ret(unexpect, std::forward<Exp>(exp).error());
} }
#else #else
template <class> struct TC; template <class> struct TC;
template <class Exp, class F, template <class Exp, class F,
@@ -1948,6 +2118,7 @@ constexpr auto and_then_impl(Exp &&exp, F &&f) -> Ret {
#endif #endif
#ifdef TL_EXPECTED_CXX14 #ifdef TL_EXPECTED_CXX14
template<class Exp, class F, template<class Exp, class F,
detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr, detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr,
class Ret = decltype(detail::invoke(std::declval<F>(), class Ret = decltype(detail::invoke(std::declval<F>(),
@@ -1998,6 +2169,7 @@ auto expected_map_impl(Exp &&exp, F &&f) {
return result(unexpect, std::forward<Exp>(exp).error()); return result(unexpect, std::forward<Exp>(exp).error());
} }
#else #else
template <class Exp, class F, template <class Exp, class F,
detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr, detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr,
@@ -2059,6 +2231,7 @@ auto expected_map_impl(Exp &&exp, F &&f) -> expected<void, err_t<Exp>> {
#if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \ #if defined(TL_EXPECTED_CXX14) && !defined(TL_EXPECTED_GCC49) && \
!defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55) !defined(TL_EXPECTED_GCC54) && !defined(TL_EXPECTED_GCC55)
template<class Exp, class F, template<class Exp, class F,
detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr, detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr,
class Ret = decltype(detail::invoke(std::declval<F>(), class Ret = decltype(detail::invoke(std::declval<F>(),
@@ -2071,6 +2244,7 @@ constexpr auto map_error_impl(Exp &&exp, F &&f) {
: result(unexpect, detail::invoke(std::forward<F>(f), : result(unexpect, detail::invoke(std::forward<F>(f),
std::forward<Exp>(exp).error())); std::forward<Exp>(exp).error()));
} }
template<class Exp, class F, template<class Exp, class F,
detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr, detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr,
class Ret = decltype(detail::invoke(std::declval<F>(), class Ret = decltype(detail::invoke(std::declval<F>(),
@@ -2085,6 +2259,7 @@ auto map_error_impl(Exp &&exp, F &&f) {
detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error()); detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error());
return result(unexpect, monostate{}); return result(unexpect, monostate{});
} }
template<class Exp, class F, template<class Exp, class F,
detail::enable_if_t<std::is_void<exp_t<Exp>>::value> * = nullptr, detail::enable_if_t<std::is_void<exp_t<Exp>>::value> * = nullptr,
class Ret = decltype(detail::invoke(std::declval<F>(), class Ret = decltype(detail::invoke(std::declval<F>(),
@@ -2097,6 +2272,7 @@ constexpr auto map_error_impl(Exp &&exp, F &&f) {
: result(unexpect, detail::invoke(std::forward<F>(f), : result(unexpect, detail::invoke(std::forward<F>(f),
std::forward<Exp>(exp).error())); std::forward<Exp>(exp).error()));
} }
template<class Exp, class F, template<class Exp, class F,
detail::enable_if_t<std::is_void<exp_t<Exp>>::value> * = nullptr, detail::enable_if_t<std::is_void<exp_t<Exp>>::value> * = nullptr,
class Ret = decltype(detail::invoke(std::declval<F>(), class Ret = decltype(detail::invoke(std::declval<F>(),
@@ -2111,6 +2287,7 @@ auto map_error_impl(Exp &&exp, F &&f) {
detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error()); detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error());
return result(unexpect, monostate{}); return result(unexpect, monostate{});
} }
#else #else
template <class Exp, class F, template <class Exp, class F,
detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr, detail::enable_if_t<!std::is_void<exp_t<Exp>>::value> * = nullptr,
@@ -2174,6 +2351,7 @@ auto map_error_impl(Exp &&exp, F &&f) -> expected<exp_t<Exp>, monostate> {
#endif #endif
#ifdef TL_EXPECTED_CXX14 #ifdef TL_EXPECTED_CXX14
template<class Exp, class F, template<class Exp, class F,
class Ret = decltype(detail::invoke(std::declval<F>(), class Ret = decltype(detail::invoke(std::declval<F>(),
std::declval<Exp>().error())), std::declval<Exp>().error())),
@@ -2195,6 +2373,7 @@ detail::decay_t<Exp> or_else_impl(Exp &&exp, F &&f) {
: (detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error()), : (detail::invoke(std::forward<F>(f), std::forward<Exp>(exp).error()),
std::forward<Exp>(exp)); std::forward<Exp>(exp));
} }
#else #else
template <class Exp, class F, template <class Exp, class F,
class Ret = decltype(detail::invoke(std::declval<F>(), class Ret = decltype(detail::invoke(std::declval<F>(),
@@ -2227,6 +2406,7 @@ constexpr bool operator==(const expected<T, E> &lhs,
? false ? false
: (!lhs.has_value() ? lhs.error() == rhs.error() : *lhs == *rhs); : (!lhs.has_value() ? lhs.error() == rhs.error() : *lhs == *rhs);
} }
template<class T, class E, class U, class F> template<class T, class E, class U, class F>
constexpr bool operator!=(const expected<T, E> &lhs, constexpr bool operator!=(const expected<T, E> &lhs,
const expected<U, F> &rhs) { const expected<U, F> &rhs) {
@@ -2239,14 +2419,17 @@ template <class T, class E, class U>
constexpr bool operator==(const expected<T, E> &x, const U &v) { constexpr bool operator==(const expected<T, E> &x, const U &v) {
return x.has_value() ? *x == v : false; return x.has_value() ? *x == v : false;
} }
template<class T, class E, class U> template<class T, class E, class U>
constexpr bool operator==(const U &v, const expected<T, E> &x) { constexpr bool operator==(const U &v, const expected<T, E> &x) {
return x.has_value() ? *x == v : false; return x.has_value() ? *x == v : false;
} }
template<class T, class E, class U> template<class T, class E, class U>
constexpr bool operator!=(const expected<T, E> &x, const U &v) { constexpr bool operator!=(const expected<T, E> &x, const U &v) {
return x.has_value() ? *x != v : true; return x.has_value() ? *x != v : true;
} }
template<class T, class E, class U> template<class T, class E, class U>
constexpr bool operator!=(const U &v, const expected<T, E> &x) { constexpr bool operator!=(const U &v, const expected<T, E> &x) {
return x.has_value() ? *x != v : true; return x.has_value() ? *x != v : true;
@@ -2256,14 +2439,17 @@ template <class T, class E>
constexpr bool operator==(const expected<T, E> &x, const unexpected<E> &e) { constexpr bool operator==(const expected<T, E> &x, const unexpected<E> &e) {
return x.has_value() ? false : x.error() == e.value(); return x.has_value() ? false : x.error() == e.value();
} }
template<class T, class E> template<class T, class E>
constexpr bool operator==(const unexpected<E> &e, const expected<T, E> &x) { constexpr bool operator==(const unexpected<E> &e, const expected<T, E> &x) {
return x.has_value() ? false : x.error() == e.value(); return x.has_value() ? false : x.error() == e.value();
} }
template<class T, class E> template<class T, class E>
constexpr bool operator!=(const expected<T, E> &x, const unexpected<E> &e) { constexpr bool operator!=(const expected<T, E> &x, const unexpected<E> &e) {
return x.has_value() ? true : x.error() != e.value(); return x.has_value() ? true : x.error() != e.value();
} }
template<class T, class E> template<class T, class E>
constexpr bool operator!=(const unexpected<E> &e, const expected<T, E> &x) { constexpr bool operator!=(const unexpected<E> &e, const expected<T, E> &x) {
return x.has_value() ? true : x.error() != e.value(); return x.has_value() ? true : x.error() != e.value();
@@ -1,4 +1,4 @@
#include "parsecontext.h" #include "parse_context.h"
#include "assert.h" #include "assert.h"
#include <iostream> #include <iostream>
@@ -6,26 +6,24 @@
#include <cmath> #include <cmath>
ParseContext::ParseContext() ParseContext::ParseContext() {
{
applicationDir = std::string(); applicationDir = std::string();
} }
unsigned ParseContext::addFile(const std::string &fileName, unsigned ParseContext::addFile(const std::string &fileName,
const std::string& source) const std::string &source) {
{ fileMap.emplace_back(fileName, source);
fileMap.push_back(SourceFile(fileName, source));
return fileMap.size(); return fileMap.size();
} }
SourceFile& ParseContext::getFile(unsigned fileId) SourceFile &ParseContext::getFile(unsigned fileId) {
{
return fileMap[fileId - 1]; return fileMap[fileId - 1];
} }
struct pad { struct pad {
char ch; int count; char ch;
int count;
pad(char ch, int count) : ch(ch), count(count) {} pad(char ch, int count) : ch(ch), count(count) {}
}; };
@@ -42,14 +40,11 @@ std::ostream& operator<<(std::ostream& stream, const pad& p) {
void ParseContext::formatError(const SourceError &sourceError, void ParseContext::formatError(const SourceError &sourceError,
std::ostream &stream, std::ostream &stream,
const std::string& ErrorOrWarning) const std::string &errorOrWarning) {
{
ATLAS_ASSERT(sourceError.span.lo.col != 0 ATLAS_ASSERT(sourceError.span.lo.col != 0
&& sourceError.span.hi.col != 0); && sourceError.span.hi.col != 0);
SourceFile sourceFile = getFile(sourceError.span.lo.fileId); stream << errorOrWarning << sourceError.message << std::endl;
stream << ErrorOrWarning << sourceError.message << std::endl;
Span span = sourceError.span; Span span = sourceError.span;
@@ -100,20 +95,16 @@ void ParseContext::formatError(const SourceError& sourceError,
} }
SourceFile::SourceFile(std::string fileName, std::string source) SourceFile::SourceFile(std::string fileName, std::string source)
: fileName(std::move(fileName)), source(std::move(source)) : fileName(std::move(fileName)), source(std::move(source)) {
{
lineOffsets.push_back(0); lineOffsets.push_back(0);
} }
const std::string& SourceFile::getFileName() const const std::string &SourceFile::getFileName() const {
{
return fileName; return fileName;
} }
const std::string SourceFile::getLine(unsigned line) const std::string SourceFile::getLine(unsigned line) const {
{
auto lineOffset = lineOffsets[line - 1]; auto lineOffset = lineOffsets[line - 1];
auto nextLF = source.find('\n', lineOffset); auto nextLF = source.find('\n', lineOffset);
auto nextCR = source.find('\r', lineOffset); auto nextCR = source.find('\r', lineOffset);
@@ -122,7 +113,6 @@ const std::string SourceFile::getLine(unsigned line) const
return source.substr(lineOffset, nl - lineOffset); return source.substr(lineOffset, nl - lineOffset);
} }
void SourceFile::addLineOffset(unsigned offset) void SourceFile::addLineOffset(unsigned offset) {
{
lineOffsets.push_back(offset); lineOffsets.push_back(offset);
} }
@@ -2,30 +2,28 @@
#define PARSE_CONTEXT_H #define PARSE_CONTEXT_H
#include "sourceerror.h" #include "source_error.h"
#include <string> #include <string>
#include <vector> #include <vector>
class SourceFile { class SourceFile {
std::string fileName; std::string fileName;
std::string source; std::string source;
std::vector<unsigned> lineOffsets; std::vector<unsigned> lineOffsets;
public: public:
// qqq source je CIJELI comdel fajl,
// za inicijalizaciju source se koristi std::move, a možda treba biti referenca???
SourceFile(std::string fileName, std::string source); SourceFile(std::string fileName, std::string source);
const std::string &getFileName() const; const std::string &getFileName() const;
const std::string getLine(unsigned line) const;
std::string getLine(unsigned line) const;
void addLineOffset(unsigned offset); void addLineOffset(unsigned offset);
}; };
class ParseContext class ParseContext {
{
std::string applicationDir; std::string applicationDir;
std::vector<SourceFile> fileMap; std::vector<SourceFile> fileMap;
@@ -33,9 +31,10 @@ public:
ParseContext(); ParseContext();
unsigned addFile(const std::string &fileName, const std::string &source); unsigned addFile(const std::string &fileName, const std::string &source);
SourceFile &getFile(unsigned file); SourceFile &getFile(unsigned file);
void formatError(const SourceError &sourceError, std::ostream &stream, const std::string& ErrorOrWarning); void formatError(const SourceError &sourceError, std::ostream &stream, const std::string &errorOrWarning);
}; };
@@ -1,16 +1,15 @@
#include "comdellexer.h" #include "comdel_lexer.h"
#include "comdelparser.h" #include "comdel_parser.h"
#include "parserutil.h" #include "parser_util.h"
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <QDir> #include <QDir>
std::optional<LibraryNode> loadLibraryFromFile(ParseContext * parseContext, std::optional<LibraryNode> load_library_from_file(ParseContext *parseContext,
const char *name, const char *name,
std::ostream& stream) std::ostream &stream) {
{
std::ifstream in(name, std::ios::in | std::ios::binary); std::ifstream in(name, std::ios::in | std::ios::binary);
if (!in) { if (!in) {
stream << "ERROR: cannot open file '" << name stream << "ERROR: cannot open file '" << name
@@ -23,15 +22,15 @@ std::optional<LibraryNode> loadLibraryFromFile(ParseContext * parseContext,
ComdelLexer lexer(name, source, parseContext); ComdelLexer lexer(name, source, parseContext);
LexerResult lexerResult = lexer.tokenize(); LexerResult lexerResult = lexer.tokenize();
if (lexerResult.errors.size()) { if (!lexerResult.errors.empty()) {
for (auto &error: lexerResult.errors) { for (auto &error: lexerResult.errors) {
parseContext->formatError(error, stream, "ERROR: "); parseContext->formatError(error, stream, "ERROR: ");
} }
return std::nullopt; // if lexer has found errors => don't parse return std::nullopt; // if lexer has found errors => don't parseLibrary
} }
ComdelParser parser(lexerResult.tokens); ComdelParser parser(lexerResult.tokens);
auto unit = parser.parse(); auto unit = parser.parseLibrary();
if (!unit) { if (!unit) {
for (auto &error: parser.getErrors()) { for (auto &error: parser.getErrors()) {
parseContext->formatError(error, stream, "ERROR: "); parseContext->formatError(error, stream, "ERROR: ");
@@ -43,10 +42,9 @@ std::optional<LibraryNode> loadLibraryFromFile(ParseContext * parseContext,
} }
std::optional<SchemaNode> loadSchemaFromFile(ParseContext * parseContext, std::optional<SchemaNode> load_schema_from_file(ParseContext *parseContext,
const char *name, const char *name,
std::ostream& stream) std::ostream &stream) {
{
std::ifstream in(name, std::ios::in | std::ios::binary); std::ifstream in(name, std::ios::in | std::ios::binary);
if (!in) { if (!in) {
stream << "ERROR: cannot open file '" << name stream << "ERROR: cannot open file '" << name
@@ -59,11 +57,11 @@ std::optional<SchemaNode> loadSchemaFromFile(ParseContext * parseContext,
ComdelLexer lexer(name, source, parseContext); ComdelLexer lexer(name, source, parseContext);
LexerResult lexerResult = lexer.tokenize(); LexerResult lexerResult = lexer.tokenize();
if (lexerResult.errors.size()) { if (!lexerResult.errors.empty()) {
for (auto &error: lexerResult.errors) { for (auto &error: lexerResult.errors) {
parseContext->formatError(error, stream, "ERROR: "); parseContext->formatError(error, stream, "ERROR: ");
} }
return std::nullopt; // if lexer has found errors => don't parse return std::nullopt; // if lexer has found errors => don't parseLibrary
} }
ComdelParser parser(lexerResult.tokens); ComdelParser parser(lexerResult.tokens);
@@ -78,12 +76,12 @@ std::optional<SchemaNode> loadSchemaFromFile(ParseContext * parseContext,
if (unit->source) { if (unit->source) {
QFileInfo info(name); QFileInfo info(name);
auto filepath = info.absolutePath(); auto filepath = info.absolutePath();
QDir dir; QDir::setCurrent(filepath);
dir.setCurrent(filepath);
QDir dir;
auto libraryPath = dir.absoluteFilePath(QString::fromStdString(unit->source->asString())); auto libraryPath = dir.absoluteFilePath(QString::fromStdString(unit->source->asString()));
unit->library = loadLibraryFromFile(parseContext, libraryPath.toUtf8().data(), stream); unit->library = load_library_from_file(parseContext, libraryPath.toUtf8().data(), stream);
if (unit->library == std::nullopt) { if (unit->library == std::nullopt) {
return std::nullopt; return std::nullopt;
} }
+15
View File
@@ -0,0 +1,15 @@
#ifndef PARSER_UTIL_H
#define PARSER_UTIL_H
#include "ast_nodes.h"
#include "parse_context.h"
std::optional<LibraryNode> load_library_from_file(ParseContext *parseContext,
const char *name,
std::ostream &stream);
std::optional<SchemaNode> load_schema_from_file(ParseContext *parseContext,
const char *name,
std::ostream &stream);
#endif // PARSER_UTIL_H
-15
View File
@@ -1,15 +0,0 @@
#ifndef PARSER_UTIL_H
#define PARSER_UTIL_H
#include "astnode.h"
#include "parsecontext.h"
std::optional<LibraryNode> loadLibraryFromFile(ParseContext * parseContext,
const char* name,
std::ostream& stream);
std::optional<SchemaNode> loadSchemaFromFile(ParseContext * parseContext,
const char* name,
std::ostream& stream);
#endif // PARSERUTIL_H
+53 -97
View File
@@ -30,34 +30,28 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <type_traits> #include <type_traits>
#include <typeinfo> #include <typeinfo>
namespace detail namespace detail {
{
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Implementation detail classes // Implementation detail classes
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
template<class T> template<class T>
struct default_copy struct default_copy {
{ T *operator()(const T &t) const {
T* operator()(const T& t) const
{
return new T(t); return new T(t);
} }
}; };
template<class T> template<class T>
struct default_delete struct default_delete {
{ void operator()(const T *t) const {
void operator()(const T* t) const
{
delete t; delete t;
} }
}; };
template<class T> template<class T>
struct control_block struct control_block {
{
virtual ~control_block() = default; virtual ~control_block() = default;
virtual std::unique_ptr<control_block> clone() const = 0; virtual std::unique_ptr<control_block> clone() const = 0;
@@ -66,76 +60,63 @@ namespace detail
}; };
template<class T, class U = T> template<class T, class U = T>
class direct_control_block : public control_block<T> class direct_control_block : public control_block<T> {
{
static_assert(!std::is_reference<U>::value, ""); static_assert(!std::is_reference<U>::value, "");
U u_; U u_;
public: public:
template<class... Ts> template<class... Ts>
explicit direct_control_block(Ts&&... ts) : u_(U(std::forward<Ts>(ts)...)) explicit direct_control_block(Ts &&... ts) : u_(U(std::forward<Ts>(ts)...)) {
{
} }
std::unique_ptr<control_block<T>> clone() const override std::unique_ptr<control_block<T>> clone() const override {
{
return std::make_unique<direct_control_block>(*this); return std::make_unique<direct_control_block>(*this);
} }
T* ptr() override T *ptr() override {
{
return std::addressof(u_); return std::addressof(u_);
} }
}; };
template<class T, class U, class C = default_copy<U>, template<class T, class U, class C = default_copy<U>,
class D = default_delete<U>> class D = default_delete<U>>
class pointer_control_block : public control_block<T>, public C class pointer_control_block : public control_block<T>, public C {
{
std::unique_ptr<U, D> p_; std::unique_ptr<U, D> p_;
public: public:
explicit pointer_control_block(U *u, C c = C{}, D d = D{}) explicit pointer_control_block(U *u, C c = C{}, D d = D{})
: C(std::move(c)), p_(u, std::move(d)) : C(std::move(c)), p_(u, std::move(d)) {
{
} }
explicit pointer_control_block(std::unique_ptr<U, D> p, C c = C{}) explicit pointer_control_block(std::unique_ptr<U, D> p, C c = C{})
: C(std::move(c)), p_(std::move(p)) : C(std::move(c)), p_(std::move(p)) {
{
} }
std::unique_ptr<control_block<T>> clone() const override std::unique_ptr<control_block<T>> clone() const override {
{
assert(p_); assert(p_);
return std::make_unique<pointer_control_block>( return std::make_unique<pointer_control_block>(
C::operator()(*p_), static_cast<const C &>(*this), p_.get_deleter()); C::operator()(*p_), static_cast<const C &>(*this), p_.get_deleter());
} }
T* ptr() override T *ptr() override {
{
return p_.get(); return p_.get();
} }
}; };
template<class T, class U> template<class T, class U>
class delegating_control_block : public control_block<T> class delegating_control_block : public control_block<T> {
{
std::unique_ptr<control_block<U>> delegate_; std::unique_ptr<control_block<U>> delegate_;
public: public:
explicit delegating_control_block(std::unique_ptr<control_block<U>> b) explicit delegating_control_block(std::unique_ptr<control_block<U>> b)
: delegate_(std::move(b)) : delegate_(std::move(b)) {
{
} }
std::unique_ptr<control_block<T>> clone() const override std::unique_ptr<control_block<T>> clone() const override {
{
return std::make_unique<delegating_control_block>(delegate_->clone()); return std::make_unique<delegating_control_block>(delegate_->clone());
} }
T* ptr() override T *ptr() override {
{
return static_cast<T *>(delegate_->ptr()); return static_cast<T *>(delegate_->ptr());
} }
}; };
@@ -143,13 +124,11 @@ namespace detail
} // end namespace detail } // end namespace detail
class bad_poly_construction : std::exception class bad_poly_construction : std::exception {
{
public: public:
bad_poly_construction() noexcept = default; bad_poly_construction() noexcept = default;
const char* what() const noexcept override const char *what() const noexcept override {
{
return "Dynamic and static type mismatch in poly " return "Dynamic and static type mismatch in poly "
"construction"; "construction";
} }
@@ -159,13 +138,11 @@ template <class T>
class poly; class poly;
template<class T> template<class T>
struct is_poly : std::false_type struct is_poly : std::false_type {
{
}; };
template<class T> template<class T>
struct is_poly<poly<T>> : std::true_type struct is_poly<poly<T>> : std::true_type {
{
}; };
@@ -174,18 +151,20 @@ struct is_poly<poly<T>> : std::true_type
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
template<class T> template<class T>
class poly class poly {
{
static_assert(!std::is_union<T>::value, ""); static_assert(!std::is_union<T>::value, "");
static_assert(std::is_class<T>::value, ""); static_assert(std::is_class<T>::value, "");
template<class U> template<class U>
friend class poly; friend
class poly;
template<class T_, class U, class... Ts> template<class T_, class U, class... Ts>
friend poly<T_> make_poly(Ts &&... ts); friend poly<T_> make_poly(Ts &&... ts);
template<class T_, class... Ts> template<class T_, class... Ts>
friend poly<T_> make_poly(Ts &&... ts); friend poly<T_> make_poly(Ts &&... ts);
template<class T_, class U> template<class T_, class U>
friend poly<T_> poly_cast(poly<U> p); friend poly<T_> poly_cast(poly<U> p);
@@ -204,17 +183,14 @@ public:
// Constructors // Constructors
// //
poly() poly() {
{
} }
template<class U, class C = detail::default_copy<U>, template<class U, class C = detail::default_copy<U>,
class D = detail::default_delete<U>, class D = detail::default_delete<U>,
class V = std::enable_if_t<std::is_convertible<U *, T *>::value>> class V = std::enable_if_t<std::is_convertible<U *, T *>::value>>
explicit poly(U* u, C copier = C{}, D deleter = D{}) explicit poly(U *u, C copier = C{}, D deleter = D{}) {
{ if (!u) {
if (!u)
{
return; return;
} }
@@ -236,10 +212,8 @@ public:
// Copy-constructors // Copy-constructors
// //
poly(const poly& p) poly(const poly &p) {
{ if (!p) {
if (!p)
{
return; return;
} }
auto tmp_cb = p.cb_->clone(); auto tmp_cb = p.cb_->clone();
@@ -251,8 +225,7 @@ public:
// Move-constructors // Move-constructors
// //
poly(poly&& p) noexcept poly(poly &&p) noexcept {
{
ptr_ = p.ptr_; ptr_ = p.ptr_;
cb_ = std::move(p.cb_); cb_ = std::move(p.cb_);
p.ptr_ = nullptr; p.ptr_ = nullptr;
@@ -265,8 +238,7 @@ public:
template<class U, template<class U,
class V = std::enable_if_t<!std::is_same<T, U>::value && class V = std::enable_if_t<!std::is_same<T, U>::value &&
std::is_convertible<U *, T *>::value>> std::is_convertible<U *, T *>::value>>
poly(const poly<U>& p) poly(const poly<U> &p) {
{
poly<U> tmp(p); poly<U> tmp(p);
ptr_ = tmp.ptr_; ptr_ = tmp.ptr_;
cb_ = std::make_unique<detail::delegating_control_block<T, U>>( cb_ = std::make_unique<detail::delegating_control_block<T, U>>(
@@ -276,8 +248,7 @@ public:
template<class U, template<class U,
class V = std::enable_if_t<!std::is_same<T, U>::value && class V = std::enable_if_t<!std::is_same<T, U>::value &&
std::is_convertible<U *, T *>::value>> std::is_convertible<U *, T *>::value>>
poly(poly<U>&& p) poly(poly<U> &&p) {
{
ptr_ = p.ptr_; ptr_ = p.ptr_;
cb_ = std::make_unique<detail::delegating_control_block<T, U>>( cb_ = std::make_unique<detail::delegating_control_block<T, U>>(
std::move(p.cb_)); std::move(p.cb_));
@@ -294,8 +265,7 @@ public:
poly(U &&u) poly(U &&u)
: cb_(std::make_unique< : cb_(std::make_unique<
detail::direct_control_block<T, std::decay_t<U>>>( detail::direct_control_block<T, std::decay_t<U>>>(
std::forward<U>(u))) std::forward<U>(u))) {
{
ptr_ = cb_->ptr(); ptr_ = cb_->ptr();
} }
@@ -303,15 +273,12 @@ public:
// Assignment // Assignment
// //
poly& operator=(const poly& p) poly &operator=(const poly &p) {
{ if (std::addressof(p) == this) {
if (std::addressof(p) == this)
{
return *this; return *this;
} }
if (!p) if (!p) {
{
cb_.reset(); cb_.reset();
ptr_ = nullptr; ptr_ = nullptr;
return *this; return *this;
@@ -328,10 +295,8 @@ public:
// Move-assignment // Move-assignment
// //
poly& operator=(poly&& p) noexcept poly &operator=(poly &&p) noexcept {
{ if (std::addressof(p) == this) {
if (std::addressof(p) == this)
{
return *this; return *this;
} }
@@ -346,8 +311,7 @@ public:
// Modifiers // Modifiers
// //
void swap(poly& p) noexcept void swap(poly &p) noexcept {
{
using std::swap; using std::swap;
swap(ptr_, p.ptr_); swap(ptr_, p.ptr_);
swap(cb_, p.cb_); swap(cb_, p.cb_);
@@ -358,31 +322,26 @@ public:
// Observers // Observers
// //
explicit operator bool() const explicit operator bool() const {
{
return (bool) cb_; return (bool) cb_;
} }
const T* operator->() const const T *operator->() const {
{
assert(ptr_); assert(ptr_);
return ptr_; return ptr_;
} }
const T& operator*() const const T &operator*() const {
{
assert(*this); assert(*this);
return *ptr_; return *ptr_;
} }
T* operator->() T *operator->() {
{
assert(*this); assert(*this);
return ptr_; return ptr_;
} }
T& operator*() T &operator*() {
{
assert(*this); assert(*this);
return *ptr_; return *ptr_;
} }
@@ -392,17 +351,16 @@ public:
// poly creation // poly creation
// //
template<class T, class... Ts> template<class T, class... Ts>
poly<T> make_poly(Ts&&... ts) poly<T> make_poly(Ts &&... ts) {
{
poly<T> p; poly<T> p;
p.cb_ = std::make_unique<detail::direct_control_block<T, T>>( p.cb_ = std::make_unique<detail::direct_control_block<T, T>>(
std::forward<Ts>(ts)...); std::forward<Ts>(ts)...);
p.ptr_ = p.cb_->ptr(); p.ptr_ = p.cb_->ptr();
return std::move(p); return std::move(p);
} }
template<class T, class U, class... Ts> template<class T, class U, class... Ts>
poly<T> make_poly(Ts&&... ts) poly<T> make_poly(Ts &&... ts) {
{
poly<T> p; poly<T> p;
p.cb_ = std::make_unique<detail::direct_control_block<T, U>>( p.cb_ = std::make_unique<detail::direct_control_block<T, U>>(
std::forward<Ts>(ts)...); std::forward<Ts>(ts)...);
@@ -414,8 +372,7 @@ template <class T, class U>
const T *poly_view(const poly<U> &p) { const T *poly_view(const poly<U> &p) {
if (p) { if (p) {
return dynamic_cast<const T *>(&*p); return dynamic_cast<const T *>(&*p);
} } else {
else {
return nullptr; return nullptr;
} }
} }
@@ -424,8 +381,7 @@ template <class T, class U>
T *poly_view(poly<U> &p) { T *poly_view(poly<U> &p) {
if (p) { if (p) {
return dynamic_cast<T *>(&*p); return dynamic_cast<T *>(&*p);
} } else {
else {
return nullptr; return nullptr;
} }
} }
+3 -4
View File
@@ -2,7 +2,7 @@
#define PRESULT_H #define PRESULT_H
#include "expected.h" #include "expected.h"
#include "sourceerror.h" #include "source_error.h"
#include <optional> #include <optional>
@@ -40,11 +40,10 @@ struct PResult : tl::expected<T, SourceError> {
/// if U is convertible to T /// if U is convertible to T
template<typename U, class V = std::enable_if_t<std::is_convertible<std::decay_t<U>, T>::value>> template<typename U, class V = std::enable_if_t<std::is_convertible<std::decay_t<U>, T>::value>>
PResult(PResult<U> presult) : PResult(PResult<U> presult) :
tl::expected<T, SourceError>(presult ? PResult<T>(*presult) : PError(presult.error())) tl::expected<T, SourceError>(presult ? PResult<T>(*presult) : PError(presult.error())) {}
{}
}; };
/// Alternative to PResult that is returned from parse* functions /// Alternative to PResult that is returned from parseLibrary* functions
/// that only consume one token so they can fail without affecting /// that only consume one token so they can fail without affecting
/// the parser state /// the parser state
template<typename T> template<typename T>
+5
View File
@@ -0,0 +1,5 @@
#include "source_error.h"
SourceError::SourceError(Span span, std::string message)
: span(span), message(std::move(message)) {}
File renamed without changes.
-6
View File
@@ -1,6 +0,0 @@
#include "sourceerror.h"
SourceError::SourceError(Span span, std::string message)
: span(std::move(span)), message(std::move(message))
{}
+8 -17
View File
@@ -4,32 +4,23 @@
Position::Position() Position::Position()
: fileId(0), line(0), col(0), offset(0) : fileId(0), line(0), col(0), offset(0) {}
{}
Position::Position(unsigned file, unsigned line, unsigned col, unsigned offset) Position::Position(unsigned file, unsigned line, unsigned col, unsigned offset)
: fileId(file), line(line), col(col), offset(offset) : fileId(file), line(line), col(col), offset(offset) {}
{}
Span::Span() Span::Span()
: lo(0, 0, 0, 0), hi(0, 0, 0, 0) : lo(0, 0, 0, 0), hi(0, 0, 0, 0) {}
{}
Span::Span(Position begin, Position end) Span::Span(Position begin, Position end)
: lo(begin), hi(end) : lo(begin), hi(end) {}
{}
Span::Span(Position begin) Span::Span(Position begin)
: Span(begin, Position(begin.fileId, begin.line, begin.col+1, begin.offset+1)) : Span(begin, Position(begin.fileId, begin.line, begin.col + 1, begin.offset + 1)) {}
{}
Span Span::to(const Span &span) const Span Span::to(const Span &span) const {
{ return {this->lo, span.hi};
return Span(this->lo, span.hi);
} }
Token::Token(TokenType type, Span span, std::string text) Token::Token(TokenType type, Span span, std::string text)
: type(type) : type(type), span(span), text(std::move(text)) {}
, span(std::move(span))
, text(std::move(text))
{}
+4
View File
@@ -12,6 +12,7 @@ struct Position {
unsigned offset; unsigned offset;
Position(); Position();
Position(unsigned fileId, unsigned line, unsigned col, unsigned offset); Position(unsigned fileId, unsigned line, unsigned col, unsigned offset);
}; };
@@ -23,7 +24,9 @@ struct Span {
Position hi; Position hi;
Span(); Span();
Span(Position lo, Position hi); Span(Position lo, Position hi);
Span(Position lo); Span(Position lo);
Span to(const Span &span) const; Span to(const Span &span) const;
@@ -72,6 +75,7 @@ enum class TokenType {
KW_NAME, KW_NAME,
KW_INFO, KW_INFO,
KW_HEADER, KW_HEADER,
KW_COMPONENT_HEADER,
KW_DIRECTORY, KW_DIRECTORY,
KW_LIBRARY, KW_LIBRARY,
KW_ADDRESS, KW_ADDRESS,
+173
View File
@@ -0,0 +1,173 @@
#include "tokens_type.h"
enum TokenClass {
KEYWORD_NAME = 4,
TOKENIZABLE = 8
};
struct TokenInfo {
std::string text;
unsigned short attributes;
};
struct TokenTables {
std::unordered_map<TokenType, TokenInfo> allTokensInfo;
std::unordered_map<std::string, TokenType> keywords;
TokenType tokenize(std::string value, TokenType initial);
bool is_keyword(TokenType token);
void add(TokenType tokenType, const std::string &txt,
unsigned short attribs = 0);
TokenTables();
};
static TokenTables tokenTables;
void TokenTables::add(TokenType tokenType, const std::string &txt,
unsigned short attribs) {
allTokensInfo[tokenType] = TokenInfo{txt, attribs};
if (attribs & KEYWORD_NAME)
keywords[txt] = tokenType;
}
TokenTables::TokenTables() {
add(TokenType::IDENTIFIER, "identifier");
add(TokenType::KEYWORD, "keyword", KEYWORD_NAME);
// Literals (bool is not here, it has two keywords: false and true)
add(TokenType::NUMBER, "number");
add(TokenType::STRING, "string");
add(TokenType::COLOR, "color");
// Parentheses of all kinds
add(TokenType::LPAREN, "(");
add(TokenType::RPAREN, ")");
add(TokenType::LBRACKET, "[");
add(TokenType::RBRACKET, "]");
add(TokenType::LBRACE, "{");
add(TokenType::RBRACE, "}");
add(TokenType::LT, "<");
add(TokenType::GT, ">");
// assignments
add(TokenType::EQUALS, "=");
// miscellaneous
add(TokenType::DOT, ".");
add(TokenType::COLON, ":");
add(TokenType::SEMICOLON, ";");
add(TokenType::COMMA, ",");
add(TokenType::WHITESPACE, "whitespace");
add(TokenType::COMMENT, "comment");
add(TokenType::TRUE, "true", TOKENIZABLE);
add(TokenType::FALSE, "false", TOKENIZABLE);
add(TokenType::IF, "if", TOKENIZABLE);
add(TokenType::ELSE, "else", TOKENIZABLE);
add(TokenType::DEFAULT, "default", TOKENIZABLE);
add(TokenType::NIL, "null", TOKENIZABLE);
// all keywords
add(TokenType::KW_NAME, "@name", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_INFO, "@info", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_HEADER, "@header", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_COMPONENT_HEADER, "@componentHeader", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_DIRECTORY, "@directory", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_LIBRARY, "@library", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_ADDRESS, "@address", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_COMPONENT, "@component", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_MESSAGES, "@messages", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_INSTANCE_NAME, "@instanceName", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_COUNT, "@count", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_DISPLAY, "@display", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_PIN, "@pin", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_TOOLTIP, "@tooltip", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_CONNECTION, "@connection", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_ATTRIBUTE, "@attribute", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_SOURCE, "@source", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_POPUP, "@popup", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_RULE, "@rule", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_TITLE, "@title", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_TEXT, "@text", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_BUS, "@bus", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_WIRES, "@wires", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_ENUMERATED, "@enumerated", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_WIRE, "@wire", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_INSTANCE, "@instance", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_SCHEMA, "@schema", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_POSITION, "@position", TOKENIZABLE | KEYWORD_NAME);
add(TokenType::KW_SIZE, "@size", TOKENIZABLE | KEYWORD_NAME);
// All types
add(TokenType::INT_TYPE, "int", TOKENIZABLE);
add(TokenType::STRING_TYPE, "string", TOKENIZABLE);
add(TokenType::BOOL_TYPE, "bool", TOKENIZABLE);
add(TokenType::WIRE_TYPE, "wire", TOKENIZABLE);
// WireNode types
add(TokenType::WIRE_DEFAULT, "normal_wire", TOKENIZABLE);
add(TokenType::WIRE_AND, "wired_and", TOKENIZABLE);
add(TokenType::WIRE_OR, "wired_or", TOKENIZABLE);
add(TokenType::R_WIRE, "r_wire", TOKENIZABLE);
// ComponentNode types
add(TokenType::CT_MEMORY, "memory", TOKENIZABLE);
add(TokenType::CT_PROCESSOR, "processor", TOKENIZABLE);
// PinNode type
add(TokenType::PIN_IN, "in", TOKENIZABLE);
add(TokenType::PIN_OUT, "out", TOKENIZABLE);
add(TokenType::PIN_IN_OUT, "inOut", TOKENIZABLE);
// ActionNode types
add(TokenType::ERROR, "error", TOKENIZABLE);
add(TokenType::WARNING, "warning", TOKENIZABLE);
// WireNode settings types
add(TokenType::HIDDEN, "hidden", TOKENIZABLE);
add(TokenType::TERMINATE_WITH, "terminate_with", TOKENIZABLE);
// Miscelaneous
add(TokenType::END_OF_FILE, "eof");
}
const std::string &tokenTypeToString(TokenType tokenType) {
return tokenTables.allTokensInfo[tokenType].text;
}
TokenType TokenTables::tokenize(std::string value, TokenType initial) {
for (auto &[key, param]: allTokensInfo) {
if (param.attributes & TOKENIZABLE && param.text == value) {
return key;
}
}
return initial;
}
bool TokenTables::is_keyword(TokenType token) {
for (auto &[key, param]: allTokensInfo) {
if (param.attributes & KEYWORD_NAME && key == token) {
return true;
}
}
return false;
}
TokenType from_token(std::string value, TokenType initial) {
return tokenTables.tokenize(value, initial);
}
bool is_keyword(TokenType tokenType) {
return tokenTables.is_keyword(tokenType);
}
File renamed without changes.
-175
View File
@@ -1,175 +0,0 @@
#include "tokenstype.h"
enum TokenClass {
BUILT_IN_FUNC = 1,
DATA_TYPE = 2,
KEYWORD_NAME = 4,
TOKENIZABLE = 8
};
struct TokenInfo {
std::string text;
unsigned short attributes;
};
struct TokenTables {
std::unordered_map<TokenType, TokenInfo> allTokensInfo;
std::unordered_map<std::string, TokenType> keywords;
TokenType tokenize(std::string value, TokenType initial);
bool is_keyword(TokenType token);
void add(TokenType tokenType, const std::string& txt,
unsigned short attribs = 0);
TokenTables();
};
static TokenTables tokenTables;
void TokenTables::add (TokenType tokenType, const std::string& txt,
unsigned short attribs)
{
allTokensInfo[tokenType] = TokenInfo{txt, attribs};
if(attribs & KEYWORD_NAME)
keywords[txt] = tokenType;
}
TokenTables::TokenTables() {
add( TokenType::IDENTIFIER, "identifier" );
add( TokenType::KEYWORD, "keyword" , KEYWORD_NAME);
// Literals (bool is not here, it has two keywords: false and true)
add( TokenType::NUMBER, "number");
add( TokenType::STRING, "string");
add( TokenType::COLOR, "color");
// Parentheses of all kinds
add( TokenType::LPAREN, "(" );
add( TokenType::RPAREN, ")" );
add( TokenType::LBRACKET, "[" );
add( TokenType::RBRACKET, "]" );
add( TokenType::LBRACE, "{" );
add( TokenType::RBRACE, "}" );
add( TokenType::LT, "<" );
add( TokenType::GT, ">" );
// assignments
add( TokenType::EQUALS, "=" );
// miscellaneous
add( TokenType::DOT, "." );
add( TokenType::COLON, ":" );
add( TokenType::SEMICOLON, ";" );
add( TokenType::COMMA, "," );
add( TokenType::WHITESPACE, "whitespace" );
add( TokenType::COMMENT, "comment" );
add( TokenType::TRUE, "true", TOKENIZABLE ),
add( TokenType::FALSE, "false", TOKENIZABLE),
add( TokenType::IF, "if", TOKENIZABLE ),
add( TokenType::ELSE, "else", TOKENIZABLE),
add( TokenType::DEFAULT, "default", TOKENIZABLE),
add( TokenType::NIL, "null", TOKENIZABLE),
// all keywords
add( TokenType::KW_NAME, "@name", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_INFO, "@info", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_HEADER, "@header", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_DIRECTORY, "@directory", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_LIBRARY, "@library", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_ADDRESS, "@address", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_COMPONENT, "@component", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_MESSAGES, "@messages", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_INSTANCE_NAME, "@instanceName", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_COUNT, "@count", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_DISPLAY, "@display", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_PIN, "@pin", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_TOOLTIP, "@tooltip", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_CONNECTION, "@connection", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_ATTRIBUTE, "@attribute", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_SOURCE, "@source", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_POPUP, "@popup", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_RULE, "@rule", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_TITLE, "@title", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_TEXT, "@text", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_BUS, "@bus", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_WIRES, "@wires", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_ENUMERATED, "@enumerated", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_WIRE, "@wire", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_INSTANCE, "@instance", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_SCHEMA, "@schema", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_POSITION, "@position", TOKENIZABLE | KEYWORD_NAME),
add( TokenType::KW_SIZE, "@size", TOKENIZABLE | KEYWORD_NAME),
// All types
add( TokenType::INT_TYPE, "int", TOKENIZABLE),
add( TokenType::STRING_TYPE, "string", TOKENIZABLE),
add( TokenType::BOOL_TYPE, "bool", TOKENIZABLE),
add( TokenType::WIRE_TYPE, "wire", TOKENIZABLE),
// WireNode types
add( TokenType::WIRE_DEFAULT, "normal_wire", TOKENIZABLE),
add( TokenType::WIRE_AND, "wired_and", TOKENIZABLE),
add( TokenType::WIRE_OR, "wired_or", TOKENIZABLE),
add( TokenType::R_WIRE, "r_wire", TOKENIZABLE),
// ComponentNode types
add( TokenType::CT_MEMORY, "memory", TOKENIZABLE),
add( TokenType::CT_PROCESSOR, "processor", TOKENIZABLE),
// PinNode type
add( TokenType::PIN_IN, "in", TOKENIZABLE),
add( TokenType::PIN_OUT, "out", TOKENIZABLE),
add( TokenType::PIN_IN_OUT, "inOut", TOKENIZABLE),
// ActionNode types
add( TokenType::ERROR, "error", TOKENIZABLE),
add( TokenType::WARNING, "warning", TOKENIZABLE),
// WireNode settings types
add( TokenType::HIDDEN, "hidden", TOKENIZABLE),
add( TokenType::TERMINATE_WITH, "terminate_with", TOKENIZABLE),
// Miscelaneous
add( TokenType::END_OF_FILE, "eof" );
}
const std::string &tokenTypeToString(TokenType tokenType) {
return tokenTables.allTokensInfo[tokenType].text;
}
TokenType TokenTables::tokenize(std::string value, TokenType initial) {
for(auto& [key, param]: allTokensInfo) {
if(param.attributes & TOKENIZABLE && param.text == value) {
return key;
}
}
return initial;
}
bool TokenTables::is_keyword(TokenType token) {
for(auto& [key, param]: allTokensInfo) {
if(param.attributes & KEYWORD_NAME && key == token) {
return true;
}
}
return false;
}
TokenType from_token(std::string value, TokenType initial) {
return tokenTables.tokenize(value, initial);
}
bool is_keyword(TokenType tokenType) {
return tokenTables.is_keyword(tokenType);
}
@@ -0,0 +1,53 @@
// Version 0.0.1
#include "libraries\frisc\vjezba1\FRISC.cdl"
#include "libraries\frisc\vjezba1\memory.cdl"
component System
{
clock 100MHz;
//glavnaSabirnica
wire<32> ADR;
wire<32> DATA;
wire READ;
wire WRITE;
wired_and WAIT;
wired_and INT0;
wired_and INT1;
wired_and INT2;
wired_and INT3;
wire<3> SIZE;
wire --IACK;
wire --BREQ;
wire --BACK;
//directRam
wire INT;
// components --------------------------------------------
subcomponent Memorija memorija<false, 1, 65536, 8, 0>(ADR, DATA, READ, WRITE, SIZE, WAIT, *, *, *, INT);
subcomponent FRISC procesor(ADR, DATA, READ, WRITE, SIZE, WAIT, INT0, INT1, INT2, INT3, --IACK, 1, *, INT, *, *, *);
display {
component { x: -377; y: -302; ref: "procesor"; }
component { x: -56; y: -80; ref: "memorija"; }
// glavnaSabirnica bus
rectangle {
x: -377; y: -106;
w: 100; h: 20;
}
// directRam bus
line {x1:-6; y1:-96; x2:-326; y2:-95;}
line {x1:-327; y1:-186; x2:-326; y2:-95;}
line {x1:-72; y1:-52; x2:-261; y2:-252;}
}
}
+137 -35
View File
@@ -1,23 +1,24 @@
@name "Jednostavni FRISC" @name "Jednostavni FRISC"
@info "Ova biblioteka omogućuje kreiranje sustava s procesorom FRISC s pojednostavljenim načinom spajanja bez potrebe da se spajaju pojedine linije sabirnice." @info "Ova biblioteka omogućuje kreiranje sustava s procesorom FRISC s pojednostavljenim načinom spajanja bez potrebe da se spajaju pojedine linije sabirnice."
@directory "libraries\frisc\vjezba1" @directory "libraries\frisc\vjezba1"
@header "// Version 0.0.1"
@componentHeader " clock 100MHz;"
@address glavniAS(0,0xFFFFFFFF) @address glavniAS(0,0xFFFFFFFF)
@address pomocniAS(0xFFFF0000,0xFFFFFFFF) @address pomocniAS(0xFFFF0000,0xFFFFFFFF)
@messages { @messages {
Ok: "OK"; ok: "OK";
Yes: "Da"; yes: "Da";
No: "Ne"; no: "Ne";
Cancel: "Otkaži"; cancel: "Odustani";
noneBusLine: "nespojen"; noneBusLine: "nespojen";
noneValue: "nema vrijednosti"; noneValue: "nema vrijednosti";
generalPinNotConnected: "Opći pin nije spojen niti na jednu sabirnicu";
} }
@component FRISC processor { @component FRISC processor {
@instanceName "procesor" @instanceName procesor
@tooltip "procesor FRISC, mora postojati jedan" @tooltip "Procesor FRISC, mora postojati jedan"
@count (1, 1) @count (1, 1)
@source "FRISC.cdl" @source "FRISC.cdl"
@display { @display {
@@ -48,33 +49,67 @@
rect { rect {
x: 0; y:0; w:100; h:100; x: 0; y:0; w:100; h:100;
} }
text {
x: 0; y: 0; w: 100; h: 100;
text: "FRISC";
} }
@pin glavniPin inOut { text {
x: 0; y: 16; w: 100; h: 100;
text: "{instanceName}";
}
}
@pin glavniPin in {
@tooltip "pin za spajanje na glavnu sabirnicu" @tooltip "pin za spajanje na glavnu sabirnicu"
@connection check_only("COMDEL se ne može stvoriti. FRISC nije spojen na sabirnicu") @connection required("COMDEL se ne može stvoriti. FRISC nije spojen na sabirnicu")
@display { @display {
/*
pin {
x: 100; y: 50;
fillColor: white;
lineColor: red;
fillColorConnected: white;
lineColorConnected: black;
side: left;
size: 20;
}*/
pin { pin {
x: 42; y: 100; w: 16; h:16; x: 42; y: 100; w: 16; h:16;
} type: "in";
orientation: "bottom";
} }
} }
} }
@pin memDirect inOut {
@tooltip "pin za izravno spajanje na RAM"
@connection optional("COMDEL se ne može stvoriti. FRISC nije spojen na sabirnicu")
@display {
pin {
x: 100; y: 42; w: 16; h:16;
type: "in";
orientation: "right";
}
}
@wires{null, null, null, null}
}
}
@component Memorija memory { @component Memorija memory {
@instanceName "memorija" @instanceName memorija
@tooltip "Memorijska komponenta, mora postojati barem jedna" @tooltip "Memorijska komponenta, mora postojati barem jedna"
@count (1,1000) @count (1,1000)
@source "memory.cdl" @source "memory.cdl"
@attribute sinkroniziran bool default false {
@popup automatic {
@title "Postavite sinkroniziranost"
@text "Zadajte sinkronizaciju"
}
}
@attribute brzina int default 1 {
@popup automatic {
@title "Postavite brzinu memorije"
@text "Zadajte brzinu memorije"
@enumerated {
"Spora" = 1,
"Srednja" = 5,
"Brza" = 25,
}
}
}
@attribute kapacitet int default 65536 { @attribute kapacitet int default 65536 {
@popup automatic { @popup automatic {
@title "Kapacitet memorije" @title "Kapacitet memorije"
@@ -146,7 +181,7 @@
@pin glavniPin inOut { @pin glavniPin inOut {
@tooltip "pin za spajanje na glavnu sabirnicu" @tooltip "pin za spajanje na glavnu sabirnicu"
@connection check_only("COMDEL se ne može stvoriti. Memorija nije spojena na sabirnicu") @connection required("COMDEL se ne može stvoriti. Memorija nije spojena na sabirnicu")
@display { @display {
/* /*
pin { pin {
@@ -161,22 +196,49 @@
*/ */
pin { pin {
x: 42; y: -16; w: 16; h:16; x: 42; y: -16; w: 16; h:16;
} type: "out";
orientation: "top";
} }
} }
} }
@pin memDirect inOut {
@tooltip "pin za spajanje na procesor"
@connection optional("COMDEL se ne može stvoriti. Memorija nije spojena na sabirnicu")
@display {
/*
pin {
x: 100; y: 25;
fillColor: blue;
lineColor: red;
fillColorConnected: white;
lineColorConnected: black;
side: right;
size: 20;
}
*/
pin {
x: -16; y: 20; w: 16; h:16;
type: "out";
orientation: "left";
}
}
@wires{null, null, null, null}
}
}
@component DMA { @component DMA {
@instanceName "dma" @instanceName dma
@tooltip "DMA-kontroler" @tooltip "DMA-kontroler"
@count (1,1000) @count (0,1000)
@source "dma.cdl" @source "dma.cdl"
@attribute pocetnaAdresa int default 0 { @attribute pocetnaAdresa int default 0 {
@popup automatic { @popup automatic {
@title "Početna adresa DMA-kontrolera" @title "Početna adresa DMA-kontrolera"
@text "Zadajte početnu adresu DMA-kontrolera" @text "Zadajte početnu adresu DMA-kontrolera"
@rule { @rule {
if(contains_address(glavniAS, pocetnaAdresa)) { if(!contains_address(glavniAS, pocetnaAdresa)) {
error("Početna adresa memorije je izvan 32 bitnog adresnog prostora") error("Početna adresa memorije je izvan 32 bitnog adresnog prostora")
} }
} }
@@ -186,7 +248,7 @@
} }
} }
@rule { @rule {
if(unique(glavniAS, pocetnaAdresa, 32)) { if(!unique(glavniAS, pocetnaAdresa, 32)) {
error("Adrese memorije nisu jedinstvene u adresnom prostoru (preklapaju se s nekom drugom komponentom)") error("Adrese memorije nisu jedinstvene u adresnom prostoru (preklapaju se s nekom drugom komponentom)")
} }
} }
@@ -214,7 +276,7 @@
@pin glavniPin in { @pin glavniPin in {
@tooltip "pin za spajanje na glavnu sabirnicu" @tooltip "pin za spajanje na glavnu sabirnicu"
@connection check_only("COMDEL se ne može stvoriti. DMA nije spojena na sabirnicu") @connection required("COMDEL se ne može stvoriti. DMA nije spojena na sabirnicu")
@display { @display {
/* /*
pin { pin {
@@ -228,17 +290,19 @@
} }
*/ */
pin { pin {
x: 100; y: 50; w: 16; h: 16; x: 100; y: 30; w: 16; h: 16;
orientation: "right";
} }
} }
} }
@pin dodatnaPoveznica in { @pin dodatnaPoveznica in {
@tooltip "pin za spajanje na pomocnu sabirnicu" @tooltip "pin za spajanje na pomocnu sabirnicu"
@connection check_only("COMDEL se ne može stvoriti. DMA nije spojen na nesto!") @connection optional("COMDEL se ne može stvoriti. DMA nije spojen na nesto!")
@display { @display {
pin { pin {
x: 0; y: 0; w: 0; h: 0; x: 100; y: 7; w: 16; h: 16;
orientation: "right";
} }
} }
@wires { 0, 0, null, null} @wires { 0, 0, null, null}
@@ -246,6 +310,7 @@
} }
@bus glavnaSabirnica regular { @bus glavnaSabirnica regular {
@instanceName glavnaSabirnica
@tooltip "sabirnica za spajanje FRISC a s memorijama i UI/jedinicama" @tooltip "sabirnica za spajanje FRISC a s memorijama i UI/jedinicama"
@count (1,1) @count (1,1)
@display { @display {
@@ -257,9 +322,13 @@
orientation: horizontal; orientation: horizontal;
lineColor: black; lineColor: black;
}*/ }*/
rect { bus {
x:0; y:0; x:0; y:0;
h: 20; w: 100; h: 20; w: 100;
orientation: "horizontal";
resizable: true;
lineColor: #bbbbbb;
fillColor: #bbbbbb;
} }
} }
@wires { @wires {
@@ -273,12 +342,14 @@
INT2 wired_and, INT2 wired_and,
INT3 wired_and, INT3 wired_and,
SIZE<3>, SIZE<3>,
IACK, IACK hidden,
BREQ, BREQ hidden,
BACK BACK hidden
} }
} }
@bus PIOSabirnica automatic { @bus PIOSabirnica automatic {
@instanceName PIOSabirnica
@count (0, 20)
@wires { @wires {
PIO_DATA<8>, PIO_DATA<8>,
READY, READY,
@@ -286,6 +357,14 @@
} }
} }
@bus directRam singleAutomatic {
@instanceName directRam
@wires {
INT
}
}
@connection (FRISC.glavniPin, glavnaSabirnica) { @connection (FRISC.glavniPin, glavnaSabirnica) {
@wires{ADR, DATA, READ, WRITE, SIZE, WAIT, INT0, INT1, INT2, INT3, IACK, 1, null} @wires{ADR, DATA, READ, WRITE, SIZE, WAIT, INT0, INT1, INT2, INT3, IACK, 1, null}
} }
@@ -308,3 +387,26 @@
} }
@wires{ADR, DATA, READ, WRITE, SIZE, WAIT, interupt, BREQ, BACK} @wires{ADR, DATA, READ, WRITE, SIZE, WAIT, interupt, BREQ, BACK}
} }
@connection (DMA.dodatnaPoveznica, PIOSabirnica, FRISC.glavniPin) {
@wires{PIO_DATA, READY, STROBE}
@wires{PIO_DATA, READY, STROBE}
}
@connection (FRISC.memDirect, directRam, Memorija.memDirect) {
@attribute procConn string default "INT" {
@popup automatic {
@title "Frisc INT"
@text "Processor interupt 1"
}
}
@attribute memConn string default "INT" {
@popup automatic {
@title "Mem INT"
@text "Memory interupt 1"
}
}
@wires{"INT", null, 1, "INT2"}
@wires{"INT", null, 1, "INT2"}
}
+4 -32
View File
@@ -3,13 +3,16 @@
@schema { @schema {
@instance proc FRISC { @instance proc FRISC {
@position (0, 0) @position (0, 0)
@attribute _memory null
} }
@instance mem Memorija { @instance mem Memorija {
@position (0, 250) @position (0, 250)
@attribute sinkroniziran false
@attribute brzina 1
@attribute kapacitet 1024 @attribute kapacitet 1024
@attribute size 8 @attribute size 8
@attribute pocetnaAdresa 1023 @attribute pocetnaAdresa 1024
} }
@instance bus glavnaSabirnica { @instance bus glavnaSabirnica {
@@ -17,39 +20,8 @@
@size 100 @size 100
} }
@wire wire_001 {
@position (50, 116)
@display {
/*
line {
color: blue;
points: 100;
}*/
line {
x1: 0; y1: 0; x2: 0; y2: 84;
}
}
}
@wire wire_002 {
@position (50, 220)
@display {
/*
line {
color: blue;
points: 100;
}*/
line {
x1: 0; y1: 0; x2: 0; y2: 14;
}
}
}
@connection (proc.glavniPin, bus) { @connection (proc.glavniPin, bus) {
@wire wire_001
} }
@connection (mem.glavniPin, bus) { @connection (mem.glavniPin, bus) {
@wire wire_002
} }
} }
+7 -27
View File
@@ -1,34 +1,14 @@
@source "frisc_library.csl" @source "/home/bbr/Documents/Personal/FER/schema_editor/examples/simplified FRISC model/frisc_library.csl"
@schema { @schema {
@instance proc FRISC { @instance procesor FRISC {
@position (0, 0) @position (-177, -122)
@attribute _memory memorija2
} }
@instance mem Memorija { @instance procesor FRISC {
@position (0, 250) @position (49, -97)
@attribute kapacitet 1024 @attribute _memory null
@attribute size 8
@attribute pocetnaAdresa 1023
} }
@instance bus glavnaSabirnica {
@position (0, 200)
@size 100
}
@wire wire_001 {
@position (50, 116)
@display {}
}
@wire wire_002 {
@position (50, 220)
@display {}
}
@connection (proc.glavniPin, bus) {
@wire wire_001
}
@connection (mem.glavniPin, bus) {
@wire wire_002
}
} }
@@ -0,0 +1,29 @@
@source "/home/bbr/Documents/Personal/FER/schema_editor/examples/simplified FRISC model/frisc_library.csl"
@schema {
@instance procesor FRISC {
@position (-464, -314)
@attribute _memory null
}
@instance dma DMA {
@position (-269, 46)
@attribute pocetnaAdresa 0
}
@instance glavnaSabirnica glavnaSabirnica {
@position (-579, -160)
@size 100
}
@instance PIOSabirnica PIOSabirnica {
@position (0, 0)
@size 0
}
@connection (dma.glavniPin, glavnaSabirnica) {
@attribute interupt INT2
}
@connection (dma.glavniPin, PIOSabirnica, procesor.glavniPin) {
}
}
@@ -0,0 +1,16 @@
@source "/home/bbr/Documents/Personal/FER/schema_editor/examples/simplified FRISC model/frisc_library.csl"
@schema {
@instance procesor FRISC {
@position (-543, -304)
@attribute _memory null
}
@instance glavnaSabirnica glavnaSabirnica {
@position (-544, -91)
@size 100
}
@connection (procesor.glavniPin, glavnaSabirnica) {
}
}
Loaded 100 of 104 files, more files were not shown because too many files have changed in this diff. Show more