munificent / craftinginterpreters

Repository for the book "Crafting Interpreters"
http://www.craftinginterpreters.com/
Other
8.75k stars 1.03k forks source link

Private class methods #973

Closed mcfriend99 closed 3 years ago

mcfriend99 commented 3 years ago

I was wondering how one can implement or if anyone has successfully implemented private methods in Lox. Has anyone done this?? Is there a reference?? Any help appreciated.

heinthanth commented 3 years ago

I have done once, but stuck with super ( deciding method should be public when called with super ). Here's the reference: https://github.com/uitverse/uit-lang.

mcfriend99 commented 3 years ago

Thanks @heinthanth for the reference. I am actually looking for a solution in C.

Jason2605 commented 3 years ago

@mcfriend99 you can take a look at Dictu, which was initially based from clox: https://github.com/dictu-lang/Dictu

mcfriend99 commented 3 years ago

Thanks @Jason2605. This is exactly what I have implemented today. I was looking for a more compact solution that requires less runtime checks as those are pretty expensive operations. Guess I'll have to stick with this for now.

However, while I do take a completely different approach for modules based on the language requirements, I am quite curious as to how modules work in Dictu. I know I can go into wren's source code and figure it out, but it's easier to understand when you have an idea of what exactly you are looking for.

Care to explain the flow??

Jason2605 commented 3 years ago

Sure, so modules in Dictu are essentially a wrapper around a file. Every time you import a file a new module is created, and any top level value (think globals) are defined within a values table within the module itself. This allows us to have a Python-esque feeling where importing modules you're already behind the module namespace so no global pollution will happen (unlike something like PHP where the include statement is glorified copy and paste).

In terms of more technical detail, what was it specifically you're looking for? Although I'm conscious we're going to end up hijacking this issue and talking about something that isn't really anymore related to Lox, is that fine @munificent?

mcfriend99 commented 3 years ago

I feel we may be derailing off the issue too. I'll create this as another issue for discussion.