refactored instruction creation

This commit is contained in:
Lauchmelder 2021-11-28 15:28:50 +01:00
parent 01165d43fb
commit f105e32af5
2 changed files with 24 additions and 32 deletions

View 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]();
}
}

View file

@ -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();