-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathp5CommandList.js
99 lines (89 loc) · 2.18 KB
/
p5CommandList.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
class p5CommandList extends p5Command
{
constructor(parent,elmt)
{
super(parent,elmt)
this.commands = [];
this.pc = 0;
this.elmt.children("p.command").each( (index,c) => this.addCommand(c) )
this.nbLoopMax = 0;
this.nbLoop = 0;
this.loopDoneCb = null;
this.listDoneCb = null;
}
isLoop()
{
return this.nbLoopMax > 0;
}
getLastCommand()
{
return this.commands[this.commands.length-1];
}
addCommand(c)
{
let command = makeCommand(this,c);
if (command instanceof p5CommandVariableDef)
g.interpreter.defineVariable(command.name, command.value);
this.commands.push( command );
}
getVariableValue(name)
{
return g.interpreter.getVariableValue(name);
}
executeCommand(resolve)
{
if (this.pc < this.commands.length)
{
// Current command
let command = this.commands[this.pc];
// Execute command
command.execute()
.then ( _=> command.wait())
.then ( _=>
{
this.pc++;
this.executeCommand(resolve);
})
} else resolve();
}
executeLoop(resolve)
{
if (this.nbLoop < this.nbLoopMax)
{
this.executeCommands().then( () =>
{
if (this.loopDoneCb) this.loopDoneCb();
this.nbLoop++;
this.executeLoop(resolve);
// this.wait();
});
}
else resolve();
}
loop(nb,cb)
{
this.nbLoopMax = max(0,nb);
this.loopDoneCb = cb;
if (nb == 0)
{
return this.executeCommands();
}
else
{
this.nbLoop = 0;
return new Promise ( (resolve,reject) => this.executeLoop(resolve) );
}
}
executeCommands()
{
this.pc = 0;
return new Promise ( (resolve,reject) =>
{
this.executeCommand(resolve);
})
}
execute()
{
return this.loop(0);
}
}