srcgen:语义源代码生成框架
srcgen的Python项目详细描述
srcgen
srcgen是一个语义代码生成框架。语义意味着代码生成器和 生成的代码也具有类似的结构;例如,if语句的主体在 生成和生成的代码。下面是一个简短的示例:
>>> from srcgen.python import PythonModule >>> >>> m = PythonModule("foo") >>> m.import_("sys") >>> m.import_("os") >>> m.sep() >>> m.stmt("x = 5") >>> with m.if_("x > 8"): ... m.stmt("print 'oh no'") ... >>> with m.else_(): ... m.stmt("print 'oh yes'") ... >>> print m import sys import os x = 5 if x > 8: print 'oh no' else: print 'oh yes'
当然,您可以嵌套得更深,也可以将常见的代码片段提取到函数中:
m = PythonModule("foo") with m.class_("MyClass"): with m.method("__init__", "
语言
目前srcgen支持:
- C
- Python
- 赛顿
- HTML/XML
将根据公众需求添加更多语言(欢迎您参与此项工作!)