File size: 9,137 Bytes
30c32c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const BlockType = require('../../extension-support/block-type');
const ArgumentType = require('../../extension-support/argument-type');
const Cast = require('../../util/cast');

/**
 * Class
 * @constructor
 */
class tempVars {
    constructor (runtime) {
        /**
         * The runtime instantiating this block package.
         * @type {runtime}
         */
        this.runtime = runtime;
    }

    getThreadVars (thread) {
        if (!thread.tempVars) {
            thread.tempVars = Object.create(null);
        }
        return thread.tempVars;
    }

    /**
     * @returns {object} metadata for this extension and its blocks.
     */
    getInfo () {
        return {
            id: 'tempVars',
            name: 'Temporary Variables',
            color1: '#0069c2',
            color2: '#0060B4',
            color3: '#0060B4',
            blocks: [
                {
                    opcode: 'setVariable',
                    text: 'set [name] to [value]',
                    arguments: {
                        name: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Variable'
                        },
                        value: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Value'
                        }
                    },
                    blockType: BlockType.COMMAND
                },
                {
                    opcode: 'changeVariable',
                    text: 'change [name] by [value]',
                    arguments: {
                        name: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Variable'
                        },
                        value: {
                            type: ArgumentType.NUMBER,
                            defaultValue: '1'
                        }
                    },
                    blockType: BlockType.COMMAND
                },
                {
                    opcode: 'getVariable',
                    text: 'get [name]',
                    arguments: {
                        name: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Variable'
                        }
                    },
                    allowDropAnywhere: true,
                    blockType: BlockType.REPORTER
                },
                '---',
                {
                    opcode: 'deleteVariable',
                    text: 'delete [name]',
                    arguments: {
                        name: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Variable'
                        }
                    },
                    blockType: BlockType.COMMAND
                },
                {
                    opcode: 'deleteAllVariables',
                    text: 'delete all variables',
                    blockType: BlockType.COMMAND
                },
                {
                    opcode: 'variableExists',
                    text: 'variable [name] exists?',
                    arguments: {
                        name: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Variable'
                        }
                    },
                    disableMonitor: true,
                    blockType: BlockType.BOOLEAN
                },
                {
                    opcode: 'allVariables',
                    text: 'current variables',
                    arguments: {
                        name: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Variable'
                        }
                    },
                    disableMonitor: true,
                    blockType: BlockType.REPORTER
                },
                '---',
                {
                    opcode: 'forEachTempVar',
                    text: 'for each [NAME] in [REPEAT]',
                    branchCount: 1,
                    blockType: BlockType.LOOP,
                    arguments: {
                        NAME: {
                            type: ArgumentType.STRING,
                            defaultValue: 'Variable'
                        },
                        REPEAT: {
                            type: ArgumentType.NUMBER,
                            defaultValue: 10
                        }
                    }
                }
            ]
        };
    }
    /**
     * This function is used for any compiled blocks in the extension if they exist.
     * Data in this function is given to the IR & JS generators.
     * Data must be valid otherwise errors may occur.
     * @returns {object} functions that create data for compiled blocks.
     * @deprecated nolonger in use as all of this is now inside the compiler
     */
    getCompileInfoOld() {
        return {
            ir: {
                forEachTempVar: (generator, block) => {
                    generator.analyzeLoop();
                    return {
                        kind: 'stack',
                        name: generator.descendInputOfBlock(block, 'NAME'),
                        repeat: generator.descendInputOfBlock(block, 'REPEAT'),
                        do: generator.descendSubstack(block, 'SUBSTACK')
                    };
                }
            },
            js: {
                forEachTempVar: (node, compiler, imports) => {
                    const index = compiler.localVariables.next();
                    const name = compiler.localVariables.next();
                    const inputName = compiler.descendInput(node.name).asString();
                    const realName = `('threadVar_' + ${inputName})`;
                    compiler.source += `var ${index} = 0; `;
                    compiler.source += `var ${name} = ${realName}; `;
                    compiler.source += `while (${index} < ${compiler.descendInput(node.repeat).asNumber()}) { `;
                    compiler.source += `${index}++; `;
                    compiler.source += `if (!thread.tempVars) { `;
                    compiler.source += `thread.tempVars = {}; `;
                    compiler.source += `}\n`;
                    compiler.source += `thread.tempVars[${name}] = ${index};\n`;
                    compiler.descendStack(node.do, new imports.Frame(true));
                    compiler.yieldLoop();
                    compiler.source += '}\n';
                }
            }
        }
    }

    setVariable (args, util) {
        const tempVars = this.getThreadVars(util.thread);
        const name = `threadVar_${args.name}`;
        tempVars[name] = args.value;
    }

    changeVariable (args, util) {
        const tempVars = this.getThreadVars(util.thread);
        const name = `threadVar_${args.name}`;
        const oldNum = Number(tempVars[name]);
        const newNum = oldNum + args.value;
        if (!oldNum) {
            tempVars[name] = Number(args.value);
            return;
        }
        tempVars[name] = newNum;
    }

    getVariable (args, util) {
        const tempVars = this.getThreadVars(util.thread);
        const name = `threadVar_${args.name}`;
        const value = tempVars[name];
        if (!value) return '';
        return value;
    }

    deleteVariable (args, util) {
        const tempVars = this.getThreadVars(util.thread);
        const name = `threadVar_${args.name}`;
        if (!(name in tempVars)) return;
        delete tempVars[name];
    }
    
    deleteAllVariables (_, util) {
        // resets the vars
        util.thread.tempVars = Object.create(null);
    }
    
    variableExists (args, util) {
        const tempVars = this.getThreadVars(util.thread);
        const name = `threadVar_${args.name}`;
        return (name in tempVars);
    }

    allVariables (_, util) {
        const tempVars = this.getThreadVars(util.thread);
        const keys = Object.keys(tempVars);
        const mapped = keys.map(name => name.replace('threadVar_', ''));
        return JSON.stringify(mapped);
    }

    forEachTempVar (args, util) {
        // compiled blocks need an interpreter version
        // for edge activated hats
        const count = Cast.toNumber(args.REPEAT);
        const name = Cast.toString(args.NAME);
        // Initialize loop
        if (typeof util.stackFrame.loopCounter === 'undefined') {
            util.stackFrame.loopCounter = count;
        }
        // Only execute once per frame.
        // When the branch finishes, `repeat` will be executed again and
        // the second branch will be taken, yielding for the rest of the frame.
        // Decrease counter
        util.stackFrame.loopCounter--;
        // If we still have some left, start the branch.
        if (util.stackFrame.loopCounter >= 0) {
            const i = (count - (util.stackFrame.loopCounter)) - 1;
            this.setVariable({ name: name, value: i + 1 }, util);
            util.startBranch(1, true);
        }
    }
}

module.exports = tempVars;