This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsb2js.js
256 lines (251 loc) · 4.87 KB
/
sb2js.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"use strict";
const convertSB2JS = c => {
let js = c.replace( // Remove leading spaces
/^(.*)/gim,
"$1"
)
js = convertMathMethods(js) // Convert whole Math Library
js = convertTextMethods(js) // Convert whole Text Library
js = convertArrays(js) // Convert everything about arrays
js = js
.replace( // Convert End of Blocks
/(EndIf$)|(EndFor$)|(EndWhile$)|(EndSub$)/gim,
"}"
)
.replace( // COnverts constants
/CONST (.*)/gim,
"const $1"
)
.replace( // Replace For loop without Step
/For ([a-zA-Z0-9_$]+) = ([a-zA-Z0-9_$]+) To ([a-zA-Z0-9_$]+)$/gim,
"for (let $1 = $2; $1 <= $3; $1++){"
)
.replace( // Replace For loop with Step
/For ([a-zA-Z0-9_$]+) = ([a-zA-Z0-9_$&]+) To ([a-zA-Z0-9_$&]+) Step ([a-zA-Z0-9_$&]+)/gim,
"for (let $1 = $2; $1 <= $3; $1+=$4){"
)
.replace( // Convert Variable initialisation
/^([a-zA-Z0-9_$]*\s*=.*)$/gim,
"let $1"
)
.replace( // Replace unequal
/(<>)/gim,
"!="
)
.replace( // Replace TextWindow.WriteLine and .Write
/TextWindow.Write(Line)?\((.*)\)/gim,
"console.log($2)"
)
.replace( // Replace TextWindow.Read
/TextWindow.Read\((.*)\)/gim,
"//prompt('Please insert your data')"
)
.replace( // Replace and
/ AND /gim,
" && "
)
.replace( // Replace or
/ OR /gim,
" || "
)
.replace( // Replace If
/If\s*(.+)\s*Then/gim,
"if ($1) {"
)
.replace( // Replace ElseIf
/ElseIf\s*(.*)\s*Then/gim,
"} else if ($1) {"
)
.replace( // Replace Else
/Else/gim,
"} else {"
)
.replace( // Replace While
/While\s*(.*)/gim,
"while ($1) {"
)
.replace( // Replace comments
/(.*)'(.*)/gim,
"$1//$2"
)
.replace( // Replace True
/True/gim,
"true"
)
.replace( // Replace True
/False/gim,
"false"
)
.replace( // Replace Subs
/Sub (.+)/gim,
"function $1 () {"
)
.replace( // Add ; after every line which doesn't end with { or }
/^(.+)(?<!({|}))$/gim,
"$1;"
)
// TODO:
// Change = to ==, but only in conditions
return js;
}
const convertArrays = s => {
// Benutzer[1] = "Tim" Same Syntax as in JS, so we do not have to convert this
return s
.replace(
/Array.ContainsIndex\((.*),(.*)\)/gim,
"$1[$2]!==undefiend"
)
.replace(
/Array.ContainsValue\((.*),(.*)\)/gim,
"$1.includes($2)"
)
.replace(
/Array.GetAllIndices\((.*)\)/gim,
"Object.keys($1)"
)
.replace(
/Array.GetItemCount\((.*)\)/gim,
"$1.length"
)
.replace(
/Array.IsArray\((.*)\)/gim,
"Array.isArray($1)"
)
.replace(
/Array.SetValue\((.*),(.*),(.*)\)/gim,
"$1[$2] = $3"
)
.replace(
/Array.GetValue\((.*),(.*)\)/gim,
"$1[$2]"
)
.replace(
/Array.RemoveValue\((.*),(.*)\)/gim,
"delete $1[$2]"
)
}
const convertTextMethods = s => {
return s
.replace(
/Text.Append\((.*),(.*)\)/gim,
"$1.concat($2)"
)
.replace(
/Text.GetLength\((.*)\)/gim,
"$1.length"
)
.replace(
/Text.IsSubText\((.*),(.*)\)/gim,
"$1.includes($2)"
)
.replace(
/Text.EndsWith\((.*),(.*)\)/gim,
"$1.endsWith($2)"
)
.replace(
/Text.StartsWith\((.*),(.*)\)/gim,
"$1.startsWith($2)"
)
.replace(
/Text.GetSubTextToEnd\((.*),(.*)\)/gim,
"$1.substr($2)"
)
.replace(
/Text.GetIndexOf\((.*),(.*)\)/gim,
"$1.indexOf($2)"
)
.replace(
/Text.ConvertToLowerCase\((.*)\)/gim,
"$1.toLowerCase()"
)
.replace(
/Text.ConvertToUpperCase\((.*)\)/gim,
"$1.toUpperCase()"
)
.replace(
/Text.GetCharacter\((.*)\)/gim,
"String.fromCharCode($1)"
)
.replace(
/Text.GetCharacterCode\((.*)\)/gim,
"$1.charCodeAt()"
)
}
const convertMathMethods = s => {
return s
.replace(
/Math.Abs\((.*)\)/gim,
"Math.abs($1)"
)
.replace(
/Math.Ceiling\((.*)\)/gim,
"Math.ceil($1)"
)
.replace(
/Math.Floor\((.*)\)/gim,
"Math.floor($1)"
)
.replace(
/Math.NaturalLog\((.*)\)/gim,
"Math.log($1)"
)
.replace(
/Math.Log\((.*)\)/gim,
"Math.log10($1)"
)
.replace(
/Math.Cos\((.*)\)/gim,
"Math.cos($1)"
)
.replace(
/Math.Sin\((.*)\)/gim,
"Math.sin($1)"
)
.replace(
/Math.Tan\((.*)\)/gim,
"Math.tan($1)"
)
.replace(
/Math.ArcTan\((.*)\)/gim,
"Math.atan($1)"
)
.replace(
/Math.ArcSin\((.*)\)/gim,
"Math.asin($1)"
)
.replace(
/Math.ArcCos\((.*)\)/gim,
"Math.acos($1)"
)
.replace(
/Math.SquareRoot\((.*)\)/gim,
"Math.sqrt($1)"
)
.replace(
/Math.Power\((.*),(.*)\)/gim,
"Math.pow($1,$2)"
)
.replace(
/Math.Round\((.*)\)/gim,
"Math.round($1)"
)
.replace(
/Math.Max\((.*),(.*)\)/gim,
"Math.max($1,$2)"
)
.replace(
/Math.Min\((.*),(.*)\)/gim,
"Math.min($1,$2)"
)
.replace(
/Math.Remainder\((.*),(.*)\)/gim,
"$1%$2"
)
/**
* Missing functions:
* - GetDegrees
* - GetRadians
* - GetRandomNumber
* - DoubleToDecimal
*/
}