-
Notifications
You must be signed in to change notification settings - Fork 33
/
command.js
220 lines (205 loc) · 6.01 KB
/
command.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* This are the types a Command Argument can be:
* - STR: String, as its parsed it will returned.
* - INT: Integer, the parsed value will be casted into base10 Integer.
* - FLOAT: Float, the parsed value will be casted into a float js object.
* - COMMANDS: List of commands, the parsed value will be parse into Command
* Executors that will be executed.
* - PARAMETERS: Parameters, it almost doesnt make sense but is here to
* demonstrate the power of this way of parsing the content.
*/
const ARGUMENT_TYPES = {
STR: "STR",
INT: "INT",
FLOAT: "FLOAT",
COMMANDS: "COMMANDS",
EXPRESSION: "EXPRESSION",
PARAMETERS: "PARAMETERS" // Example
};
/**
* Argument a command can accept. The most important of this class is the
* type property.
*
* @class CommandArg
*/
class CommandArg {
/**
* Creates an instance of CommandArg.
* @param {String} name Name of the argument
* @param {ARGUMENT_TYPES} type Type of the argument. Its important because
* this will define how the token is parsed.
* @param {*} validator Function used to check if the argument is valid
* @memberof CommandArg
*/
constructor(name, type, validator = undefined) {
this.name = name;
this.type = type;
if (validator === undefined) {
switch (type) {
case ARGUMENT_TYPES.INT:
this.validator = (str) => {
return /^\d+$/.test(str);
}
break;
case ARGUMENT_TYPES.FLOAT:
this.validator = (str) => {
return /^[-+]?[0-9]*\.?[0-9]*$/.test(str);
}
break;
case ARGUMENT_TYPES.EXPRESSION:
this.validator = (str) => {
let res = /^[-+]?([0-9]+\.?[0-9]?|[0-9]*\.?[0-9]+)(\s[+-/*]{1}\s[-+]?([0-9]+\.?[0-9]?|[0-9]*\.?[0-9]+))*$/.test(str);
return res;
}
break;
case ARGUMENT_TYPES.COMMANDS:
this.validator = (str,offset)=>{
let p = new Parser(str,null,offset);
p.parse()
return true;
}
break;
}
} else
this.validator = validator;
}
}
/**
* Command that can be executed.
*
* @class Command
*/
class Command {
/**
* Creates an instance of Command.
* @param {String} name Name of the command, againts this name the tokens will be
* matched.
* @param {[CommandArg]} args An array of CommandArg that this command needs in
* order to work.
* @param {*} func The JS function that will be executed when this command needs
* to be executed.
* @memberof Command
*/
constructor(name, args, func) {
this.name = name;
this.argsTemplate = args;
this.func = func;
}
}
/**
* The command executor purpose is to hold the command to execute and the parsed
* arguments that has been read from the code.
*/
class CommandExecutor {
/**
* Creates an instance of CommandExecutor.
* @param {Command} command The command you will want to execute.
* @param {[String]} values An array of string tokens, this array needs
* to be the same length of the arguments the commands can accept.
* These values will be casted to the argument type it corresponds to.
* @param {Function} callback Function to execute after the command is executed.
* @memberof CommandExecutor
*/
constructor(command, values, callback) {
this.callback = callback;
this.command = command;
this.values = [];
for (let i = 0; i < values.length; i++) {
let value = values[i];
let argTemplate = this.command.argsTemplate[i].type;
switch (argTemplate) {
case ARGUMENT_TYPES.STR:
this.values.push(value);
break;
case ARGUMENT_TYPES.INT:
this.values.push(parseInt(value));
break;
case ARGUMENT_TYPES.FLOAT:
this.values.push(parseFloat(value));
break;
case ARGUMENT_TYPES.COMMANDS:
this.values.push(
new Parser(value, this.callback).parse()
);
break;
case ARGUMENT_TYPES.EXPRESSION:
this.values.push(this.parseExpression(value).eval());
break;
case ARGUMENT_TYPES.PARAMETERS: // Example
this.values.push(value.split(" "));
break;
default:
console.log("Unknown argType: ", argTemplate);
break;
}
}
}
parseExpression(ExpressionString) {
let p = new Parser(ExpressionString,null);
let token = p.nextToken();
let next = p.nextToken();
let e;
let right;
if (next == '/' || next == '*' || next == '+' || next == '-') {
right = this.parseExpression(p.getArgs());
e = new Expression(next,new Expression('$',token), right);
} else
return new Expression('$', token);
if (right.lvl() > e.lvl()) {
let new_left = new Expression(next, new Expression('$',token), right.left);
e = new Expression(right.type, new_left, right.right);
}
return e;
}
/**
* Executes the command with the values given at the creation of the
* instance.
*
* @memberof CommandExecutor
*/
execute(repcount) {
this.command.func.apply(this, this.values);
if (this.callback) {
this.callback();
}
}
}
/**
* It stores all the commands available.
*
* @class CommandLookUp
*/
class CommandLookUp {
/**
* Creates an instance of CommandLookUp.
* @memberokUp
*/
constructor() {
this.commands = [];
}
/**
* Adding a new command to the list.
*
* @param {Command} command New command to add to the list.
* @memberof CommandLookUp
*/
add(command) {
this.commands.push(command);
}
/**
* Return a command that matches the name.
*
* @param {String} name The name of the command you want back.
* @returns The command that matches the name, or null if it can't find it.
* @memberof CommandLookUp
*/
get(name) {
let item = null;
let index = 0;
while (!item && index < this.commands.length) {
if (this.commands[index].name === name) item = this.commands[index];
index++;
}
return item;
}
}