-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
207 lines (195 loc) · 7 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
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="shonagon.js" type="module"></script>
<script src="escodegen.js"></script>
<style type="text/css" media="screen">
#editor {
width: 600px;
height: 300px;
}
#editor2 {
width: 600px;
height: 300px;
}
</style>
</head>
<body>
<div id="editor">
function oddEven(val){
if (val % 2) return "odd"
return 'even'
}
</div>
<button id="convert">Convert</button>
<textarea id="assertions">
</textarea>
<div id="result"></div>
<div id="editor2"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.13/ace.min.js" type="text/javascript"
charset="utf-8"></script>
<script type="module">
document.getElementById('assertions').value = JSON.stringify(
{
assertions:
[
{
message: '正しく関数が宣言されていません',
assertion: {
all: [{
type: 'FunctionDeclaration',
}]
}
},
{
message: '関数の引数が正しく設定されていません',
assertion: {
all: [{
type: 'FunctionDeclaration',
params: [
{type: 'Identifier'},
]
}]
}
},
{
message: '条件分岐が行われていません',
assertion: {
all: [
{
type: 'IfStatement'
}
]
}
},
{
message: '判定条件に%が用いられていません',
assertion: {
all: [
{
type: 'BinaryExpression',
operator: '%'
}
]
}
},
{
message: '正しい文字列が使われていません',
assertion: {
all: [
{
type: 'Literal',
value: 'even'
},
{
type: 'Literal',
value: 'odd'
}
]
}
},
{
message: '判定条件の値が誤っています',
assertion: {
all: [
{
none: [{
all: [
{
type: 'BinaryExpression',
operator: '%',
left: {
type: 'Identifier'
},
right: {
type: 'Literal',
value: 2
}
},
{
none: [
{
type: 'BinaryExpression',
operator: '%'
}
]
}
]
}]
},
{
oneOf: [
{
type: 'BinaryExpression',
operator: '%',
left: {
type: 'Identifier'
},
right: {
type: 'Literal',
value: 2
}
},
{
none: [
{
type: 'BinaryExpression',
operator: '%'
}
]
}
]
}
]
}
},
{
message: '関数内でreturn文が使われていません',
assertion: {
all: [
{
type: 'ReturnStatement'
}
]
}
},
{
message: '関数から値が返却されていません',
assertion: {
none: [
{
type: 'ReturnStatement',
argument: null
}
]
}
},
]
}
)
import {ConvertAllCode, ConvertAllToNode, RunAssertions} from "./shonagon.js";
var editor = ace.edit("editor");
editor.setTheme("ace/theme/twilight");
editor.session.setMode("ace/mode/javascript");
var editor2 = ace.edit("editor2");
editor2.setTheme("ace/theme/twilight");
editor2.session.setMode("ace/mode/javascript");
editor2.setReadOnly(true);
document.getElementById('convert').onclick = () => {
let code = editor.getValue()
const result = document.getElementById('result')
let newCode = ConvertAllCode(code);
const fail = RunAssertions(JSON.parse(document.getElementById('assertions').value), ConvertAllToNode(code))
console.log(fail)
if (fail.length) {
result.innerText = fail.map(j => j.message).join('\n')
} else {
result.innerText = 'success'
}
editor2.setValue(newCode)
}
</script>
</body>
</html>