Minor tidying of Generator

This commit is contained in:
David Evans 2017-10-24 21:45:50 +01:00
parent 8cbdddec20
commit 5ecce9a6ab
1 changed files with 47 additions and 49 deletions

View File

@ -12,15 +12,34 @@ define(() => {
} }
} }
function removeElement(list, item) {
const p = list.indexOf(item);
if(p !== -1) {
list.splice(p, 1);
}
}
function lastElement(list) { function lastElement(list) {
return list[list.length - 1]; return list[list.length - 1];
} }
class AgentState {
constructor(visible, locked = false) {
this.visible = visible;
this.locked = locked;
}
}
const LOCKED_AGENT = new AgentState(false, true);
const DEFAULT_AGENT = new AgentState(false);
return class Generator { return class Generator {
constructor() { constructor() {
this.agentStates = new Map(); this.agentStates = new Map();
this.blockCount = 0; this.blockCount = 0;
this.nesting = []; this.nesting = [];
this.currentSection = null;
this.currentNest = null;
this.stageHandlers = { this.stageHandlers = {
'agent define': this.handleAgentDefine.bind(this), 'agent define': this.handleAgentDefine.bind(this),
@ -39,14 +58,8 @@ define(() => {
} }
addColumnBounds(target, agentL, agentR, involvedAgents = []) { addColumnBounds(target, agentL, agentR, involvedAgents = []) {
const oldL = target.indexOf(agentL); removeElement(target, agentL);
if(oldL !== -1) { removeElement(target, agentR);
target.splice(oldL, 1);
}
const oldR = target.indexOf(agentR);
if(oldR !== -1) {
target.splice(oldR, 1);
}
let indexL = 0; let indexL = 0;
let indexR = target.length; let indexR = target.length;
@ -63,50 +76,43 @@ define(() => {
target.splice(indexR + 1, 0, agentR); target.splice(indexR + 1, 0, agentR);
} }
addAgentMod(stageAgents, markVisible, mode) { setAgentVis(agents, visible, mode, checked = false) {
if(stageAgents.length === 0) { const filteredAgents = agents.filter((agent) => {
const state = this.agentStates.get(agent) || DEFAULT_AGENT;
if(state.locked) {
if(checked) {
throw new Error('Cannot modify agent ' + agent);
} else {
return false;
}
}
return state.visible !== visible;
});
if(filteredAgents.length === 0) {
return; return;
} }
stageAgents.forEach((agent) => { filteredAgents.forEach((agent) => {
const state = this.agentStates.get(agent); const state = this.agentStates.get(agent);
if(state) { if(state) {
state.visible = markVisible; state.visible = visible;
} else { } else {
this.agentStates.set(agent, { this.agentStates.set(agent, new AgentState(visible));
visible: markVisible,
locked: false,
});
} }
}); });
const type = (markVisible ? 'agent begin' : 'agent end'); const type = (visible ? 'agent begin' : 'agent end');
const existing = lastElement(this.currentSection.stages) || {}; const existing = lastElement(this.currentSection.stages) || {};
if(existing.type === type && existing.mode === mode) { if(existing.type === type && existing.mode === mode) {
mergeSets(existing.agents, stageAgents); mergeSets(existing.agents, filteredAgents);
mergeSets(this.currentNest.agents, stageAgents); mergeSets(this.currentNest.agents, filteredAgents);
} else { } else {
this.addStage({ this.addStage({
type, type,
agents: stageAgents, agents: filteredAgents,
mode, mode,
}); });
} }
} }
filterVis(stageAgents, visible, implicit = false) {
return stageAgents.filter((agent) => {
const state = this.agentStates.get(agent);
if(!state) {
return !visible;
} else if(!state.locked) {
return state.visible === visible;
} else if(!implicit) {
throw new Error('Cannot modify agent ' + agent);
} else {
return false;
}
});
}
beginNested(mode, label, name) { beginNested(mode, label, name) {
const agents = []; const agents = [];
const stages = []; const stages = [];
@ -122,8 +128,8 @@ define(() => {
leftColumn: name + '[', leftColumn: name + '[',
rightColumn: name + ']', rightColumn: name + ']',
}; };
this.agentStates.set(name + '[', {visible: false, locked: true}); this.agentStates.set(name + '[', LOCKED_AGENT);
this.agentStates.set(name + ']', {visible: false, locked: true}); this.agentStates.set(name + ']', LOCKED_AGENT);
this.nesting.push(this.currentNest); this.nesting.push(this.currentNest);
return {agents, stages}; return {agents, stages};
@ -133,11 +139,11 @@ define(() => {
} }
handleAgentBegin({agents, mode}) { handleAgentBegin({agents, mode}) {
this.addAgentMod(this.filterVis(agents, false), true, mode); this.setAgentVis(agents, true, mode, true);
} }
handleAgentEnd({agents, mode}) { handleAgentEnd({agents, mode}) {
this.addAgentMod(this.filterVis(agents, true), false, mode); this.setAgentVis(agents, false, mode, true);
} }
handleBlockBegin({mode, label}) { handleBlockBegin({mode, label}) {
@ -182,11 +188,7 @@ define(() => {
} }
handleUnknownStage(stage) { handleUnknownStage(stage) {
this.addAgentMod( this.setAgentVis(stage.agents, true, 'box');
this.filterVis(stage.agents, false, true),
true,
'box'
);
this.addStage(stage); this.addStage(stage);
} }
@ -211,11 +213,7 @@ define(() => {
throw new Error('Invalid block nesting'); throw new Error('Invalid block nesting');
} }
this.addAgentMod( this.setAgentVis(globals.agents, false, meta.terminators || 'none');
this.filterVis(globals.agents, true, true),
false,
meta.terminators || 'none'
);
this.addColumnBounds( this.addColumnBounds(
globals.agents, globals.agents,