2024-06-26 16:36:20 发布
网友
我正在尝试为一种自定义语言编写一个“解释程序”。在
这种语言与python极其相似,但有一些不同之处,例如:
Python:
def subRoutine(): print("hello")
自定义:
解决这个问题的最好办法是什么? 如果我要修改python源代码并重新编译,是否可以编写一个允许我在如此低的级别上修改内容的库?在
干杯
我将从this file开始,记住:
Changing the grammar specified in this file will most likely require corresponding changes in the parser module (../Modules/parsermodule.c).
在line 27:
funcdef: 'def' NAME parameters ['->' test] ':' suite
然而,语法不应该因为def的简单替换而改变,但是stdlib的整个Python代码都将受到这个更改的严重影响。在
def
我自己测试过它,使用这个源tarball,并将'def'替换为Grammar文件中的'def'。 但是,在CPython编译期间,会引发此错误:
'def'
Grammar
{1}编译python中的任何一条语句都必须用一个来替换。在
注意:仅使用find . -type f -name "*" -exec sed -i 's/def /createfunc /g' {} +是不够的:
find . -type f -name "*" -exec sed -i 's/def /createfunc /g' {} +
/usr/bin/mkdir -p Include python3.4 ./Parser/asdl_c.py -h Include ./Parser/Python.asdl File "./Parser/asdl_c.py", line 14 createfunc get_c_type(name): ^ SyntaxError: invalid syntax
命令python3.4 ./Parser/asdl_c.py -h Include ./Parser/Python.asdl和this comment显示了问题:您需要一个python来编译python,并且两者都需要理解相同的源代码。 linked article引入的技巧是添加关键字而不是替换它。在
python3.4 ./Parser/asdl_c.py -h Include ./Parser/Python.asdl
稍后,您可以编译第三个python,它使用第二个python(它同时理解def和{})进行编译,语法中def关键字已被新的关键字完全替换。在
我将从this file开始,记住:
在line 27:
然而,语法不应该因为
def
的简单替换而改变,但是stdlib的整个Python代码都将受到这个更改的严重影响。在我自己测试过它,使用这个源tarball,并将
^{pr2}$'def'
替换为Grammar
文件中的'def'
。 但是,在CPython编译期间,会引发此错误:{1}编译python中的任何一条语句都必须用一个来替换。在
注意:仅使用
find . -type f -name "*" -exec sed -i 's/def /createfunc /g' {} +
是不够的:命令
python3.4 ./Parser/asdl_c.py -h Include ./Parser/Python.asdl
和this comment显示了问题:您需要一个python来编译python,并且两者都需要理解相同的源代码。 linked article引入的技巧是添加关键字而不是替换它。在稍后,您可以编译第三个python,它使用第二个python(它同时理解})进行编译,语法中def关键字已被新的关键字完全替换。在
def
和{相关问题 更多 >
编程相关推荐