Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New example to generate AST from scratch #507

Merged
merged 3 commits into from May 31, 2023
Merged

Conversation

Andree37
Copy link
Contributor

@Andree37 Andree37 commented May 29, 2023

As I was doing some research and I came along this

I found there was no example on how to create an AST from scratch and print out the target C code.

I purpose something of this sort as an initial example. As I am a beginner of the module, please don't be afraid to suggest changes :)

Copy link
Owner

@eliben eliben left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I suggest a different file name because "generate" is ambiguous here (this library is all about generating ASTs from C source): examples/construct_ast_from_scratch.py

# Andre Ribeiro [https://github.com/Andree37]
# License: BSD
# -----------------------------------------------------------------
from __future__ import print_function
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unnecessary now -- I also just removed it from other examples for consistency

constant_zero = c_ast.Constant(type='int', value='0')
return_node = c_ast.Return(expr=constant_zero)
compound_node = c_ast.Compound(block_items=[return_node])
type_decl_node = c_ast.TypeDecl(declname='main', quals=[], type=c_ast.IdentifierType(names=['int']), align=[])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pycparser code typically tries to wrap at 80 columns
should be easy here with all the keyword params (like you do in func_def_node)

# }


def create_your_ast():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More descriptive name: maybe something like "empty_main_function_ast"

@Andree37 Andree37 requested a review from eliben May 30, 2023 22:40
@Andree37
Copy link
Contributor Author

Corrected :)

@eliben
Copy link
Owner

eliben commented May 30, 2023

Some of the builders are failing - check out other examples to see how to set up the import path to work properly.

@Andree37
Copy link
Contributor Author

Some of the builders are failing - check out other examples to see how to set up the import path to work properly.

I believe I have corrected it now, unless I also needed to import platform

@eliben eliben merged commit 6cf69df into eliben:master May 31, 2023
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants