GithubHelp home page GithubHelp logo

支持导入dongbei模块 about dongbei HOT 7 OPEN

zhanyong-wan avatar zhanyong-wan commented on August 16, 2024
支持导入dongbei模块

from dongbei.

Comments (7)

xinxiao avatar xinxiao commented on August 16, 2024

好奇这个要怎么做 感觉现有ast感觉很难控制模块的scoping 但如果precompile成Python再植入的话变量名感觉又会起冲突

from dongbei.

zhanyong-wan avatar zhanyong-wan commented on August 16, 2024

我觉着 precompile 成 py 可行。

from dongbei.

xinxiao avatar xinxiao commented on August 16, 2024

precompile成Py的话这个issue的blocker可能是要先调整一下现在dongbei 编译到 python 时候lexical addressing的方法

现在所有的变量在被编译到Python是就已经被flatten成 _db_var* 的格式了 precompile的时候如果保持这个模式 用原先的方法做单纯地python import 在dongbei应该没有办法正确的refer原先东北代码里的variable

举个🌰:
这是东北lib 弄他.dongbei 的src

听见啥弄他 装 "你瞅啥"。

按照现在的方法 precompile成Python 这个lib会变成 弄他.py

_db_var1 = "你瞅啥"

然后假设我们import弄他

翠花,上 弄他。

唠唠:弄他.听见啥弄他。 # 弄他.听见啥弄他 must be undefined.

from dongbei.

zhanyong-wan avatar zhanyong-wan commented on August 16, 2024

现在变量名已经不再转换成 _db_var* 了。源程序里啥名转成 python 还是啥名。

这样理论上是有一些漏洞,但实际问题不大。

from dongbei.

xinxiao avatar xinxiao commented on August 16, 2024

beautiful 看来我应该升级一下interpreter了lol

这个方案等基本同于用中文字符写python,source的编码可能是唯一问题。

py_compile 可以对dongbei的python intermediate做binary precompile 直接实现dongbei -> pyc 的转换 performance可以更进一步

from dongbei.

AoiKuiyuyou avatar AoiKuiyuyou commented on August 16, 2024

可以通过修改importlib.machinery.SourceFileLoader.SOURCE_SUFFIXESimportlib.machinery.SourceFileLoader.source_to_code的方式支持导入.dongbei模块。修改后可以像导入普通Python模块一样导入.dongbei模块。这里有个例子

AST方面推荐转成使用Python自带的ast模块里的对象,这样解析完成后获得的ast.Module对象可以直接传给原来的importlib.machinery.SourceFileLoader.source_to_code函数实现模块加载,省去了转成Python源码这一中间步骤。这样做有个很大的好处是,代码执行报错时可以直接定位到.dongbei源码的行和列(需在构建Python的AST对象时提供对应的行列信息)。

from dongbei.

zhanyong-wan avatar zhanyong-wan commented on August 16, 2024

此计甚妙!!

from dongbei.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.