python transpiler的徽标
chelodina的Python项目详细描述
龟头虫
Chelodina是一款可转换徽标的开卷机:
TO triangle :length REPEAT 3 [ FORWARD :length RIGHT 120 ] END TO flower :length :count REPEAT 150 [ triangle :length RIGHT 360 / :count ] END TO web REPEAT 6 [ flower 150 18 ] END web
进入python:
importturtledeftriangle(p_length):for_inrange(3):turtle.forward(p_length)turtle.right(120.0)defflower(p_length,p_count):for_inrange(150):triangle(p_length)turtle.right(360.0/p_count)defweb():for_inrange(6):flower(150.0,18.0)web()turtle.done()
安装
pip install chelodina
用法
usage: chelodina [-h] --input INPUT [--run]
arguments:
-h, --help show this help message and exit
--input INPUT Logo source file
--run Run the transpiled code
如果要克隆存储库,可以尝试使用一些就绪的examples:
chelodina --input examples/example1.logo
您还可以触发程序在gui中运行:
chelodina --input examples/example1.logo --run
状态
- 实施:
- 运动函数:
FORWARD
,BACK
,LEFT
,RIGHT
,SETPOS
,SETX
,SETY
,SETHEADING
,SETH
,HOME
- 屏幕功能:
DONE
,CLEARSCREEN
- 控制结构:
REPEAT
- 带有可选参数的函数调用和定义
- 二进制操作
- 运动函数:
这还处于初级阶段,我专注于做一些examples工作。具有类似结构的函数实现起来很简单,但我想首先集中精力改进测试。
语法参考请看grammar.bnf。
已知的怪癖
- 一个有效的AST的验证(例如,确保在一个语句上使用它之前存在一个函数参数)不是用户友好的。我建议你先仔细检查一下你的程序。
- 目前为止只在Python3.6+上测试过。
开发
使用pipenv安装依赖项:
pipenv install --dev
运行测试
使用以下命令运行测试:
pipenv run tests
检测到更改后自动运行测试,使用:
pipenv run tests-watch
调试
- 简单的方法是编写测试来调试代码。如有必要,请随意使用
assert False
。 - 如果需要沿着发送到标准输出或需要标准输出的任何内容(pdb或pudb之类的调试器)运行测试,则需要直接运行
pytest
:pytest --capture=no
。 - 有一个库可以在
utils
包中很好地打印您的ast。用法示例:
fromchelodinaimportcompilerfromchelodina.utils.debugimportparseprintlogo_code="""to myfunction forward 51.0 left 91end"""parsed_ast=compiler.get_ast(logo_code)parseprint(parsed)
您将看到生成的ast:
Module(body=[Import(names=[alias(name='turtle',asname=None),]),FunctionDef(name='myfunction',args=arguments(args=[],vararg=None,kwonlyargs=[],kw_defaults=[],kwarg=None,defaults=[]),body=[Expr(value=Call(func=Attribute(value=Name(id='turtle',ctx=Load()),attr='forward',ctx=Load()),args=[Num(n=51.0),],keywords=[])),Expr(value=Call(func=Attribute(value=Name(id='turtle',ctx=Load()),attr='left',ctx=Load()),args=[Num(n=91.0),],keywords=[])),],decorator_list=[],returns=None),Expr(value=Call(func=Attribute(value=Name(id='turtle',ctx=Load()),attr='done',ctx=Load()),args=[],keywords=[])),])
代码样式
我使用black进行代码格式化,请在执行pr之前安装pre-commit hook。这将在以后通过linter检查强制执行。
您也可以手动运行该命令:
black .
不是必需的,但是如果您在您选择的编辑器中安装EditorConfig,它将使您的生活更轻松。