added functions to parser
This commit is contained in:
parent
79c54f3c0d
commit
01165d43fb
5 changed files with 91 additions and 11 deletions
|
@ -4,28 +4,32 @@ enum InstructionType
|
|||
{
|
||||
Point,
|
||||
Line,
|
||||
Circle
|
||||
Circle,
|
||||
Length
|
||||
}
|
||||
|
||||
abstract class Instruction
|
||||
{
|
||||
public fn: InstructionType;
|
||||
public params :Parameter[];
|
||||
private argc: number;
|
||||
|
||||
constructor(type: InstructionType)
|
||||
constructor(type: InstructionType, argc: number)
|
||||
{
|
||||
this.fn = type;
|
||||
this.argc = argc;
|
||||
this.params = [];
|
||||
}
|
||||
|
||||
abstract eval();
|
||||
public getParameterCount(): number { return this.argc; }
|
||||
}
|
||||
|
||||
class PointInstruction extends Instruction
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
super(InstructionType.Point);
|
||||
super(InstructionType.Point, 2);
|
||||
}
|
||||
|
||||
eval()
|
||||
|
@ -38,7 +42,7 @@ class LineInstruction extends Instruction
|
|||
{
|
||||
constructor()
|
||||
{
|
||||
super(InstructionType.Line);
|
||||
super(InstructionType.Line, 2);
|
||||
}
|
||||
|
||||
eval()
|
||||
|
@ -48,4 +52,36 @@ class LineInstruction extends Instruction
|
|||
this.params[1].eval()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class CircleInstruction extends Instruction
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
super(InstructionType.Line, 2);
|
||||
}
|
||||
|
||||
eval()
|
||||
{
|
||||
return [
|
||||
this.params[0].eval(),
|
||||
this.params[1].eval()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class LengthInstruction extends Instruction
|
||||
{
|
||||
constructor()
|
||||
{
|
||||
super(InstructionType.Line, 1);
|
||||
}
|
||||
|
||||
eval()
|
||||
{
|
||||
let line = this.params[0].eval();
|
||||
let dx = line[1].x - line[0].x;
|
||||
let dy = line[1].y - line[0].y;
|
||||
return Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue