Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

class forward call definition problem #375

Open
paddy0489 opened this issue Oct 18, 2023 · 3 comments
Open

class forward call definition problem #375

paddy0489 opened this issue Oct 18, 2023 · 3 comments

Comments

@paddy0489
Copy link

berry.exe state.be is working well
but if i call berry and import state.be
than an redefinition error comes.

import "state.be" as state
syntax_error: state.be:17: redefinition of 'OffState'
stack traceback:
stdin:1: in function main

#- Step 1: Define the State Interface
class State()
    def handle(context):
        pass
    end
end
-#
# Step 2: Create Concrete States
var OffState = nil
class OnState
    def handle(context)
        print("Light is already ON. Switching it OFF now.")
        context.state = OffState()
    end
end

class OffState
    def handle(context)
        print("Light is OFF. Switching it ON now.")
        context.state = OnState()
    end
end


# Step 3: Create the Context
class LightSwitch
    var state
    def init()
        self.state = OffState()
    end
    def press()
        self.state.handle(self)
    end
end

# Testing the FSM
switch = LightSwitch()
switch.press()  # Expected: "Light is OFF. Switching it ON now."
switch.press()  # Expected: "Light is already ON. Switching it OFF now."
switch.press()  # Expected: "Light is OFF. Switching it ON now."
@s-hadinger
Copy link
Contributor

Hmm. I don't see an easy way around this. Let's take a simplified example:

var B = nil
class A def init() return B end end
class B def init() return A end end

But when you import it, Berry implicitly wraps it in a virtual function equivalent to:

def f()
    var B = nil
    class A def init() return B end end
    class B def init() return A end end
end

In the implicit function, you can't redefine a variable with a class. Unfortunately the only way I can think about would be to use indirection variables (I put 2 of them for symmetry):

var A = nil
var B = nil
class A_class def init() return B end end
class B_class def init() return A end end
A = A_class
B = B_class

@paddy0489
Copy link
Author

It's working, thank you very much :-)

It looks like a workaround, will it be fixed in a future interpreter version?

@s-hadinger
Copy link
Contributor

I'm not sure we can easily fix this. This is the price to pay for a lightweight single pass compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants