-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jisp.js
129 lines (105 loc) · 2.9 KB
/
Jisp.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
var Jisp = {
"Get": function (name) {
return Jisp[name];
},
"Set": function (name, value) {
return Jisp[name] = Jisp.Eval(value);
},
"Function": function (parameters, body) {
return function () {
for (var i = 0; i < parameters.length; i++) {
Jisp[parameters[i]] = arguments[i];
}
return Jisp.Eval(body);
}
},
"If": function (condition, trueBody, falseBody) {
if (Jisp.Eval(condition)) {
return Jisp.Eval(trueBody);
}
else {
return Jisp.Eval(falseBody);
}
},
"While": function (condition, body) {
var ret = [];
while (Jisp.Eval(condition)) {
ret.push(Jisp.Eval(body));
}
return ret;
},
"For": function (variable, from, to, step, body) {
var ret = [];
from = Jisp.Eval(from);
to = Jisp.Eval(to);
steps = Jisp.Eval(step);
for (var i = from; i <= to; i += steps) {
Jisp[variable] = i;
ret.push(Jisp.Eval(body));
}
return ret;
},
"+": function (left, right) {
return Jisp.Eval(left) + Jisp.Eval(right);
},
"-": function (left, right) {
return Jisp.Eval(left) - Jisp.Eval(right);
},
"*": function (left, right) {
return Jisp.Eval(left) * Jisp.Eval(right);
},
"/": function (left, right) {
return Jisp.Eval(left) / Jisp.Eval(right);
},
"<": function (left, right) {
return Jisp.Eval(left) < Jisp.Eval(right);
},
">": function (left, right) {
return Jisp.Eval(left) > Jisp.Eval(right);
},
"++": function (variable) {
var ret = Jisp[variable];
Jisp[variable] = ret + 1;
return ret;
},
"--": function (variable) {
var ret = Jisp[variable];
Jisp[variable] = ret - 1;
return ret;
},
"Print": function (value) {
value = Jisp.Eval(value);
console.log(value);
return value;
},
"Sin": function (value) {
return Math.sin(Jisp.Eval(value));
},
"Cos": function (value) {
return Math.cos(Jisp.Eval(value));
},
"Eval": function (exp) {
//literal
if (!Array.isArray(exp)) {
return exp;
}
//boş
if (exp.length == 0) {
return [];
}
//bul
if (typeof exp[0] == "string") {
exp[0] = Jisp[exp[0]];
}
//call
exp[0] = Jisp.Eval(exp[0]);
if (typeof exp[0] == "function") {
return exp[0].apply(this, exp.slice(1));
}
//liste
for (var i = 1; i < exp.length; i++) {
exp[i] = Jisp.Eval(exp[i]);
}
return exp;
}
};