-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathkwnodefun.js
57 lines (45 loc) · 2.03 KB
/
kwnodefun.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
const constants = require("../../constants.js");
const BaseNode = require("../basenode.js");
const kwNodeJeki = require("./kwnodejeki.js");
const feedbackMessages = require("../../feedbackMessages.js");
const bracketExpressionNl = require("../nodeLiterals/bracketexpressionnl.js");
class KwNodeFun extends BaseNode {
constructor () {
super();
if (this.isDependenciesInValid()) {
throw new Error(feedbackMessages.baseNodeType("Dependencies"));
}
}
isDependenciesInValid () {
return !(kwNodeJeki instanceof BaseNode) && !(bracketExpressionNl instanceof BaseNode);
}
getNode () {
this.skipKeyword(constants.KW.FUN);
this.skipPunctuation(constants.SYM.L_BRACKET);
const node = {};
node.operation = constants.KW.FUN;
node.init = kwNodeJeki.getNode.call(this);
node.condition = bracketExpressionNl.getNode.call(this, { isArithmeticExpression: false, isBracketExpected: false, });
this.skipPunctuation(constants.SYM.STATEMENT_TERMINATOR);
node.increment = kwNodeJeki.getNode.call(this, { shouldExpectTerminator: false, });
if (KwNodeFun.isInValidFunIncrementStatement(node)) {
this.throwError(feedbackMessages.funIncrementAndDecrementMsg());
}
this.skipPunctuation(constants.SYM.R_BRACKET);
node.body = this.parseBlock(constants.KW.FUN);
return node;
}
static isInValidFunIncrementStatement (funNode) {
const incrementNode = funNode.increment.right;
if ([ constants.SYM.PLUS, constants.SYM.MINUS, ].includes(incrementNode.operation)) {
// e.g fun (tí i =0; i < 10; tí i = i + 1;)
// make sure there is variable 'i' in atleast one child of the incrementNode
// i.e jeki i = i + 1 or jeki i = 1 + i or jeki i = i + i
if ([ incrementNode.left.name, incrementNode.right.name, ].includes(funNode.init.left)) {
return false;
}
}
return true;
}
}
module.exports = new KwNodeFun();