-
Notifications
You must be signed in to change notification settings - Fork 0
/
VM.js
382 lines (308 loc) · 8.6 KB
/
VM.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const _ = require('lodash')
const chalk = require('chalk')
const Lexer = require('./Lexer')
require('draftlog').into(console)
module.exports = class VM {
constructor(cursorDelegate, robotDelegate) {
this.cursor = cursorDelegate
this.robot = robotDelegate
// Debug mode
this.debug = true
// last command
this.lastCommand = 0
// keep position of saved bookmark
this.bookmarks = {}
// initialize all registers with 0
this.registers = {
'PURPLE' : 0,
'BEIGE' : 0,
'DGREEN' : 0,
'LGREEN' : 0,
'ORANGE' : 0,
'BLUE' : 0,
'RED' : 0,
}
// defined a starting register
this.currentRegister = 'PURPLE'
this.currentCommand = null
// readed lines
this.commands = []
this.lines = []
// Rendering flags
this.blockFrom = null
this.blockTo = null
// Render Register bar
this.renderRegisterBar()
}
async run () {
// find the beginning of the program
this.cursor.goToStart()
try {
// while program not finished
// while( !(await this.cursor.finished()) ) {
do {
// Clear jump being rendered
this.renderJump()
// read the command
let command = this.currentCommand = await this.read()
// check if commands is valid
if (!Lexer.validate(command)) {
throw new Error('Invalid command: \n'
+ command + ' ' + this.renderBlockCommand(command))
}
//draw command
await this.draw(true)
// execute command
await this.executeCommand(command)
} while (await this.next())
} catch (e) {
console.log()
console.log()
console.log()
console.log(e)
this.cursor.halt()
}
console.log('Reseting head...')
await this.resetHead()
console.log('Finished Execution.')
this.currentCommand = 'finished'
this.draw(false)
}
async resetHead(){
this.currentCommand = 'resetHead'
this.renderJump(this.lastCommand, 0)
while(this.lastCommand > 0)
await this.previous()
this.renderJump()
}
async executeCommand(command) {
let { fn, args } = Lexer.findOperation(command)
await this[fn](args)
}
async saveBookmark(bookmark) {
// console.log('Saving bookmark:' + bookmark)
this.bookmarks[bookmark] = this.lastCommand;
}
async goToBookmark(bookmark) {
// console.log('Going to bookmark: ' + bookmark)
// If bookmark is already saved, go to it, else, go next
let bookmarkPosition = this.bookmarks[bookmark]
if (bookmarkPosition == null) {
this.renderJump(this.lastCommand, 100000)
while (this.bookmarks[bookmark] == null) {
if (!await this.next())
throw new Error('Could not find bookmark: ' + bookmark)
let command = await this.read()
if (Lexer.findOperation(command).fn == 'saveBookmark'){
this.executeCommand(command)
}
}
}
let action = 'previous'
if (bookmarkPosition > this.lastCommand)
action = 'next'
this.renderJump(this.lastCommand, this.bookmarks[bookmark])
while(this.lastCommand > this.bookmarks[bookmark]) {
await this[action]()
await this.read()
}
}
selectRegister(register) {
this.currentRegister = register
}
async readFromSensor() {
this.registers[this.currentRegister] = await this.robot.read()
}
incrementRegister() {
this.registers[this.currentRegister]++
}
decrementRegister() {
this.registers[this.currentRegister]--
}
async jumpFalse() {
if(this.registers[this.currentRegister] == 0) {
this.renderJump(this.lastCommand, this.lastCommand + 2)
await this.next()
await this.read()
}
}
async jumpTrue() {
if(this.registers[this.currentRegister] != 0) {
this.renderJump(this.lastCommand, this.lastCommand + 2)
await this.next()
await this.read()
}
}
/*
* Cursor
*/
async previous() {
await this.cursor.previous()
this.lastCommand--;
this.draw()
}
async next() {
let ret = await this.cursor.next()
if (ret) {
this.lastCommand++;
}
this.draw()
return ret
}
async read() {
// Cache
if (this.commands[this.lastCommand])
return this.commands[this.lastCommand]
let command = await this.cursor.read()
this.commands[this.lastCommand] = command
return command
}
/*
* Robot
*/
async front() {
await this.robot.front()
}
async right() {
await this.robot.right()
}
async left() {
await this.robot.left()
}
async beep() {
await this.robot.beep()
}
begin () {
}
// draw current execution in the terminal
draw(executing) {
// Fill up lines
while (this.lines.length < this.commands.length) {
this.lines.push(console.draft('...'))
}
// Render Register bar
this.renderRegisterBar()
// Render commands
for (let index in this.lines) {
let draft = this.lines[index]
let command = this.commands[index]
// Line Number
let line = index + ': '
line = chalk.dim(' '.repeat(4 - line.length) + line)
// Arrow
let arrow = ' '
let block = this.renderBlockCommand(command)
let jump = this.renderBlockJump(index)
let explain = Lexer.name(command)
if (index == this.lastCommand) {
arrow = this.renderArrowState(executing)
explain = chalk.white(explain)
} else {
explain = chalk.dim(explain)
}
draft(line + arrow + block + jump + explain)
}
}
renderBlockJump (index) {
let {blockFrom, blockTo} = this
if (blockFrom == null && blockTo == null)
return ' '
if (blockFrom == index) {
return blockFrom < blockTo ? ' ╮ ' : ' ╯ '
}
if (blockTo == index) {
return blockFrom < blockTo ? '◂╯ ' : '◂╮ '
}
let min = Math.min(blockFrom, blockTo)
let max = Math.max(blockFrom, blockTo)
if (min < index && index < max) {
return ' │ '
}
return ' '
}
renderRegisterBar() {
if (!this.loadedRegisterBar) {
this.loadedRegisterBar = true
this.barRegs = console.draft('...')
this.barBooks = console.draft('...')
this.barStatus = console.draft('...')
this.barCmd = console.draft('...')
}
// Register bar
let bar = chalk.bgBlack.white(' REGISTERS ')
for (let color in this.registers) {
let value = this.registers[color]
let reg = chalk.black.bgRgb(...this.color2RGB(color))(` ${value} `)
if (this.currentRegister == color)
reg = chalk.underline(reg)
bar += reg
}
// Bookmarks bar
let bookm = chalk.bgBlack.white(' BOOKMARKS ')
let books = ['PURPLE','BEIGE','DGREEN','LGREEN','BROWN','BLUE','RED']
for (let color of books) {
let value = this.bookmarks[color] ? '✔' : '✖'
let reg = chalk.black.bgRgb(...this.color2RGB(color))(` ${value} `)
bookm += reg
}
// Status Bar (Line, Current register, ...)
let status = ''
status += chalk.bgBlack.white(' LINE ')
let line = (this.lastCommand < 10 ? ' ' : '') + this.lastCommand
status += chalk.bgBlackBright.white(` ${line} `)
status += chalk.bgBlack.white(' REG: ')
status += chalk.bgRgb(...this.color2RGB(this.currentRegister))(' ')
// Current Command Bar
let cmd = ''
cmd += chalk.bgBlack.white(' COMMAND ')
let command = this.currentCommand
let commandName = command
if (command && !_.isString(command))
commandName = Lexer.name(command)
cmd += chalk.bgBlackBright.white(` ${commandName} `)
this.barRegs(bar)
this.barBooks(bookm)
this.barStatus(status)
this.barCmd(cmd)
}
renderBlockCommand(command) {
if (command.rendered)
return command.rendered
let first = chalk.bgRgb(...this.color2RGB(command[0]))(' ')
// check if begin of code
let second
if(command[1])
second = chalk.bgRgb(...this.color2RGB(command[1]))(' ')
else
second = first
let rendered = first + second
command.rendered = rendered
return rendered
}
renderJump(from, to) {
this.blockFrom = from
this.blockTo = to
this.draw()
}
renderArrowState(active) {
let arrow = '➜ '
if (active)
arrow = chalk.white(arrow)
else
arrow = chalk.dim(arrow)
return arrow
}
color2RGB(color) {
return {
'RED': [255, 20, 20],
'WHITE': [255, 255, 255],
'PURPLE': [128, 0 , 128],
'LGREEN': [153, 255, 51],
'DGREEN': [0, 153, 0],
'BROWN': [102, 51, 0],
'BEIGE' : [255, 204, 53],
'ORANGE': [255, 128, 0],
'BLUE': [70, 153, 255]
}[color] || [0,0,0]
}
}