-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
201 lines (172 loc) · 4.02 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jex test</title>
<script src="escodegen.browser.js"></script>
<script src="JexParser.js"></script>
<script>
function compile(){
var src = document.getElementById('src').value;
var dst = document.getElementById('dst');
try{
var ast = jexParser.parse(src);
dst.value = escodegen.generate(ast);
return true;
}catch(e){
dst.value = e.stack;
return false;
}
}
function run(){
var dst = document.getElementById('dst').value;
var s = document.createElement('script');
s.innerHTML = dst;
document.head.appendChild(s);
document.head.removeChild(s);
}
onload = function(){
var log = console.log;
console.log = function(){
var args = [].slice.call(arguments);
log.apply(console, args);
var domlog = document.getElementById('log');
args.forEach(function(arg){
var v = '';
if(arg == null) v = 'null';
else if(typeof arg === 'function') v = arg.toString();
else v = JSON.stringify(arg, null, ' ');
domlog.value += v + '\n';
});
domlog.scrollTop = 99999999;
};
var editor = document.getElementById('src');
window.onkeydown = function(e){
if(e.ctrlKey || e.metaKey){
if(e.keyCode === 83 || e.keyCode === 13){
e.preventDefault();
compile() && run();
return false;
}
}
};
editor.value = function(){/*
function es5func(a, b){
return a + b;
}
// # は functionと同じ
# altJsFunc(c, d){
return es5func(c + d, 100);
}
var func2 = #(a){console.log(a);};
func2(altJsFunc(5, 10));
// 演算子関数(演算子オーバーロード)
// ^^ は return
var obj = {
value: 100,
+: #(v){
@value += v;
return this;
},
-: #(v){
@value -= v;
^^@;
},
<: #(elm){
@children.push(elm);
^^@;
},
>>: #(){
console.log(@);
^^@;
},
children: []
};
// 使うときは演算子のあとに:
obj <: 'child';
obj +: 100 -: 50 >>: ;
String.prototype['*'] = #(n){
var res = '';
for(var i = 0;i < n; ++i) res += @;
^^res;
};
console.log('simir' + 'a' *: 4);
# Pos(x, y){
@x = x;
@y = y;
}
Pos.prototype = {
-: #(p){
@x -= p.x;
@y -= p.y;
return @;
},
<: #(p){
^^ @ ?: < p ?: ;
},
<=: #(p){
^^ @ ?: <= p ?: ;
},
?: function(){
^^Math.sqrt(@x * @x + @y * @y);
},
};
var p1 = new Pos(1, 2);
var p2 = new Pos(3, 4);
console.log(p1 <: p2);
p2 -: p1 -: p1;
console.log(p1 <=: p2);
var p1 ?:
]> a - p2 ?:
]>> p2.x;
console.log(a, p2);
Number.prototype['**'] = #(n){
^^Math.pow(@, n);
};
console.log(2 **: 10);
*/}.toString().replace(/.*\/\*|\*\/.*/g, '');
compile() && run();
document.getElementById('run').onclick = function(){compile()&&run();};
}
</script>
<style media="screen">
.editor{
width: 48%;
height: 50vh;
}
#log {
width: 95%;
height: 20vh;
}
.flex-between{
display: flex;
justify-content:space-around;
}
</style>
</head>
<body>
<h1>AltJS のテスト</h1>
<p>
README: <a href="https://github.com/simiraaaa/altjstest/blob/master/README.md">https://github.com/simiraaaa/altjstest/blob/master/README.md</a>
</p>
<p>
<button type="button" name="button" id="run">実行!(Command(Ctrl) + Enter)</button>
</p>
<div class="flex-between">
<span>altjs:</span>
<span>コンパイル結果:</span>
</div>
<div>
<textarea id="src" name="editor" class="jex editor">
</textarea>
<textarea id="dst" class="compiled editor">
</textarea>
</div>
<div>
<p>
console:
</p>
<textarea id="log"></textarea>
</div>
</body>
</html>