-
Notifications
You must be signed in to change notification settings - Fork 614
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
Conversation
There was a problem hiding this 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
examples/generate_ast.py
Outdated
# Andre Ribeiro [https://github.com/Andree37] | ||
# License: BSD | ||
# ----------------------------------------------------------------- | ||
from __future__ import print_function |
There was a problem hiding this comment.
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
examples/generate_ast.py
Outdated
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=[]) |
There was a problem hiding this comment.
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)
examples/generate_ast.py
Outdated
# } | ||
|
||
|
||
def create_your_ast(): |
There was a problem hiding this comment.
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"
Corrected :) |
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 |
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 :)