Skip to content

Commit 5e0e060

Browse files
rename method field to method
1 parent 17f988b commit 5e0e060

File tree

3 files changed

+83
-9
lines changed

3 files changed

+83
-9
lines changed

ast.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export type Expr<A> =
5454
| { a?: A; tag: "parenthesis"; expr: Expr<A> }
5555
| { a?: A; tag: "call"; name: string; args: Expr<A>[] }
5656
| { a?: A; tag: "getfield"; obj: Expr<A>; field: string }
57-
| { a?: A; tag: "method"; obj: Expr<A>; field: string; args: Expr<A>[] };
57+
| { a?: A; tag: "method"; obj: Expr<A>; method: string; args: Expr<A>[] };
5858

5959
export enum UniOp {
6060
NOT = "not",

parser.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,17 @@ export function traverseExpr(c: TreeCursor, s: string): Expr<null> {
517517
return { tag: "call", name, args };
518518
}
519519
case "MemberExpression": {
520-
const obj = traverseMemberExpr(c, s);
520+
const memberInfo = traverseMemberExpr(c, s);
521521
c.nextSibling();
522522
const args = traverseArgs(c, s);
523523
c.parent();
524524

525-
return { tag: "method", ...obj, args };
525+
return {
526+
tag: "method",
527+
obj: memberInfo.obj,
528+
method: memberInfo.field,
529+
args,
530+
};
526531
}
527532
}
528533
}

tests/pa3-my-tests.test.ts

+75-6
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ describe("Method calls", () => {
526526
expr: {
527527
tag: "method",
528528
obj: { tag: "id", name: "x" },
529-
field: "test",
529+
method: "test",
530530
args: [],
531531
},
532532
},
@@ -541,7 +541,7 @@ describe("Method calls", () => {
541541
expr: {
542542
tag: "method",
543543
obj: { tag: "getfield", obj: { tag: "id", name: "x" }, field: "y" },
544-
field: "test",
544+
method: "test",
545545
args: [],
546546
},
547547
},
@@ -558,15 +558,85 @@ describe("Method calls", () => {
558558
obj: {
559559
tag: "method",
560560
obj: { tag: "id", name: "x" },
561-
field: "y",
561+
method: "y",
562562
args: [],
563563
},
564-
field: "test",
564+
method: "test",
565565
args: [],
566566
},
567567
},
568568
],
569569
});
570+
571+
assertTCFail(
572+
"Ensure we can't call nonexistant methods",
573+
`
574+
class C(object):
575+
def test(self : C) -> int:
576+
return 3
577+
578+
x : C = None
579+
x = C()
580+
x.funcThatDoesntExist()`
581+
);
582+
583+
assertTCFail(
584+
"Ensure we can't call on nonclasses",
585+
`
586+
x : int = 3
587+
x.asString()`
588+
);
589+
590+
assertTCFail(
591+
"Ensure method calls are checked like functions",
592+
`
593+
class C(object):
594+
def test(self : C, x : int) -> int:
595+
return x
596+
597+
x : C = None
598+
x = C()
599+
x.test()`
600+
);
601+
602+
assertTC(
603+
"Ensure we can call a method",
604+
`
605+
class C(object):
606+
def test(self : C, x : int) -> int:
607+
return x
608+
609+
x : C = None
610+
x = C()
611+
x.test(3)`,
612+
NUM
613+
);
614+
615+
assertTC(
616+
"Ensure we can call a method with no args",
617+
`
618+
class C(object):
619+
def id(self : C) -> C:
620+
return self
621+
622+
x : C = None
623+
x = C()
624+
x.id()`,
625+
{ tag: "object", class: "C" }
626+
);
627+
628+
assertTC(
629+
"Ensure we can call a function with None",
630+
`
631+
class C(object):
632+
def otherId(self : C, other : C) -> C:
633+
return other
634+
635+
x : C = None
636+
x = C()
637+
x.otherId(None)`,
638+
{ tag: "object", class: "C" }
639+
);
570640
});
571641

572642
// Questions: print_none?
@@ -575,5 +645,4 @@ describe("Method calls", () => {
575645
// TOOD: method calls on None, runtime error
576646
// TODO: calls like r1.mul(None), None is an acceptable parameter for a class but should cause a runtime error!
577647

578-
// TODO: class with no fields, but has methods! Can be called and stuff!
579-
// TODO: method that returns None in place of an object (typechecked)
648+
// TODO: class with no fields, but has methods! Can be called and stuff with other classes existing!

0 commit comments

Comments
 (0)