-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddChildRule.ts
56 lines (46 loc) · 1.74 KB
/
AddChildRule.ts
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
///<reference path="Command.ts"/>
///<reference path="Environment.ts"/>
///<reference path="CommandBuilder.ts"/>
///<reference path="Rule.ts"/>
/**
* Created by Evan on 10/17/2016.
*/
class AddChildRule implements Rule{
checkToken(token: string): boolean {
console.log("Inside add child");
var tokens = token.split(" ");
if(!(tokens[0].split(".").length == 2)) {
console.log(tokens[0].split(".").length);
console.log(tokens[0].split(".")[0]);
console.log("1");
return false
}
var firstDot = tokens[0].indexOf(".");
var firstPar = tokens[0].indexOf("(");
var arg = tokens[0].substring(firstDot + 1, firstPar + 1);
if(!(arg == "addChild(")){
console.log("2 " + arg);
return false;
}
return true;
}
getCommand(token: string): Command {
var run = function(environment: Environment){
var tokens = token.split(" ");
var varHeader = tokens[0];
var firstDot = tokens[0].indexOf(".");
var firstPar = tokens[0].indexOf("(");
var lastPar = tokens[0].lastIndexOf(")");
var arg = tokens[0].substring(firstPar + 1, lastPar);
var varName = tokens[0].substring(0, firstDot);
if(!(environment.hasVariable(varName))){
throw new Error("No such variable \"" + varName + "\" : " + token);
}
if(!(environment.hasVariable(arg))){
throw new Error("No such variable \"" + arg + "\" : " + token);
}
environment.getVariable(varName).content.addChild(environment.getVariable(arg).content);
};
return CommandBuilder.build(run);
}
}