| 64 | } |
| 65 | |
| 66 | Value *SymbolScanner::getValueForConst(AstNode *const node, DataType *const constDataType) |
| 67 | { |
| 68 | Value *constVal = nullptr; |
| 69 | bool isStringConst = constDataType->getTrueDataType()->isString(); |
| 70 | if (rhsIsAStringLiteral(node)) |
| 71 | { |
| 72 | if (!isStringConst) |
| 73 | { |
| 74 | throw syntax_error2("Attempt to assign a string to a non-string data type", |
| 75 | node->getChild(0)->getToken().getLocation(), m_fileName); |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | constVal = node->getChild(2)->getTokenValue(); |
| 80 | } |
| 81 | } |
| 82 | else if (rhsIsAnExpression(node)) |
| 83 | { |
| 84 | // const node -> virtual expression node -> actual expression (id or number) |
| 85 | Token &rhsExpressionChildTok = node->getChild(2)->getChild(0)->getToken(); |
| 86 | if (rhsExpressionChildTok.isNumberTok()) |
| 87 | { |
| 88 | if (isStringConst) |
| 89 | { |
| 90 | throw syntax_error2("Attempt to assign a number to a string data type", |
| 91 | node->getChild(0)->getToken().getLocation(), m_fileName); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | constVal = rhsExpressionChildTok.getValue(); |
| 96 | } |
| 97 | } |
| 98 | else if (rhsExpressionChildTok.isIdentifierTok()) |
| 99 | { |
| 100 | constVal = getValueFromSymbol(rhsExpressionChildTok); |
| 101 | if (kStringValue == constVal->getType()) |
| 102 | { |
| 103 | if (!isStringConst) |
| 104 | { |
| 105 | delete constVal; |
| 106 | throw syntax_error2("Attempt to assign a string to a non-string data type", |
| 107 | node->getChild(0)->getToken().getLocation(), m_fileName); |
| 108 | } |
| 109 | } |
| 110 | else // kStringValue != constVal->getType() |
| 111 | { |
| 112 | if (isStringConst) |
| 113 | { |
| 114 | delete constVal; |
| 115 | throw syntax_error2("Attempt to assign a number to a string data type", |
| 116 | node->getChild(0)->getToken().getLocation(), m_fileName); |
| 117 | } |
| 118 | } |
| 119 | } // else do nothing |
| 120 | } |
| 121 | assert(nullptr != constVal); |
| 122 | return constVal; |
| 123 | } |
nothing calls this directly
no test coverage detected