-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[class]wire.ts
108 lines (95 loc) · 3.11 KB
/
[class]wire.ts
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {File} from "./[class]File";
import { addListener } from "cluster";
interface WireOptions{
mode: string
}
export interface WireValue{
start : number,
end : number,
expression : (string | Wire)[]
}
export class Wire{
public value : WireValue[] = [];
public start : number = 0;
public end : number = 0;
public name : string = "";
public constantValue : number = -1;
public isConstant : boolean = false;
constructor(file: File, options: WireOptions){
let isReference : boolean;
if(options.mode === "reference") isReference = true;
else if(options.mode === "declaration" || options.mode === "definition") isReference = false;
else throw("invalid mode option");
this.read(file, isReference);
}
private read(file : File, isReference : boolean) : void{
if(file.isNextNumber()){
this.name = file.popToken();
for(let aux = 0; aux < this.name.length; aux ++){
if(this.name[aux] != "0" && this.name[aux] != "1")
file.throwError("invalid number base");
}
this.isConstant = true;
this.constantValue = parseInt(this.name, 2);
return;
}
file.mustBeNextName("invalid wire name");
this.name = file.popToken();
if(!file.isNext("[")) return;
file.popToken();
file.mustBeNextNumber("invalid start size");
this.start = parseInt(file.popToken());
if(this.start < 0) file.throwError("not allowed negative sizes");
if(file.isNext("to")){
file.popToken();
file.mustBeNextNumber("invalid end size");
this.end = parseInt(file.popToken());
if(this.end < 0) file.throwError("not allowed negative sizes");
}
else{
if(isReference){
this.end = this.start;
}
else{
this.end = this.start - 1;
this.start = 0;
if(this.end < 0)
file.throwError("not allowed null sizes at declaration");
}
}
file.mustBeNext("]","expected \"]\" at end of wire reference");
file.popToken();
if(this.end < this.start){
let buffer = this.end;
this.end = this.start;
this.start = buffer;
}
}
get size() : number{
if(this.isConstant)
return this.name.length;
return this.end - this.start + 1;
}
hasValueAt(start : number, end : number) : boolean{
for(let aux1 = 0; aux1 < this.value.length; aux1 ++){
if(
(start >= this.value[aux1].start &&
start <= this.value[aux1].end) ||
(end >= this.value[aux1].start &&
end <= this.value[aux1].end)
) return true;
}
return false;
}
containsWireIndex(index : number) : boolean{
return(
index >= this.start && index <= this.end
);
}
containsSomeWireRange(start : number, end : number) : boolean{
return this.containsWireIndex(start) || this.containsWireIndex(end);
}
containsAllWireRange(start : number, end : number) : boolean{
return this.containsWireIndex(start) && this.containsWireIndex(end);
}
}