Skip to content

Instantly share code, notes, and snippets.

@devilholk
Created November 28, 2016 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devilholk/ffa1e8dd966e2076941a7f2d67a6ebd5 to your computer and use it in GitHub Desktop.
Save devilholk/ffa1e8dd966e2076941a7f2d67a6ebd5 to your computer and use it in GitHub Desktop.
Example for text manipulation in python
T = indented_block
program_template = T([
'//Include directives',
'¶includes',
'',
'//Main application',
'¶main',
])
include_directive = T('#include <file>')
includes = 'stdio.h', 'stdlib.h'
function = T([
'returns name(args) {',
'\tbody',
'}',
])
program = program_template.replace('¶includes',
[include_directive.replace('file', i) for i in includes])
main_body = T([
'printf("Hello World!\\n");'
])
program = program.replace('¶main',
function.replace_kw(
name='main',
returns='int',
args='int argc, char* argv[]',
body=main_body
))
print(program.to_text())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment