forked from hitode909/auto-oneliner-music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
b.js
179 lines (179 loc) · 5.01 KB
/
b.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
var Shiki, stdin, t;
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
function ctor() { this.constructor = child; }
ctor.prototype = parent.prototype;
child.prototype = new ctor;
child.__super__ = parent.prototype;
return child;
};
Shiki = (function() {
function Shiki(variable_name) {
this.variable_name = variable_name;
this.root = new Shiki.Operator('*');
this.root.left = new Shiki.Operand.Variable(this.variable_name);
this.root.right = new Shiki.Operand.Number(1);
}
Shiki.prototype.getFunction = function() {
return eval(("(function(" + this.variable_name + "){return ") + this.getString() + ";})");
};
Shiki.prototype.getString = function() {
return this.root.getString();
};
Shiki.prototype.step = function() {
var after, before, i, node, _results;
i = 0;
before = this.root.getString();
after = before;
_results = [];
while (i < 10 && before === after) {
node = this.getRandomOperator(this.root);
Shiki.choise([this.wrapNode, this.cutNode, this.bang]).apply(this, [node]);
after = this.root.getString();
_results.push(i++);
}
return _results;
};
Shiki.prototype.getRandomOperator = function(root) {
var current, index;
current = root;
while (Math.random() < 0.3) {
index = current.randomIndex();
if (current[index].isOperator) {
current = current[index];
} else {
return current;
}
}
return current;
};
Shiki.prototype.getRandomNode = function(root) {
var current;
current = root;
while (current.isOperator && Math.random() < 0.8) {
current = current[current.randomIndex()];
}
return current;
};
Shiki.prototype.getRandomOperand = function(root) {
var current;
current = root;
while (current.isOperator) {
current = current[current.randomIndex()];
}
return current;
};
Shiki.prototype.wrapNode = function(node) {
var index, lr, operator;
index = node.randomIndex();
operator = this.randomOperator();
lr = [node[index], this.randomInstance()];
if (Math.random() > 0.5) {
lr = [lr[1], lr[0]];
}
operator.left = lr[0];
operator.right = lr[1];
return node[index] = operator;
};
Shiki.prototype.cutNode = function(node) {
var child, index;
index = node.randomIndex();
child = this.getRandomNode(node[index]);
return node[index] = child;
};
Shiki.prototype.bang = function(node) {
return node.bang();
};
Shiki.prototype.changeValue = function(node) {
return node[node.randomIndex()] = this.randomInstance();
};
Shiki.prototype.randomOperator = function() {
var r;
r = new Shiki.Operator(Shiki.choise(Shiki.Operator.operators));
r.left = this.randomInstance();
r.right = this.randomInstance();
return r;
};
Shiki.prototype.randomInstance = function() {
var rand;
rand = Math.random();
if (rand > 0.7) {
return this.randomOperator();
} else if (rand > 0.4) {
return new Shiki.Operand.Number;
} else {
return new Shiki.Operand.Variable(this.variable_name);
}
};
return Shiki;
})();
Shiki.choise = function(list) {
return list[Math.floor(Math.random() * list.length)];
};
Shiki.Operator = (function() {
function Operator(operator) {
if (operator != null) {
this.operator = operator;
} else {
this.bang();
}
this.left = new Shiki.Operand(0);
this.right = new Shiki.Operand(0);
}
Operator.prototype.getString = function() {
return "(" + [this.left.getString(), this.operator, this.right.getString()].join('') + ")";
};
Operator.prototype.bang = function() {
this.operator = Shiki.choise(Shiki.Operator.operators);
if (this.left) {
this.left.bang();
}
if (this.right) {
return this.right.bang();
}
};
Operator.prototype.isOperator = true;
Operator.prototype.randomIndex = function() {
return Shiki.choise(['left', 'right']);
};
return Operator;
})();
Shiki.Operator.operators = '* % / + & | ^ << >>'.split(/\s+/);
Shiki.Operand = (function() {
function Operand(value) {
this.value = value;
}
Operand.prototype.getString = function() {
return this.value;
};
Operand.prototype.isOperator = false;
return Operand;
})();
Shiki.Operand.Variable = (function() {
__extends(Variable, Shiki.Operand);
function Variable() {
Variable.__super__.constructor.apply(this, arguments);
}
Variable.prototype.bang = function() {};
return Variable;
})();
Shiki.Operand.Number = (function() {
__extends(Number, Shiki.Operand);
function Number() {
this.bang();
}
Number.prototype.bang = function() {
return this.value = Math.floor(Math.random() * 10) + 1;
};
return Number;
})();
t = new Shiki('t');
stdin = process.openStdin();
stdin.on('data', function(event) {
if (event.toString() === "reset\n") {
t = new Shiki('t');
} else {
t.step();
}
return console.log(t.getString());
});