-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclora.js
189 lines (171 loc) · 5.68 KB
/
clora.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
/*
* Clora language virtual machine
*
* This code is free, do what ever you want
*/
Clora = function(program) {
if (program === undefined || typeof program !== 'string') {
throw 'Invalid clora program';
}
this.programs = program.split(';');
this.useNext = function(program, i) {
return (program[i + 1] == 'N') || (program[i + 2] == 'N');
};
this.evaluate = function(P, I, N, opcode, p1, p2) {
if (opcode === 'R') { // READ
if (p1 === 'P') {
return P;
}
if (p1 === 'I') {
return I;
}
if (p1 === 'N') {
return N;
}
return p1;
}
if (opcode === 'T'){ //Translate
return p1[I];
}
if (opcode === '=') { // EQUALS
var v1 = this.evaluate(P, I, N, 'R', p1);
var v2 = this.evaluate(P, I, N, 'R', p2);
return v1 == v2;
}
if (opcode === '<') { // SMALLER
var v1 = this.evaluate(P, I, N, 'R', p1);
var v2 = this.evaluate(P, I, N, 'R', p2);
return v1 < v2;
}
if (opcode === '>') { // BIGGER
var v1 = this.evaluate(P, I, N, 'R', p1);
var v2 = this.evaluate(P, I, N, 'R', p2);
return v1 > v2;
}
}
this.run = function(P, I, N, program) {
var trueFlag = false;
var numericMode = false;
var result = '';
// Quick check for NEXT variable use
var skip = false;
for (var i = 0, l = program.length; i < l; i++) {
if(program[i] === 'N' && !skip){
if(N === undefined){
return undefined;
}
}
if(program[i] === '['){
skip = true;
}
if(program[i] === ']'){
skip = false;
}
}
debugger;
for (var i = 0, l = program.length; i < l; i++) {
console.log("PIN="+P+'/'+I+'/'+N +" CODE="+program.substr(i));
if(program[i] === '@'){ // Numeric mode
numericMode = !numericMode;
i++;
}
if(program[i] === '!'){// Result Input
result = I;
i++;
}
if(program[i] === '+'){ // ADD
if(program[i+1] === 'N'){
I = I + N;
}else{
I = I + Number.parseInt(program.substr(i+1));
}
}
if(program[i] === '%'){ // Modulus
if(program[i+1] === 'N'){
I = I % N;
}else{
I = I % Number.parseInt(program.substr(i+1));
}
}
if(program[i] === 'T'){ // Translate
var array = [];
if(program[i+1] !== '[' ){
throw 'You cant translate without an array';
}
var buff = '';
for(var c=i+2; c<l; c++){
if(program[c] == ','){
if(numericMode){
array.push(Number.parseInt(buff));
}else{
array.push(buff);
}
buff = '';
}else if(program[c] === ']'){
break;
}else{
buff = buff + program[c];
}
i=c;
}
if(numericMode){
array.push(Number.parseInt(buff));
}else{
array.push(buff);
}
I=this.evaluate(P,I,N,'T',array);
}
if ((program[i] == '=' || program[i] == '<' || program[i] == '>') && i + 2 < l) { // Comparators
trueFlag = this.evaluate(P, I, N, program[i], program[i + 1], program[i + 2]);
i = i + 3;
}
if (program[i] == '?' && i + 2 < l) { // If
if (trueFlag) {
result += program[i + 1];
} else {
result += program[i + 2];
}
i = i + 3;
}
}
return result;
}
this.executeProgram = function(program, input) {
if (input === undefined || typeof input !== 'string' || input.length == 0) {
throw 'Wrong input, it must be a string';
}
var P = undefined;
var result = '';
if(program[0] === '@'){ // Numeric mode
input = input.split(' ').map(function(n){
return Number.parseInt(n);
});
}
for (var inputIterator = 0, inputLength = input.length; inputIterator < inputLength; inputIterator++) {
var I = input[inputIterator];
var N = undefined;
if (inputIterator + 1 < inputLength) {
N = input[inputIterator + 1];
}
var ret = this.run(P, I, N, program);
if(ret !== undefined){
result += ret;
}
P = I;
}
return result;
}
this.execute = function(input, callback) {
for (var i = 0, l = this.programs.length; i < l; i++) {
var result = this.executeProgram(this.programs[i], input);
if(result !== undefined){
callback && callback(result);
}
}
}
};
x = new Clora('<0I?01');
x.execute('10101110101010010100010001010110101001010', function(r){
console.log("EXPECTED=10101110101010010100010001010110101001010");
console.log("RESULTED="+r);
});