-
Notifications
You must be signed in to change notification settings - Fork 118
/
pair.ts
163 lines (133 loc) · 4.47 KB
/
pair.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Token, TokenFactory, Semantic, ComparableToken } from './token';
import { OrToken } from './or';
export class PairToken extends ComparableToken {
static prim = 'pair';
constructor(
protected val: { prim: string; args: any[]; annots: any[] },
protected idx: number,
protected fac: TokenFactory
) {
super(val, idx, fac);
}
public Encode(args: any[]): any {
const leftToken = this.createToken(this.val.args[0], this.idx);
let keyCount = 1;
if (leftToken instanceof PairToken) {
keyCount = Object.keys(leftToken.ExtractSchema()).length;
}
const rightToken = this.createToken(this.val.args[1], this.idx + keyCount);
return {
prim: 'Pair',
args: [leftToken.Encode(args), rightToken.Encode(args)],
};
}
public ExtractSignature(): any {
const leftToken = this.createToken(this.val.args[0], this.idx);
let keyCount = 1;
if (leftToken instanceof OrToken) {
keyCount = Object.keys(leftToken.ExtractSchema()).length;
}
const rightToken = this.createToken(this.val.args[1], this.idx + keyCount);
const newSig = [];
for (const leftSig of leftToken.ExtractSignature()) {
for (const rightSig of rightToken.ExtractSignature()) {
newSig.push([...leftSig, ...rightSig]);
}
}
return newSig;
}
public ToBigMapKey(val: any) {
return {
key: this.EncodeObject(val),
type: this.typeWithoutAnnotations(),
};
}
public ToKey(val: any) {
return this.Execute(val);
}
public EncodeObject(args: any): any {
const leftToken = this.createToken(this.val.args[0], this.idx);
let keyCount = 1;
if (leftToken instanceof PairToken) {
keyCount = Object.keys(leftToken.ExtractSchema()).length;
}
const rightToken = this.createToken(this.val.args[1], this.idx + keyCount);
let leftValue;
if (leftToken instanceof PairToken && !leftToken.hasAnnotations()) {
leftValue = args;
} else {
leftValue = args[leftToken.annot()];
}
let rightValue;
if (rightToken instanceof PairToken && !rightToken.hasAnnotations()) {
rightValue = args;
} else {
rightValue = args[rightToken.annot()];
}
return {
prim: 'Pair',
args: [leftToken.EncodeObject(leftValue), rightToken.EncodeObject(rightValue)],
};
}
private traversal(getLeftValue: (token: Token) => any, getRightValue: (token: Token) => any) {
const leftToken = this.createToken(this.val.args[0], this.idx);
let keyCount = 1;
let leftValue;
if (leftToken instanceof PairToken && !leftToken.hasAnnotations()) {
leftValue = getLeftValue(leftToken);
keyCount = Object.keys(leftToken.ExtractSchema()).length;
} else {
leftValue = { [leftToken.annot()]: getLeftValue(leftToken) };
}
const rightToken = this.createToken(this.val.args[1], this.idx + keyCount);
let rightValue;
if (rightToken instanceof PairToken && !rightToken.hasAnnotations()) {
rightValue = getRightValue(rightToken);
} else {
rightValue = { [rightToken.annot()]: getRightValue(rightToken) };
}
const res = {
...leftValue,
...rightValue,
};
return res;
}
public Execute(val: any, semantics?: Semantic): { [key: string]: any } {
return this.traversal(
leftToken => leftToken.Execute(val.args[0], semantics),
rightToken => rightToken.Execute(val.args[1], semantics)
);
}
public ExtractSchema(): any {
return this.traversal(
leftToken => leftToken.ExtractSchema(),
rightToken => rightToken.ExtractSchema()
);
}
public compare(val1: any, val2: any) {
const leftToken = this.createToken(this.val.args[0], this.idx);
let keyCount = 1;
if (leftToken instanceof PairToken) {
keyCount = Object.keys(leftToken.ExtractSchema()).length;
}
const rightToken = this.createToken(this.val.args[1], this.idx + keyCount);
const getValue = (token: Token, args: any) => {
if (token instanceof PairToken && !token.hasAnnotations()) {
return args;
} else {
return args[token.annot()];
}
};
if (leftToken instanceof ComparableToken && rightToken instanceof ComparableToken) {
const result: number = leftToken.compare(
getValue(leftToken, val1),
getValue(leftToken, val2)
);
if (result === 0) {
return rightToken.compare(getValue(rightToken, val1), getValue(rightToken, val2));
}
return result;
}
throw new Error('Not a comparable pair');
}
}