Added support for integers and null to connections

This commit is contained in:
Borna Rajkovic 2022-04-08 22:46:47 +02:00
parent 9bd68ecf23
commit dfc7bf48dc
10 changed files with 95 additions and 39 deletions

View File

@ -58,6 +58,8 @@ Value toType(ValueNode node) {
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);
}
@ -228,12 +230,12 @@ std::optional<Connection> ComdelGenerator::loadConnection(ConnectionNode node)
auto componentInstance = getComponentPin(component, pin);
if(!componentInstance) {
errors.push_back(SourceError(node.span, "pin does not exist"));
errors.emplace_back(node.span, "pin does not exist");
}
auto busInstance = getBus(bus);
if(!busInstance) {
errors.push_back(SourceError(node.span, "bus does not exist"));
errors.emplace_back(node.span, "bus does not exist");
}
std::set<std::string> wireNames;
@ -258,12 +260,20 @@ std::optional<Connection> ComdelGenerator::loadConnection(ConnectionNode node)
std::vector<Value> wires;
for(uint i=0; i<node.wires.size(); i++) {
if(attributeNames.count(node.wires[i].value)) {
wires.push_back(Value::fromReference(node.wires[i].value, Value::ATTRIBUTE_REFERENCE));
} else if(wireNames.count(node.wires[i].value)) {
wires.push_back(Value::fromReference(node.wires[i].value, Value::WIRE_REFERENCE));
if(node.wires[i].is(ValueNode::NIL)) {
wires.push_back(Value::fromNull());
} else if(node.wires[i].is(ValueNode::INT)) {
wires.push_back(Value::fromInt(node.wires[i].asInt()));
} else if(node.wires[i].is(ValueNode::IDENTIFIER)) {
if(attributeNames.count(node.wires[i].asIdentifier())) {
wires.push_back(Value::fromReference(node.wires[i].asIdentifier(), Value::ATTRIBUTE_REFERENCE));
} else if(wireNames.count(node.wires[i].asIdentifier())) {
wires.push_back(Value::fromReference(node.wires[i].asIdentifier(), Value::WIRE_REFERENCE));
} else {
errors.emplace_back(node.wires[i].span, "unknown identifier");
}
} else {
errors.push_back(SourceError(node.wires[i].span, "unknown identifier"));
errors.emplace_back(node.wires[i].span, "unknown value type");
}
}
@ -520,26 +530,34 @@ std::optional<Popup> ComdelGenerator::loadPopup(PopupNode node, std::string name
std::vector<Enumeration> enumeration;
if(node.enumerated) {
for(uint i=0; i<node.enumeration.size(); i++) {
if(toType(node.enumeration[i].value.getType()) != type) {
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"});
}
}
auto value = toType(node.enumeration[i].value);
if(value.isType(Value::UNDEFINED) && 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.getType() != type) {
errors.push_back(SourceError{node.enumeration[i].span, "wrong type"});
}
enumeration.push_back(Enumeration(node.enumeration[i].key.asString(), value));
}
} else {
if(type == Value::WIRE_REFERENCE && !current().inConnection) {
errors.push_back(SourceError{node.span, "@enumeration is required for attributes of type wire"});
}
}
pop();

View File

@ -105,4 +105,10 @@ Value Value::ofType(Value::ValueType type) {
}
Value Value::fromNull() {
Value val;
val.type = Value::NIL;
return val;
}
} // namespace domain

View File

@ -19,6 +19,7 @@ public:
ADDRESS_SPACE,
ATTRIBUTE_REFERENCE,
WIRE_REFERENCE,
NIL,
UNDEFINED,
};
@ -54,6 +55,7 @@ public:
static Value fromInt(long long value);
static Value fromString(std::string value);
static Value fromBool(bool value);
static Value fromNull();
static Value fromAddressSpace(AddressSpace addressSpace);
static Value fromReference(std::string value, ValueType type);
static Value ofType(ValueType type);

View File

@ -74,7 +74,8 @@ public:
STRING,
BOOL,
WIRE,
IDENTIFIER
IDENTIFIER,
NIL,
};
@ -148,6 +149,12 @@ public:
return value;
}
static ValueNode ofNull() {
ValueNode value;
value.type = NIL;
return value;
}
static ValueNode ofWire(std::string _value) {
ValueNode value;
value.type = WIRE;
@ -293,7 +300,7 @@ struct ConnectionNode: AstNode
IdentifierNode pin;
IdentifierNode bus;
std::vector<AttributeNode> attributes;
std::vector<IdentifierNode> wires;
std::vector<ValueNode> wires;
};

View File

@ -21,7 +21,7 @@ inline bool identifierContinue(char ch) {
}
inline bool numberStart(char ch) {
return isdigit(ch);
return isdigit(ch) || ch == '-';
}
inline bool isWhitespace(char ch) {
@ -64,10 +64,10 @@ LexerResult ComdelLexer::tokenize() {
tokenType = from_token(text, tokenType.value());
tokens.push_back(Token(*tokenType, Span(tokenBegin, position), text));
tokens.emplace_back(*tokenType, Span(tokenBegin, position), text);
}
tokens.push_back(Token( TokenType::END_OF_FILE, Span(position), ""));
tokens.emplace_back( TokenType::END_OF_FILE, Span(position), "");
return LexerResult { tokens, errors };
}
@ -251,6 +251,9 @@ PResult<TokenType> ComdelLexer::nextTokenType() {
}
else if (numberStart(ch))
{
if(ch == '-') {
bump();
}
unsigned takenDigits;
Radix radix = takeRadix();
takenDigits = takeNumberInRadix(radix);
@ -393,15 +396,6 @@ PResult<TokenType> ComdelLexer::nextTokenType() {
bump();
return TokenType::EQUALS;
}
else if (ch == '<')
{
bump();
return TokenType::LT;
}
else if (ch == '>')
{
return TokenType::GT;
}
else
{
std::stringstream message;

View File

@ -711,8 +711,10 @@ PResult<AttributeNode> ComdelParser::parseAttribute() {
if(attribute.type == ValueNode::BOOL) {
if(check(TokenType::TRUE)) {
bump();
attribute.defaultValue = ValueNode::ofBool(true);
} else if(check(TokenType::FALSE)) {
bump();
attribute.defaultValue = ValueNode::ofBool(false);
} else {
return unexpected();
@ -742,7 +744,10 @@ PResult<AttributeNode> ComdelParser::parseAttribute() {
if(check(TokenType::IDENTIFIER)) {
auto identifier = parseIdentifier();
attribute.defaultValue = ValueNode::ofWire(identifier->value);
} else {
} else if(check(TokenType::NIL)) {
bump();
attribute.defaultValue = ValueNode::ofNull();
} else {
return unexpected();
}
}
@ -868,8 +873,8 @@ PResult<ConnectionNode> ComdelParser::parseConnection() {
APPEND_OR_RETURN_IF_ERR(connection.attributes, parseAttribute());
} else if(check(TokenType::KW_WIRES)) {
bump();
auto wires = parseList<IdentifierNode>(std::optional<TokenType>(TokenType::LBRACE), TokenType::RBRACE, std::optional<TokenType>(TokenType::COMMA), false,
[this] { return parseIdentifier(); });
auto wires = parseList<ValueNode>(std::optional<TokenType>(TokenType::LBRACE), TokenType::RBRACE, std::optional<TokenType>(TokenType::COMMA), false,
[this] { return parseConnectionWire(); });
RETURN_IF_ERR(wires);
connection.wires = *wires;
} else {
@ -973,9 +978,14 @@ PResult<ValueNode> ComdelParser::parseValue() {
} else if(check(TokenType::NUMBER)) {
value = ValueNode::ofInt(parseNumber()->value);
} else if(check(TokenType::TRUE)) {
bump();
value = ValueNode::ofBool(true);
} else if(check(TokenType::FALSE)) {
bump();
value = ValueNode::ofBool(false);
} else if(check(TokenType::NIL)) {
bump();
value = ValueNode::ofNull();
} else {
return unexpected();
}
@ -1167,3 +1177,16 @@ PResult<ConnectionInstanceNode> ComdelParser::parseConnectionInstance() {
return spanner(connection);
}
PResult<ValueNode> ComdelParser::parseConnectionWire() {
if(check(TokenType::NUMBER)) {
return ValueNode::ofInt(parseNumber()->value);
} else if(check(TokenType::NIL)) {
bump();
return ValueNode::ofNull();
} else if(check(TokenType::IDENTIFIER)) {
return ValueNode::ofIdentifier(parseIdentifier()->value);
} else {
return unexpected();
}
}

View File

@ -69,6 +69,7 @@ private:
PResult<PropertyNode> parseProperty(std::optional<TokenType> valueType);
PResult<EnumerationNode> parseEnumeration();
PResult<ValueNode> parseConnectionWire();
PResult<ComponentNode> parseComponent();
PResult<AddressSpaceNode> parseAddress();
PResult<PinNode> parsePin();
@ -84,6 +85,7 @@ private:
PResult<IfStatementNode> parseIfStatement();
PResult<ValueNode> parseValue();
PResult<WireInstanceNode> parseWireInstance();
PResult<CountNode> parsePosition();
PResult<InstanceNode> parseInstance();

View File

@ -65,6 +65,9 @@ enum class TokenType {
TRUE,
FALSE,
// NULL
NIL,
// KEYWORDS
KW_NAME,
KW_INFO,

View File

@ -76,6 +76,7 @@ TokenTables::TokenTables() {
add( TokenType::ELSE, "else", TOKENIZABLE),
add( TokenType::DEFAULT, "default", TOKENIZABLE),
add( TokenType::NIL, "null", TOKENIZABLE),
// all keywords
add( TokenType::KW_NAME, "@name", TOKENIZABLE),

View File

@ -63,14 +63,14 @@ void MainWindow::onTestModal() {
library = generator.loadLibrary(*schemaNode->library);
for (auto& error : generator.getErrors()) {
parseContext.formatError(error, std::cout, "ERROR: ");
parseContext.formatError(error, std::cout, "GENERATOR ERROR: ");
}
if(library) {
schema = generator.loadSchema(*schemaNode, *library);
for (auto& error : generator.getErrors()) {
parseContext.formatError(error, std::cout, "ERROR: ");
parseContext.formatError(error, std::cout, "LIBRARY ERROR: ");
}
}