Skip to content

Commit f44cb5a

Browse files
committed
Address todos
1 parent f3ce929 commit f44cb5a

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

core/src/runtime/builtin_functions.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ This file contains all builtin functions. Some defined in the insert, others wer
77
/* eslint-disable @typescript-eslint/no-unsafe-unary-minus */
88

99

10-
import { ArrayVariableType, BuiltinFunctionData, PrimitiveVariableType, PrimitiveVariableTypeMapping, PrimitiveVariableTypeName, SetVariableType, VariableTypeMapping, VariableValue } from "../runtime/runtime-types.js";
11-
import type { Runtime } from "../runtime/runtime.js";
12-
import { f, fail, tryRun, unreachable } from "../utils/funcs.js";
10+
import { Token } from "../lexer/index.js";
11+
import { ExpressionAST } from "../parser/index.js";
12+
import { ArrayVariableType, BuiltinFunctionData, PrimitiveVariableType, PrimitiveVariableTypeMapping, PrimitiveVariableTypeName, SetVariableType, VariableType, VariableTypeMapping, VariableValue } from "../runtime/runtime-types.js";
13+
import { Runtime } from "../runtime/runtime.js";
14+
import { crash, f, fail, fakeObject, tryRun, unreachable } from "../utils/funcs.js";
1315
import type { BoxPrimitive, RangeAttached } from "../utils/types.js";
1416

1517

@@ -95,6 +97,11 @@ function fn<const T extends BuiltinFunctionArg[], const S extends PrimitiveVaria
9597
return data as PreprocesssedBuiltinFunctionData<BuiltinFunctionArg[], PrimitiveVariableTypeName>;
9698
}
9799

100+
function initializeArray(x:ArrayVariableType<false>):ArrayVariableType<true> {
101+
x.init(fakeObject<Runtime>({}));
102+
return x as never as ArrayVariableType<true>;
103+
}
104+
98105
/** Cached result from calling getBuiltinFunctions() */
99106
let builtinFunctionsCache;
100107
/** Thunk to prevent initializing variable types at file load */
@@ -106,8 +113,7 @@ export const getBuiltinFunctions = ():Record<keyof typeof preprocessedBuiltinFun
106113
passMode: "reference",
107114
type: (Array.isArray(type) ? type : [type]).map(t =>
108115
Array.isArray(t) ?
109-
//TODO fix type error: this is ArrayVariableType<false>
110-
new ArrayVariableType(null, null, t[0] == "ANY" ? null : PrimitiveVariableType.get(t[0]), [-1, -1])
116+
initializeArray(new ArrayVariableType<false>(null, null, t[0] == "ANY" ? null : PrimitiveVariableType.get(t[0]), [-1, -1]))
111117
: typeof t == "string" ?
112118
PrimitiveVariableType.get(t)
113119
: "set" in t ?

core/src/runtime/runtime-types.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ export class IntegerRangeVariableType extends BaseVariableType {
342342
}
343343
//TODO! refactor variable types
344344
//Stop mutating them to initialize, create a different class
345-
/** Contains data about an array type. Processed from an ExpressionASTArrayTypeNode. */
345+
/** Contains data about an array type. Processed from an {@link ExpressionASTArrayTypeNode}. */
346346
export class ArrayVariableType<Init extends boolean = true> extends BaseVariableType {
347347
totalLength:number | null = null;
348348
arraySizes:number[] | null = null;
@@ -653,33 +653,32 @@ export class SetVariableType<Init extends boolean = true> extends BaseVariableTy
653653
constructor(
654654
public initialized: Init,
655655
public name: string,
656-
//TODO rename this to elementType
657-
public baseType: (Init extends true ? never : UnresolvedVariableType) | VariableType | null,
656+
public elementType: (Init extends true ? never : UnresolvedVariableType) | VariableType | null,
658657
){super();}
659658
init(runtime:Runtime){
660-
if(Array.isArray(this.baseType)) this.baseType = runtime.resolveVariableType(this.baseType);
659+
if(Array.isArray(this.elementType)) this.elementType = runtime.resolveVariableType(this.elementType);
661660
(this as SetVariableType<true>).initialized = true;
662661
}
663662
fmtText():string {
664-
return f.text`${this.name} (user-defined set type containing "${this.baseType ?? "ANY"}")`;
663+
return f.text`${this.name} (user-defined set type containing "${this.elementType ?? "ANY"}")`;
665664
}
666665
fmtShort():string {
667666
return this.name;
668667
}
669668
toQuotedString():string {
670-
return f.text`"${this.name}" (user-defined set type containing "${this.baseType ?? "ANY"}")`;
669+
return f.text`"${this.name}" (user-defined set type containing "${this.elementType ?? "ANY"}")`;
671670
}
672671
fmtDebug():string {
673-
return f.debug`SetVariableType [${this.name}] (contains: ${this.baseType ?? "ANY"})`;
672+
return f.debug`SetVariableType [${this.name}] (contains: ${this.elementType ?? "ANY"})`;
674673
}
675674
getInitValue(runtime:Runtime):VariableValue | null {
676675
crash(`Cannot initialize a variable of type SET`);
677676
}
678677
mapValues<T>(value:VariableTypeMapping<SetVariableType>, callback:(tval:TypedValue) => T):T[] {
679-
const baseType = (this as SetVariableType<true>).baseType
678+
const elementType = (this as SetVariableType<true>).elementType
680679
?? crash(`Attempted to display a set with no element type`);
681680
return value.map(v =>
682-
callback(typedValue(baseType, v))
681+
callback(typedValue(elementType, v))
683682
);
684683
}
685684
asHTML(value:VariableTypeMapping<SetVariableType>):string {
@@ -869,7 +868,7 @@ export function typesEqual(a:VariableType | UnresolvedVariableType, b:VariableTy
869868
types.some(([_a, _b]) => a == _a && b == _b) || //Prevent infinite recursion on infinite pointer types
870869
typesEqual(a.target, b.target, types.concat([[a, b]]))
871870
)) ||
872-
(a instanceof SetVariableType && b instanceof SetVariableType && a.baseType == b.baseType)
871+
(a instanceof SetVariableType && b instanceof SetVariableType && a.elementType == b.elementType)
873872
;
874873
}
875874

@@ -912,7 +911,7 @@ export function typesAssignable(base:VariableType | UnresolvedVariableType, ext:
912911
return typesEqual(base.target, ext.target) || [f.quote`Types ${base.target} and ${ext.target} are not equal`];
913912
}
914913
if(base instanceof SetVariableType && ext instanceof SetVariableType){
915-
return base.baseType == null || (ext.baseType != null && typesEqual(base.baseType, ext.baseType) || [f.quote`Types ${base.baseType} and ${ext.baseType ?? "ANY"} are not equal`]);
914+
return base.elementType == null || (ext.elementType != null && typesEqual(base.elementType, ext.elementType) || [f.quote`Types ${base.elementType} and ${ext.elementType ?? "ANY"} are not equal`]);
916915
}
917916
if(base instanceof ClassVariableType && ext instanceof ClassVariableType){
918917
return ext.inherits(base) || false;

core/src/runtime/runtime.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ function checkValueEquality<T extends VariableType>(type:T, a:VariableTypeMappin
9696
if(type instanceof EnumeratedVariableType)
9797
return a == b;
9898
if(type instanceof SetVariableType){
99-
const baseType = type.baseType ?? crash(`Unreachable: checking equality between sets, it should not be possible that both sets have element type "ANY"`);
99+
const elementType = type.elementType
100+
?? crash(`Unreachable: checking equality between sets, it should not be possible that both sets have element type "ANY"`);
100101
forceType<VariableTypeMapping<SetVariableType>>(a);
101102
forceType<VariableTypeMapping<SetVariableType>>(b);
102103
return a.length == b.length &&
103104
[...zip(a.values(), b.values())].every(
104-
([aElement, bElement], i) => checkValueEquality(baseType, aElement, bElement, `${aPath}[${i}]`, `${bPath}[${i}]`, range)
105+
([aElement, bElement], i) => checkValueEquality(elementType, aElement, bElement, `${aPath}[${i}]`, `${bPath}[${i}]`, range)
105106
);
106107
}
107108
if(type instanceof ArrayVariableType){

core/src/statements/statements.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class DefineStatement extends Statement {
9797
declaration: this,
9898
mutable: true,
9999
value: this.values.map(t => (
100-
Runtime.evaluateToken(t, type.baseType as PrimitiveVariableType)
100+
Runtime.evaluateToken(t, type.elementType as PrimitiveVariableType)
101101
?? crash(`evaluating a literal token cannot fail`)
102102
).value)
103103
}, this.name);

0 commit comments

Comments
 (0)