Skip to content

Commit c3862ae

Browse files
committed
add annotations across the codebase
1 parent e27efef commit c3862ae

26 files changed

+173
-119
lines changed

compiler/astnodes/classdef.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def toJSON(self, dump_location=True):
4646
for decl in self.declarations]
4747
return d
4848

49-
def getIdentifier(self):
49+
def getIdentifier(self) -> Identifier:
5050
return self.name
5151

5252
def getDefaultConstructor(self) -> FuncDef:

compiler/astnodes/funcdef.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
from .typedvar import TypedVar
44
from .typeannotation import TypeAnnotation
55
from .stmt import Stmt
6+
from ..types import FuncType
67
from typing import List
78

89

910
class FuncDef(Declaration):
11+
freevars: List[Identifier] # used in AST transformations, not printed out
12+
type: FuncType = None # type signature of function
1013

1114
# The AST for
1215
# def NAME(PARAMS) -> RETURNTYPE:
@@ -22,8 +25,7 @@ def __init__(self, location: List[int], name: Identifier, params: List[TypedVar]
2225
self.declarations = declarations
2326
self.statements = [s for s in statements if s is not None]
2427
self.isMethod = isMethod
25-
self.freevars = [] # used in AST transformations, not printed out
26-
self.type = None # type signature of function
28+
self.freevars = []
2729

2830
def getFreevarNames(self):
2931
return set([v.name for v in self.freevars])
@@ -56,5 +58,5 @@ def toJSON(self, dump_location=True):
5658
d["statements"] = [s.toJSON(dump_location) for s in self.statements]
5759
return d
5860

59-
def getIdentifier(self):
61+
def getIdentifier(self) -> Identifier:
6062
return self.name

compiler/astnodes/identifier.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .expr import Expr
22
from typing import List
3+
from ..types import VarInstance
34

45
CIL_KEYWORDS = set(["char", "value", "int32", "int64", "string", "long", "null"] +
56
["add",
@@ -121,11 +122,11 @@
121122

122123

123124
class Identifier(Expr):
125+
varInstance: VarInstance = None
124126

125127
def __init__(self, location: List[int], name: str):
126128
super().__init__(location, "Identifier")
127129
self.name = name
128-
self.varInstance = None
129130

130131
def visit(self, visitor):
131132
return visitor.Identifier(self)

compiler/astnodes/node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def preorder(self, visitor):
1919
def postorder(self, visitor):
2020
return self.visit(visitor)
2121

22-
def toJSON(self, dump_location=True):
22+
def toJSON(self, dump_location=True) -> dict:
2323
d = {}
2424
d['kind'] = self.kind
2525
if dump_location:

compiler/astnodes/typedvar.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
from .node import Node
22
from .identifier import Identifier
33
from .typeannotation import TypeAnnotation
4+
from ..types import ValueType, VarInstance
45
from typing import List
56

67

78
class TypedVar(Node):
9+
t: ValueType = None # the typechecked type goes here
10+
varInstance: VarInstance = None
811

912
def __init__(self, location: List[int], identifier: Identifier, typ: TypeAnnotation):
1013
super().__init__(location, "TypedVar")
1114
self.identifier = identifier
1215
self.type = typ
13-
self.t = None # the typechecked type goes here
14-
self.varInstance = None
1516

1617
def name(self):
1718
return self.identifier.name

compiler/astnodes/vardef.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from .declaration import Declaration
22
from .expr import Expr
3+
from .identifier import Identifier
34
from .typedvar import TypedVar
4-
from typing import List
5+
from typing import List, Optional
56

67

78
class VarDef(Declaration):
9+
attrOfClass: Optional[str]
810

911
def __init__(self, location: List[int], var: TypedVar, value: Expr, isAttr: bool = False, attrOfClass=None):
1012
super().__init__(location, "VarDef")
@@ -31,7 +33,7 @@ def toJSON(self, dump_location=True):
3133
d["value"] = self.value.toJSON(dump_location)
3234
return d
3335

34-
def getIdentifier(self):
36+
def getIdentifier(self) -> Identifier:
3537
return self.var.identifier
3638

3739
def getName(self) -> str:

compiler/cil_backend.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88

99

1010
class CilStackLoc:
11-
def __init__(self, name, loc, t, isArg):
11+
def __init__(self, name: str, loc: int, t: str, isArg: bool):
1212
self.name = name
1313
self.loc = loc
1414
self.isArg = isArg
1515
self.t = t
1616

17-
def decl(self):
17+
def decl(self) -> str:
1818
return f"[{self.loc}] {self.t} {self.name}"
1919

2020

@@ -26,6 +26,7 @@ def __init__(self, main: str, ts: TypeSystem):
2626
self.builder = Builder(main)
2727
self.main = main # name of main class
2828
self.ts = ts
29+
self.locals = []
2930
self.enterScope()
3031

3132
def indent(self):
@@ -612,7 +613,7 @@ def BooleanLiteral(self, node: BooleanLiteral):
612613
def IntegerLiteral(self, node: IntegerLiteral):
613614
self.instr(f"ldc.i8 {node.value}")
614615

615-
def NoneLiteral(self, node: NoneLiteral):
616+
def NoneLiteral(self, _: NoneLiteral):
616617
self.instr("ldnull")
617618

618619
def StringLiteral(self, node: StringLiteral):

compiler/closurevisitor.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22
from .types import *
33
from .visitor import Visitor
44
from .varcollector import VarCollector
5-
from typing import List
6-
7-
8-
class VarInstance:
9-
def __init__(self):
10-
self.isNonlocal = False
11-
self.isGlobal = False
12-
self.isSelf = False
5+
from typing import List, Dict
136

147

158
def newInstance(tv: TypedVar) -> VarInstance:
169
tv.varInstance = VarInstance()
1710
return tv.varInstance
1811

1912

20-
def merge(d1, d2):
13+
def merge(d1: dict, d2: dict) -> dict:
2114
combined = {}
2215
for k in d1:
2316
combined[k] = d1[k]
@@ -43,10 +36,11 @@ class ClosureVisitor(Visitor):
4336

4437
# instances that are captured by nested functions are marked as refs
4538
# instances that correspond to global variables are marked as such
39+
globals: Dict[str, VarInstance]
40+
decls: List[Dict[str, VarInstance]]
4641

4742
def __init__(self):
4843
self.globals = {}
49-
self.nonlocals = [] # uncaptured nonlocals
5044
self.decls = []
5145

5246
def getInstance(self, name: str) -> VarInstance:

compiler/compiler.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818

1919
class Compiler:
20+
transformer: ClosureTransformer = None
21+
2022
def __init__(self):
2123
self.ts = TypeSystem()
2224
self.parser = Parser()
2325
self.typechecker = TypeChecker(self.ts)
24-
self.transformer = None
2526

2627
def parse(self, infile) -> Node:
2728
astparser = self.parser

compiler/empty_list_typer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@
99

1010

1111
class EmptyListTyper(Visitor):
12-
13-
def __init__(self):
14-
self.expectedType = None
15-
self.expReturnType = None
12+
expectedType: ValueType = None
13+
expReturnType: ValueType = None
1614

1715
def visit(self, node: Node):
1816
return node.preorder(self)

0 commit comments

Comments
 (0)