Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit 7a02d61

Browse files
authored
Merge pull request #58 from Andndre/main
Implemented `for of loop`, `break`, and `continue`
2 parents 88aa614 + 2d49053 commit 7a02d61

File tree

10 files changed

+100
-12
lines changed

10 files changed

+100
-12
lines changed

Command.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ Comparison :
8383

8484
### Loop
8585

86+
#### **`For` loop**:
8687
```
8788
fomo i endup 10
8889
kalo i lebih gede 3
@@ -98,6 +99,27 @@ for (let i = 0; i < 10; i++) {
9899
}
99100
```
100101

102+
#### **`For of` loop**:
103+
```
104+
fomo semua foo dari bar
105+
spill foo
106+
udahan
107+
108+
// transform to
109+
for (const foo of bar) {
110+
console.log(foo);
111+
}
112+
```
113+
114+
#### **`break`** and **`continue`**:
115+
```
116+
stop
117+
// break;
118+
119+
skip
120+
// continue;
121+
```
122+
101123
### Function
102124
```
103125
so about my_story
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const loopForOf = require('../../../lib/logics/parser/loopForOf');
2+
const getJsFormat = require('../test-parser-helper');
3+
4+
describe('Test var assign', () => {
5+
it('Should return null if not match', () => {
6+
const test1 = loopForOf('fomo semu foo dari bar');
7+
const test2 = loopForOf('fomo smua foo dar bar');
8+
expect(test1).toBe(null);
9+
expect(test2).toBe(null);
10+
});
11+
12+
it('Should return correctly flexing', () => {
13+
const jsFormat = getJsFormat(`
14+
fomo semua foo dari bar
15+
spill foo
16+
udahan
17+
`);
18+
expect(jsFormat).not.toBeNull();
19+
let shouldMatch = ['for (const foo of bar) {', 'console.log(foo)', '}'];
20+
jsFormat.split('\n').every((v, i) => {
21+
if (!shouldMatch[i]) return true;
22+
return expect(v).toContain(shouldMatch[i]);
23+
});
24+
});
25+
});

lib/logics/index.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const conditionElIf = require("./parser/conditionElIf");
77
const conditionElse = require("./parser/conditionElse");
88
const conditionClose = require("./parser/conditionClose");
99
const loopFor = require("./parser/loopFor");
10+
const loopForOf = require("./parser/loopForOf");
1011
const functionDeclarationBegin = require("./parser/functionDeclarationBegin");
1112
const functionDeclarationEnd = require("./parser/functionDeclarationEnd");
1213
const functionCall = require("./parser/functionCall");
@@ -16,6 +17,8 @@ const catchFn = require("./parser/catchFn");
1617
const finallyFn = require("./parser/finallyFn");
1718
const functionDeclarationAsyncBegin = require("./parser/functionDeclarationAsyncBegin");
1819
const awaitProcess = require("./parser/awaitProcess");
20+
const breakStatement = require("./parser/break");
21+
const continueStatement = require("./parser/continue");
1922

2023
function getCmd(cmdLines) {
2124
let parser = [
@@ -27,6 +30,7 @@ function getCmd(cmdLines) {
2730
conditionElIf,
2831
conditionElse,
2932
conditionClose,
33+
loopForOf,
3034
loopFor,
3135
functionDeclarationBegin,
3236
functionDeclarationEnd,
@@ -36,8 +40,9 @@ function getCmd(cmdLines) {
3640
catchFn,
3741
finallyFn,
3842
functionDeclarationAsyncBegin,
39-
awaitProcess
40-
43+
awaitProcess,
44+
breakStatement,
45+
continueStatement
4146
];
4247

4348
return cmdLines

lib/logics/parser/break.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const breakStatement = (msg) => {
2+
let format = /stop/;
3+
let match = msg.match(format);
4+
if (!match) return null;
5+
6+
return {
7+
exp: `break;`,
8+
};
9+
};
10+
11+
module.exports = breakStatement;
12+

lib/logics/parser/constAssign.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const valueTransform = require("../../utils/valueTransform");
22

33
const constAssign = (msg) => {
4-
let format = /seriously ([a-zA-Z0-9]+) itu ([^\[\]\(\)\n]+)/;
4+
let format = /seriously ([a-zA-Z_]+[a-zA-Z0-9_]*) itu ([^\[\]\(\)\n]+)/;
55
let match = msg.match(format);
66
if (!match) return null;
77

lib/logics/parser/continue.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const continueStatement = (msg) => {
2+
let format = /skip/;
3+
let match = msg.match(format);
4+
if (!match) return null;
5+
6+
return {
7+
exp: `continue;`,
8+
};
9+
};
10+
11+
module.exports = continueStatement;
12+

lib/logics/parser/loopFor.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const loopFor = (msg) => {
2-
let format = /fomo ([a-zA-Z0-9]+) endup ([a-zA-Z0-9]+)/;
3-
let match = msg.match(format);
4-
if (!match) return null;
2+
let format = /fomo ([a-zA-Z0-9]+) endup ([a-zA-Z0-9]+)/;
3+
let match = msg.match(format);
4+
if (!match) return null;
55

6-
return {
7-
exp: `for(let ${match[1]} = 0; ${match[1]} <= ${match[2]}; ${match[1]}++)`,
8-
openGroup: true,
9-
};
6+
return {
7+
exp: `for(let ${match[1]} = 0; ${match[1]} <= ${match[2]}; ${match[1]}++)`,
8+
openGroup: true,
9+
};
1010
};
1111

1212
module.exports = loopFor;

lib/logics/parser/loopForOf.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const loopForOf = (msg) => {
2+
let format = /fomo semua ([a-zA-Z]+[a-zA-Z0-9]*) dari ([a-zA-Z]+[a-zA-Z0-9]*)/;
3+
let match = msg.match(format);
4+
if (!match) return null;
5+
6+
return {
7+
exp: `for (const ${match[1]} of ${match[2]})`,
8+
openGroup: true,
9+
};
10+
};
11+
12+
module.exports = loopForOf;

lib/logics/parser/varAssign.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const valueTransform = require("../../utils/valueTransform");
22

33
const varAssign = (msg) => {
4-
let format = /literally ([a-zA-Z0-9]+) itu ([^\[\]\(\)\n]+)/;
4+
let format = /literally ([a-zA-Z_]+[a-zA-Z0-9_]*) itu ([^\[\]\(\)\n]+)/;
55
let match = msg.match(format);
66
if (!match) return null;
77

lib/logics/parser/varReassign.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const valueTransform = require("../../utils/valueTransform");
22

33
const varReassign = (msg) => {
4-
let format = /whichis ([a-zA-Z0-9]+) itu ([^\[\]\(\)\n]+)/;
4+
let format = /whichis ([a-zA-Z_]+[a-zA-Z0-9_]*) itu ([^\[\]\(\)\n]+)/;
55
let match = msg.match(format);
66
if (!match) return null;
77

0 commit comments

Comments
 (0)