Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented for of loop, break, and continue #58

Merged
merged 7 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions Command.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Comparison :

### Loop

#### **`For` loop**:
```
fomo i endup 10
kalo i lebih gede 3
Expand All @@ -98,6 +99,27 @@ for (let i = 0; i < 10; i++) {
}
```

#### **`For of` loop**:
```
fomo semua foo dari bar
spill foo
udahan

// transform to
for (const foo of bar) {
console.log(foo);
}
```

#### **`break`** and **`continue`**:
```
stop
// break;

skip
// continue;
```

### Function
```
so about my_story
Expand Down
25 changes: 25 additions & 0 deletions __test__/logics/parser/loopForOf.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const loopForOf = require('../../../lib/logics/parser/loopForOf');
const getJsFormat = require('../test-parser-helper');

describe('Test var assign', () => {
it('Should return null if not match', () => {
const test1 = loopForOf('fomo semu foo dari bar');
const test2 = loopForOf('fomo smua foo dar bar');
expect(test1).toBe(null);
expect(test2).toBe(null);
});

it('Should return correctly flexing', () => {
const jsFormat = getJsFormat(`
fomo semua foo dari bar
spill foo
udahan
`);
expect(jsFormat).not.toBeNull();
let shouldMatch = ['for (const foo of bar) {', 'console.log(foo)', '}'];
jsFormat.split('\n').every((v, i) => {
if (!shouldMatch[i]) return true;
return expect(v).toContain(shouldMatch[i]);
});
});
});
9 changes: 7 additions & 2 deletions lib/logics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const conditionElIf = require("./parser/conditionElIf");
const conditionElse = require("./parser/conditionElse");
const conditionClose = require("./parser/conditionClose");
const loopFor = require("./parser/loopFor");
const loopForOf = require("./parser/loopForOf");
const functionDeclarationBegin = require("./parser/functionDeclarationBegin");
const functionDeclarationEnd = require("./parser/functionDeclarationEnd");
const functionCall = require("./parser/functionCall");
Expand All @@ -16,6 +17,8 @@ const catchFn = require("./parser/catchFn");
const finallyFn = require("./parser/finallyFn");
const functionDeclarationAsyncBegin = require("./parser/functionDeclarationAsyncBegin");
const awaitProcess = require("./parser/awaitProcess");
const breakStatement = require("./parser/break");
const continueStatement = require("./parser/continue");

function getCmd(cmdLines) {
let parser = [
Expand All @@ -27,6 +30,7 @@ function getCmd(cmdLines) {
conditionElIf,
conditionElse,
conditionClose,
loopForOf,
loopFor,
functionDeclarationBegin,
functionDeclarationEnd,
Expand All @@ -36,8 +40,9 @@ function getCmd(cmdLines) {
catchFn,
finallyFn,
functionDeclarationAsyncBegin,
awaitProcess

awaitProcess,
breakStatement,
continueStatement
];

return cmdLines
Expand Down
12 changes: 12 additions & 0 deletions lib/logics/parser/break.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const breakStatement = (msg) => {
let format = /stop/;
let match = msg.match(format);
if (!match) return null;

return {
exp: `break;`,
};
};

module.exports = breakStatement;

2 changes: 1 addition & 1 deletion lib/logics/parser/constAssign.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const valueTransform = require("../../utils/valueTransform");

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

Expand Down
12 changes: 12 additions & 0 deletions lib/logics/parser/continue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const continueStatement = (msg) => {
let format = /skip/;
let match = msg.match(format);
if (!match) return null;

return {
exp: `continue;`,
};
};

module.exports = continueStatement;

14 changes: 7 additions & 7 deletions lib/logics/parser/loopFor.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const loopFor = (msg) => {
let format = /fomo ([a-zA-Z0-9]+) endup ([a-zA-Z0-9]+)/;
let match = msg.match(format);
if (!match) return null;
let format = /fomo ([a-zA-Z0-9]+) endup ([a-zA-Z0-9]+)/;
let match = msg.match(format);
if (!match) return null;

return {
exp: `for(let ${match[1]} = 0; ${match[1]} <= ${match[2]}; ${match[1]}++)`,
openGroup: true,
};
return {
exp: `for(let ${match[1]} = 0; ${match[1]} <= ${match[2]}; ${match[1]}++)`,
openGroup: true,
};
};

module.exports = loopFor;
12 changes: 12 additions & 0 deletions lib/logics/parser/loopForOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const loopForOf = (msg) => {
let format = /fomo semua ([a-zA-Z]+[a-zA-Z0-9]*) dari ([a-zA-Z]+[a-zA-Z0-9]*)/;
let match = msg.match(format);
if (!match) return null;

return {
exp: `for (const ${match[1]} of ${match[2]})`,
openGroup: true,
};
};

module.exports = loopForOf;
2 changes: 1 addition & 1 deletion lib/logics/parser/varAssign.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const valueTransform = require("../../utils/valueTransform");

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

Expand Down
2 changes: 1 addition & 1 deletion lib/logics/parser/varReassign.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const valueTransform = require("../../utils/valueTransform");

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

Expand Down