risor-io / risor

Fast and flexible scripting for Go developers and DevOps.
https://risor.io
Apache License 2.0
611 stars 26 forks source link

Enable `from xxx import xx as xx` statement in Risor #116

Closed abadfox233 closed 9 months ago

abadfox233 commented 10 months ago

This enhancement introduces support for the from and as keywords within the Risor compiler, extending its capabilities.

New Keywords: FROM and AS

Compiler Enhancement: New Opcode FromImport

Virtual Machine Implementation

Should the initial import fail, the system then tries to import variables within the module. For example, with from a.b.c import d, the virtual machine tries to import the a.b.c module and subsequently imports the global variable d from the c module.

Similar to Python, both imported modules and variables are set as constants after the import process.

This enhancement expands Risor's functionality to provide more flexible and comprehensive import statements, resembling Python's behavior while maintaining consistency within the Risor environment.

myzie commented 10 months ago

Thanks @abadfox233! This looks great at a glance. Will get hands on with it soon.

Did you come across any limitations or issues with the approach in your testing so far?

It would be good to add some unit tests as we get closer to merging this.

abadfox233 commented 10 months ago

Thank you for your feedback! I've gone ahead and added some unit tests as suggested. These tests cover various scenarios and should help ensure the reliability of the code. If there are specific areas you'd like me to focus on or any further suggestions, please let me know. I'm committed to enhancing the quality of the codebase.

myzie commented 9 months ago

This is working well. One case we should consider is importing from built-in modules. We should probably make this work, for example:

>>> from math import min
module not found: math
>>> math.min
builtin(min)

We could also add this in follow up work.

Otherwise this seems to match what you'd expect coming from Python, which is good.