Skip to content

Commit

Permalink
Address todos
Browse files Browse the repository at this point in the history
  • Loading branch information
BalaM314 committed Nov 4, 2024
1 parent f3ce929 commit f44cb5a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
16 changes: 11 additions & 5 deletions core/src/runtime/builtin_functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ This file contains all builtin functions. Some defined in the insert, others wer
/* eslint-disable @typescript-eslint/no-unsafe-unary-minus */


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


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

function initializeArray(x:ArrayVariableType<false>):ArrayVariableType<true> {
x.init(fakeObject<Runtime>({}));
return x as never as ArrayVariableType<true>;
}

/** Cached result from calling getBuiltinFunctions() */
let builtinFunctionsCache;
/** Thunk to prevent initializing variable types at file load */
Expand All @@ -106,8 +113,7 @@ export const getBuiltinFunctions = ():Record<keyof typeof preprocessedBuiltinFun
passMode: "reference",
type: (Array.isArray(type) ? type : [type]).map(t =>
Array.isArray(t) ?
//TODO fix type error: this is ArrayVariableType<false>
new ArrayVariableType(null, null, t[0] == "ANY" ? null : PrimitiveVariableType.get(t[0]), [-1, -1])
initializeArray(new ArrayVariableType<false>(null, null, t[0] == "ANY" ? null : PrimitiveVariableType.get(t[0]), [-1, -1]))
: typeof t == "string" ?
PrimitiveVariableType.get(t)
: "set" in t ?
Expand Down
21 changes: 10 additions & 11 deletions core/src/runtime/runtime-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ export class IntegerRangeVariableType extends BaseVariableType {
}
//TODO! refactor variable types
//Stop mutating them to initialize, create a different class
/** Contains data about an array type. Processed from an ExpressionASTArrayTypeNode. */
/** Contains data about an array type. Processed from an {@link ExpressionASTArrayTypeNode}. */
export class ArrayVariableType<Init extends boolean = true> extends BaseVariableType {
totalLength:number | null = null;
arraySizes:number[] | null = null;
Expand Down Expand Up @@ -653,33 +653,32 @@ export class SetVariableType<Init extends boolean = true> extends BaseVariableTy
constructor(
public initialized: Init,
public name: string,
//TODO rename this to elementType
public baseType: (Init extends true ? never : UnresolvedVariableType) | VariableType | null,
public elementType: (Init extends true ? never : UnresolvedVariableType) | VariableType | null,
){super();}
init(runtime:Runtime){
if(Array.isArray(this.baseType)) this.baseType = runtime.resolveVariableType(this.baseType);
if(Array.isArray(this.elementType)) this.elementType = runtime.resolveVariableType(this.elementType);
(this as SetVariableType<true>).initialized = true;
}
fmtText():string {
return f.text`${this.name} (user-defined set type containing "${this.baseType ?? "ANY"}")`;
return f.text`${this.name} (user-defined set type containing "${this.elementType ?? "ANY"}")`;
}
fmtShort():string {
return this.name;
}
toQuotedString():string {
return f.text`"${this.name}" (user-defined set type containing "${this.baseType ?? "ANY"}")`;
return f.text`"${this.name}" (user-defined set type containing "${this.elementType ?? "ANY"}")`;
}
fmtDebug():string {
return f.debug`SetVariableType [${this.name}] (contains: ${this.baseType ?? "ANY"})`;
return f.debug`SetVariableType [${this.name}] (contains: ${this.elementType ?? "ANY"})`;
}
getInitValue(runtime:Runtime):VariableValue | null {
crash(`Cannot initialize a variable of type SET`);
}
mapValues<T>(value:VariableTypeMapping<SetVariableType>, callback:(tval:TypedValue) => T):T[] {
const baseType = (this as SetVariableType<true>).baseType
const elementType = (this as SetVariableType<true>).elementType
?? crash(`Attempted to display a set with no element type`);
return value.map(v =>
callback(typedValue(baseType, v))
callback(typedValue(elementType, v))
);
}
asHTML(value:VariableTypeMapping<SetVariableType>):string {
Expand Down Expand Up @@ -869,7 +868,7 @@ export function typesEqual(a:VariableType | UnresolvedVariableType, b:VariableTy
types.some(([_a, _b]) => a == _a && b == _b) || //Prevent infinite recursion on infinite pointer types
typesEqual(a.target, b.target, types.concat([[a, b]]))
)) ||
(a instanceof SetVariableType && b instanceof SetVariableType && a.baseType == b.baseType)
(a instanceof SetVariableType && b instanceof SetVariableType && a.elementType == b.elementType)
;
}

Expand Down Expand Up @@ -912,7 +911,7 @@ export function typesAssignable(base:VariableType | UnresolvedVariableType, ext:
return typesEqual(base.target, ext.target) || [f.quote`Types ${base.target} and ${ext.target} are not equal`];
}
if(base instanceof SetVariableType && ext instanceof SetVariableType){
return base.baseType == null || (ext.baseType != null && typesEqual(base.baseType, ext.baseType) || [f.quote`Types ${base.baseType} and ${ext.baseType ?? "ANY"} are not equal`]);
return base.elementType == null || (ext.elementType != null && typesEqual(base.elementType, ext.elementType) || [f.quote`Types ${base.elementType} and ${ext.elementType ?? "ANY"} are not equal`]);
}
if(base instanceof ClassVariableType && ext instanceof ClassVariableType){
return ext.inherits(base) || false;
Expand Down
5 changes: 3 additions & 2 deletions core/src/runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,13 @@ function checkValueEquality<T extends VariableType>(type:T, a:VariableTypeMappin
if(type instanceof EnumeratedVariableType)
return a == b;
if(type instanceof SetVariableType){
const baseType = type.baseType ?? crash(`Unreachable: checking equality between sets, it should not be possible that both sets have element type "ANY"`);
const elementType = type.elementType
?? crash(`Unreachable: checking equality between sets, it should not be possible that both sets have element type "ANY"`);
forceType<VariableTypeMapping<SetVariableType>>(a);
forceType<VariableTypeMapping<SetVariableType>>(b);
return a.length == b.length &&
[...zip(a.values(), b.values())].every(
([aElement, bElement], i) => checkValueEquality(baseType, aElement, bElement, `${aPath}[${i}]`, `${bPath}[${i}]`, range)
([aElement, bElement], i) => checkValueEquality(elementType, aElement, bElement, `${aPath}[${i}]`, `${bPath}[${i}]`, range)
);
}
if(type instanceof ArrayVariableType){
Expand Down
2 changes: 1 addition & 1 deletion core/src/statements/statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class DefineStatement extends Statement {
declaration: this,
mutable: true,
value: this.values.map(t => (
Runtime.evaluateToken(t, type.baseType as PrimitiveVariableType)
Runtime.evaluateToken(t, type.elementType as PrimitiveVariableType)
?? crash(`evaluating a literal token cannot fail`)
).value)
}, this.name);
Expand Down

0 comments on commit f44cb5a

Please sign in to comment.