lauchpioos/src/parser/instruction.ts

88 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-11-27 21:30:20 +00:00
/// <reference path="../vector.ts" />
2021-11-27 19:54:04 +00:00
enum InstructionType
{
Point,
Line,
2021-11-28 01:20:12 +00:00
Circle,
Length
2021-11-27 19:54:04 +00:00
}
2021-11-27 21:30:20 +00:00
abstract class Instruction
2021-11-27 19:54:04 +00:00
{
2021-11-28 14:46:47 +00:00
private type: InstructionType;
2021-11-27 19:54:04 +00:00
public params :Parameter[];
2021-11-28 01:20:12 +00:00
private argc: number;
2021-11-27 19:54:04 +00:00
2021-11-28 01:20:12 +00:00
constructor(type: InstructionType, argc: number)
2021-11-27 19:54:04 +00:00
{
2021-11-28 14:46:47 +00:00
this.type = type;
2021-11-28 01:20:12 +00:00
this.argc = argc;
2021-11-27 19:54:04 +00:00
this.params = [];
}
2021-11-27 21:30:20 +00:00
abstract eval();
2021-11-28 01:20:12 +00:00
public getParameterCount(): number { return this.argc; }
2021-11-28 14:46:47 +00:00
public getType(): InstructionType { return this.type; }
2021-11-27 21:30:20 +00:00
}
class PointInstruction extends Instruction
{
constructor()
{
2021-11-28 01:20:12 +00:00
super(InstructionType.Point, 2);
2021-11-27 21:30:20 +00:00
}
eval()
{
return new Vector2D(this.params[0].eval(), this.params[1].eval());
}
}
class LineInstruction extends Instruction
{
constructor()
{
2021-11-28 01:20:12 +00:00
super(InstructionType.Line, 2);
2021-11-27 21:30:20 +00:00
}
eval()
{
return [
this.params[0].eval(),
this.params[1].eval()
];
}
2021-11-28 01:20:12 +00:00
}
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));
}
2021-11-27 19:54:04 +00:00
}