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

Parse C #2

Closed
wants to merge 15 commits into from
Closed

Parse C #2

wants to merge 15 commits into from

Conversation

MichaelCampos-eng
Copy link
Collaborator

This is a program that parses C code.

Input: string of C code

eg. Parse test = new Parse(code);

Output: Object that holds a repository of all C variables and its values.

MichaelCampos-eng and others added 15 commits October 27, 2021 18:27
We found a parsing library last week. There is a test file showing that it works, but need to also test it on our own files. Will do that as soon as possible, sorry for the late commit
Seems to read java, need help to read header files
This reverts commit e2ccaaf.
Copy link
Member

@ducky64 ducky64 left a comment

Choose a reason for hiding this comment

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

Looked at Parse.java and ParseTest.java - both are on the right track and seem functionally fine, though there's some style comments (general note: this is always expected from a code review, and style is important for code readability and maintainability in team projects) and some larger points we can discuss.

Additionally, there seems to be a lot of other irrelevant files - can you do a first pass on purging those? Good practice is to keep the repository clean and pull requests focused on the feature at hand. Pulling in external code directly may also have copyright implications.

if (child instanceof CPPASTSimpleDeclaration) {

/**Setup*/
CPPASTSimpleDeclaration declaration = (CPPASTSimpleDeclaration) child;
Copy link
Member

Choose a reason for hiding this comment

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

Style nit: it may be helpful to add an example here of what is being extracted, eg uint8_t PEDAL_POS = 8; here, at least if these are different (I'm not sure)

CPPASTEqualsInitializer init = (CPPASTEqualsInitializer) declarator.getInitializer();

/**Get what is needed: Value, Name, bigType & smallType*/
CPPASTLiteralExpression value = (CPPASTLiteralExpression) init.getInitializerClause(); //get value
Copy link
Member

Choose a reason for hiding this comment

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

Style comment (... about comments!): comments should generally not be redundant with what can be understood by reading the code - in this case, I would say that these short comments are unnecessary, since you did a good job of writing self-documenting code.

In general, comments are often useful for providing examples, summarizing behavior, or documenting tricky behavior / oddities.

/**Get what is needed: Value, Name, bigType & smallType*/
CPPASTLiteralExpression value = (CPPASTLiteralExpression) init.getInitializerClause(); //get value
CPPASTName name = (CPPASTName) declarator.getName(); //get name
IToken bigType = specifier.getSyntax(); //get big type (iToken)
Copy link
Member

Choose a reason for hiding this comment

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

What do you mean big type and small type? From the below, if you mean bigType for things like const, it's more formally called a type qualifier, while uint32_t is the actual type. Using (and finding) standardized terminology is helpful for writing clear code.

There might also be other ways to simplify this distinction - we should talk about it.

}

/**Adds a new hashmap for Large Types if bType does not exist*/
private void checkBigTypeExists(IToken bType) {
Copy link
Member

Choose a reason for hiding this comment

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

The function name (check...) implies this should return something, but it doesn't? Perhaps you mean "recordBigType" or "addBigType"?

We might also think of ways to restructure the data structures to simplify this - we should discuss that.

Similar for below.

public class Parse {

private IASTTranslationUnit translationUnit; //current translation unit node
public HashMap<IToken, HashMap> bigTypes;
Copy link
Member

Choose a reason for hiding this comment

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

Did you know you can (I think) also add type parameters for the inner hash map, eg HashMap<IToken, HashMap<..., ...>>? Comprehensively documenting types is very useful.

}

/**Initializes the translation unit setup*/
public static IASTTranslationUnit getIASTTranslationUnit(char[] code) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

Can code be a string instead?

public void showNameValue() {
iterateBigTypes();
}
/**Iterate through all big types eg. const*/
Copy link
Member

Choose a reason for hiding this comment

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

In production code, I generally prefer not to leave in debugging code. Obviously a bit of season-to-taste (especially if you're building data structures utilities).

If you directly print the HashMap, will it do the right thing and print the contents?

Comment on lines +20 to +26
String value1 = "0x16";
String value2 = "0x32";
String value3 = "0x64";

String n1 = "CAN_ID";
String n2 = "CAN_ID2";
String n3 = "CAN_ID3";
Copy link
Member

Choose a reason for hiding this comment

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

Feel free to inline these into the below lines for conciseness.

Sometimes, declaring values separately can be a stylistic benefit, by giving them a name, but this test code would be clear with the "magic numbers" (or magic strings in this case) directly inline.

CPPASTName name2 = new CPPASTName(n2.toCharArray());
CPPASTName name3 = new CPPASTName(n3.toCharArray());

String litExpr1 = test.get(name1).getRawSignature();
Copy link
Member

Choose a reason for hiding this comment

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

Would it make sense to have get take a string as its argument instead of this CPPASTName?


@Test
public void checkLitExprdiverse() throws Exception {
String code = "const uint16_t CAN_ID = 0x16; \n" +
Copy link
Member

Choose a reason for hiding this comment

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

Similar comments here - I believe inlining should make this test a lot more readable.

@ducky64
Copy link
Member

ducky64 commented Feb 27, 2022

I'll also acknowledge that part of this is probably a bit chaotic (with regards to file organization, not the particular code written) since I didn't set up a template project so it's less clear what should go where. We should also work through that, which might also be related to #1.

@MichaelCampos-eng MichaelCampos-eng deleted the SimpleApp branch February 27, 2022 09:20
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.

2 participants