Skip to content

Commit

Permalink
python: generate fresh identifiers when typechecking values
Browse files Browse the repository at this point in the history
If we have a list of list of x, then the index variable 'x' in our
for loops clash, messing everything up. We need to generate a fresh
index variable each time.

Signed-off-by: David Scott <[email protected]>
  • Loading branch information
David Scott committed Jul 15, 2015
1 parent 2481139 commit 2e6ebe9
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions generator/src/python.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ let rec lines_of_t t =

let string_of_ts ts = String.concat "\n" (List.concat (List.map lines_of_t ts))

(* generate a fresh id *)
let fresh_id =
let counter = ref 0 in
fun () ->
incr counter;
"tmp_" ^ (string_of_int !counter)

(** [typecheck ty v] returns a python fragment which checks
[v] has type [ty] *)
let rec typecheck env ty v =
Expand Down Expand Up @@ -47,20 +54,22 @@ let rec typecheck env ty v =
] in
(member true hd) @ (List.concat (List.map (member false) tl))
| Array t ->
let id = fresh_id () in
[
Line (sprintf "if type(%s) <> type([]):" v);
Block [ raise_type_error ];
Line (sprintf "for x in %s:" v);
Block (typecheck env t "x")
Line (sprintf "for %s in %s:" id v);
Block (typecheck env t id)
]
| Dict (key, va) ->
let id = fresh_id () in
[
Line (sprintf "if type(%s) <> type({}):" v);
Block [ raise_type_error ];
Line (sprintf "for x in %s.keys():" v);
Block (typecheck env (Basic key) "x");
Line (sprintf "for x in %s.values():" v);
Block (typecheck env va "x")
Line (sprintf "for %s in %s.keys():" id v);
Block (typecheck env (Basic key) id);
Line (sprintf "for %s in %s.values():" id v);
Block (typecheck env va id)
]
| Name x ->
let ident =
Expand Down

0 comments on commit 2e6ebe9

Please sign in to comment.