-
Notifications
You must be signed in to change notification settings - Fork 2
[feat] ASM functions #34
Changes from all commits
1ed4598
e9d79aa
7ad9ff7
bce6dc0
5cb07c8
634537b
9f48fdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| #define TOKENS_H | ||
|
|
||
| enum TokenType { | ||
| ASM_FUNCTION, | ||
| FUNCTION, | ||
| RETURN, | ||
| VAR, | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |||||||||||||||||||||||||||
| * Function-related AST parsing. | ||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #include <stdlib.h> | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #include "./variables.h" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #include "../parser.h" | ||||||||||||||||||||||||||||
|
|
@@ -10,6 +12,8 @@ | |||||||||||||||||||||||||||
| #include "../../lexer/tokens.h" | ||||||||||||||||||||||||||||
| #include "../../lexer/lexer.h" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| #include "../../utils/logging.c" | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||
| * Parse the parameters from a function definition (for example). | ||||||||||||||||||||||||||||
| * @param result the lexer result. | ||||||||||||||||||||||||||||
|
|
@@ -138,3 +142,53 @@ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index) { | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return node; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { | ||||||||||||||||||||||||||||
| AST_NODE* node = createASTNode(AST_ASM_FUNCTION_DECLARATION); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| node->left = createASTNode(AST_FUNCTION_HEADER); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if(result.tokens[index].type != KEYWORD) { | ||||||||||||||||||||||||||||
| return NULL; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| AST_NODE* params = parseParameters(result, index + 1); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if(params == NULL) return NULL; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| node->left->left = params; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| index = params->endingIndex; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| int buffSize = 32; | ||||||||||||||||||||||||||||
| int buffIndex = 0; | ||||||||||||||||||||||||||||
| char* buff = malloc(buffSize); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| for(; index <= result.size; ++index) { | ||||||||||||||||||||||||||||
| struct Token t = result.tokens[index]; | ||||||||||||||||||||||||||||
|
Comment on lines
+167
to
+168
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct loop boundary condition to prevent out-of-bounds access The loop condition Apply this diff to fix the loop condition: - for(; index <= result.size; ++index) {
+ for(; index < result.size; ++index) {📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if(t.type == BRACKETS_CLOSE) { | ||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if(t.type != STRING) { | ||||||||||||||||||||||||||||
| return NULL; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| char c; | ||||||||||||||||||||||||||||
| while(c = *t.value++) { | ||||||||||||||||||||||||||||
| buff[buffIndex] = c; | ||||||||||||||||||||||||||||
|
Comment on lines
+178
to
+180
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid modifying the original token value pointer The line Apply this diff to refactor the code: - while(c = *t.value++) {
+ char* valPtr = t.value;
+ while((c = *valPtr++)) {📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||
| buffIndex++; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| if(buffIndex >= buffSize) { | ||||||||||||||||||||||||||||
| buffSize *= 1.5; | ||||||||||||||||||||||||||||
| buff = realloc(buff, buffSize); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+183
to
+186
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential memory leak when reallocating buffer If Apply this diff to fix the issue: - buff = realloc(buff, buffSize);
+ char* temp = realloc(buff, buffSize);
+ if (temp == NULL) {
+ free(buff);
+ return NULL;
+ }
+ buff = temp;📝 Committable suggestion
Suggested change
🧰 Tools🪛 cppcheck (2.10-2)[error] 185-185: Common realloc mistake (memleakOnRealloc) |
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| buff[buffIndex] = '\0'; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| node->value = buff; | ||||||||||||||||||||||||||||
| return node; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
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.
Check for
mallocfailure to ensure robust memory allocationThe
malloccall forbuffdoes not check if the allocation was successful. IfmallocreturnsNULL, subsequent operations onbuffwill result in undefined behavior. Add a check to handle allocation failure.Apply this diff to handle
mallocfailure: