Skip to content

Commit 9a27c2f

Browse files
committed
feat: else and else if
1 parent 653ab60 commit 9a27c2f

File tree

7 files changed

+102
-20
lines changed

7 files changed

+102
-20
lines changed

src/code-quality/complexity/CyclomaticComplexity.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class CyclomaticComplexity {
1515
let cyclomaticComplexity = -1;
1616

1717
tokenizedCode.forEach((token) => {
18+
// console.log(`token: ${token}`);
1819
switch (token) {
1920
case CodeTokens.FOR:
2021
cyclomaticComplexity += 1;
@@ -25,6 +26,12 @@ export class CyclomaticComplexity {
2526
case CodeTokens.IF:
2627
cyclomaticComplexity += 1;
2728
break;
29+
case CodeTokens.ELSEIF:
30+
cyclomaticComplexity += 1;
31+
break;
32+
case CodeTokens.ELSE:
33+
cyclomaticComplexity += 1;
34+
break;
2835
case CodeTokens.CASE:
2936
cyclomaticComplexity += 1;
3037
break;
@@ -37,6 +44,7 @@ export class CyclomaticComplexity {
3744
default:
3845
break;
3946
}
47+
// console.log(`cyclo: ${cyclomaticComplexity}`);
4048
});
4149

4250
return cyclomaticComplexity;

src/code-quality/tokenizer/CodeTokenizer.ts

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,31 +59,36 @@ export class CodeTokenizer {
5959
) {
6060
tokenizedCode.push(CodeTokens.WHILE);
6161
// If line
62-
} else if (
63-
trimmedLine.startsWith(languageIdentifier.ifIdentifier()) &&
64-
trimmedLine.endsWith(languageIdentifier.endLoopAndCondIdentifier())
65-
) {
62+
} else if (languageIdentifier.isIfBlock(trimmedLine)) {
6663
tokenizedCode.push(CodeTokens.IF);
67-
const andOccurrences = CodeTokenizer.numberTokenPresentInLine(
68-
languageIdentifier.andIdentifier(),
64+
tokenizedCode = CodeTokenizer.getAndOrTokens(
6965
trimmedLine,
70-
);
71-
tokenizedCode = CodeTokenizer.pushTokensInTab(
66+
languageIdentifier,
7267
tokenizedCode,
73-
CodeTokens.AND,
74-
andOccurrences,
7568
);
7669

77-
const orOccurrences = CodeTokenizer.numberTokenPresentInLine(
78-
languageIdentifier.orIdentifier(),
70+
// Else if line
71+
} else if (
72+
CodeTokenizer.numberTokenPresentInLine(
73+
languageIdentifier.elseIfIdentifier(),
7974
trimmedLine,
80-
);
81-
tokenizedCode = CodeTokenizer.pushTokensInTab(
75+
) > 0
76+
) {
77+
tokenizedCode.push(CodeTokens.ELSEIF);
78+
tokenizedCode = CodeTokenizer.getAndOrTokens(
79+
trimmedLine,
80+
languageIdentifier,
8281
tokenizedCode,
83-
CodeTokens.OR,
84-
orOccurrences,
8582
);
8683

84+
// Else line
85+
} else if (
86+
CodeTokenizer.numberTokenPresentInLine(
87+
languageIdentifier.elseIdentifier(),
88+
trimmedLine,
89+
) > 0
90+
) {
91+
tokenizedCode.push(CodeTokens.ELSE);
8792
// Case line
8893
} else if (
8994
trimmedLine.startsWith(languageIdentifier.caseIdentifier()) &&
@@ -102,10 +107,39 @@ export class CodeTokenizer {
102107
tokenizedCode.push(CodeTokens.LINE);
103108
}
104109

105-
console.log(trimmedLine);
110+
// console.log(trimmedLine);
106111
});
107112

108-
console.log(tokenizedCode);
113+
// console.log(tokenizedCode);
114+
115+
return tokenizedCode;
116+
}
117+
118+
private static getAndOrTokens(
119+
line: string,
120+
languageIdentifier: CommonLangIdentifiers,
121+
tokens: CodeTokens[],
122+
): CodeTokens[] {
123+
let tokenizedCode = tokens;
124+
const andOccurrences = CodeTokenizer.numberTokenPresentInLine(
125+
languageIdentifier.andIdentifier(),
126+
line,
127+
);
128+
tokenizedCode = CodeTokenizer.pushTokensInTab(
129+
tokenizedCode,
130+
CodeTokens.AND,
131+
andOccurrences,
132+
);
133+
134+
const orOccurrences = CodeTokenizer.numberTokenPresentInLine(
135+
languageIdentifier.orIdentifier(),
136+
line,
137+
);
138+
tokenizedCode = CodeTokenizer.pushTokensInTab(
139+
tokenizedCode,
140+
CodeTokens.OR,
141+
orOccurrences,
142+
);
109143

110144
return tokenizedCode;
111145
}

src/code-quality/tokenizer/CodeTokens.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export enum CodeTokens {
77
WHILE = 'WHILE',
88
// conditions
99
IF = 'IF',
10+
ELSE = 'ELSE',
11+
ELSEIF = 'ELSEIF',
1012
CASE = 'CASE',
1113
OR = 'OR',
1214
AND = 'AND',

src/code-quality/tokenizer/identifiers/CommonLangIdentifiers.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,32 @@ export class CommonLangIdentifiers {
3535
return '//';
3636
}
3737

38+
elseIdentifier(): string {
39+
return 'else';
40+
}
41+
42+
elseIfIdentifier(): string {
43+
return 'else if';
44+
}
45+
3846
isFunction(line: string): boolean {
3947
return (
4048
line.startsWith(this.functionIdentifier()) &&
4149
line.endsWith(this.endLoopAndCondIdentifier())
4250
);
4351
}
52+
53+
isIfBlock(line: string): boolean {
54+
return (
55+
line.startsWith(this.ifIdentifier()) &&
56+
line.endsWith(this.endLoopAndCondIdentifier())
57+
);
58+
}
59+
60+
isElseBlock(line: string): boolean {
61+
return (
62+
line.startsWith(this.elseIdentifier()) &&
63+
line.endsWith(this.endLoopAndCondIdentifier())
64+
);
65+
}
4466
}

src/code-quality/tokenizer/identifiers/CppIdentifiers.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@ export class CppIdentifiers extends CommonLangIdentifiers {
88
line.startsWith('int') ||
99
line.startsWith('float') ||
1010
line.startsWith('double')) &&
11-
line.endsWith('{')
11+
(line.endsWith('{') || line.endsWith(')')) &&
12+
line.split('=').length <= 1
13+
);
14+
}
15+
16+
isIfBlock(line: string): boolean {
17+
return (
18+
super.isIfBlock(line) ||
19+
(line.split('if').length >= 2 && line.endsWith(')'))
1220
);
1321
}
1422
}

src/code-quality/tokenizer/identifiers/PythonIdentifiers.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ export class PythonIdentifiers extends CommonLangIdentifiers {
1616
endLoopAndCondIdentifier(): string {
1717
return ':';
1818
}
19+
20+
elseIdentifier(): string {
21+
return 'else';
22+
}
23+
24+
elseIfIdentifier(): string {
25+
return 'elif';
26+
}
1927
}

src/submissions/submissions.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ export class SubmissionsService {
6262
submission.benchmark,
6363
insertSubmissionDTO.language,
6464
);
65-
// submission.self = submission;
6665
const tokenizedCode = this.codeTokenizer.tokenize(
6766
submission.code,
6867
submission.language,
6968
);
7069
submission.cyclomaticComplexity =
7170
this.cyclomaticComplexity.compute(tokenizedCode);
71+
// console.log(cycloComp);
7272

7373
return submission.save();
7474
}

0 commit comments

Comments
 (0)