-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarryless_demo_common.js
156 lines (136 loc) · 3.24 KB
/
carryless_demo_common.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
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
// @flow
/* eslint-env browser */
'use strict';
/* ::
import {
impossible,
strictParseInt,
VChildError,
styleNoWrap,
} from './demo_common';
import { inlineMath } from './math';
*/
/*
global
preact,
impossible,
strictParseInt,
VChildError,
styleNoWrap,
inlineMath,
*/
// eslint-disable-next-line no-unused-vars
const padStart = (
s /* : string */,
targetLength /* : number */,
padString /* : string */
) /* : string */ => {
if (s.length > targetLength) {
return s;
}
const padLength = targetLength - s.length;
let padding = padString;
if (padLength > padding.length) {
padding += padding.repeat(padLength / padding.length);
}
return padding.slice(0, padLength) + s;
};
// eslint-disable-next-line no-unused-vars
const parseNonNegativeInt = (
name /* : string */,
s /* : string */
) /* : BigInteger */ => {
const n = strictParseInt(name, s);
if (n.signum() < 0) {
throw new VChildError([inlineMath(name), ' cannot be negative.']);
}
return n;
};
const digitBound = 50;
// eslint-disable-next-line no-unused-vars
const nonNegativeIntPatternCapped = `0*[0-9]{1,${digitBound}}`;
// eslint-disable-next-line no-unused-vars
const parseNonNegativeIntCapped = (
name /* : string */,
s /* : string */
) /* : BigInteger */ => {
const n = parseNonNegativeInt(name, s);
if (n.toString(10).length > digitBound) {
throw new VChildError([inlineMath(name), ' is too big.']);
}
return n;
};
/* ::
type ArithmeticType = 'standard' | 'carry-less';
*/
// eslint-disable-next-line no-unused-vars
const addOpStr = (arithmeticType /* : ArithmeticType */) /* : string */ => {
switch (arithmeticType) {
case 'standard':
return '+';
case 'carry-less':
return '^';
default:
return impossible(arithmeticType);
}
};
// eslint-disable-next-line no-unused-vars
const subOpStr = (arithmeticType /* : ArithmeticType */) /* : string */ => {
switch (arithmeticType) {
case 'standard':
return '-';
case 'carry-less':
return '^';
default:
return impossible(arithmeticType);
}
};
// eslint-disable-next-line no-unused-vars
const mulOpStr = (arithmeticType /* : ArithmeticType */) /* : string */ => {
switch (arithmeticType) {
case 'standard':
return ' *';
case 'carry-less':
return '^*';
default:
return impossible(arithmeticType);
}
};
// eslint-disable-next-line no-unused-vars
const arithmeticTypeChoice = (
name /* : string */,
chosenArithmeticType /* : ArithmeticType */,
onArithmeticTypeChange /* : (ArithmeticType) => void */,
trailer /* : string */
) => {
const { h } = preact;
const choiceRadio = (
arithmeticType /* : ArithmeticType */,
t /* : string */
) =>
h(
'label',
styleNoWrap,
h('input', {
checked: chosenArithmeticType === arithmeticType,
name,
type: 'radio',
onChange: () => onArithmeticTypeChange(arithmeticType),
}),
` ${arithmeticType} arithmetic${t}`
);
return [choiceRadio('standard', ''), ' ', choiceRadio('carry-less', trailer)];
};
/* ::
export {
padStart,
nonNegativeIntPatternCapped,
parseNonNegativeIntCapped,
impossible,
addOpStr,
subOpStr,
mulOpStr,
arithmeticTypeChoice,
};
export type { ArithmeticType };
*/