-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduction.js
126 lines (112 loc) · 3.98 KB
/
reduction.js
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
/*
Fōrmulæ bitwise package. Module for reduction.
Copyright (C) 2015-2025 Laurence R. Ugalde
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
'use strict';
export class Bitwise extends Formulae.Package {}
Bitwise.binary = async (binary, session) => {
let result;
let bi;
let tag = binary.getTag();
let integer;
for (let i = 0, n = binary.children.length; i < n; ++i) {
integer = CanonicalArithmetic.getInteger(binary.children[i]);
if (integer === undefined) {
ReductionManager.setInError(binary.children[i], "Expression must be an integer number");
throw new ReductionError();
}
if (i == 0) {
result = integer;
}
else {
switch (tag) {
case "Bitwise.And":
result = result.bitwiseAnd(integer);
break;
case "Bitwise.Or":
result = result.bitwiseOr(integer);
break;
case "Bitwise.XOr":
result = result.bitwiseXOr(integer);
break;
case "Bitwise.LeftShift":
result = result.bitwiseLeftShift(integer);
break;
case "Bitwise.RightShift":
result = result.bitwiseRightShift(integer);
break;
case "Bitwise.SetBit":
result = result.bitwiseSetBit(integer);
break;
case "Bitwise.ClearBit":
result = result.bitwiseClearBit(integer);
break;
case "Bitwise.FlipBit":
resulr = result.bitwiseFlipBit(integer);
break;
case "Bitwise.GetBit":
result = result.bitwiseGetBit(integer);
break;
case "Bitwise.TestBit":
binary.replaceBy(
Formulae.createExpression(
result.bitwiseGetBit(integer).isZero() ? "Logic.False" : "Logic.True"
)
);
return true;
}
}
}
binary.replaceBy(
CanonicalArithmetic.createInternalNumber(result, session)
);
return true;
};
Bitwise.unary = async (unary, session) => {
let n = CanonicalArithmetic.getInteger(unary.children[0]);
if (n === undefined) {
ReductionManager.setInError(unary.children[0], "Expression must be an integer number");
throw new ReductionError();
}
let result;
switch (unary.getTag()) {
case "Bitwise.Not":
result = n.bitwiseNot();
break;
case "Bitwise.BitLength":
result = n.bitwiseBitLength();
break;
case "Bitwise.BitCount":
result = n.bitwiseBitCount();
break;
}
unary.replaceBy(
CanonicalArithmetic.createInternalNumber(result, session)
);
return true;
};
Bitwise.setReducers = () => {
ReductionManager.addReducer("Bitwise.And", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.Or", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.XOr", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.LeftShift", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.RightShift", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.SetBit", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.ClearBit", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.FlipBit", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.GetBit", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.TestBit", Bitwise.binary, "Bitwise.binary");
ReductionManager.addReducer("Bitwise.Not", Bitwise.unary, "Bitwise.unary");
ReductionManager.addReducer("Bitwise.BitLength", Bitwise.unary, "Bitwise.unary");
ReductionManager.addReducer("Bitwise.BitCount", Bitwise.unary, "Bitwise.unary");
};