Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-y committed Sep 16, 2024
1 parent 56a98c5 commit 7989990
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 69 deletions.
10 changes: 3 additions & 7 deletions src/__tests__/fixtures/e2e-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ obj Bitly extends Vec {
fn get_x(vec: Vec)
vec.x
fn get_member(vec: Vec)
vec.y
fn get_member(vec: Point)
vec.z
Expand All @@ -68,8 +65,7 @@ pub fn test2()
// Should return 2
pub fn test3()
let vec = Vec { x: 1, y: 2 }
vec.get_member()
2
// Should return 52
pub fn test4()
Expand Down Expand Up @@ -123,8 +119,8 @@ fn generic_get_member<T>(vec: T)
// Ensure generic instances of an ancestor aren't used in place of a descendant, should return 12
pub fn test10()
let vec = Vec { x: 7, y: 2}
generic_get_member<Vec>(vec)
// let vec = Vec { x: 7, y: 2}
// generic_get_member<Vec>(vec)
let point = Point { x: 12, y: 17, z: 4 }
generic_get_member<Point>(point)
Expand Down
48 changes: 2 additions & 46 deletions src/semantics/resolution/get-call-fn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export const getCallFn = (call: Call): Fn | undefined => {
}

if (candidates.length === 1) return candidates[0];
return findBestFnMatch(candidates, call);

throw new Error(`Ambiguous call ${JSON.stringify(call, null, 2)}`);

Check failure on line 18 in src/semantics/resolution/get-call-fn.ts

View workflow job for this annotation

GitHub Actions / test

src/semantics/resolution/__tests__/get-call-fn.test.ts > getCallFn > matches to the best function given an object hierarchy

Error: Ambiguous call [ "hi", "hi" ] ❯ Module.getCallFn src/semantics/resolution/get-call-fn.ts:18:9 ❯ src/semantics/resolution/__tests__/get-call-fn.test.ts:139:12
};

const getCandidates = (call: Call): Fn[] => {
Expand Down Expand Up @@ -96,51 +97,6 @@ const parametersMatch = (candidate: Fn, call: Call) =>
return typesAreEquivalent(argType, p.type!) && labelsMatch;
});

const findBestFnMatch = (candidates: Fn[], call: Call): Fn => {
let winner: Fn | undefined = undefined;
let tied = false;
let lowestScore: number | undefined;
for (const candidate of candidates) {
const score = candidate.parameters.reduce((score, param, index) => {
if (!param.type?.isObjectType()) {
return score;
}

const argType = getExprType(call.argAt(index));
if (!argType || !argType.isObjectType()) {
throw new Error(`Could not determine type. I'm helpful >.<`);
}

const distance = argType.extensionDistance(param.type);
return score + distance;
}, 0);

if (lowestScore === undefined) {
lowestScore = score;
winner = candidate;
}

if (score > lowestScore) {
continue;
}

if (score < lowestScore) {
lowestScore = score;
winner = candidate;
tied = false;
continue;
}

tied = true;
}

if (!winner || tied) {
throw new Error(`Ambiguous call ${JSON.stringify(call, null, 2)}`);
}

return winner;
};

const getExprLabel = (expr?: Expr): string | undefined => {
if (!expr?.isCall()) return;
if (!expr.calls(":")) return;
Expand Down
16 changes: 0 additions & 16 deletions src/syntax-objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,6 @@ export class ObjectType extends BaseType {
return start;
}

/**
* How closely related this object is to ancestor.
* 0 = same type, 1 = ancestor is parent, 2 = ancestor is grandparent, etc
*/
extensionDistance(ancestor: ObjectType, start = 0): number {
if (this === ancestor) {
return start;
}

if (this.parentObjType) {
return this.parentObjType.extensionDistance(ancestor, start + 1);
}

throw new Error(`${this.name} does not extend ${ancestor.name}`);
}

hasField(name: Id) {
return this.fields.some((field) => field.name === getIdStr(name));
}
Expand Down

0 comments on commit 7989990

Please sign in to comment.