Skip to content

Commit

Permalink
Improve IndexOperation ValType inference
Browse files Browse the repository at this point in the history
  • Loading branch information
out-of-phaze committed Nov 8, 2024
1 parent 4445086 commit d7f84e3
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion DMCompiler/DM/Expressions/Dereference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ public sealed class IndexOperation : Operation {
/// The index expression. (eg. x[expr])
/// </summary>
public required DMExpression Index { get; init; }
public DMComplexValueType UnnestValType(DMListValueTypes? listValueTypes) {
if (listValueTypes is null) return DMValueType.Anything;
if (listValueTypes.NestedListValType is null) return listValueTypes.NestedListKeyType | DMValueType.Null;
return Index.ValType.Type switch {
// if Index.ValType is only null, we return null
DMValueType.Null => DMValueType.Null,
// if it's only num, return key
DMValueType.Num => listValueTypes.NestedListKeyType,
// else, return valtype|null
_ => listValueTypes.NestedListValType.Value | DMValueType.Null
};
}
}

public sealed class CallOperation : NamedOperation {
Expand Down Expand Up @@ -82,7 +94,7 @@ private DMComplexValueType DetermineValType() {

type = operation switch {
FieldOperation fieldOperation => dmObject.GetVariable(fieldOperation.Identifier)?.ValType ?? DMValueType.Anything,
IndexOperation indexOperation => type.ListValueTypes is null ? DMValueType.Anything : (indexOperation.Index.ValType.Type.HasFlag(DMValueType.Num) ? type.ListValueTypes.NestedListKeyType : type.ListValueTypes.NestedListValType ?? type.ListValueTypes.NestedListKeyType) | DMValueType.Null, // TODO: Keys of assoc lists
IndexOperation indexOperation => indexOperation.UnnestValType(type.ListValueTypes), // TODO: Keys of assoc lists
CallOperation callOperation => dmObject.GetProcReturnTypes(callOperation.Identifier, callOperation.Parameters) ?? DMValueType.Anything,
_ => throw new InvalidOperationException("Unimplemented dereference operation")
};
Expand Down

0 comments on commit d7f84e3

Please sign in to comment.