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.
This commit is contained in:
parent
32c8f4440c
commit
66be7b292b
5
calc.py
5
calc.py
@ -50,8 +50,6 @@ NEGATE_AST = 5
|
|||||||
|
|
||||||
index = 0
|
index = 0
|
||||||
|
|
||||||
print("here")
|
|
||||||
|
|
||||||
def current ():
|
def current ():
|
||||||
return (tokens[index])[0]
|
return (tokens[index])[0]
|
||||||
|
|
||||||
@ -116,4 +114,5 @@ def interpret (ast):
|
|||||||
elif ast[0] == NEGATE_AST:
|
elif ast[0] == NEGATE_AST:
|
||||||
return -interpret(ast[1])
|
return -interpret(ast[1])
|
||||||
|
|
||||||
print("Your answer: " + str(interpret(expr())))
|
ast = expr()
|
||||||
|
print("Your answer: " + str(interpret(ast)))
|
||||||
|
@ -322,7 +322,6 @@ let build_ast tokens =
|
|||||||
let eat token =
|
let eat token =
|
||||||
if current () = token then (
|
if current () = token then (
|
||||||
index := !index + 1;
|
index := !index + 1;
|
||||||
print_endline ("Ate: " ^ string_of_token token)
|
|
||||||
) else
|
) else
|
||||||
raise (Failure ("Invalid Token! Expected: " ^ string_of_token token ^ ", got: " ^ string_of_token (current ())))
|
raise (Failure ("Invalid Token! Expected: " ^ string_of_token token ^ ", got: " ^ string_of_token (current ())))
|
||||||
in
|
in
|
||||||
@ -399,7 +398,6 @@ let build_ast tokens =
|
|||||||
|
|
||||||
let rec variable () =
|
let rec variable () =
|
||||||
eat_whitespace ();
|
eat_whitespace ();
|
||||||
print_endline (string_of_token (current ()));
|
|
||||||
let node = ref (Variable (string_of_id (current ()))) in
|
let node = ref (Variable (string_of_id (current ()))) in
|
||||||
eat_id ();
|
eat_id ();
|
||||||
!node
|
!node
|
||||||
@ -938,12 +936,13 @@ let interpret ast=
|
|||||||
| Variable v -> (
|
| Variable v -> (
|
||||||
let found = ref false in
|
let found = ref false in
|
||||||
let returnable = ref Nothing 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 (
|
if Hashtbl.mem (List.nth vars i) (Variable v) then (
|
||||||
found := true;
|
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
|
if !found <> true then
|
||||||
returnable := get_var (Variable v);
|
returnable := get_var (Variable v);
|
||||||
!returnable
|
!returnable
|
||||||
@ -1072,18 +1071,22 @@ let interpret ast=
|
|||||||
)
|
)
|
||||||
| _ -> (
|
| _ -> (
|
||||||
let returnable = ref Nothing in
|
let returnable = ref Nothing in
|
||||||
|
|
||||||
let function_call = ref PlaceHolder in
|
let function_call = ref PlaceHolder in
|
||||||
let function_call_input = ref PlaceHolder in
|
let function_call_input = ref PlaceHolder in
|
||||||
|
|
||||||
for i = 0 to List.length funcs - 1 do
|
for i = 0 to List.length funcs - 1 do
|
||||||
if Hashtbl.mem (List.nth funcs i) func then (
|
if Hashtbl.mem (List.nth funcs i) func then (
|
||||||
function_call := Hashtbl.find (List.nth funcs i) func;
|
function_call := Hashtbl.find (List.nth funcs i) func;
|
||||||
function_call_input := Hashtbl.find (List.nth funcinputs i) func
|
function_call_input := Hashtbl.find (List.nth funcinputs i) func
|
||||||
)
|
)
|
||||||
done;
|
done;
|
||||||
if !function_call = PlaceHolder then
|
|
||||||
|
if !function_call = PlaceHolder then (
|
||||||
if Hashtbl.mem functions func then (
|
if Hashtbl.mem functions func then (
|
||||||
function_call := Hashtbl.find functions func;
|
function_call := Hashtbl.find functions func;
|
||||||
function_call_input := Hashtbl.find functions_input func
|
function_call_input := Hashtbl.find functions_input func
|
||||||
|
)
|
||||||
);
|
);
|
||||||
if !function_call <> PlaceHolder then (
|
if !function_call <> PlaceHolder then (
|
||||||
let arglist = list_of_arglist args in
|
let arglist = list_of_arglist args in
|
||||||
@ -1093,7 +1096,7 @@ let interpret ast=
|
|||||||
let func_funcs = Hashtbl.copy (List.nth funcs 0) in
|
let func_funcs = Hashtbl.copy (List.nth funcs 0) in
|
||||||
let func_funcinputs = Hashtbl.copy (List.nth funcinputs 0) in
|
let func_funcinputs = Hashtbl.copy (List.nth funcinputs 0) in
|
||||||
for i = 0 to List.length inputlist - 1 do
|
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;
|
done;
|
||||||
returnable := eval ([func_vars] @ vars) (ref ([[]] @ !globals)) (ref ([[]] @ !nonlocals)) ([func_funcs] @ funcs) ([func_funcinputs] @ funcinputs) !function_call;
|
returnable := eval ([func_vars] @ vars) (ref ([[]] @ !globals)) (ref ([[]] @ !nonlocals)) ([func_funcs] @ funcs) ([func_funcinputs] @ funcinputs) !function_call;
|
||||||
if List.length !return_stack >= 1 then (
|
if List.length !return_stack >= 1 then (
|
||||||
@ -1101,10 +1104,12 @@ let interpret ast=
|
|||||||
return_stack := List.tl !return_stack;
|
return_stack := List.tl !return_stack;
|
||||||
);
|
);
|
||||||
!returnable
|
!returnable
|
||||||
) else
|
) else (
|
||||||
raise (Failure ("Too many / too little args for function: " ^ name_of_variable func))
|
raise (Failure ("Too many / too little args for function: " ^ name_of_variable func))
|
||||||
) else
|
)
|
||||||
|
) else (
|
||||||
raise (Failure ("Undefined Function: " ^ name_of_variable func))
|
raise (Failure ("Undefined Function: " ^ name_of_variable func))
|
||||||
|
);
|
||||||
!returnable
|
!returnable
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
10
recursive.py
Normal file
10
recursive.py
Normal file
@ -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)
|
Loading…
x
Reference in New Issue
Block a user