@@ -54,7 +54,7 @@ def constraints(pyast, env=None):
54
54
55
55
elif isinstance (pyast , ast .Expression ):
56
56
expr_ty = constraints_expr (pyast .body , env = env )
57
- return Constrained_env (env = env , constraints = expr_ty .constraints )
57
+ return ConstrainedEnv (env = env , constraints = expr_ty .constraints )
58
58
59
59
else :
60
60
raise Exception ('Unknown ast node: %s' % pyast )
@@ -129,16 +129,29 @@ def constraints_expr(expr, env=None):
129
129
env = env or {}
130
130
131
131
if isinstance (expr , ast .Name ) and isinstance (expr .ctx , ast .Load ):
132
- if expr .id in env :
132
+ if expr .id in ['False' , 'True' ]: # Unlike other literals, these are actually just global identifiers
133
+ return ConstrainedType (type = types .AtomicType ('bool' ))
134
+ elif expr .id in env :
133
135
return ConstrainedType (type = env [expr .id ])
134
136
else :
135
137
raise Exception ('Variable not found in environment: %s' % expr .id )
136
138
137
139
elif isinstance (expr , ast .Num ):
138
- return ConstrainedType (type = types .AtomicType ('num' ))
140
+ # The python ast module already chose the type of the num
141
+ if isinstance (expr .n , int ):
142
+ return ConstrainedType (type = types .AtomicType ('int' ))
143
+ elif isinstance (expr .n , long ):
144
+ return ConstrainedType (type = types .AtomicType ('long' ))
145
+ elif isinstance (expr .n , float ):
146
+ return ConstrainedType (type = types .AtomicType ('float' ))
147
+ elif isinstance (expr .n , complex ):
148
+ return ConstrainedType (type = types .AtomicType ('complex' ))
139
149
140
150
elif isinstance (expr , ast .Str ):
141
151
return ConstrainedType (type = types .AtomicType ('str' ))
152
+
153
+ elif isinstance (expr , ast .List ):
154
+ return ConstrainedType (type = types .ListType (elem_ty = types .fresh ()))
142
155
143
156
elif isinstance (expr , ast .BinOp ):
144
157
left = constraints_expr (expr .left , env = env )
@@ -161,5 +174,7 @@ def constraints_expr(expr, env=None):
161
174
if __name__ == '__main__' :
162
175
with open (sys .argv [1 ]) as fh :
163
176
proggy = ast .parse (fh .read ())
177
+
178
+ print ast .dump (proggy )
164
179
165
- print constraints ({}, proggy ).pretty ()
180
+ print constraints (proggy ).pretty ()
0 commit comments