-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathshift.ts
More file actions
29 lines (28 loc) · 1.08 KB
/
shift.ts
File metadata and controls
29 lines (28 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import { Node } from './base';
/**
* This node consumes and stores an integer into a specified
* `field` from the input before forwarding execution.
*/
export class Shift extends Node {
/**
* @param field State's property name
* @param bits number of bits to shift it can be between 1 and 8
* @param lshift if true, bits are shifted to the left.
* otherwise, it goes in the opposite direction.
*/
constructor (public readonly field: string, public readonly bits:number, public readonly lshift: boolean){
const name = (lshift) ? "lshift" : "rshift";
const operation = (bits > 1) ? `_${bits * 8}`: "";
super(`${name}_${field}${operation}`);
if (/^_/.test(field)) {
throw new Error(`Can't use internal field in \`.${name}()\`: "${field}"`);
}
if (bits < 1 || bits > 8){
throw new Error(`bits must be a number between 1 to 8`);
}
}
/** `.otherwise()` is not supported on this type of node as it consumes a bit.
* enabling this defeats the purpose.
*/
public otherwise(): this { throw new Error('Not supported'); }
}