From 66be7b292b2fcb41f7b706cd64178cf13c032999 Mon Sep 17 00:00:00 2001 From: Xnoe Date: Fri, 25 Sep 2020 11:31:40 +0100 Subject: [PATCH] Identified the bug!!! Issue with not breaking when finding variable in deep vars, caused recursive function to always default to initial value. Added recursive.py during debug. Updated calc.py somewhat. --- calc.py | 5 ++--- python_interpreter.ml | 29 +++++++++++++++++------------ recursive.py | 10 ++++++++++ 3 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 recursive.py diff --git a/calc.py b/calc.py index 577c067..c854af1 100644 --- a/calc.py +++ b/calc.py @@ -50,8 +50,6 @@ NEGATE_AST = 5 index = 0 -print("here") - def current (): return (tokens[index])[0] @@ -116,4 +114,5 @@ def interpret (ast): elif ast[0] == NEGATE_AST: return -interpret(ast[1]) -print("Your answer: " + str(interpret(expr()))) +ast = expr() +print("Your answer: " + str(interpret(ast))) diff --git a/python_interpreter.ml b/python_interpreter.ml index b78c6f5..b3968df 100644 --- a/python_interpreter.ml +++ b/python_interpreter.ml @@ -322,7 +322,6 @@ let build_ast tokens = let eat token = if current () = token then ( index := !index + 1; - print_endline ("Ate: " ^ string_of_token token) ) else raise (Failure ("Invalid Token! Expected: " ^ string_of_token token ^ ", got: " ^ string_of_token (current ()))) in @@ -399,7 +398,6 @@ let build_ast tokens = let rec variable () = eat_whitespace (); - print_endline (string_of_token (current ())); let node = ref (Variable (string_of_id (current ()))) in eat_id (); !node @@ -938,12 +936,13 @@ let interpret ast= | Variable v -> ( let found = ref false in let returnable = ref Nothing in - for i = 0 to List.length vars - 1 do + let _ = try for i = 0 to List.length vars - 1 do if Hashtbl.mem (List.nth vars i) (Variable v) then ( found := true; - returnable := Hashtbl.find (List.nth vars i) (Variable v) + returnable := Hashtbl.find (List.nth vars i) (Variable v); + raise Exit; ) - done; + done with Exit -> () in if !found <> true then returnable := get_var (Variable v); !returnable @@ -1072,19 +1071,23 @@ let interpret ast= ) | _ -> ( let returnable = ref Nothing in + let function_call = ref PlaceHolder in let function_call_input = ref PlaceHolder in + for i = 0 to List.length funcs - 1 do if Hashtbl.mem (List.nth funcs i) func then ( function_call := Hashtbl.find (List.nth funcs i) func; function_call_input := Hashtbl.find (List.nth funcinputs i) func ) done; - if !function_call = PlaceHolder then + + if !function_call = PlaceHolder then ( if Hashtbl.mem functions func then ( function_call := Hashtbl.find functions func; function_call_input := Hashtbl.find functions_input func - ); + ) + ); if !function_call <> PlaceHolder then ( let arglist = list_of_arglist args in let inputlist = list_of_inputlist !function_call_input in @@ -1093,7 +1096,7 @@ let interpret ast= let func_funcs = Hashtbl.copy (List.nth funcs 0) in let func_funcinputs = Hashtbl.copy (List.nth funcinputs 0) in for i = 0 to List.length inputlist - 1 do - Hashtbl.replace func_vars (List.nth inputlist i) (eval vars globals nonlocals funcs funcinputs (List.nth arglist i)) + Hashtbl.replace func_vars (List.nth inputlist i) (eval vars globals nonlocals funcs funcinputs (List.nth arglist i)); done; returnable := eval ([func_vars] @ vars) (ref ([[]] @ !globals)) (ref ([[]] @ !nonlocals)) ([func_funcs] @ funcs) ([func_funcinputs] @ funcinputs) !function_call; if List.length !return_stack >= 1 then ( @@ -1101,10 +1104,12 @@ let interpret ast= return_stack := List.tl !return_stack; ); !returnable - ) else + ) else ( raise (Failure ("Too many / too little args for function: " ^ name_of_variable func)) - ) else - raise (Failure ("Undefined Function: " ^ name_of_variable func)) + ) + ) else ( + raise (Failure ("Undefined Function: " ^ name_of_variable func)) + ); !returnable ) ) @@ -1250,4 +1255,4 @@ let interpret ast= let tokens = tokenise program;; let ast = build_ast tokens;; -interpret ast; \ No newline at end of file +interpret ast; diff --git a/recursive.py b/recursive.py new file mode 100644 index 0000000..0802855 --- /dev/null +++ b/recursive.py @@ -0,0 +1,10 @@ +def rec(x): + if x == 0: + print("Fin.") + else: + print(str(x)) + print(str(x-1)) + rec(x-1) + + +rec(5)