Fixed ugly bug in nested function support

This commit is contained in:
Dmitry Boulytchev 2019-12-23 21:05:57 +03:00
parent 3f0e1c4b15
commit de2955cbc9
8 changed files with 38 additions and 3 deletions

View file

@ -0,0 +1 @@
> 0

View file

@ -0,0 +1 @@
> 0

11
regression/test069.expr Normal file
View file

@ -0,0 +1,11 @@
fun f (x) {
fun inner (y) {
return if y == 0 then 0 else inner (y-1) fi
}
return inner (x)
}
local n = read ();
write (f (5))

1
regression/test069.input Normal file
View file

@ -0,0 +1 @@
5

15
regression/test070.expr Normal file
View file

@ -0,0 +1,15 @@
fun f (x) {
fun inner1 (y) {
return if y == 0 then 0 else inner2 (y-1) fi
}
fun inner2 (y) {
return if y == 0 then 0 else inner1 (y-1) fi
}
return inner1 (x)
}
local n = read ();
write (f (5))

1
regression/test070.input Normal file
View file

@ -0,0 +1 @@
5

View file

@ -132,7 +132,7 @@ module State =
| I
| G of (string * bool) list * (string, 'a) arrow
| L of (string * bool) list * (string, 'a) arrow * 'a t
with show,html
with show, html
(* Get the depth level of a state *)
let rec level = function

View file

@ -425,8 +425,8 @@ object (self : 'self)
};
scope = init_scope (
let rec readdress_to_closure = function
| State.L (xs, _, tl) ->
State.L (xs, (fun _ -> Value.Access (~-1)), readdress_to_closure tl)
| State.L (xs, st, tl) ->
State.L (xs, (fun name -> match st name with Value.Fun _ as x -> x | _ -> Value.Access (~-1)), readdress_to_closure tl)
| st -> st
in
readdress_to_closure st'
@ -739,7 +739,12 @@ let compile cmd ((imports, infixes), p) =
env, true, se @ (if fe then [LABEL lexp] else []) @ [DUP] @ (List.flatten @@ List.rev code) @ [JMP l]
in
let rec compile_fundef env ((name, args, stmt, st) as fd) =
(* Printf.eprintf "Compile fundef: %s, state=%s\n" name (show(State.t) (show(Value.designation)) st); *)
(* Printf.eprintf "st (inner) = %s\n" (try show(Value.designation) @@ State.eval st "inner" with _ -> " not found"); *)
let env = env#open_fun_scope fd in
(*Printf.eprintf "Lookup: %s\n%!" (try show(Value.designation) @@ snd (env#lookup "inner") with _ -> "no inner..."); *)
let env = List.fold_left (fun env arg -> env#add_arg arg) env args in
let lend, env = env#get_label in
let env, flag, code = compile_expr lend env stmt in