refactored instruction creation
This commit is contained in:
parent
01165d43fb
commit
f105e32af5
19
src/parser/instructionFactory.ts
Normal file
19
src/parser/instructionFactory.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
/// <reference path="instruction.ts" />
|
||||
|
||||
abstract class InstructionFactory
|
||||
{
|
||||
private static symbolDict: { [name: string] : Function } = {
|
||||
"point": (): PointInstruction => { return new PointInstruction(); },
|
||||
"line": (): LineInstruction => { return new LineInstruction(); },
|
||||
"circle": (): CircleInstruction => { return new CircleInstruction(); },
|
||||
"len": (): LengthInstruction => { return new LengthInstruction(); }
|
||||
}
|
||||
|
||||
public static createInstruction(name: string): Instruction
|
||||
{
|
||||
if(!(name in InstructionFactory.symbolDict))
|
||||
return null;
|
||||
|
||||
return InstructionFactory.symbolDict[name]();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/// <reference path="parameter.ts" />
|
||||
/// <reference path="instruction.ts" />
|
||||
/// <reference path="instructionFactory.ts" />
|
||||
|
||||
class Parser
|
||||
{
|
||||
|
@ -66,38 +66,11 @@ class Parser
|
|||
}
|
||||
params.push(paramlist);
|
||||
|
||||
let newInstruction;
|
||||
switch(symbol)
|
||||
let newInstruction = InstructionFactory.createInstruction(symbol);
|
||||
if(newInstruction === null)
|
||||
{
|
||||
case "point":
|
||||
{
|
||||
newInstruction = new PointInstruction();
|
||||
break;
|
||||
}
|
||||
|
||||
case "line":
|
||||
{
|
||||
newInstruction = new LineInstruction();
|
||||
break;
|
||||
}
|
||||
|
||||
case "circle":
|
||||
{
|
||||
newInstruction = new CircleInstruction();
|
||||
break;
|
||||
}
|
||||
|
||||
case "len":
|
||||
{
|
||||
newInstruction = new LengthInstruction();
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
console.error("Unknown instruction \"" + symbol + "\"");
|
||||
return null;
|
||||
}
|
||||
console.error("Unknown instruction: \"" + symbol + "\"");
|
||||
return null;
|
||||
}
|
||||
|
||||
let expectedArgs = newInstruction.getParameterCount();
|
||||
|
|
Loading…
Reference in a new issue