1 | // Porting from
|
---|
2 | // https://github.com/jeanlazarou/blockly2ruby
|
---|
3 | // Copyright (c) 2014 Jean Lazarou
|
---|
4 | // MIT Lisence
|
---|
5 | using System;
|
---|
6 | using System.Linq;
|
---|
7 | using Bridge;
|
---|
8 | using System.Collections.Generic;
|
---|
9 | using System.Runtime.InteropServices;
|
---|
10 |
|
---|
11 | namespace WebMrbc
|
---|
12 | {
|
---|
13 | [ComVisible(true)]
|
---|
14 | public partial class Ruby : Generator, IMrbParser
|
---|
15 | {
|
---|
16 | static Ruby()
|
---|
17 | {
|
---|
18 | /**
|
---|
19 | * List of illegal variable names.
|
---|
20 | * This is not intended to be a security feature. Blockly is 100% client-side,
|
---|
21 | * so bypassing this list is trivial. This is intended to prevent users from
|
---|
22 | * accidentally clobbering a built-in object or function.
|
---|
23 | * @private
|
---|
24 | */
|
---|
25 | addReservedWords(
|
---|
26 | "Class,Object,BEGIN,END,__ENCODING__,__END__,__FILE__,__LINE__" +
|
---|
27 | "alias,and,begin,break,case,class,def,defined?,do,else,elsif,end,ensure,false,for,if,in,module,next" +
|
---|
28 | "nil,not,or,redo,rescue,retry,return,self,super,then,true,undef,unless,until,when,while,yield");
|
---|
29 | }
|
---|
30 |
|
---|
31 | node tree;
|
---|
32 | node[] allNodes;
|
---|
33 | locals_node locals;
|
---|
34 | bool global = true;
|
---|
35 |
|
---|
36 | public Ruby(string filename)
|
---|
37 | : base("Ruby")
|
---|
38 | {
|
---|
39 | this.filename = filename;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Initialise the database of variable names.
|
---|
44 | */
|
---|
45 | public override void init(Workspace workspace)
|
---|
46 | {
|
---|
47 | locals = new locals_node(null);
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Prepend the generated code with the variable definitions.
|
---|
52 | * @param {string} code Generated code.
|
---|
53 | * @return {string} Completed code.
|
---|
54 | */
|
---|
55 | public override string finish(node[] codes)
|
---|
56 | {
|
---|
57 | tree = new scope_node(this, new begin_node(this, codes));
|
---|
58 | var cond = new ruby_code_cond(filename, INDENT);
|
---|
59 | tree.to_ruby(cond);
|
---|
60 | allNodes = cond.nodes;
|
---|
61 | return cond.ToString();
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Naked values are top-level blocks with outputs that aren't plugged into
|
---|
66 | * anything.
|
---|
67 | * @param {string} line Line of generated code.
|
---|
68 | * @return {string} Legal line of code.
|
---|
69 | */
|
---|
70 | public override node[] scrubNakedValue(node[] line)
|
---|
71 | {
|
---|
72 | return line;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Common tasks for generating Ruby from blocks.
|
---|
77 | * Handles comments for the specified block and any connected value blocks.
|
---|
78 | * Calls any statements following this block.
|
---|
79 | * @param {!Blockly.Block} block The current block.
|
---|
80 | * @param {string} code The Ruby code created for this block.
|
---|
81 | * @return {string} Ruby code with comments and subsequent blocks added.
|
---|
82 | * @this {Blockly.CodeGenerator}
|
---|
83 | * @private
|
---|
84 | */
|
---|
85 | public override node[] scrub_(Block block, node[] code)
|
---|
86 | {
|
---|
87 | var commentCode = "";
|
---|
88 | // Only collect comments for blocks that aren't inline.
|
---|
89 | if (block.outputConnection == null || block.outputConnection.targetConnection == null) {
|
---|
90 | // Collect comment for this block.
|
---|
91 | var comment = block.getCommentText();
|
---|
92 | if (!String.IsNullOrEmpty(comment)) {
|
---|
93 | commentCode += this.prefixLines(comment, "# ") + "\n";
|
---|
94 | }
|
---|
95 | // Collect comments for all value arguments.
|
---|
96 | // Don't collect comments for nested statements.
|
---|
97 | var inputList = block.inputList;
|
---|
98 | for (var x = 0; x < inputList.Length; x++) {
|
---|
99 | if (inputList[x].type == Blockly.INPUT_VALUE) {
|
---|
100 | var childBlock = inputList[x].connection.targetBlock();
|
---|
101 | if (childBlock != null) {
|
---|
102 | comment = allNestedComments(childBlock);
|
---|
103 | if (!String.IsNullOrEmpty(comment)) {
|
---|
104 | commentCode += this.prefixLines(comment, "# ");
|
---|
105 | }
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|
110 | Block nextBlock = null;
|
---|
111 | if (block.nextConnection != null)
|
---|
112 | nextBlock = block.nextConnection.targetBlock();
|
---|
113 | var nextCode = blockToCode(nextBlock);
|
---|
114 | if (nextCode == null)
|
---|
115 | return code;
|
---|
116 | return code.Concat(nextCode).ToArray();
|
---|
117 | }
|
---|
118 |
|
---|
119 | public int lineno { get; set; }
|
---|
120 | public int column { get; set; }
|
---|
121 | public string filename { get; }
|
---|
122 |
|
---|
123 | string[] syms = new string[0];
|
---|
124 |
|
---|
125 | private mrb_sym get_sym(string str)
|
---|
126 | {
|
---|
127 | int i = Array.IndexOf(syms, str);
|
---|
128 | if (i < 0) {
|
---|
129 | i = syms.Length;
|
---|
130 | syms.Push(str);
|
---|
131 | }
|
---|
132 | return (mrb_sym)(i + 1);
|
---|
133 | }
|
---|
134 |
|
---|
135 | public string sym2name(mrb_sym sym)
|
---|
136 | {
|
---|
137 | int i = (int)sym - 1;
|
---|
138 | if ((i < 0) || (i >= syms.Length))
|
---|
139 | return ((int)sym).ToString();
|
---|
140 | return syms[i];
|
---|
141 | }
|
---|
142 |
|
---|
143 | mrb_sym intern(string str)
|
---|
144 | {
|
---|
145 | return get_sym(str);
|
---|
146 | }
|
---|
147 |
|
---|
148 | mrb_sym get_var_name(string str)
|
---|
149 | {
|
---|
150 | int i = Array.IndexOf(syms, str);
|
---|
151 |
|
---|
152 | // ローカル変数なら登録されているはずなので、
|
---|
153 | if (i < 0)
|
---|
154 | // グローバル変数とする
|
---|
155 | return get_sym((global ? "$" : "@") + str);
|
---|
156 |
|
---|
157 | var sym = (mrb_sym)(i + 1);
|
---|
158 |
|
---|
159 | // ローカル変数でなければ、
|
---|
160 | if (!local_var_p(sym))
|
---|
161 | // グローバル変数とする
|
---|
162 | return get_sym((global ? "$" : "@") + str);
|
---|
163 |
|
---|
164 | return sym;
|
---|
165 | }
|
---|
166 |
|
---|
167 | node new_var_node(mrb_sym sym)
|
---|
168 | {
|
---|
169 | var name = sym2name(sym);
|
---|
170 | if (name.StartsWith("$")) {
|
---|
171 | return new gvar_node(this, sym);
|
---|
172 | }
|
---|
173 | else if (name.StartsWith("@@")) {
|
---|
174 | return new cvar_node(this, sym);
|
---|
175 | }
|
---|
176 | else if (name.StartsWith("@")) {
|
---|
177 | return new ivar_node(this, sym);
|
---|
178 | }
|
---|
179 | else {
|
---|
180 | return new lvar_node(this, sym);
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | node new_num_node(string num)
|
---|
185 | {
|
---|
186 | var result = MrbParser.parse(num);
|
---|
187 | var begin = result as begin_node;
|
---|
188 | if ((begin != null) && (begin.progs.Length == 1))
|
---|
189 | return begin.progs[0];
|
---|
190 | return result;
|
---|
191 | }
|
---|
192 |
|
---|
193 | node new_str_node(string text)
|
---|
194 | {
|
---|
195 | var result = MrbParser.parse("\"" + text + "\"");
|
---|
196 | var begin = result as begin_node;
|
---|
197 | if ((begin != null) && (begin.progs.Length == 1))
|
---|
198 | return begin.progs[0];
|
---|
199 | return result;
|
---|
200 | }
|
---|
201 |
|
---|
202 | locals_node local_switch()
|
---|
203 | {
|
---|
204 | var prev = this.locals;
|
---|
205 | this.locals = new locals_node(null);
|
---|
206 | return prev;
|
---|
207 | }
|
---|
208 |
|
---|
209 | void local_resume(locals_node prev)
|
---|
210 | {
|
---|
211 | this.locals = prev;
|
---|
212 | }
|
---|
213 |
|
---|
214 | void local_nest()
|
---|
215 | {
|
---|
216 | this.locals = new locals_node(this.locals);
|
---|
217 | }
|
---|
218 |
|
---|
219 | void local_unnest()
|
---|
220 | {
|
---|
221 | if (this.locals != null) {
|
---|
222 | this.locals = this.locals.cdr;
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | bool local_var_p(mrb_sym sym)
|
---|
227 | {
|
---|
228 | locals_node l = this.locals;
|
---|
229 |
|
---|
230 | while (l != null) {
|
---|
231 | if (l.symList.Contains(sym))
|
---|
232 | return true;
|
---|
233 | l = l.cdr;
|
---|
234 | }
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 |
|
---|
238 | void local_add_f(mrb_sym sym)
|
---|
239 | {
|
---|
240 | if (this.locals != null) {
|
---|
241 | this.locals.push(sym);
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | mrb_sym local_add_f(string name)
|
---|
246 | {
|
---|
247 | var sym = intern(name);
|
---|
248 | local_add_f(sym);
|
---|
249 | return sym;
|
---|
250 | }
|
---|
251 |
|
---|
252 | void local_add(mrb_sym sym)
|
---|
253 | {
|
---|
254 | if (!local_var_p(sym)) {
|
---|
255 | local_add_f(sym);
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | public mrb_sym[] locals_node()
|
---|
260 | {
|
---|
261 | return this.locals != null ? this.locals.symList : null;
|
---|
262 | }
|
---|
263 |
|
---|
264 | void assignable(node lhs)
|
---|
265 | {
|
---|
266 | var lvar = lhs as lvar_node;
|
---|
267 | if (lvar != null) {
|
---|
268 | local_add(lvar.name);
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | node var_reference(node lhs)
|
---|
273 | {
|
---|
274 | node n;
|
---|
275 |
|
---|
276 | var lvar = lhs as lvar_node;
|
---|
277 | if (lvar != null) {
|
---|
278 | if (!local_var_p(lvar.name)) {
|
---|
279 | n = new fcall_node(this, lvar.name, null);
|
---|
280 | return n;
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | return lhs;
|
---|
285 | }
|
---|
286 |
|
---|
287 | public string[] GetBlockId(string filename, int lineno)
|
---|
288 | {
|
---|
289 | var nodes = allNodes;
|
---|
290 | var result = new string[0];
|
---|
291 | foreach (var node in nodes) {
|
---|
292 | if (node.filename != filename)
|
---|
293 | continue;
|
---|
294 | if (lineno < node.start_lineno)
|
---|
295 | continue;
|
---|
296 | if (node.column == 0) {
|
---|
297 | if (lineno >= node.lineno)
|
---|
298 | continue;
|
---|
299 | }
|
---|
300 | else {
|
---|
301 | if (lineno > node.lineno)
|
---|
302 | continue;
|
---|
303 | }
|
---|
304 | if (String.IsNullOrEmpty(node.block_id))
|
---|
305 | continue;
|
---|
306 |
|
---|
307 | result.Push(node.block_id);
|
---|
308 | }
|
---|
309 | return result;
|
---|
310 | }
|
---|
311 |
|
---|
312 | public string[] GetBlockId(string filename, int lineno, int column)
|
---|
313 | {
|
---|
314 | var nodes = allNodes;
|
---|
315 | var result = new string[0];
|
---|
316 | foreach (var node in nodes) {
|
---|
317 | if (node.filename != filename)
|
---|
318 | continue;
|
---|
319 | if ((lineno < node.start_lineno))
|
---|
320 | continue;
|
---|
321 | if ((lineno == node.start_lineno) && (column < node.column))
|
---|
322 | continue;
|
---|
323 | if (node.column == 0) {
|
---|
324 | if (lineno >= node.lineno)
|
---|
325 | continue;
|
---|
326 | }
|
---|
327 | else {
|
---|
328 | if ((lineno == node.lineno) && (column > node.column))
|
---|
329 | continue;
|
---|
330 | if (lineno > node.lineno)
|
---|
331 | continue;
|
---|
332 | }
|
---|
333 | if (String.IsNullOrEmpty(node.block_id))
|
---|
334 | continue;
|
---|
335 |
|
---|
336 | result.Push(node.block_id);
|
---|
337 | }
|
---|
338 | return result;
|
---|
339 | }
|
---|
340 |
|
---|
341 | public void yyError(string message, params object[] expected)
|
---|
342 | {
|
---|
343 | App.WriteLine($"{filename}({lineno},{column}): error {String.Format(message, expected)}");
|
---|
344 | }
|
---|
345 | }
|
---|
346 | }
|
---|