added functions to parser

This commit is contained in:
Lauchmelder 2021-11-28 02:20:12 +01:00
parent 79c54f3c0d
commit 01165d43fb
5 changed files with 91 additions and 11 deletions

View file

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