@@ -9,97 +9,95 @@ All rights reserved.
9
9
10
10
Analyzer::Analyzer (bool isSingleStatement, const std::string &fileInput, JSONParser jsonParser,
11
11
const std::string &inputCommentLineIdentifiers, const std::string &inputCommentBlockOpenIdentifiers,
12
- const std::string &inputCommentBlockCloseIdentifiers): jsonParser(std::move(jsonParser)) {
13
- this ->isSingleStatement = isSingleStatement;
12
+ const std::string &inputCommentBlockCloseIdentifiers)
13
+ : jsonParser(std::move(jsonParser)) {
14
+ this ->isSingleStatement = isSingleStatement;
14
15
15
- // Initialize parser
16
- parser = Parser (isSingleStatement, fileInput, inputCommentLineIdentifiers,
17
- inputCommentBlockOpenIdentifiers, inputCommentBlockCloseIdentifiers);
16
+ // Initialize parser
17
+ parser = Parser (isSingleStatement, fileInput, inputCommentLineIdentifiers, inputCommentBlockOpenIdentifiers ,
18
+ inputCommentBlockCloseIdentifiers);
18
19
19
- // Parse abstract syntax tree
20
- ast = parser.parseAST ();
21
- }
22
-
23
- TopLevelExprAST *Analyzer::getAST () {
24
- return ast;
20
+ // Parse abstract syntax tree
21
+ ast = parser.parseAST ();
25
22
}
26
23
27
24
void Analyzer::executeAnalysis () {
28
- // Execute checks
29
- checkDataTypeCompatibility ();
30
- // ToDo: Add more checks here
25
+ // Execute checks
26
+ checkDataTypeCompatibility ();
27
+ // ToDo: Add more checks here
31
28
}
32
29
33
30
// ------------------------------------------------- Private functions -------------------------------------------------
34
31
35
32
void Analyzer::checkDataTypeCompatibility () {
36
- if (isSingleStatement) {
37
- if (ast->getType () != TopLevelExprType ::STMT_LST_EXPR)
38
- throw std::runtime_error (" Input was no single statement list" );
39
- auto * stmtLst = static_cast <StmtLstExprAST *>(ast);
40
- checkDataTypeCompatibilityStmtList (stmtLst);
41
- } else {
42
- auto * content = static_cast <ContentExprAST *>(ast);
43
- checkDataTypeCompatibilityContent (content);
44
- }
33
+ if (isSingleStatement) {
34
+ if (ast->type != ASTRootNode ::STMT_LST_EXPR)
35
+ throw std::runtime_error (" Input was no single statement list" );
36
+ auto * stmtLst = static_cast <ASTStmtListNode *>(ast);
37
+ checkDataTypeCompatibilityStmtList (stmtLst);
38
+ } else {
39
+ auto * content = static_cast <ASTContentExprNode *>(ast);
40
+ checkDataTypeCompatibilityContent (content);
41
+ }
45
42
}
46
43
47
- void Analyzer::checkDataTypeCompatibilityContent (ContentExprAST* content) {
48
- // Loop through sections
49
- for (const std::unique_ptr<ContentBlockExprAST>& contentBlock : content->getContentBlocks ()) {
50
- if (contentBlock->getType () == ContentBlockExprType::SECTION_EXPR) {
51
- auto * relevantSection = static_cast <SectionExprAST*>(contentBlock.get ());
52
- // Loop through comBlocks
53
- for (const std::unique_ptr<ComBlockExprAST>& comBlock : relevantSection->getComBlocks ()) {
54
- switch (comBlock->getType ()) {
55
- case ComBlockExprType::COM_LINE_BLOCK_EXPR: {
56
- auto * lineBlockExpr = static_cast <ComLineBlockExprAST*>(comBlock.get ());
57
- checkDataTypeCompatibilityStmtList (lineBlockExpr->getIfBlock ()->getStmtList ().get ());
58
- continue ;
59
- }
60
- case ComBlockExprType::COM_BLOCK_BLOCK_EXPR: {
61
- auto * blockBlockExpr = static_cast <ComBlockBlockExprAST*>(comBlock.get ());
62
- checkDataTypeCompatibilityStmtList (blockBlockExpr->getIfBlock ()->getStmtList ().get ());
63
- continue ;
64
- }
65
- default :
66
- throw std::runtime_error (" Got unknown ComBlock object" );
67
- }
68
- }
44
+ void Analyzer::checkDataTypeCompatibilityContent (ASTContentExprNode *content) {
45
+ // Loop through sections
46
+ for (const std::unique_ptr<ASTContentBlockExprNode> &contentBlock : content->contentBlocks ) {
47
+ if (contentBlock->type == ASTArbitraryExprNode::SECTION_EXPR) {
48
+ auto *relevantSection = static_cast <ASTSectionExprNode *>(contentBlock.get ());
49
+ // Loop through comBlocks
50
+ for (const std::unique_ptr<ASTComBlockExprNode> &comBlock : relevantSection->comBlocks ) {
51
+ switch (comBlock->type ) {
52
+ case ASTComBlockExprNode::COM_LINE_BLOCK_EXPR: {
53
+ auto *lineBlockExpr = static_cast <ASTComLineBlockExprNode *>(comBlock.get ());
54
+ checkDataTypeCompatibilityStmtList (lineBlockExpr->ifBlock ->stmtList .get ());
55
+ continue ;
56
+ }
57
+ case ASTComBlockExprNode::COM_BLOCK_BLOCK_EXPR: {
58
+ auto *blockBlockExpr = static_cast <ASTComBlockBlockExprNode *>(comBlock.get ());
59
+ checkDataTypeCompatibilityStmtList (blockBlockExpr->ifBlock ->stmtList .get ());
60
+ continue ;
69
61
}
62
+ default :
63
+ throw std::runtime_error (" Got unknown ComBlock object" );
64
+ }
65
+ }
70
66
}
67
+ }
71
68
}
72
69
73
- void Analyzer::checkDataTypeCompatibilityStmtList (StmtLstExprAST* stmtLst) {
74
- // Loop through statements
75
- for (const std::unique_ptr<StmtExprAST>& stmt : stmtLst->getStatements ()) {
76
- if (stmt->getType () == StmtExprType::COMP_STMT_EXPR) {
77
- auto * compStmt = static_cast <CompStmtExprAST*>(stmt.get ());
78
- checkDataTypeCompatibilityCompStmt (compStmt);
79
- }
70
+ void Analyzer::checkDataTypeCompatibilityStmtList (ASTStmtListNode *stmtLst) {
71
+ // Loop through statements
72
+ for (const std::unique_ptr<ASTStmtNode> &stmt : stmtLst->stmts ) {
73
+ if (stmt->type == ASTCompStmtNode::COMP_STMT_EXPR) {
74
+ auto *compStmt = static_cast <ASTCompStmtNode *>(stmt.get ());
75
+ checkDataTypeCompatibilityCompStmt (compStmt);
80
76
}
77
+ }
81
78
}
82
79
83
- void Analyzer::checkDataTypeCompatibilityCompStmt (CompStmtExprAST* compStmt) {
84
- // Abort check successfully when the key does not exist. Non-existent keys will be evaluated to false by the interpreter
85
- if (!jsonParser.jsonKeyExists (compStmt->getKey ())) return ;
86
- // Check if 'value' has the same type as the JSON key value
87
- auto jsonKeyValue = jsonParser.getJSONValueFromKey (compStmt->getKey ());
80
+ void Analyzer::checkDataTypeCompatibilityCompStmt (ASTCompStmtNode *compStmt) {
81
+ // Abort check successfully when the key does not exist. Non-existent keys will be evaluated to false by the interpreter
82
+ if (!jsonParser.jsonKeyExists (compStmt->key ))
83
+ return ;
84
+ // Check if 'value' has the same type as the JSON key value
85
+ auto jsonKeyValue = jsonParser.getJSONValueFromKey (compStmt->key );
88
86
89
- switch (compStmt->getValue ()-> getType () ) {
90
- case ValueExprType ::STRING_EXPR:
91
- if (!jsonKeyValue.is_string ())
92
- throw IncompatibleTypesException (jsonKeyValue.dump (), " a string" );
93
- return ;
94
- case ValueExprType ::BOOLEAN_EXPR:
95
- if (!jsonKeyValue.is_boolean ())
96
- throw IncompatibleTypesException (jsonKeyValue.dump (), " a boolean" );
97
- return ;
98
- case ValueExprType ::NUMBER_EXPR:
99
- if (!jsonKeyValue.is_number_integer ())
100
- throw IncompatibleTypesException (jsonKeyValue.dump (), " an integer" );
101
- return ;
102
- case ValueExprType ::VALUE_EXPR:
103
- throw UnknownDataTypeException (jsonKeyValue.dump ());
104
- }
87
+ switch (compStmt->value -> type ) {
88
+ case ASTValueExprNode ::STRING_EXPR:
89
+ if (!jsonKeyValue.is_string ())
90
+ throw IncompatibleTypesException (jsonKeyValue.dump (), " a string" );
91
+ return ;
92
+ case ASTValueExprNode ::BOOLEAN_EXPR:
93
+ if (!jsonKeyValue.is_boolean ())
94
+ throw IncompatibleTypesException (jsonKeyValue.dump (), " a boolean" );
95
+ return ;
96
+ case ASTValueExprNode ::NUMBER_EXPR:
97
+ if (!jsonKeyValue.is_number_integer ())
98
+ throw IncompatibleTypesException (jsonKeyValue.dump (), " an integer" );
99
+ return ;
100
+ case ASTValueExprNode ::VALUE_EXPR:
101
+ throw UnknownDataTypeException (jsonKeyValue.dump ());
102
+ }
105
103
}
0 commit comments