Skip to content

Commit e1f9e19

Browse files
committed
arch
1 parent ab500d7 commit e1f9e19

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1628
-1105
lines changed

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
- The "Domain" expression has been deprecated. Use types instead (see below).
3333

3434
- Some `BoxedExpression` properties have been removed:
35-
- Instead of `expr.isZero`, use `expr.isEqual(0)`.
36-
- Instead of `expr.isNotZero`, use `expr.isEqual(0) === false`.
37-
- Instead of `expr.isOne`, use `expr.isEqual(1)`.
38-
- Instead of `expr.isNegativeOne`, use `expr.isEqual(-1)`.
35+
- Instead of `expr.isZero`, use `expr.is(0)`.
36+
- Instead of `expr.isNotZero`, use `!expr.is(0)`.
37+
- Instead of `expr.isOne`, use `expr.is(1)`.
38+
- Instead of `expr.isNegativeOne`, use `expr.is(-1)`.
3939

4040
### New Features and Improvements
4141

src/common/syntax-highlighter.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export type SyntaxGrammar = {
186186

187187
const defaultGrammar: SyntaxGrammar = {
188188
comment: (buf: Buffer) => {
189-
let pos = buf.pos;
189+
const pos = buf.pos;
190190
if (buf.match('//')) {
191191
while (!buf.atEnd() && buf.peek() !== '\n') buf.consume();
192192
return { content: buf.s.slice(pos, buf.pos), tag: 'comment' };
@@ -205,7 +205,7 @@ const defaultGrammar: SyntaxGrammar = {
205205
},
206206

207207
number: (buf: Buffer) => {
208-
let pos = buf.pos;
208+
const pos = buf.pos;
209209
if (buf.match('0x')) {
210210
while (!buf.atEnd() && /[0-9a-fA-F]/.test(buf.peek())) buf.consume();
211211
return { content: buf.s.slice(buf.pos), tag: 'literal' };
@@ -229,7 +229,7 @@ const defaultGrammar: SyntaxGrammar = {
229229
},
230230

231231
string: (buf: Buffer) => {
232-
let pos = buf.pos;
232+
const pos = buf.pos;
233233
const quote = buf.peek();
234234
if (quote !== '"' && quote !== "'") return undefined;
235235

@@ -244,7 +244,7 @@ const defaultGrammar: SyntaxGrammar = {
244244
},
245245

246246
regex: (buf: Buffer) => {
247-
let pos = buf.pos;
247+
const pos = buf.pos;
248248
if (buf.match('//')) {
249249
buf.pos = pos;
250250
return undefined;
@@ -263,7 +263,7 @@ const defaultGrammar: SyntaxGrammar = {
263263
},
264264

265265
identifier: (buf: Buffer) => {
266-
let pos = buf.pos;
266+
const pos = buf.pos;
267267
while (!buf.atEnd() && /[a-zA-Z0-9_]/.test(buf.peek())) buf.consume();
268268
if (buf.pos === pos) return undefined;
269269
return { content: buf.s.slice(pos, buf.pos), tag: 'identifier' };
@@ -279,7 +279,7 @@ const defaultGrammar: SyntaxGrammar = {
279279
// Sort by length, longest first
280280
keywords.sort((a, b) => b.length - a.length);
281281

282-
let pos = buf.pos;
282+
const pos = buf.pos;
283283
for (const keyword of keywords) {
284284
buf.pos = pos;
285285
if (buf.match(keyword)) {
@@ -323,7 +323,7 @@ export function parseCode(
323323
}
324324

325325
// If last span is default, coalesce with this one
326-
let lastSpan = spans.length ? spans[spans.length - 1] : null;
326+
const lastSpan = spans.length ? spans[spans.length - 1] : null;
327327
if (lastSpan?.tag === 'default') {
328328
lastSpan.content += buf.consume();
329329
} else {

src/common/terminal.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ class ColorTerminal extends Terminal {
133133
const newStyle = style;
134134
const currentStyle = this.state;
135135

136-
// if (style.bg === 'bright-blue') debugger;
137-
138136
if (isDefaultStyle(newStyle) && !isDefaultStyle(currentStyle)) {
139137
// Reset all attributes
140138
codes.push(0);

src/common/type/parse.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class TypeParser {
100100
const reqArgs: NamedElement[] = [];
101101
const optArgs: NamedElement[] = [];
102102
while (true) {
103-
let arg = this.parseNamedElement();
103+
const arg = this.parseNamedElement();
104104
if (arg === null) break;
105105

106106
// No whitespace before '?', i.e. `x: number?`
@@ -427,6 +427,14 @@ class TypeParser {
427427
dimensions: [-1, -1],
428428
});
429429

430+
// `tensor` is equivalent to a list of numbers with any dimensions
431+
if (this.match('tensor'))
432+
return makeType({
433+
kind: 'list',
434+
elements: 'number',
435+
dimensions: undefined,
436+
});
437+
430438
// Regular list syntax: `list<number^2x3`
431439
if (!this.match('list<')) return null;
432440

src/common/type/reduce.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ function reduceMapType(type: MapType): Type {
276276
}
277277

278278
function reduceSignatureType(type: FunctionSignature): Type {
279-
let reducedArgs = type.args?.map((arg) => ({
279+
const reducedArgs = type.args?.map((arg) => ({
280280
...arg,
281281
type: reduceType(arg.type),
282282
}));
@@ -290,7 +290,7 @@ function reduceSignatureType(type: FunctionSignature): Type {
290290
type: reduceType(type.restArg.type),
291291
}
292292
: undefined;
293-
let reducedResult = reduceType(type.result);
293+
const reducedResult = reduceType(type.result);
294294

295295
if (reducedArgs?.some((arg) => arg.type === 'error')) return 'error';
296296
if (reducedOptArgs?.some((arg) => arg.type === 'error')) return 'error';

src/common/type/serialize.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ export function typeToString(type: Type, precedence = 0): string {
5050
case 'list':
5151
if (type.dimensions && isSubtype(type.elements, 'number')) {
5252
// We have a numeric list, possibly vector or matrix.
53-
if (type.dimensions.length === 1) {
53+
if (type.dimensions === undefined) {
54+
if (type.elements === 'number') result = 'tensor';
55+
} else if (type.dimensions.length === 1) {
5456
if (type.elements === 'number') {
5557
if (type.dimensions[0] < 0) result = 'vector';
5658
else result = `vector<${type.dimensions[0]}>`;

src/common/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,16 @@ export function permutations<T>(
1919

2020
return result;
2121
}
22+
23+
export function hidePrivateProperties(obj: any) {
24+
for (const key in obj) {
25+
if (key.startsWith('_') && obj.hasOwnProperty(key)) {
26+
Object.defineProperty(obj, key, {
27+
enumerable: false,
28+
configurable: true, // Allows redefinition if necessary
29+
writable: true, // Allows modification
30+
value: obj[key],
31+
});
32+
}
33+
}
34+
}

src/compute-engine/assume.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function assumeInequality(proposition: BoxedExpression): AssumeResult {
143143
const ce = proposition.engine;
144144
// Case 1
145145
if (proposition.op1!.symbol && !hasDef(ce, proposition.op1!.symbol)) {
146-
if (proposition.op2.evaluate().isEqual(0)) {
146+
if (proposition.op2.is(0)) {
147147
if (proposition.operator === 'Less') {
148148
// x < 0
149149
ce.defineSymbol(proposition.op1.symbol, {

src/compute-engine/boxed-expression/abstract-boxed-expression.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export abstract class _BoxedExpression implements BoxedExpression {
131131
}
132132
if (typeof this.string === 'string') return this.string;
133133
if (typeof this.symbol === 'string') return this.symbol;
134-
if (this.im === 0) return this.re ?? this.toString();
134+
if (this.im === 0) return this.re;
135135
return this.toString();
136136
}
137137

@@ -296,9 +296,19 @@ export abstract class _BoxedExpression implements BoxedExpression {
296296
return null;
297297
}
298298

299-
/** Object.is() */
300299
is(rhs: any): boolean {
300+
// If the rhs is a number, the result can only be true if this
301+
// is a BoxedNumber
302+
if (typeof rhs === 'number' || typeof rhs === 'bigint') return false;
303+
304+
if (typeof rhs === 'boolean') {
305+
if (this.symbol === 'True' && rhs === true) return true;
306+
if (this.symbol === 'False' && rhs === false) return true;
307+
return false;
308+
}
309+
301310
if (rhs === null || rhs === undefined) return false;
311+
302312
return same(this, this.engine.box(rhs));
303313
}
304314

@@ -436,15 +446,18 @@ export abstract class _BoxedExpression implements BoxedExpression {
436446
return false;
437447
}
438448

439-
get re(): number | undefined {
440-
return undefined;
449+
get re(): number {
450+
return NaN;
441451
}
442-
get im(): number | undefined {
443-
return undefined;
452+
453+
get im(): number {
454+
return NaN;
444455
}
456+
445457
get bignumRe(): Decimal | undefined {
446458
return undefined;
447459
}
460+
448461
get bignumIm(): Decimal | undefined {
449462
return undefined;
450463
}
@@ -504,7 +517,7 @@ export abstract class _BoxedExpression implements BoxedExpression {
504517
return this.engine.NaN;
505518
}
506519

507-
ln(base?: SemiBoxedExpression): BoxedExpression {
520+
ln(base?: number | BoxedExpression): BoxedExpression {
508521
return this.engine.NaN;
509522
}
510523

src/compute-engine/boxed-expression/apply.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ export function apply(
1515
const ce = expr.engine;
1616

1717
let result: number | Complex | Decimal | undefined = undefined;
18-
if (expr.im !== 0) result = complexFn?.(ce.complex(expr.re ?? 0, expr.im));
18+
if (expr.im !== 0) result = complexFn?.(ce.complex(expr.re, expr.im));
1919
else {
2020
const bigRe = expr.bignumRe;
2121
if (bigRe !== undefined && bignumPreferred(ce) && bigFn)
2222
result = bigFn(bigRe);
2323
else {
2424
const re = expr.re;
25-
console.assert(re !== undefined);
26-
if (bignumPreferred(ce) && bigFn) result = bigFn(ce.bignum(re!));
27-
else result = fn(re!);
25+
if (bignumPreferred(ce) && bigFn) result = bigFn(ce.bignum(re));
26+
else result = fn(re);
2827
}
2928
}
3029

3130
if (result === undefined) return undefined;
3231
if (result instanceof Complex)
3332
return ce.number(
34-
ce._numericValue({ decimal: ce.chop(result.re), im: ce.chop(result.im) })
33+
ce._numericValue({ re: ce.chop(result.re), im: ce.chop(result.im) })
3534
);
3635
return ce.number(ce.chop(result));
3736
}
@@ -47,38 +46,41 @@ export function apply2(
4746
return undefined;
4847

4948
const ce = expr1.engine;
49+
5050
let result: number | Complex | Decimal | undefined = undefined;
5151
if (expr1.im !== 0 || expr2.im !== 0) {
5252
result = complexFn?.(
53-
ce.complex(expr1.re ?? 0, expr1.im),
54-
ce.complex(expr2.re ?? 0, expr2.im)
53+
ce.complex(expr1.re, expr1.im),
54+
ce.complex(expr2.re, expr2.im)
5555
);
5656
}
5757

58-
if (bigFn) {
59-
const bigRe1 = expr1.bignumRe;
60-
const bigRe2 = expr2.bignumRe;
61-
if (bigRe1 !== undefined && bigRe2 !== undefined) {
62-
if (bignumPreferred(ce) && bigFn) result = bigFn(bigRe1, bigRe2);
63-
else result = fn(bigRe1.toNumber(), bigRe2.toNumber());
58+
if (result === undefined && bigFn) {
59+
let bigRe1 = expr1.bignumRe;
60+
let bigRe2 = expr2.bignumRe;
61+
if (bigRe1 !== undefined || bigRe2 !== undefined) {
62+
bigRe1 ??= ce.bignum(expr1.re);
63+
bigRe2 ??= ce.bignum(expr2.re);
64+
result = bigFn(bigRe1, bigRe2);
6465
}
6566
}
66-
67-
const re1 = expr1.re;
68-
const re2 = expr2.re;
69-
if (re1 !== undefined && re2 !== undefined) {
70-
if (bignumPreferred(ce) && bigFn)
71-
result = bigFn(
72-
ce.bignum(expr1.bignumRe ?? re1),
73-
ce.bignum(expr2.bignumRe ?? re2)
74-
);
75-
else result = fn(re1, re2);
67+
if (result === undefined) {
68+
const re1 = expr1.re;
69+
const re2 = expr2.re;
70+
if (!isNaN(re1) && !isNaN(re2)) {
71+
if (bignumPreferred(ce) && bigFn)
72+
result = bigFn(
73+
ce.bignum(expr1.bignumRe ?? re1),
74+
ce.bignum(expr2.bignumRe ?? re2)
75+
);
76+
else result = fn(re1, re2);
77+
}
7678
}
7779

7880
if (result === undefined) return undefined;
7981
if (result instanceof Complex)
8082
return ce.number(
81-
ce._numericValue({ decimal: ce.chop(result.re), im: ce.chop(result.im) })
83+
ce._numericValue({ re: ce.chop(result.re), im: ce.chop(result.im) })
8284
);
8385
return ce.number(ce.chop(result));
8486
}

0 commit comments

Comments
 (0)