hchiam / please

An experimental programming language (transpiler) to make it easier to write code with speech recognition.
MIT License
1 stars 1 forks source link

Can't do recursive call: move function names to a goto_stack *dictionary*? #46

Closed hchiam closed 7 years ago

hchiam commented 7 years ago

Test with https://github.com/hchiam/please/blob/06f686e6dc1793e5bea88c024c7a63b3f46d02e5/text.txt

And then test that nothing else broke with https://github.com/hchiam/please/blob/master/library/expected_output.md

Variables to fix: goto_stack: append(), pop(), [-1] goto_locations in check_for() for current_loop

hchiam commented 7 years ago

try using timer to see line number steps/goto's

hchiam commented 7 years ago

"end function" not working:

After checking goto_stack's functions' local_variables and index_called_froms, it seems that actually end function (for user-defined functions) needs to act more like return function --> otherwise once the interpreter gets to the end of the function, it keeps reading, instead of going to where it might have been called from.

hchiam commented 7 years ago

Maybe move functions from variable_dictionary to goto_stack?

hchiam commented 7 years ago

check_function(): i = function.index_called_from = None <-- goto_stack = [...] but goto_stack[-1][1].index_called_from = None

hchiam commented 7 years ago

Issue found: Tracked index_called_from for each item in goto_stack: for some reason all recursive instances have the same index_called_from (they should have different index_called_from's; i.e. the first call should have been made outside the function).

Possible fix(es):

hchiam commented 7 years ago

Problem 1: When trying to assign a function output to a variable, it seems the variable is created in the global variable_dictionary instead of the local function local_variables, and Problem 2: the value does not seem to be passed along.

Test 0c98e2c with:

please define function fibonacci with number
    please if variable number equals zero then return zero
    please if variable number equals one then return one
    please assign variable number minus one to subresult
    please assign use function fibonacci on variable subresult to variable answer
        please note assign use function fibonacci on variable number minus one to variable answer
    please print answer = variable answer
    please return variable answer
please end function
please assign use function fibonacci with zero to variable output
please print step 1: output = variable output
please assign use function fibonacci with one to variable output
please print step 2: output = variable output
please assign use function fibonacci with two to variable output
please print step 3: output = variable output
hchiam commented 7 years ago

should output:

step 1: output = zero
step 2: output = 1
answer = 1
step 3: output = 1
hchiam commented 7 years ago

not quite closed: test with:

please define function fibonacci with number
    please if variable number equals zero then return zero
    please if variable number equals one then return one
    please assign variable number minus one to subresult
    please assign use function fibonacci on variable subresult to variable answer
        please note assign use function fibonacci on variable number minus one to variable answer
    please print answer = variable answer
    please return variable answer
please end function
please assign use function fibonacci with zero to variable output
please print step 1: output = variable output
please assign use function fibonacci with one to variable output
please print step 2: output = variable output
please assign use function fibonacci with two to variable output
please print step 3: output = variable output
please assign use function fibonacci with three to variable output
please print step 4: output = variable output

this incorrectly outputs:

step 1: output = zero
step 2: output = 1
answer = 1
step 3: output = 1
answer = 1
answer = 1
step 4: output = 1

when it should output:

step 1: output = zero
step 2: output = 1
step 3: output = 1
step 4: output = 2

[EDIT:] Actually, with the code above as-is, it's not actually fibonacci --> the output IS CORRECT.

What does not work is using two recursive calls within the function (even if assigning to separate variables first).

hchiam commented 7 years ago

v0.1.0 Interpreter --> v0.2.0 Transformer (translates to Python code)