Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • A awesome-python
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 13
    • Issues 13
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 317
    • Merge requests 317
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Vinta Chen
  • awesome-python
  • Merge requests
  • !1292

add pycro

  • Review changes

  • Download
  • Email patches
  • Plain diff
Closed Administrator requested to merge github/fork/mak12776/master into master May 12, 2019
  • Overview 0
  • Commits 1
  • Pipelines 0
  • Changes 1

Created by: mak12776

What is this Python project?

Pycro is a python integrated macro preprocessor. It will interpret input texts and generates a corresponding python code that will generate the intended result, if we compile and execute that.

usage example

imagine we have this main.c file:


#include <stdio.h>

//# names = ['Oliver', 'Jack', 'Harry', 'James', 'John']

int main()
{
	//@ for name in names:
	printf("Hello ${name}!\n");
	//@ end for
	return 0;
}

we open this file and pass it to generate_code function:

#!/usr/bin/python3

import pycro

env = pycro.CompilerEnvironment(language = 'c')

with open('main.c') as infile:
    with open('main.c.py', 'w') as outfile:
        pycro.generate_code(infile, outfile, env)

another file will be created named main.c.py:

__outfile__.write('\n');
__outfile__.write('#include <stdio.h>\n');
__outfile__.write('\n');
names = ['Oliver', 'Jack', 'Harry', 'James', 'John']
__outfile__.write('\n');
__outfile__.write('int main()\n');
__outfile__.write('{\n');
for name in names:
	__outfile__.write('\tprintf("Hello ');__outfile__.write(str(name));__outfile__.write('!\\n");\n');
__outfile__.write('\treturn 0;\n');
__outfile__.write('}\n');
__outfile__.write('\n');

now we can compile and execute main.c.py using the following code:

#!/usr/bin/python3

import pycro

env = pycro.ExecutorEnvironment()

with open('main.c.py') as infile:
    with open('_main.c', 'w') as outfile:

        code_object = pycro.compile_generated_code(infile.read(), infile.name)

        pycro.execute_code_object(code_object, outfile, env)

the generated result saved as _main.c:


#include <stdio.h>


int main()
{
	printf("Hello Oliver!\n");
	printf("Hello Jack!\n");
	printf("Hello Harry!\n");
	printf("Hello James!\n");
	printf("Hello John!\n");
	return 0;
}

it's time to compile _main.c:

$ gcc _main.c -o main

and run ./main:

$ ./main:

Hello Oliver!
Hello Jack!
Hello Harry!
Hello James!
Hello John!

What's the difference between this Python project and similar ones?

I'm tired to compare them here, but it's not complete.

--

Anyone who agrees with this pull request could vote for it by adding a 👍 to it, and usually, the maintainer will merge it when votes reach 20.

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/mak12776/master