Skip to content

Commit 43145aa

Browse files
committed
Code optimizations
1 parent 5f1ac10 commit 43145aa

File tree

91 files changed

+2362
-3590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+2362
-3590
lines changed

.github/workflows/ci-go.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ jobs:
1313
strategy:
1414
matrix:
1515
go-version:
16-
- 1.18.x
1716
- 1.19.x
17+
- 1.20.x
1818
os:
1919
- ubuntu-latest
2020
- windows-latest
@@ -41,14 +41,14 @@ jobs:
4141
- name: Run build
4242
working-directory: cli
4343
run: go build -x
44-
44+
4545
- name: Run tests
4646
working-directory: cli
4747
run: go test ./...
4848

4949
- name: Install GoSec
5050
shell: bash
51-
run: curl -sfL https://raw.githubusercontent.com/securego/gosec/v2.13.1/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.13.1
51+
run: curl -sfL https://raw.githubusercontent.com/securego/gosec/v2.15.0/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.15.0
5252

5353
- name: Run security scan
5454
working-directory: cli

cli/go.mod

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
module ccom
22

3-
go 1.19
3+
go 1.20
44

5-
require (
6-
github.com/AlecAivazis/survey/v2 v2.3.6
7-
github.com/cli/safeexec v1.0.1
8-
github.com/fatih/color v1.14.1
9-
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
10-
github.com/stretchr/testify v1.8.1
11-
github.com/urfave/cli/v2 v2.24.3
12-
)
5+
require (
6+
github.com/AlecAivazis/survey/v2 v2.3.6
7+
github.com/cli/safeexec v1.0.1
8+
github.com/fatih/color v1.14.1
9+
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
10+
github.com/stretchr/testify v1.8.1
11+
github.com/urfave/cli/v2 v2.24.3
12+
)
1313

1414
require (
1515
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
16+
<<<<<<< HEAD
1617
github.com/davecgh/go-spew v1.1.1 // indirect
1718
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
1819
github.com/mattn/go-colorable v0.1.13 // indirect
@@ -30,3 +31,18 @@ require (
3031
golang.org/x/text v0.3.8 // indirect
3132
gopkg.in/yaml.v3 v3.0.1 // indirect
3233
)
34+
=======
35+
github.com/davecgh/go-spew v1.1.1 // indirect
36+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
37+
github.com/mattn/go-colorable v0.1.13 // indirect
38+
github.com/mattn/go-isatty v0.0.17 // indirect
39+
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
40+
github.com/pmezard/go-difflib v1.0.0 // indirect
41+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
42+
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
43+
golang.org/x/sys v0.3.0 // indirect
44+
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 // indirect
45+
golang.org/x/text v0.3.3 // indirect
46+
gopkg.in/yaml.v3 v3.0.1 // indirect
47+
)
48+
>>>>>>> b5cac81 (Code optimizations)

cli/go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5959
>>>>>>> 76eeb1a (Bump github.com/fatih/color from 1.13.0 to 1.14.0 in /cli (#199))
6060
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=
6161
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
62+
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
6263
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
64+
<<<<<<< HEAD
6365
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
6466
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
67+
=======
68+
>>>>>>> b5cac81 (Code optimizations)
6569
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
6670
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
6771
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

cli/vendor/golang.org/x/text/width/tables10.0.0.go

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/vendor/golang.org/x/text/width/tables11.0.0.go

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/vendor/golang.org/x/text/width/tables12.0.0.go

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/vendor/golang.org/x/text/width/tables9.0.0.go

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/vendor/modules.txt

+5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ golang.org/x/sys/windows
5555
# golang.org/x/term v0.0.0-20210503060354-a79de5458b56
5656
## explicit; go 1.17
5757
golang.org/x/term
58+
<<<<<<< HEAD
5859
# golang.org/x/text v0.3.8
5960
## explicit; go 1.17
61+
=======
62+
# golang.org/x/text v0.3.3
63+
## explicit; go 1.11
64+
>>>>>>> b5cac81 (Code optimizations)
6065
golang.org/x/text/transform
6166
golang.org/x/text/width
6267
# gopkg.in/yaml.v3 v3.0.1

compiler/.clang-format

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Language: Cpp
2+
BasedOnStyle: LLVM
3+
ColumnLimit: 130

compiler/cpp/setup-libs.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ mkdir lib
33
cd lib
44
mkdir json
55
curl -SsL "https://github.com/nlohmann/json/releases/download/v3.11.2/json.hpp" --output lib/json/json.hpp
6-
git clone --depth 1 --branch release-1.12.1 https://github.com/google/googletest.git
6+
git clone --depth 1 --branch v1.13.0 https://github.com/google/googletest.git

compiler/cpp/src/CMakeLists.txt

+2-48
Original file line numberDiff line numberDiff line change
@@ -12,65 +12,19 @@ set(SOURCES
1212
lexer/Token.h
1313
parser/Parser.cpp
1414
parser/Parser.h
15-
parser/ast/TopLevelExprAST.cpp
16-
parser/ast/TopLevelExprAST.h
17-
parser/ast/NumberExprAST.cpp
18-
parser/ast/NumberExprAST.h
19-
parser/ast/StringExprAST.cpp
20-
parser/ast/StringExprAST.h
21-
parser/ast/IdentifierExprAST.cpp
22-
parser/ast/IdentifierExprAST.h
23-
parser/ast/StmtExprAST.cpp
24-
parser/ast/StmtExprAST.h
25-
parser/ast/StmtLstExprAST.cpp
26-
parser/ast/StmtLstExprAST.h
27-
parser/ast/KeyExprAST.cpp
28-
parser/ast/KeyExprAST.h
29-
parser/ast/IfBlockExprAST.cpp
30-
parser/ast/IfBlockExprAST.h
31-
parser/ast/PayloadExprAST.cpp
32-
parser/ast/PayloadExprAST.h
33-
parser/ast/HasStmtExprAST.cpp
34-
parser/ast/HasStmtExprAST.h
35-
parser/ast/CompStmtExprAST.cpp
36-
parser/ast/CompStmtExprAST.h
37-
parser/ast/ContainsStmtExprAST.cpp
38-
parser/ast/ContainsStmtExprAST.h
39-
parser/ast/ValueExprAST.cpp
40-
parser/ast/ValueExprAST.h
41-
parser/ast/ComBlockBlockExprAST.cpp
42-
parser/ast/ComBlockBlockExprAST.h
43-
parser/ast/ComLineBlockExprAST.cpp
44-
parser/ast/ComLineBlockExprAST.h
45-
parser/ast/SectionExprAST.cpp
46-
parser/ast/SectionExprAST.h
47-
parser/ast/ComBlockExprAST.cpp
48-
parser/ast/ComBlockExprAST.h
49-
parser/ast/ContentExprAST.cpp
50-
parser/ast/ContentExprAST.h
51-
parser/ast/ContentBlockExprAST.cpp
52-
parser/ast/ContentBlockExprAST.h
53-
parser/ast/ArbitraryExprAST.cpp
54-
parser/ast/ArbitraryExprAST.h
55-
parser/ast/BooleanExprAST.cpp
56-
parser/ast/BooleanExprAST.h
15+
parser/ASTNodes.h
5716
analyzer/Analyzer.cpp
5817
analyzer/Analyzer.h
5918
interpreter/Interpreter.cpp
6019
interpreter/Interpreter.h
61-
exception/UnexpectedCharException.cpp
6220
exception/UnexpectedCharException.h
63-
exception/MaxLookaheadException.cpp
6421
exception/MaxLookaheadException.h
6522
exception/UnexpectedTokenException.cpp
6623
exception/UnexpectedTokenException.h
67-
exception/IncompatibleTypesException.cpp
6824
exception/IncompatibleTypesException.h
69-
exception/UnknownDataTypeException.cpp
7025
exception/UnknownDataTypeException.h
71-
exception/UnexpectedDataTypeException.cpp
7226
exception/UnexpectedDataTypeException.h)
7327

7428
add_executable(${BINARY}_run ${SOURCES})
7529

76-
add_library(${BINARY}_lib STATIC ${SOURCES})
30+
add_library(${BINARY}_lib STATIC ${SOURCES} parser/ASTNodes.cpp)

compiler/cpp/src/analyzer/Analyzer.cpp

+71-73
Original file line numberDiff line numberDiff line change
@@ -9,97 +9,95 @@ All rights reserved.
99

1010
Analyzer::Analyzer(bool isSingleStatement, const std::string &fileInput, JSONParser jsonParser,
1111
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;
1415

15-
// Initialize parser
16-
parser = Parser(isSingleStatement, fileInput, inputCommentLineIdentifiers,
17-
inputCommentBlockOpenIdentifiers, inputCommentBlockCloseIdentifiers);
16+
// Initialize parser
17+
parser = Parser(isSingleStatement, fileInput, inputCommentLineIdentifiers, inputCommentBlockOpenIdentifiers,
18+
inputCommentBlockCloseIdentifiers);
1819

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();
2522
}
2623

2724
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
3128
}
3229

3330
// ------------------------------------------------- Private functions -------------------------------------------------
3431

3532
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+
}
4542
}
4643

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;
6961
}
62+
default:
63+
throw std::runtime_error("Got unknown ComBlock object");
64+
}
65+
}
7066
}
67+
}
7168
}
7269

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);
8076
}
77+
}
8178
}
8279

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);
8886

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+
}
105103
}

0 commit comments

Comments
 (0)