-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
227 lines (191 loc) · 6.19 KB
/
app.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
221
222
223
224
225
226
227
var content = "";
var operations = [];
var id = {};
var program = {};
function processMute() {
content = $("#inputArea").val();
operations = content.split("\n");
// Remove nil Lines ( "" )
for (var i = 0; i < operations.length; i++) {
if (operations[i] === "") {
delete operations[i];
}
}
for (var j = 0; j < operations.length; j++) {
console.log("Line: " + j);
var value = operations[j];
var pattern = /[^a-z0-9]/i;
id = value.split(pattern);
id = id[0];
update(value);
interpreter(id);
}
function resolve(run) {
var varRegex = /[^a-z0-9.$]/i;
var operRegex = /[a-z0-9.$]/i;
var varTemp;
var operTemp;
if (run["cond"]) {
varTemp = run["cond"].split(varRegex);
operTemp = run["cond"].split(operRegex);
} else {
varTemp = "";
operTemp = "";
}
run["cond_variables"] = varTemp;
run["cond_operators"] = operTemp;
var first = renderValue(run["cond_variables"][0]);
var second = renderValue(run["cond_variables"][1]);
var operator = run["cond_operators"][0];
var resolving;
switch (operator) {
case ">":
resolving = first > second;
break;
case "<":
resolving = first < second;
break;
case "=":
resolving = first === second;
break;
}
if(run["cond_variables"].length < 2 && first > 0) {
resolving = 1;
}
if (resolving || !run["cond"]) {
return true;
} else {
return false;
}
}
function renderString(operation) {
var string = operation.match(/"([^"]+)"/);
string = string[1];
var replacements = operation.replace("\"" + string + "\",", "");
replacements = replacements.split(",");
for (var i = 0; i < replacements.length; i++) {
replacements[i] = renderValue(replacements[i]);
}
var result = strig.replace(["@1","@2","@3"], replacements);
return result;
}
function operate(operation) {
if (operation.charAt(0) === "\"") {
console.log(renderString(operation));
} else {
program = update(operation);
interpreter(id);
}
}
function interpreter(id) {
if (resolve(program[id]) && (program[id] === "oper")) {
operate(program[id] = "oper");
}
}
// Updater
function update(value) {
var pattern = /[^a-z0-9]/i;
id = value.split(pattern);
id = id[0];
var attr = value.match(/\[(.*?)\]/);
if (attr) {
attr = attr[1];
}
var cond = value.match(/\((.*?)\)/);
if (cond) {
cond = cond[1];
}
var oper = value.match(/\{(.*?)\}/);
if (oper) {
oper = oper[1];
}
id.toString();
program[id] = {"name": id};
if (attr) {
program[id] = {"attr": update_attr(attr)};
}
if (cond) {
program[id] = {"cond": cond};
}
if (oper) {
program[id] = {"oper": oper};
}
console.log(program);
}
function update_attr(attr) {
var temp = attr.split(",");
for (var i = 0; i < temp.length; i++) {
console.log("Attr: " + i);
temp[i] = renderValue(temp[i]);
}
return temp;
}
function renderValue(val) {
if (!val) {
return null;
}
var modRegex = /[a-z0-9.$]/i;
var attrMods = val.split(modRegex);
var modifier = attrMods[1];
var contRegex = /[^a-z0-9.$]/i;
var attrContent = val.split(contRegex);
var key = dotValue(attrContent[0]);
var index = dotValue(attrContent[1]);
// Random
if (val.indexOf("~") !== -1) {
console.log("Rand:", key, index);
return randomFromInterval(key, index);
}
// Length
if (val.indexOf(";") !== -1) {
console.log("Length: ", program[key] = {"attr": 0}.length);
return key.length;
}
// Merge
if (val.indexOf("&") !== -1) {
console.log("Merge: " + key, index);
return key.toString() + index.toString();
}
// Combine
if (val.indexOf("+")!== -1) {
console.log("Add: ", key, index);
return key + index;
}
//TODO: Index
if (val.indexOf(":") !== -1) {
console.log("ERROR: Index not (jet) implemented.");
}
// Default
if (program[key] = {"attr": index} && attrMods.length < 1) {
console.log("Get: ", key, index);
return program[key] = {"attr": index};
}
// Return
return val;
}
function dotValue(dotvalue) {
if (dotvalue) {
if (dotvalue.indexOf(".") !== -1) {
var elements = dotvalue.split(".");
var key = elements[0];
var index = elements[1];
console.log("Get: " + key, index);
if (program[key]["attr"][index]) {
return program[key] = {"attr": index};
}
} else if (program[dotvalue] === {"attr": 0}) {
return program[dotvalue] = {"attr": 0}
}
return dotvalue;
}
return dotvalue;
}
// From http://stackoverflow.com/questions/4959975/generate-random-value-between-two-numbers-in-javascript
// TODO: Fix random problem, http://jsfiddle.net/2XhW4/ works but this not
function randomFromInterval(from, to)
{
var rand = Math.floor(Math.random()*(to - from + 1) + from);
console.log(rand); // Not the same as link above (why?)
return Math.floor(Math.random()*(to - from + 1) + from);
}
}